Skip to content

Commit 5dde946

Browse files
committed
Merge pull request #188 from stephenplusplus/permalink
site: support permalinks. fixes #184
2 parents 994da4e + 69bbaa5 commit 5dde946

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

docs/components/docs/docs.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ <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>
55-
<h3 ng-if="method.name[0].toUpperCase() !== method.name[0]">
57+
<h3
58+
class="method-heading"
59+
ng-if="method.name[0].toUpperCase() !== method.name[0]">
60+
<a
61+
class="permalink"
62+
ng-if="!noPermalink"
63+
ng-href="{{activeUrl + '/' + method.name}}">#</a>
5664
{{method.name}}
5765
</h3>
5866
<p ng-if="method.description" ng-bind-html="method.description"></p>

docs/components/docs/docs.js

+59-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: 'components/docs/docs.html',
107126
resolve: {
108127
methods: function($http, $sce) {
109128
return $http.get('json/index.json')
110-
.then(filterDocJson($sce));
129+
.then(filterDocJson($sce))
130+
.then(function(methods) {
131+
// Prevent displaying permalinks.
132+
// ** Can remove when PubSub api is documented **
133+
methods.noPermalink = true;
134+
return methods;
135+
});
111136
}
112137
}
113138
})
@@ -126,11 +151,34 @@ angular
126151
controller: 'DocsCtrl',
127152
templateUrl: 'components/docs/docs.html',
128153
resolve: {
129-
methods: function($q, $http, $route, $sce) {
154+
methods: function($q, $http, $route, $sce, $location) {
130155
var module = $route.current.params.module;
131156
var cl = $route.current.params.class;
157+
if (MODULE_TO_CLASSES[module].length > 0) {
158+
return $http
159+
.get('json/' + module + '/' + cl + '.json')
160+
.then(filterDocJson($sce));
161+
} else {
162+
// This is not a class, this is the name of a method.
163+
var method = cl;
164+
return $http.get('json/' + module + '/index.json')
165+
.then(filterDocJson($sce))
166+
.then(setMethod($location, method));
167+
}
168+
}
169+
}
170+
})
171+
.when('/docs/:module/:class/:method', {
172+
controller: 'DocsCtrl',
173+
templateUrl: 'components/docs/docs.html',
174+
resolve: {
175+
methods: function($q, $http, $route, $sce, $location) {
176+
var module = $route.current.params.module;
177+
var cl = $route.current.params.class;
178+
var method = $route.current.params.method;
132179
return $http.get('json/' + module + '/' + cl + '.json')
133-
.then(filterDocJson($sce));
180+
.then(filterDocJson($sce))
181+
.then(setMethod($location, method));
134182
}
135183
}
136184
});
@@ -139,13 +187,20 @@ angular
139187
'use strict';
140188

141189
$scope.isActiveUrl = function(url) {
142-
return url.replace(/^#/, '') === $location.path();
190+
var current = $location.path().replace('/' + methods.singleMethod, '');
191+
var link = url
192+
.replace(/^#/, '')
193+
.replace('/' + methods.singleMethod, '');
194+
return current === link;
143195
};
144196

145197
$scope.isActiveDoc = function(doc) {
146198
return doc.toLowerCase() === $routeParams.module;
147199
};
148200

201+
$scope.activeUrl = '#' + $location.path();
202+
$scope.singleMethod = methods.singleMethod;
203+
$scope.noPermalink = methods.singleMethod || methods.noPermalink;
149204
$scope.methods = methods;
150205
$scope.module = $routeParams.module;
151206
$scope.pages = [

docs/css/main.css

+22
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,28 @@ h2, h3 {
568568
border-bottom: 1px solid rgba(0,0,0,0.05);
569569
}
570570

571+
.method-heading {
572+
position: relative;
573+
}
574+
575+
.permalink {
576+
display: none;
577+
position: absolute;
578+
padding: 0 7px;
579+
left: -24px;
580+
text-decoration: none;
581+
color: #2b70e2;
582+
}
583+
584+
.permalink:hover {
585+
color: #4285f4;
586+
display: block;
587+
}
588+
589+
.method-heading:hover .permalink {
590+
display: block;
591+
}
592+
571593
/*
572594
Page Title
573595
*/

docs/home.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ angular
77
.when('/', {
88
templateUrl: '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)