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

Commit 74ae3ed

Browse files
matskoIgorMinar
authored andcommitted
chore(ngdocs): fix the version jumper
correct the ordering and make gen-docs prepare the list of versions during the build process
1 parent 699f86c commit 74ae3ed

File tree

4 files changed

+103
-157
lines changed

4 files changed

+103
-157
lines changed

docs/component-spec/versionJumpSpec.js

+3-98
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@ describe('DocsApp', function() {
77

88
beforeEach(function() {
99
module(function($provide) {
10-
$provide.value('NG_VERSIONS',[
11-
'1.0.0',
12-
'1.0.1',
13-
'1.0.2',
14-
'1.0.3',
15-
'1.0.4',
16-
'1.0.5',
17-
'1.0.6',
18-
'1.1.0',
19-
'1.1.1',
20-
'1.1.2',
21-
'1.1.3',
22-
'1.1.4',
23-
'2.1.3'
24-
]);
2510
$provide.value('$window', window = createMockWindow());
2611
});
2712
inject(function($controller, $rootScope) {
@@ -34,91 +19,11 @@ describe('DocsApp', function() {
3419
});
3520
});
3621

37-
it('should have the correct version of angular', function() {
38-
expect(version).toBe($scope.version);
39-
});
40-
41-
it('should order versions in decending order', function() {
42-
expect($scope.versions.length).toBeGreaterThan(0);
43-
44-
var one = $scope.versions[0].version;
45-
var two = $scope.versions[1].version;
46-
47-
expect(one).toBeGreaterThan(two);
48-
});
49-
50-
it('should list unstable versions at the top of the list', function() {
51-
expect($scope.versions[0].stable).toBe(false);
52-
});
53-
54-
it('should list all items below the last stable as stable regardless of version number', function() {
55-
var limit = $scope.versions.length - 1,
56-
lastUnstableIndex = 0;
57-
58-
while(lastUnstableIndex <= limit) {
59-
if($scope.versions[lastUnstableIndex++].stable) break;
60-
}
61-
62-
for(var i=lastUnstableIndex;i<=limit;i++) {
63-
expect($scope.versions[i].stable).toBe(true);
64-
}
65-
});
66-
6722
describe('changing the URL', function() {
68-
it('should not support the old < 1.0 docs pages', function() {
69-
window.location = 'old';
70-
71-
$scope.versions.unshift({
72-
stable : true,
73-
version : '0.9.10'
74-
});
75-
$scope.jumpToDocsVersion('0.9.10');
76-
expect(window.location).toBe('old');
77-
78-
$scope.versions.unshift({
79-
stable : true,
80-
version : '0.10.1'
81-
});
82-
$scope.jumpToDocsVersion('0.10.1');
83-
expect(window.location).toBe('old');
84-
85-
$scope.jumpToDocsVersion('2.1.3');
86-
expect(window.location).toBe('http://code.angularjs.org/2.1.3/docs');
87-
});
88-
89-
it('should jump to the older versions of current docs for version >= 1.0.2', function() {
90-
$scope.jumpToDocsVersion('1.0.1');
91-
expect(window.location).toBe('http://code.angularjs.org/1.0.1/docs-1.0.1');
92-
93-
$scope.jumpToDocsVersion('1.0.2');
94-
expect(window.location).toBe('http://code.angularjs.org/1.0.2/docs');
95-
96-
$scope.jumpToDocsVersion('1.1.2');
97-
expect(window.location).toBe('http://code.angularjs.org/1.1.2/docs');
23+
it('should jump to the url provided', function() {
24+
$scope.jumpToDocsVersion({ version: '1.0.1', url : 'page123'});
25+
expect(window.location).toBe('page123');
9826
});
99-
100-
it('should use the current docs.angularjs.org page when the selected version is the last stable version', function() {
101-
$scope.versions = [{
102-
stable : true,
103-
title : 'test',
104-
version : '1.1.1'
105-
}];
106-
107-
$scope.jumpToDocsVersion('1.1.1');
108-
expect(window.location).toBe('http://docs.angularjs.org');
109-
110-
$scope.versions.unshift({
111-
stable : true,
112-
title : 'test2',
113-
version : '1.2.1'
114-
});
115-
116-
$scope.jumpToDocsVersion('1.1.1');
117-
expect(window.location).toBe('http://code.angularjs.org/1.1.1/docs');
118-
$scope.jumpToDocsVersion('1.2.1');
119-
expect(window.location).toBe('http://docs.angularjs.org');
120-
});
121-
12227
});
12328
});
12429

docs/src/ngdoc.js

+93-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,99 @@ exports.ngVersions = function() {
4747
versions.push(matches[1]);
4848
}
4949
});
50-
versions.push(exports.ngCurrentVersion().full);
51-
return versions;
50+
51+
//match the future version of AngularJS that is set in the package.json file
52+
return expandVersions(sortVersionsNatrually(versions), exports.ngCurrentVersion().full);
53+
54+
function expandVersions(versions, latestVersion) {
55+
//copy the array to avoid changing the versions param data
56+
//the latest version is not on the git tags list, but
57+
//docs.angularjs.org will always point to master as of 1.2
58+
versions = versions.concat([latestVersion]);
59+
60+
var firstUnstable, expanded = [];
61+
for(var i=versions.length-1;i>=0;i--) {
62+
var version = versions[i],
63+
split = version.split('.'),
64+
isMaster = version == latestVersion,
65+
isStable = split[1] % 2 == 0;
66+
67+
var title = 'AngularJS - v' + version;
68+
69+
//anything that is stable before being unstable is a rc1 version
70+
//just like with AngularJS 1.2.0rc1 (even though it's apart of the
71+
//1.1.5 API
72+
if(isMaster || (isStable && !firstUnstable)) {
73+
isStable = false;
74+
}
75+
else {
76+
firstUnstable = firstUnstable || version;
77+
}
78+
79+
var docsPath = version < '1.0.2' ? 'docs-' + version : 'docs';
80+
81+
var url = isMaster ?
82+
'http://docs.angularjs.org' :
83+
'http://code.angularjs.org/' + version + '/' + docsPath;
84+
85+
expanded.push({
86+
version : version,
87+
stable : isStable,
88+
title : title,
89+
group : (isStable ? 'Stable' : 'Unstable'),
90+
url : url
91+
});
92+
};
93+
94+
return expanded;
95+
};
96+
97+
function sortVersionsNatrually(versions) {
98+
var versionMap = {},
99+
NON_RC_RELEASE_NUMBER = 999;
100+
for(var i = versions.length - 1; i >= 0; i--) {
101+
var version = versions[i];
102+
var split = version.split(/\.|rc/);
103+
var baseVersion = split[0] + '.' + split[1] + '.' + split[2];
104+
105+
//create a map of RC versions for each version
106+
//this way each RC version can be sorted in "natural" order
107+
versionMap[baseVersion] = versionMap[baseVersion] || [];
108+
109+
//NON_RC_RELEASE_NUMBER is used to signal the non-RC version for the release and
110+
//it will always appear at the top of the list since the number is so high!
111+
versionMap[baseVersion].push(
112+
version == baseVersion ? NON_RC_RELEASE_NUMBER : parseInt(version.match(/rc(\d+)/)[1]));
113+
};
114+
115+
//flatten the map so that the RC versions occur in a natural sorted order
116+
//and the official non-RC version shows up at the top of the list of sorted
117+
//RC versions!
118+
var angularVersions = [];
119+
sortedKeys(versionMap).forEach(function(key) {
120+
var versions = versionMap[key];
121+
122+
//basic numerical sort
123+
versions.sort(function(a,b) {
124+
return a - b;
125+
});
126+
127+
versions.forEach(function(v) {
128+
angularVersions.push(v == NON_RC_RELEASE_NUMBER ? key : key + 'rc' + v);
129+
});
130+
});
131+
132+
return angularVersions;
133+
};
134+
135+
function sortedKeys(obj) {
136+
var keys = [];
137+
for(var key in obj) {
138+
keys.push(key);
139+
};
140+
keys.sort(true);
141+
return keys;
142+
};
52143
};
53144

54145
exports.ngCurrentVersion = function() {

docs/src/templates/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ <h4>{{ key }}</h4>
250250
<div class="span3">
251251
<div class="well">
252252
<div ng-controller="DocsVersionsCtrl">
253-
<select ng-options="v.version as v.title group by v.group for v in versions"
254-
ng-model="version"
255-
ng-change="jumpToDocsVersion(version)"
253+
<select ng-options="v as v.title group by v.group for v in docs_versions"
254+
ng-model="docs_version"
255+
ng-change="jumpToDocsVersion(docs_version)"
256256
class="docs-version-jump">
257257
</select>
258258
</div>

docs/src/templates/js/docs.js

+4-54
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,10 @@ var docsApp = {
55
};
66

77
docsApp.controller.DocsVersionsCtrl = ['$scope', '$window', 'NG_VERSIONS', 'NG_VERSION', function($scope, $window, NG_VERSIONS, NG_VERSION) {
8-
$scope.versions = expandVersions(NG_VERSIONS);
9-
$scope.version = ($scope.version || NG_VERSION).match(/^([\d\.]+\d+\S+)/)[1]; //match only the number
10-
11-
$scope.jumpToDocsVersion = function(value) {
12-
var isLastStable,
13-
version,
14-
versions = $scope.versions;
15-
for(var i=versions.length-1;i>=0;i--) {
16-
var v = versions[i];
17-
if(v.version == value) {
18-
var next = versions[i - 1];
19-
isLastStable = v.stable && (!next || next && !next.stable);
20-
version = v;
21-
break;
22-
}
23-
};
24-
25-
if(version && version.version >= '1.0.0') {
26-
//the older versions have a different path to the docs within their repo directory
27-
var docsPath = version.version < '1.0.2' ? 'docs-' + version.version : 'docs';
28-
29-
//the last stable version should redirect to docs.angularjs.org instead of code.angularjs.org
30-
var url = 'http://' +
31-
(isLastStable ?
32-
'docs.angularjs.org' :
33-
'code.angularjs.org/' + version.version + '/' + docsPath);
34-
35-
$window.location = url;
36-
}
37-
};
38-
39-
function expandVersions(angularVersions) {
40-
var unstableVersionStart = 0;
41-
angularVersions.forEach(function(version) {
42-
var split = version.split('.');
43-
unstableVersionStart = split[1] % 2 == 1 ?
44-
Math.max(unstableVersionStart, parseInt(split[0] + '' + split[1])) :
45-
unstableVersionStart;
46-
});
47-
48-
var versions = [];
49-
for(var i=angularVersions.length-1;i>=0;i--) {
50-
var version = angularVersions[i];
51-
var split = version.split('.');
52-
var stable = parseInt(split[0] + '' + split[1]) < unstableVersionStart;
53-
versions.push({
54-
version : version,
55-
stable : stable,
56-
title : 'AngularJS - v' + version,
57-
group : (stable ? 'Stable' : 'Unstable')
58-
});
59-
};
60-
61-
return versions;
8+
$scope.docs_versions = NG_VERSIONS;
9+
$scope.docs_version = NG_VERSIONS[0];
10+
$scope.jumpToDocsVersion = function(version) {
11+
$window.location = version.url;
6212
};
6313
}];
6414

0 commit comments

Comments
 (0)