From 079101f36d1f460215fa8bfb3f8b75d0cf5b6527 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Mon, 17 Oct 2016 15:35:32 +0200 Subject: [PATCH] chore(docs-app): allow switching to newer versions With this patch, the docs app will request available versions data from the snapshot, which are now stored in a separate versions-data.json file. This only affects docs apps that contain this commit, so older doc apps will still only display the versions up to their own version. --- Gruntfile.js | 5 +- docs/app/src/versions.js | 70 ++++++++++++++----- docs/config/templates/indexPage.template.html | 2 +- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 5b31c8ea24be..e44e3a7b3621 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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?'); } @@ -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: { diff --git a/docs/app/src/versions.js b/docs/app/src/versions.js index 04abeedc5615..dea0b0e43a00 100644 --- a/docs/app/src/versions.js +++ b/docs/app/src/versions.js @@ -2,23 +2,35 @@ 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'); + }).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'); @@ -26,12 +38,34 @@ angular.module('versions', []) $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; + } + } }]); diff --git a/docs/config/templates/indexPage.template.html b/docs/config/templates/indexPage.template.html index a97fc9e6ed55..04fe4925afd7 100644 --- a/docs/config/templates/indexPage.template.html +++ b/docs/config/templates/indexPage.template.html @@ -166,7 +166,7 @@

{{ key }}

-