From 11f7a25d9f4e0bc6e667929f97b3894313f121db Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 5 Sep 2014 20:25:12 -0400 Subject: [PATCH] site: support permalinks. fixes #184 --- docs/components/docs/docs.html | 9 +++-- docs/components/docs/docs.js | 65 +++++++++++++++++++++++++++++++--- docs/home.js | 12 +++++++ 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/docs/components/docs/docs.html b/docs/components/docs/docs.html index 9184f13f685..d303581583c 100644 --- a/docs/components/docs/docs.html +++ b/docs/components/docs/docs.html @@ -48,12 +48,17 @@

{{module[0].toUpperCase() + module.substr(1)}}

-
+

{{method.name}}

- {{method.name}} + {{method.name}} + + {{method.name}} +

Parameters

diff --git a/docs/components/docs/docs.js b/docs/components/docs/docs.js index 7204d6b79d5..398c50aac24 100644 --- a/docs/components/docs/docs.js +++ b/docs/components/docs/docs.js @@ -100,6 +100,25 @@ angular }; } + function setMethod($location, methodName) { + return function(methods) { + var methodExists = methods.some(function(methodObj) { + return methodName === methodObj.name; + }); + if (methodExists) { + methods.singleMethod = methodName; + return methods; + } else { + $location.path('/docs/' + module + '/' + cl); + } + }; + } + + var MODULE_TO_CLASSES = { + datastore: ['dataset', 'query'], + storage: [] + }; + $routeProvider .when('/docs', { controller: 'DocsCtrl', @@ -107,7 +126,13 @@ angular resolve: { methods: function($http, $sce) { return $http.get('/gcloud-node/json/index.json') - .then(filterDocJson($sce)); + .then(filterDocJson($sce)) + .then(function(methods) { + // Prevent headings from turning into links. + // ** Can remove when PubSub api is documented ** + methods.noHeadingLink = true; + return methods; + }); } } }) @@ -117,6 +142,9 @@ angular resolve: { methods: function($http, $route, $sce) { var module = $route.current.params.module; + if (!MODULE_TO_CLASSES[module]) { + return []; + } return $http.get('/gcloud-node/json/' + module + '/index.json') .then(filterDocJson($sce)); } @@ -126,11 +154,33 @@ angular controller: 'DocsCtrl', templateUrl: '/gcloud-node/components/docs/docs.html', resolve: { - methods: function($q, $http, $route, $sce) { + methods: function($q, $http, $route, $sce, $location) { var module = $route.current.params.module; var cl = $route.current.params.class; + if (MODULE_TO_CLASSES[module].length > 0) { + return $http + .get('/gcloud-node/json/' + module + '/' + cl + '.json') + .then(filterDocJson($sce)); + } else { + var method = cl; + return $http.get('/gcloud-node/json/' + module + '/index.json') + .then(filterDocJson($sce)) + .then(setMethod($location, method)); + } + } + } + }) + .when('/docs/:module/:class/:method', { + controller: 'DocsCtrl', + templateUrl: '/gcloud-node/components/docs/docs.html', + resolve: { + methods: function($q, $http, $route, $sce, $location) { + var module = $route.current.params.module; + var cl = $route.current.params.class; + var method = $route.current.params.method; return $http.get('/gcloud-node/json/' + module + '/' + cl + '.json') - .then(filterDocJson($sce)); + .then(filterDocJson($sce)) + .then(setMethod($location, method)); } } }); @@ -139,13 +189,20 @@ angular 'use strict'; $scope.isActiveUrl = function(url) { - return url.replace(/^\/gcloud-node\/#/, '') === $location.path(); + var current = $location.path().replace('/' + methods.singleMethod, ''); + var link = url + .replace(/^\/gcloud-node\/#/, '') + .replace('/' + methods.singleMethod, ''); + return current === link; }; $scope.isActiveDoc = function(doc) { return doc.toLowerCase() === $routeParams.module; }; + $scope.activeUrl = '/gcloud-node/#' + $location.path(); + $scope.singleMethod = methods.singleMethod; + $scope.noHeadingLink = methods.singleMethod || methods.noHeadingLink; $scope.methods = methods; $scope.module = $routeParams.module; $scope.pages = [ diff --git a/docs/home.js b/docs/home.js index 1968653ad1c..672f5caf7bb 100644 --- a/docs/home.js +++ b/docs/home.js @@ -7,4 +7,16 @@ angular .when('/', { templateUrl: '/gcloud-node/home.html' }); + }) + .run(function($rootScope, $location) { + 'use strict'; + + $rootScope.$on('$routeChangeStart', function(event) { + var hash = $location.hash(); + if (hash) { + event.preventDefault(); + $location.hash(''); + $location.replace().path($location.path() + '/' + hash); + } + }); });