|
1 |
| -var stache = require("can-stache"); |
2 | 1 | var Control = require("can-control");
|
3 |
| -var makeTree = require("./make-tree"); |
| 2 | +var TableOfContents = require("./toc-control"); |
4 | 3 |
|
5 |
| -var template = stache("{{#each nodes}}{{renderNode .}}{{/each}}"); |
6 |
| - |
7 |
| -var renderNode = stache( |
8 |
| - "<li>" + |
9 |
| - "<a href='#{{id}}'>{{text}}</a>" + |
10 |
| - "{{#if children.length}}" + |
11 |
| - "<ul>{{#each children}}{{renderNode .}}{{/each}}</ul>" + |
12 |
| - "{{/if}}" + |
13 |
| - "</li>" |
14 |
| -); |
| 4 | +var TOCContainer = Control.extend({ |
| 5 | + init: function(el) { |
| 6 | + el.style.display = "none"; |
15 | 7 |
|
16 |
| -stache.registerSimpleHelper("renderNode", renderNode); |
| 8 | + var depth = this.getOutlineDepth(); |
| 9 | + var tagName = this.getOutlineTagName(); |
| 10 | + var toc = document.createElement(tagName); |
17 | 11 |
|
18 |
| -var TableOfContents = Control.extend({ |
19 |
| - init: function(el, options) { |
20 |
| - var titles = this.collectTitles(); |
| 12 | + toc.className = "on-this-page"; |
| 13 | + el.appendChild(toc); |
21 | 14 |
|
22 |
| - // If there are no titles, bail |
23 |
| - if (!titles.length) { |
24 |
| - el.parentNode.removeChild(el); |
25 |
| - return; |
26 |
| - } else { |
27 |
| - el.parentNode.style.display = 'block'; |
28 |
| - } |
| 15 | + new TableOfContents(toc, { |
| 16 | + depth: depth, |
| 17 | + tagName: tagName, |
| 18 | + headingsContainerSelector: "article" |
| 19 | + }); |
| 20 | + }, |
29 | 21 |
|
30 |
| - // Append our template |
31 |
| - this.element.appendChild(template({ |
32 |
| - nodes: titles |
33 |
| - })); |
| 22 | + getOutlineTagName: function() { |
| 23 | + var outline = window.docObject.outline || {}; |
| 24 | + return (outline.tag === "ol") ? "ol" : "ul"; |
34 | 25 | },
|
35 | 26 |
|
36 | 27 | getOutlineDepth: function() {
|
37 | 28 | var depth = window.docObject.outline && window.docObject.outline.depth;
|
38 | 29 | return (typeof depth === "number" ? Math.min(depth, 6) : 1);
|
39 |
| - }, |
40 |
| - |
41 |
| - makeSelector: function(tagName) { |
42 |
| - return "article " + tagName; |
43 |
| - }, |
44 |
| - |
45 |
| - collectTitles: function() { |
46 |
| - var selector = this.getHeadings().map(this.makeSelector).join(","); |
47 |
| - var titles = selector ? document.querySelectorAll(selector) : []; |
48 |
| - |
49 |
| - return makeTree(titles); |
50 |
| - }, |
51 |
| - |
52 |
| - getHeadings: function() { |
53 |
| - var headings = []; |
54 |
| - var depth = this.getOutlineDepth(); |
55 |
| - |
56 |
| - for(var i = 0; i < depth; i++) { |
57 |
| - headings.push("h" + (i + 2)); |
58 |
| - } |
59 |
| - return headings; |
60 |
| - } |
61 |
| -}); |
62 |
| - |
63 |
| -var TOCContainer = Control.extend({ |
64 |
| - init: function(el) { |
65 |
| - el.style.display = "none"; |
66 |
| - |
67 |
| - var toc = document.createElement("ul"); |
68 |
| - toc.className = "on-this-page"; |
69 |
| - el.appendChild(toc); |
70 |
| - |
71 |
| - new TableOfContents(toc); |
72 | 30 | }
|
73 | 31 | });
|
74 | 32 |
|
|
0 commit comments