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

chore(docs-app): allow switching to newer versions #15281

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = function(grunt) {
NG_VERSION.cdn = versionInfo.cdnVersion;
var dist = 'angular-' + NG_VERSION.full;

var NG_VERSIONS = versionInfo.previousVersions;

if (versionInfo.cdnVersion == null) {
throw new Error('Unable to read CDN version, are you offline or has the CDN not been properly pushed?');
}
Expand Down Expand Up @@ -301,7 +303,8 @@ module.exports = function(grunt) {

write: {
versionTXT: {file: 'build/version.txt', val: NG_VERSION.full},
versionJSON: {file: 'build/version.json', val: JSON.stringify(NG_VERSION)}
versionJSON: {file: 'build/version.json', val: JSON.stringify(NG_VERSION)},
versionsJSON: {file: 'build/versions.json', val: JSON.stringify(NG_VERSIONS.concat([NG_VERSION]))}
},

bump: {
Expand Down
70 changes: 52 additions & 18 deletions docs/app/src/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,70 @@

angular.module('versions', [])

.controller('DocsVersionsCtrl', ['$scope', '$location', '$window', 'NG_VERSIONS', function($scope, $location, $window, NG_VERSIONS) {
.controller('DocsVersionsCtrl', [
'$scope',
'$location',
'$window',
'$http',
'$q',
'NG_VERSIONS', function($scope, $location, $window, $http, $q, NG_VERSIONS) {

$scope.docs_version = NG_VERSIONS[0];
$scope.docs_versions = 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;
// If this is not the snapshot version, request the snapshot's versions data
// to fill the version list with current data
$q(function(resolve, reject) {
if ($scope.docs_version.isSnapshot) {
reject();
} else {
resolve();
}
version.isLatest = true;
minor = version.minor;
}
}).then(function() {
return $http.get('../snapshot/versions_data.json');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this relative URL work when it is running from the root of https://docs.angularjs.org?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. So for https://docs.angularjs.org (which is the snapshot) we cannot use this relative path. At the moment, it doesn't matter, because we only get the snapshot versions-data when we are not on the snapshot.

For all other versions the root is https://code.angularjs.org//docs. In that case, this should be either ../../snapshot/versions_data.json or /snapshot/versions_data.json.

However, since we want to point docs.angularjs.org to the latest stable, an absolute url is probably the best idea. At the moment, this doesn't work because of CORS, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this fail silently when people run the docs app locally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It will also fail silently if the snapshot json data can't be requested for some reason. It's strictly an optional enhancement.

}).then(function(response) {
$scope.docs_versions = response.data;
}).catch(function() {
// Ignore error. This either means this is already the snapshot version or that
// the requested versions-data file could not be found
}).finally(function() {
setLatestVersion($scope.docs_versions);
});

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

$scope.jumpToDocsVersion = function(version) {
var currentPagePath = $location.path().replace(/\/$/, ''),
url = '';
if (version.isOldDocsUrl) {
url = version.docsUrl;
} else {
url = version.docsUrl + currentPagePath;
url = version.docsUrl;

//Workaround for an ngOptions quirk that can cause ngChange to be called
//with the same version
if (url === window.location + '') {
return;
}

if (!version.isOldDocsUrl) {
url += currentPagePath;
}

$window.location = url;
};

function setLatestVersion(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;
}
}
}]);
2 changes: 1 addition & 1 deletion docs/config/templates/indexPage.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ <h4 class="search-results-group-heading">{{ key }}</h4>
<div class="container main-grid main-header-grid">
<div class="grid-left">
<div ng-controller="DocsVersionsCtrl" class="picker version-picker">
<select ng-options="v as (v.isSnapshot ? v.branch : ('v' + v.version)) group by getGroupName(v) for v in docs_versions"
<select ng-options="v as (v.isSnapshot ? v.branch : ('v' + v.version)) group by getGroupName(v) for v in docs_versions track by v.version"
ng-model="docs_version"
ng-change="jumpToDocsVersion(docs_version)"
class="docs-version-jump">
Expand Down