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

Commit 4928d1d

Browse files
chore(docs): improve version picker
1 parent 4aa9534 commit 4928d1d

File tree

3 files changed

+125
-29
lines changed

3 files changed

+125
-29
lines changed

docs/app/src/versions.js

Lines changed: 116 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,129 @@
22

33
angular.module('versions', [])
44

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

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

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

docs/config/templates/indexPage.template.html

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 3 deletions
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');

0 commit comments

Comments
 (0)