-
Notifications
You must be signed in to change notification settings - Fork 27.4k
chore(docs): improve version picker #15385
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
'use strict'; | ||
/* global console */ | ||
|
||
angular.module('versions', []) | ||
angular.module('versions', ['currentVersionData', 'allVersionsData']) | ||
|
||
.controller('DocsVersionsCtrl', ['$scope', '$location', '$window', 'NG_VERSIONS', function($scope, $location, $window, NG_VERSIONS) { | ||
$scope.docs_version = NG_VERSIONS[0]; | ||
$scope.docs_versions = NG_VERSIONS; | ||
.directive('versionPicker', function() { | ||
return { | ||
restrict: 'E', | ||
scope: true, | ||
controllerAs: '$ctrl', | ||
controller: ['$location', '$window', 'CURRENT_NG_VERSION', 'ALL_NG_VERSIONS', | ||
/** @this VersionPickerController */ | ||
function VersionPickerController($location, $window, CURRENT_NG_VERSION, ALL_NG_VERSIONS) { | ||
|
||
for (var i = 0, minor = NaN; i < NG_VERSIONS.length; i++) { | ||
var version = NG_VERSIONS[i]; | ||
if (version.isSnapshot) { | ||
version.isLatest = true; | ||
continue; | ||
} | ||
// NaN will give false here | ||
if (minor <= version.minor) { | ||
continue; | ||
} | ||
version.isLatest = true; | ||
minor = version.minor; | ||
} | ||
var versionStr = CURRENT_NG_VERSION.isSnapshot ? 'snapshot' : CURRENT_NG_VERSION.version; | ||
|
||
$scope.getGroupName = function(v) { | ||
return v.isLatest ? 'Latest' : ('v' + v.major + '.' + v.minor + '.x'); | ||
this.versions = ALL_NG_VERSIONS; | ||
this.selectedVersion = find(ALL_NG_VERSIONS, function(value) { return value.version.version === versionStr; }); | ||
|
||
this.jumpToDocsVersion = function(value) { | ||
var currentPagePath = $location.path().replace(/\/$/, ''); | ||
$window.location = value.docsUrl + currentPagePath; | ||
}; | ||
}], | ||
template: | ||
'<div class="picker version-picker">' + | ||
' <select ng-options="v as v.label group by v.group for v in $ctrl.versions"' + | ||
' ng-model="$ctrl.selectedVersion"' + | ||
' ng-change="$ctrl.jumpToDocsVersion($ctrl.selectedVersion)"' + | ||
' class="docs-version-jump">' + | ||
' </select>' + | ||
'</div>' | ||
}; | ||
|
||
$scope.jumpToDocsVersion = function(version) { | ||
var currentPagePath = $location.path().replace(/\/$/, ''), | ||
url = ''; | ||
if (version.isOldDocsUrl) { | ||
url = version.docsUrl; | ||
} else { | ||
url = version.docsUrl + currentPagePath; | ||
function find(collection, matcherFn) { | ||
for (var i = 0, ii = collection.length; i < ii; ++i) { | ||
if (matcherFn(collection[i])) { | ||
return collection[i]; | ||
} | ||
} | ||
$window.location = url; | ||
}; | ||
}]); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var exec = require('shelljs').exec; | ||
var semver = require('semver'); | ||
|
||
/** | ||
* @dgProcessor generateVersionDocProcessor | ||
|
@@ -12,23 +13,96 @@ module.exports = function generateVersionDocProcessor(gitData) { | |
return { | ||
$runAfter: ['generatePagesDataProcessor'], | ||
$runBefore: ['rendering-docs'], | ||
// the blacklist is to remove rogue builds that are in npm but not on code.angularjs.org | ||
blacklist: ['1.3.4-build.3588'], | ||
$process: function(docs) { | ||
|
||
var versionDoc = { | ||
docType: 'versions-data', | ||
id: 'versions-data', | ||
template: 'versions-data.template.js', | ||
outputPath: 'js/versions-data.js', | ||
currentVersion: gitData.version | ||
}; | ||
|
||
versionDoc.versions = _(gitData.versions) | ||
.filter(function(version) { return version.major > 0; }) | ||
.push(gitData.version) | ||
.reverse() | ||
.value(); | ||
|
||
docs.push(versionDoc); | ||
var blacklist = this.blacklist; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the meaning of the blacklist? Can you please add a comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. The main reason was that we appear to have npm published a random rogue release, 1.3.4-build.3588, which does not have an associated folder on code.angularjs.org |
||
var currentVersion = require('../../../build/version.json'); | ||
var output = exec('npm info angular versions --json', { silent: true }).stdout; | ||
var allVersions = processAllVersionsResponse(JSON.parse(output)); | ||
|
||
docs.push({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we call this current-version-data? This makes it clearer what's the difference from version-data.js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I agree that the visual difference between versions and version is not that obvious |
||
docType: 'current-version-data', | ||
id: 'current-version-data', | ||
template: 'angular-service.template.js', | ||
outputPath: 'js/current-version-data.js', | ||
ngModuleName: 'currentVersionData', | ||
serviceName: 'CURRENT_NG_VERSION', | ||
serviceValue: currentVersion | ||
}); | ||
|
||
docs.push({ | ||
docType: 'allversions-data', | ||
id: 'allversions-data', | ||
template: 'angular-service.template.js', | ||
outputPath: 'js/all-versions-data.js', | ||
ngModuleName: 'allVersionsData', | ||
serviceName: 'ALL_NG_VERSIONS', | ||
serviceValue: allVersions | ||
}); | ||
|
||
|
||
function processAllVersionsResponse(versions) { | ||
|
||
var latestMap = {}; | ||
|
||
versions = versions | ||
.filter(function(versionStr) { | ||
return blacklist.indexOf(versionStr) === -1; | ||
}) | ||
.map(function(versionStr) { | ||
return semver.parse(versionStr); | ||
}) | ||
.filter(function(version) { | ||
return version && version.major > 0; | ||
}) | ||
.map(function(version) { | ||
var key = version.major + '.' + version.minor; | ||
var latest = latestMap[key]; | ||
if (!latest || version.compare(latest) > 0) { | ||
latestMap[key] = version; | ||
} | ||
return version; | ||
}) | ||
.map(function(version) { | ||
return makeOption(version); | ||
}) | ||
.reverse(); | ||
|
||
var latest = sortObject(latestMap, reverse(semver.compare)) | ||
.map(function(version) { return makeOption(version, 'Latest'); }); | ||
|
||
return [makeOption({version: 'snapshot'}, 'Latest', 'master')] | ||
.concat(latest) | ||
.concat(versions); | ||
} | ||
|
||
function makeOption(version, group, label) { | ||
return { | ||
version: version, | ||
label: label || 'v' + version.raw, | ||
group: group || 'v' + version.major + '.' + version.minor, | ||
docsUrl: createDocsUrl(version) | ||
}; | ||
} | ||
|
||
function createDocsUrl(version) { | ||
var url = 'https://code.angularjs.org/' + version.version + '/docs'; | ||
// Versions before 1.0.2 had a different docs folder name | ||
if (version.major === 1 && version.minor === 0 && version.patch < 2) { | ||
url += '-' + version.version; | ||
} | ||
return url; | ||
} | ||
|
||
function reverse(fn) { | ||
return function(left, right) { return -fn(left, right); }; | ||
} | ||
|
||
function sortObject(obj, cmp) { | ||
return Object.keys(obj).map(function(key) { return obj[key]; }).sort(cmp); | ||
} | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
angular.module('{$ doc.ngModuleName $}', []) | ||
.value('{$ doc.serviceName $}', {$ doc.serviceValue | json $}); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because the version.json file is generated by the
write
grunt task. So we need it to run before thedocs
task, which now relies upon it.