Skip to content

Commit 11f7a25

Browse files
site: support permalinks. fixes googleapis#184
1 parent b2721e0 commit 11f7a25

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

docs/components/docs/docs.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ <h1>{{module[0].toUpperCase() + module.substr(1)}}</h1>
4848
</article>
4949
</article>
5050

51-
<article ng-repeat="method in methods" id="{{method.name}}">
51+
<article
52+
ng-repeat="method in methods"
53+
ng-hide="singleMethod && method.name !== singleMethod">
5254
<h2 ng-if="method.name[0].toUpperCase() === method.name[0]">
5355
{{method.name}}
5456
</h2>
5557
<h3 ng-if="method.name[0].toUpperCase() !== method.name[0]">
56-
{{method.name}}
58+
<span ng-if="noHeadingLink">{{method.name}}</span>
59+
<a ng-if="!noHeadingLink" ng-href="{{activeUrl + '/' + method.name}}">
60+
{{method.name}}
61+
</a>
5762
</h3>
5863
<p ng-if="method.description" ng-bind-html="method.description"></p>
5964
<h4 ng-show="method.params">Parameters</h4>

docs/components/docs/docs.js

+61-4
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,39 @@ angular
100100
};
101101
}
102102

103+
function setMethod($location, methodName) {
104+
return function(methods) {
105+
var methodExists = methods.some(function(methodObj) {
106+
return methodName === methodObj.name;
107+
});
108+
if (methodExists) {
109+
methods.singleMethod = methodName;
110+
return methods;
111+
} else {
112+
$location.path('/docs/' + module + '/' + cl);
113+
}
114+
};
115+
}
116+
117+
var MODULE_TO_CLASSES = {
118+
datastore: ['dataset', 'query'],
119+
storage: []
120+
};
121+
103122
$routeProvider
104123
.when('/docs', {
105124
controller: 'DocsCtrl',
106125
templateUrl: '/gcloud-node/components/docs/docs.html',
107126
resolve: {
108127
methods: function($http, $sce) {
109128
return $http.get('/gcloud-node/json/index.json')
110-
.then(filterDocJson($sce));
129+
.then(filterDocJson($sce))
130+
.then(function(methods) {
131+
// Prevent headings from turning into links.
132+
// ** Can remove when PubSub api is documented **
133+
methods.noHeadingLink = true;
134+
return methods;
135+
});
111136
}
112137
}
113138
})
@@ -117,6 +142,9 @@ angular
117142
resolve: {
118143
methods: function($http, $route, $sce) {
119144
var module = $route.current.params.module;
145+
if (!MODULE_TO_CLASSES[module]) {
146+
return [];
147+
}
120148
return $http.get('/gcloud-node/json/' + module + '/index.json')
121149
.then(filterDocJson($sce));
122150
}
@@ -126,11 +154,33 @@ angular
126154
controller: 'DocsCtrl',
127155
templateUrl: '/gcloud-node/components/docs/docs.html',
128156
resolve: {
129-
methods: function($q, $http, $route, $sce) {
157+
methods: function($q, $http, $route, $sce, $location) {
130158
var module = $route.current.params.module;
131159
var cl = $route.current.params.class;
160+
if (MODULE_TO_CLASSES[module].length > 0) {
161+
return $http
162+
.get('/gcloud-node/json/' + module + '/' + cl + '.json')
163+
.then(filterDocJson($sce));
164+
} else {
165+
var method = cl;
166+
return $http.get('/gcloud-node/json/' + module + '/index.json')
167+
.then(filterDocJson($sce))
168+
.then(setMethod($location, method));
169+
}
170+
}
171+
}
172+
})
173+
.when('/docs/:module/:class/:method', {
174+
controller: 'DocsCtrl',
175+
templateUrl: '/gcloud-node/components/docs/docs.html',
176+
resolve: {
177+
methods: function($q, $http, $route, $sce, $location) {
178+
var module = $route.current.params.module;
179+
var cl = $route.current.params.class;
180+
var method = $route.current.params.method;
132181
return $http.get('/gcloud-node/json/' + module + '/' + cl + '.json')
133-
.then(filterDocJson($sce));
182+
.then(filterDocJson($sce))
183+
.then(setMethod($location, method));
134184
}
135185
}
136186
});
@@ -139,13 +189,20 @@ angular
139189
'use strict';
140190

141191
$scope.isActiveUrl = function(url) {
142-
return url.replace(/^\/gcloud-node\/#/, '') === $location.path();
192+
var current = $location.path().replace('/' + methods.singleMethod, '');
193+
var link = url
194+
.replace(/^\/gcloud-node\/#/, '')
195+
.replace('/' + methods.singleMethod, '');
196+
return current === link;
143197
};
144198

145199
$scope.isActiveDoc = function(doc) {
146200
return doc.toLowerCase() === $routeParams.module;
147201
};
148202

203+
$scope.activeUrl = '/gcloud-node/#' + $location.path();
204+
$scope.singleMethod = methods.singleMethod;
205+
$scope.noHeadingLink = methods.singleMethod || methods.noHeadingLink;
149206
$scope.methods = methods;
150207
$scope.module = $routeParams.module;
151208
$scope.pages = [

docs/home.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ angular
77
.when('/', {
88
templateUrl: '/gcloud-node/home.html'
99
});
10+
})
11+
.run(function($rootScope, $location) {
12+
'use strict';
13+
14+
$rootScope.$on('$routeChangeStart', function(event) {
15+
var hash = $location.hash();
16+
if (hash) {
17+
event.preventDefault();
18+
$location.hash('');
19+
$location.replace().path($location.path() + '/' + hash);
20+
}
21+
});
1022
});

0 commit comments

Comments
 (0)