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

Commit

Permalink
chore(ngdocs): fix the version jumper
Browse files Browse the repository at this point in the history
correct the ordering and make gen-docs prepare the list of versions
during the build process
  • Loading branch information
matsko authored and IgorMinar committed Aug 23, 2013
1 parent 699f86c commit 74ae3ed
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 157 deletions.
101 changes: 3 additions & 98 deletions docs/component-spec/versionJumpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ describe('DocsApp', function() {

beforeEach(function() {
module(function($provide) {
$provide.value('NG_VERSIONS',[
'1.0.0',
'1.0.1',
'1.0.2',
'1.0.3',
'1.0.4',
'1.0.5',
'1.0.6',
'1.1.0',
'1.1.1',
'1.1.2',
'1.1.3',
'1.1.4',
'2.1.3'
]);
$provide.value('$window', window = createMockWindow());
});
inject(function($controller, $rootScope) {
Expand All @@ -34,91 +19,11 @@ describe('DocsApp', function() {
});
});

it('should have the correct version of angular', function() {
expect(version).toBe($scope.version);
});

it('should order versions in decending order', function() {
expect($scope.versions.length).toBeGreaterThan(0);

var one = $scope.versions[0].version;
var two = $scope.versions[1].version;

expect(one).toBeGreaterThan(two);
});

it('should list unstable versions at the top of the list', function() {
expect($scope.versions[0].stable).toBe(false);
});

it('should list all items below the last stable as stable regardless of version number', function() {
var limit = $scope.versions.length - 1,
lastUnstableIndex = 0;

while(lastUnstableIndex <= limit) {
if($scope.versions[lastUnstableIndex++].stable) break;
}

for(var i=lastUnstableIndex;i<=limit;i++) {
expect($scope.versions[i].stable).toBe(true);
}
});

describe('changing the URL', function() {
it('should not support the old < 1.0 docs pages', function() {
window.location = 'old';

$scope.versions.unshift({
stable : true,
version : '0.9.10'
});
$scope.jumpToDocsVersion('0.9.10');
expect(window.location).toBe('old');

$scope.versions.unshift({
stable : true,
version : '0.10.1'
});
$scope.jumpToDocsVersion('0.10.1');
expect(window.location).toBe('old');

$scope.jumpToDocsVersion('2.1.3');
expect(window.location).toBe('http://code.angularjs.org/2.1.3/docs');
});

it('should jump to the older versions of current docs for version >= 1.0.2', function() {
$scope.jumpToDocsVersion('1.0.1');
expect(window.location).toBe('http://code.angularjs.org/1.0.1/docs-1.0.1');

$scope.jumpToDocsVersion('1.0.2');
expect(window.location).toBe('http://code.angularjs.org/1.0.2/docs');

$scope.jumpToDocsVersion('1.1.2');
expect(window.location).toBe('http://code.angularjs.org/1.1.2/docs');
it('should jump to the url provided', function() {
$scope.jumpToDocsVersion({ version: '1.0.1', url : 'page123'});
expect(window.location).toBe('page123');
});

it('should use the current docs.angularjs.org page when the selected version is the last stable version', function() {
$scope.versions = [{
stable : true,
title : 'test',
version : '1.1.1'
}];

$scope.jumpToDocsVersion('1.1.1');
expect(window.location).toBe('http://docs.angularjs.org');

$scope.versions.unshift({
stable : true,
title : 'test2',
version : '1.2.1'
});

$scope.jumpToDocsVersion('1.1.1');
expect(window.location).toBe('http://code.angularjs.org/1.1.1/docs');
$scope.jumpToDocsVersion('1.2.1');
expect(window.location).toBe('http://docs.angularjs.org');
});

});
});

Expand Down
95 changes: 93 additions & 2 deletions docs/src/ngdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,99 @@ exports.ngVersions = function() {
versions.push(matches[1]);
}
});
versions.push(exports.ngCurrentVersion().full);
return versions;

//match the future version of AngularJS that is set in the package.json file
return expandVersions(sortVersionsNatrually(versions), exports.ngCurrentVersion().full);

function expandVersions(versions, latestVersion) {
//copy the array to avoid changing the versions param data
//the latest version is not on the git tags list, but
//docs.angularjs.org will always point to master as of 1.2
versions = versions.concat([latestVersion]);

var firstUnstable, expanded = [];
for(var i=versions.length-1;i>=0;i--) {
var version = versions[i],
split = version.split('.'),
isMaster = version == latestVersion,
isStable = split[1] % 2 == 0;

var title = 'AngularJS - v' + version;

//anything that is stable before being unstable is a rc1 version
//just like with AngularJS 1.2.0rc1 (even though it's apart of the
//1.1.5 API
if(isMaster || (isStable && !firstUnstable)) {
isStable = false;
}
else {
firstUnstable = firstUnstable || version;
}

var docsPath = version < '1.0.2' ? 'docs-' + version : 'docs';

var url = isMaster ?
'http://docs.angularjs.org' :
'http://code.angularjs.org/' + version + '/' + docsPath;

expanded.push({
version : version,
stable : isStable,
title : title,
group : (isStable ? 'Stable' : 'Unstable'),
url : url
});
};

return expanded;
};

function sortVersionsNatrually(versions) {
var versionMap = {},
NON_RC_RELEASE_NUMBER = 999;
for(var i = versions.length - 1; i >= 0; i--) {
var version = versions[i];
var split = version.split(/\.|rc/);
var baseVersion = split[0] + '.' + split[1] + '.' + split[2];

//create a map of RC versions for each version
//this way each RC version can be sorted in "natural" order
versionMap[baseVersion] = versionMap[baseVersion] || [];

//NON_RC_RELEASE_NUMBER is used to signal the non-RC version for the release and
//it will always appear at the top of the list since the number is so high!
versionMap[baseVersion].push(
version == baseVersion ? NON_RC_RELEASE_NUMBER : parseInt(version.match(/rc(\d+)/)[1]));
};

//flatten the map so that the RC versions occur in a natural sorted order
//and the official non-RC version shows up at the top of the list of sorted
//RC versions!
var angularVersions = [];
sortedKeys(versionMap).forEach(function(key) {
var versions = versionMap[key];

//basic numerical sort
versions.sort(function(a,b) {
return a - b;
});

versions.forEach(function(v) {
angularVersions.push(v == NON_RC_RELEASE_NUMBER ? key : key + 'rc' + v);
});
});

return angularVersions;
};

function sortedKeys(obj) {
var keys = [];
for(var key in obj) {
keys.push(key);
};
keys.sort(true);
return keys;
};
};

exports.ngCurrentVersion = function() {
Expand Down
6 changes: 3 additions & 3 deletions docs/src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ <h4>{{ key }}</h4>
<div class="span3">
<div class="well">
<div ng-controller="DocsVersionsCtrl">
<select ng-options="v.version as v.title group by v.group for v in versions"
ng-model="version"
ng-change="jumpToDocsVersion(version)"
<select ng-options="v as v.title group by v.group for v in docs_versions"
ng-model="docs_version"
ng-change="jumpToDocsVersion(docs_version)"
class="docs-version-jump">
</select>
</div>
Expand Down
58 changes: 4 additions & 54 deletions docs/src/templates/js/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,10 @@ var docsApp = {
};

docsApp.controller.DocsVersionsCtrl = ['$scope', '$window', 'NG_VERSIONS', 'NG_VERSION', function($scope, $window, NG_VERSIONS, NG_VERSION) {
$scope.versions = expandVersions(NG_VERSIONS);
$scope.version = ($scope.version || NG_VERSION).match(/^([\d\.]+\d+\S+)/)[1]; //match only the number

$scope.jumpToDocsVersion = function(value) {
var isLastStable,
version,
versions = $scope.versions;
for(var i=versions.length-1;i>=0;i--) {
var v = versions[i];
if(v.version == value) {
var next = versions[i - 1];
isLastStable = v.stable && (!next || next && !next.stable);
version = v;
break;
}
};

if(version && version.version >= '1.0.0') {
//the older versions have a different path to the docs within their repo directory
var docsPath = version.version < '1.0.2' ? 'docs-' + version.version : 'docs';

//the last stable version should redirect to docs.angularjs.org instead of code.angularjs.org
var url = 'http://' +
(isLastStable ?
'docs.angularjs.org' :
'code.angularjs.org/' + version.version + '/' + docsPath);

$window.location = url;
}
};

function expandVersions(angularVersions) {
var unstableVersionStart = 0;
angularVersions.forEach(function(version) {
var split = version.split('.');
unstableVersionStart = split[1] % 2 == 1 ?
Math.max(unstableVersionStart, parseInt(split[0] + '' + split[1])) :
unstableVersionStart;
});

var versions = [];
for(var i=angularVersions.length-1;i>=0;i--) {
var version = angularVersions[i];
var split = version.split('.');
var stable = parseInt(split[0] + '' + split[1]) < unstableVersionStart;
versions.push({
version : version,
stable : stable,
title : 'AngularJS - v' + version,
group : (stable ? 'Stable' : 'Unstable')
});
};

return versions;
$scope.docs_versions = NG_VERSIONS;
$scope.docs_version = NG_VERSIONS[0];
$scope.jumpToDocsVersion = function(version) {
$window.location = version.url;
};
}];

Expand Down

0 comments on commit 74ae3ed

Please sign in to comment.