Skip to content

Commit 257e250

Browse files
author
Manuel Mujica
committed
Fix sidebar menu
1 parent dbde37e commit 257e250

File tree

5 files changed

+192
-85
lines changed

5 files changed

+192
-85
lines changed

make-example.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ var fs = require("fs");
44
var readFile = Q.denodeify(fs.readFile);
55
var path = require("path");
66

7-
var docMap = readFile(__dirname+"/docMap.json").then(function(source){
8-
return JSON.parse(""+source);
7+
var docMap = readFile(path.join(__dirname, "docMap.json"))
8+
.then(function(source) {
9+
return JSON.parse(""+source);
910
});
1011

1112
var forceBuild = process.argv.indexOf("-f") !== -1;
1213

13-
generate(docMap,{
14-
html: {
15-
templates: path.join(__dirname, "templates"),
16-
dependencies: {
17-
"bit-docs-docjs-theme": __dirname,
18-
"bit-docs-prettify": "^0.1.0",
14+
generate(docMap, {
15+
html: {
16+
templates: path.join(__dirname, "templates"),
17+
dependencies: {
18+
"bit-docs-docjs-theme": __dirname,
19+
"bit-docs-prettify": "^0.1.0",
1920
"bit-docs-html-highlight-line": "^0.2.2"
20-
}
21-
},
22-
dest: path.join(__dirname, "temp"),
23-
parent: "StealJS",
24-
forceBuild: forceBuild,
21+
}
22+
},
23+
dest: path.join(__dirname, "temp"),
24+
parent: "StealJS",
25+
forceBuild: forceBuild,
2526
minifyBuild: false,
26-
debug: true
27-
}).done();
27+
debug: true
28+
})
29+
.catch(function(error) {
30+
console.error(error);
31+
});

templates/active-menu.mustache

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
{{#if children.length}}
2-
<ul>
3-
{{#eachOrderedChildren .}}
4-
{{^if hide}}
5-
{{#ifGroup}}
6-
<li class="heading search-container">
7-
<span>{{makeTitle}}</span>
8-
{{> active-menu.mustache}}
1+
{{#with (makeChildrenContext .)}}
2+
{{#if children.length}}
3+
<ul>
4+
{{#eachOrderedChildren children}}
5+
{{^if hide}}
6+
{{#ifGroup}}
7+
<li class="heading search-container">
8+
<span>{{makeTitle}}</span>
9+
{{> active-menu.mustache}}
10+
</li>
11+
{{/ifGroup}}
12+
{{^ifGroup}}
13+
<li class="search-container{{#ifActive}} active {{^if hideChildrenInMenu}}{{#children.length}} parent{{/children.length}}{{/if}}{{/ifActive}}">
14+
<a class="{{type}}" href="{{urlTo name}}" data-search="{{#if title}}{{title}}{{else}}{{{name}}}{{/if}}">
15+
{{makeTitle}}
16+
</a>
917
</li>
10-
{{/ifGroup}}
11-
{{^ifGroup}}
12-
<li class="search-container{{#ifActive}} active {{^if hideChildrenInMenu}}{{#children.length}} parent{{/children.length}}{{/if}}{{/ifActive}}">
13-
<a class="{{type}}" href="{{urlTo name}}" data-search="{{#if title}}{{title}}{{else}}{{{name}}}{{/if}}">
14-
{{makeTitle}}
15-
</a>
16-
</li>
17-
{{/ifGroup}}
18+
{{/ifGroup}}
1819

19-
{{/if}}
20-
{{/eachOrderedChildren}}
21-
</ul>
22-
{{/if}}
20+
{{/if}}
21+
{{/eachOrderedChildren}}
22+
</ul>
23+
{{/if}}
24+
{{/with}}

templates/doc-map-info.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module.exports = function(docMap) {
2+
return {
3+
docMap: docMap,
4+
childrenMap: makeChildrenMap(docMap),
5+
6+
getChildren: function(docObject) {
7+
var children = this.childrenMap[docObject.name] || [];
8+
return children.sort(sortChildren);
9+
}
10+
};
11+
12+
function makeChildrenMap(docMap){
13+
var childrenMap = {};
14+
15+
for (var name in docMap) {
16+
var docObject = docMap[name];
17+
var parent = docObject.parent;
18+
19+
if (parent) {
20+
if (!childrenMap[parent]) {
21+
childrenMap[parent] = [];
22+
}
23+
childrenMap[parent].push(docObject);
24+
}
25+
}
26+
27+
return childrenMap;
28+
}
29+
30+
function sortChildren(child1, child2){
31+
// put groups at the end
32+
if(/group|prototype|static/i.test(child1.type)){
33+
if(!/group|prototype|static/i.test(child2.type)){
34+
return 1;
35+
} else {
36+
if(child1.type === "prototype"){
37+
return -1;
38+
}
39+
if(child2.type === "prototype"){
40+
return 1;
41+
}
42+
if(child1.type === "static"){
43+
return -1;
44+
}
45+
if(child2.type === "static"){
46+
return 1;
47+
}
48+
}
49+
}
50+
if(/prototype|static/i.test(child2.type)){
51+
return -1;
52+
}
53+
54+
if(typeof child1.order == "number"){
55+
if(typeof child2.order == "number"){
56+
// same order given?
57+
if(child1.order == child2.order){
58+
// sort by name
59+
if(child1.name < child2.name){
60+
return -1;
61+
}
62+
return 1;
63+
} else {
64+
return child1.order - child2.order;
65+
}
66+
67+
} else {
68+
return -1;
69+
}
70+
} else {
71+
if(typeof child2.order == "number"){
72+
return 1;
73+
} else {
74+
// alphabetical
75+
if(child1.name < child2.name){
76+
return -1;
77+
}
78+
return 1;
79+
}
80+
}
81+
}
82+
};

templates/helpers.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
var makeDocMapInfo = require("./doc-map-info");
2+
13
module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebars){
4+
var docMapInfo = makeDocMapInfo(docMap);
25

36
var lastPartOfName = function(str){
47
var lastIndex = Math.max( str.lastIndexOf("/"), str.lastIndexOf(".") );
@@ -78,6 +81,11 @@ module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebar
7881

7982
// GENERIC HELPERS
8083
var helpers = {
84+
makeChildrenContext: function(docObject) {
85+
var children = docMapInfo.getChildren(docObject) || [];
86+
return { children: children };
87+
},
88+
8189
/**
8290
* @function documentjs.generators.html.defaultHelpers.ifEqual
8391
*/
@@ -148,35 +156,38 @@ module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebar
148156
*/
149157
generatedWarning: function(){
150158
var current = getCurrent();
151-
return "<!--####################################################################\n" +
152-
"\tTHIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN\n\n" +
153-
'\tINSTEAD CHANGE:\n' +
154-
"\tsource: " + current.src +
155-
(current.type ? '\n\t@' + current.type + " " + current.name : '') +
156-
"\n######################################################################## -->";
159+
160+
return (
161+
"<!--####################################################################\n" +
162+
"\tTHIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN\n\n" +
163+
"\tINSTEAD CHANGE:\n" +
164+
"\tsource: " + current.src + (current.type ? '\n\t@' + current.type + " " + current.name : '') +
165+
"\n######################################################################## -->"
166+
);
157167
},
158168

159169
getParentsPathToSelf: function(name){
160170
var names = {};
161171

162172
// walk up parents until you don't have a parent
163-
var parent = docMap[name],
164-
parents = [];
173+
var parent = docMap[name];
174+
var parents = [];
175+
176+
// don't allow things that are their own parent
177+
if (parent.parent === name){
178+
return parents;
179+
}
165180

166-
// don't allow things that are their own parent
167-
if(parent.parent === name){
181+
while(parent){
182+
parents.unshift(parent);
183+
if (names[parent.name]){
168184
return parents;
169185
}
186+
names[parent.name] = true;
187+
parent = docMap[parent.parent];
188+
}
170189

171-
while(parent){
172-
parents.unshift(parent);
173-
if(names[parent.name]){
174-
return parents;
175-
}
176-
names[parent.name] = true;
177-
parent = docMap[parent.parent];
178-
}
179-
return parents;
190+
return parents;
180191
},
181192
/**
182193
* @function documentjs.generators.html.defaultHelpers.makeTitle
@@ -420,23 +431,27 @@ module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebar
420431
* where `parents` is each parent docObject of the `current` docObject and
421432
* `active` is the first docObject of current that has children.
422433
*/
423-
getActiveAndParents: function(options){
434+
getActiveAndParents: function(options) {
424435
var parents = helpers.getParentsPathToSelf(getCurrent().name);
425436
var active = parents.pop();
426437

427-
if(!active){
428-
// there are no parents, possibly nothing active
429-
parents = [];
430-
active = docMap[config.parent];
431-
} else if(!active.children && parents.length){
432-
// we want to show this item along-side it's siblings
433-
// make it's parent active
434-
active = parents.pop();
438+
if (active) {
439+
var children = docMapInfo.getChildren(active) || [];
435440

436-
// if the original active was in a group, prototype, etc, move up again
437-
if(parents.length && /group|prototype|static/i.test( active.type) ){
441+
if (!children.length && parents.length) {
442+
// we want to show this item along-side its siblings
443+
// make its parent active
438444
active = parents.pop();
445+
446+
// if the original active was in a group, prototype, etc, move up again
447+
if (parents.length && /group|prototype|static/i.test(active.type)) {
448+
active = parents.pop();
449+
}
439450
}
451+
} else {
452+
// there are no parents, possibly nothing active
453+
parents = [];
454+
active = docMap[config.parent];
440455
}
441456

442457
// remove groups because we don't want them showing up
@@ -445,13 +460,12 @@ module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebar
445460
});
446461

447462
// Make sure root is always here
448-
if(active.name !== config.parent && (!parents.length || parents[0].name !== config.parent) ){
463+
if (active.name !== config.parent &&
464+
(!parents.length || parents[0].name !== config.parent)) {
449465
parents.unshift(docMap[config.parent]);
450466
}
451-
return options.fn({
452-
parents: parents,
453-
active: active
454-
});
467+
468+
return options.fn({ parents: parents, active: active });
455469
},
456470
/**
457471
* @function documentjs.generators.html.defaultHelpers.eachFirstLevelChildren
@@ -470,7 +484,7 @@ module.exports = function(docMap, config, getCurrent, oldHelpers, OtherHandlebar
470484
*
471485
* Goes through each `children` in the sorted order.
472486
*/
473-
eachOrderedChildren: function(children, options){
487+
eachOrderedChildren: function(children, options) {
474488
children = (children || []).slice(0).sort(sortChildren);
475489
var res = "";
476490
children.forEach(function(child){

templates/menu.mustache

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<ul class="api cascading primary-grouping">
22
{{#getActiveAndParents}}
33
{{#each parents}}
4-
<li class="search-container active parent">
5-
<a class="sidebar-title" href="{{urlTo name}}" data-search="{{#if title}}{{title}}{{else}}{{{name}}}{{/if}}">
6-
{{makeTitle}}
7-
</a>
8-
{{#if ../active.hideChildrenInMenu}}
9-
{{> active-menu.mustache}}
10-
{{/if}}
11-
</li>
12-
{{/each}}
13-
{{#active}}
14-
{{^if hideChildrenInMenu}}
15-
<li class="search-container active">
4+
<li class="search-container active parent">
165
<a class="sidebar-title" href="{{urlTo name}}" data-search="{{#if title}}{{title}}{{else}}{{{name}}}{{/if}}">
176
{{makeTitle}}
187
</a>
19-
{{> active-menu.mustache}}
8+
{{#if ../active.hideChildrenInMenu}}
9+
{{> active-menu.mustache}}
10+
{{/if}}
2011
</li>
21-
{{/if}}
12+
{{/each}}
13+
14+
{{#active}}
15+
{{#unless hideChildrenInMenu}}
16+
<li class="search-container active">
17+
<a
18+
class="sidebar-title"
19+
href="{{urlTo name}}"
20+
data-search="{{#if title}}{{title}}{{else}}{{{name}}}{{/if}}"
21+
>
22+
{{makeTitle}}
23+
</a>
24+
{{> active-menu.mustache}}
25+
</li>
26+
{{/unless}}
2227
{{/active}}
2328
{{/getActiveAndParents}}
2429
</ul>

0 commit comments

Comments
 (0)