Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2e2041a

Browse files
chore(docs): improve version picker
1 parent 4aa9534 commit 2e2041a

File tree

4 files changed

+126
-29
lines changed

4 files changed

+126
-29
lines changed

docs/app/src/versions.js

+116-22
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,131 @@
11
'use strict';
2+
/* global console */
23

34
angular.module('versions', [])
45

5-
.controller('DocsVersionsCtrl', ['$scope', '$location', '$window', 'NG_VERSIONS', function($scope, $location, $window, NG_VERSIONS) {
6-
$scope.docs_version = NG_VERSIONS[0];
7-
$scope.docs_versions = NG_VERSIONS;
6+
.factory('getVersions', ['$http', '$q', function($http, $q) {
87

9-
for (var i = 0, minor = NaN; i < NG_VERSIONS.length; i++) {
10-
var version = NG_VERSIONS[i];
11-
if (version.isSnapshot) {
12-
version.isLatest = true;
13-
continue;
8+
var VERSION_REGEXP = /^(\d+)\.(\d+)\.(\d+)(?:-(?:(\w+)\.)?(\d+))?/;
9+
10+
return function() {
11+
return $q.all([
12+
// is it bad to use crossorigin??
13+
$http.get('http://crossorigin.me/https://registry.npmjs.org/angular').then(processAllVersionsResponse),
14+
$http.get('version.json').then(processCurrentVersionResponse)
15+
])
16+
.then(function(data) {
17+
return {
18+
all: data[0],
19+
current: find(data[0], function(version) { return version.version.full === data[1]; })
20+
};
21+
})
22+
.catch(function(e) {
23+
console.log('Failed to get the version information...');
24+
console.log(e.stack);
25+
});
26+
};
27+
28+
function processCurrentVersionResponse(response) {
29+
return response.data.isSnapshot ? 'snapshot' : response.data.full;
30+
}
31+
32+
function processAllVersionsResponse(response) {
33+
34+
var latestMap = {};
35+
36+
var versions = Object.keys(response.data.versions)
37+
.map(function(versionStr) {
38+
var version = parseVersion(versionStr);
39+
var key = version.major + '.' + version.minor;
40+
if (compareVersions(version, latestMap[key]) > 0) {
41+
latestMap[key] = version;
42+
}
43+
return version;
44+
})
45+
.filter(function(version) {
46+
return version && version.major > 0;
47+
})
48+
.map(function(version) {
49+
return makeOption(version);
50+
})
51+
.reverse();
52+
53+
var latest = sortObject(latestMap, reverse(compareVersions)).map(function(version) { return makeOption(version, 'Latest'); });
54+
55+
return [{version: {full: 'snapshot'}, label: 'master', group: 'Latest'}]
56+
.concat(latest)
57+
.concat(versions);
58+
}
59+
60+
function parseVersion(versionStr) {
61+
var match = VERSION_REGEXP.exec(versionStr);
62+
if (match) {
63+
return {
64+
major: parseInt(match[1],10),
65+
minor: parseInt(match[2],10),
66+
patch: parseInt(match[3],10),
67+
prerelease: [match[4], parseInt(match[5], 10)],
68+
full: versionStr
69+
};
1470
}
15-
// NaN will give false here
16-
if (minor <= version.minor) {
17-
continue;
71+
}
72+
73+
function compareVersions(left, right) {
74+
if (!left) { return -1; }
75+
if (!right) { return 1; }
76+
if (left.major !== right.major) { return left.major - right.major; }
77+
if (left.minor !== right.minor) { return left.minor - right.minor; }
78+
if (left.patch !== right.patch) { return left.patch - right.patch; }
79+
80+
// non-prelease trumps prerelease
81+
if (left.prerelease[0] === undefined && left.prerelease[1] === undefined) { return 1; }
82+
if (right.prerelease[0] === undefined && right.prerelease[1] === undefined) { return -1; }
83+
84+
if (left.prerelease[0] !== right.prerelease[0]) { return left.prerelease[0] - right.prerelease[0]; }
85+
return left.prerelease[1] - right.prerelease[1];
86+
}
87+
88+
function makeOption(version, group) {
89+
return {version: version, label: 'v' + version.full, group: group || 'v' + version.major + '.' + version.minor};
90+
}
91+
92+
function reverse(fn) {
93+
return function(left, right) { return -fn(left, right); };
94+
}
95+
96+
function sortObject(obj, cmp) {
97+
return Object.keys(obj).map(function(key) { return obj[key]; }).sort(cmp);
98+
}
99+
100+
function find(collection, matcherFn) {
101+
for (var i = 0, ii = collection.length; i < ii; ++i) {
102+
if (matcherFn(collection[i])) {
103+
return collection[i];
104+
}
18105
}
19-
version.isLatest = true;
20-
minor = version.minor;
21106
}
107+
}])
22108

23-
$scope.getGroupName = function(v) {
24-
return v.isLatest ? 'Latest' : ('v' + v.major + '.' + v.minor + '.x');
25-
};
26109

27-
$scope.jumpToDocsVersion = function(version) {
28-
var currentPagePath = $location.path().replace(/\/$/, ''),
29-
url = '';
30-
if (version.isOldDocsUrl) {
31-
url = version.docsUrl;
110+
.controller('DocsVersionsCtrl', ['$scope', '$location', '$window', 'getVersions', function($scope, $location, $window, getVersions) {
111+
112+
getVersions().then(function(NG_VERSIONS) {
113+
$scope.version = NG_VERSIONS;
114+
});
115+
116+
$scope.jumpToDocsVersion = function(value) {
117+
var currentPagePath = $location.path().replace(/\/$/, '');
118+
var version = value.version;
119+
120+
var url = 'http://code.angularjs.org/' + version.full + '/docs';
121+
122+
// Versions before 1.0.2 had a different docs folder name
123+
if (version.major === 0 || (version.major === 1 && version.minor === 0 && version.patch < 2)) {
124+
url += '-' + version.version;
32125
} else {
33-
url = version.docsUrl + currentPagePath;
126+
url += currentPagePath;
34127
}
128+
35129
$window.location = url;
36130
};
37131
}]);

docs/config/templates/indexPage.template.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ <h4 class="search-results-group-heading">{{ key }}</h4>
166166
<div class="container main-grid main-header-grid">
167167
<div class="grid-left">
168168
<div ng-controller="DocsVersionsCtrl" class="picker version-picker">
169-
<select ng-options="v as (v.isSnapshot ? v.branch : ('v' + v.version)) group by getGroupName(v) for v in docs_versions"
170-
ng-model="docs_version"
171-
ng-change="jumpToDocsVersion(docs_version)"
169+
<select ng-options="v as v.label group by v.group for v in version.all"
170+
ng-model="version.current"
171+
ng-change="jumpToDocsVersion(version.current)"
172172
class="docs-version-jump">
173173
</select>
174174
</div>

docs/gulpfile.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ var bowerFolder = 'bower_components';
2222

2323
var src = 'app/src/**/*.js';
2424
var ignoredFiles = '!src/angular.bind.js';
25-
var assets = 'app/assets/**/*';
25+
var assets = [
26+
'app/assets/**/*',
27+
'../build/version.json'
28+
];
2629

2730

2831
var getMergedEslintConfig = function(filepath) {
@@ -98,8 +101,8 @@ gulp.task('assets', ['bower'], function() {
98101
var JS_EXT = /\.js$/;
99102
return merge(
100103
gulp.src(['img/**/*']).pipe(gulp.dest(outputFolder + '/img')),
101-
gulp.src([assets]).pipe(gulp.dest(outputFolder)),
102-
gulp.src([assets])
104+
gulp.src(assets).pipe(gulp.dest(outputFolder)),
105+
gulp.src(assets)
103106
.pipe(foreach(function(stream, file) {
104107
if (JS_EXT.test(file.relative)) {
105108
var minFile = file.relative.replace(JS_EXT, '.min.js');

lib/versions/version-info.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var _ = require('lodash');
88

99
var process = require('process');
1010
// We are only interested in whether this environment variable exists, hence the !!
11-
var NO_REMOTE_REQUESTS = !!process.env['NG1_BUILD_NO_REMOTE_VERSION_REQUESTS'];
11+
var NO_REMOTE_REQUESTS = true; //!!process.env['NG1_BUILD_NO_REMOTE_VERSION_REQUESTS'];
1212
var versionSource = NO_REMOTE_REQUESTS ? 'local' : 'remote';
1313

1414
var currentPackage, previousVersions, cdnVersion;

0 commit comments

Comments
 (0)