Skip to content

Commit 2a311c8

Browse files
committed
remove stache and control deps to keep lighter
1 parent b9aba53 commit 2a311c8

File tree

3 files changed

+82
-46
lines changed

3 files changed

+82
-46
lines changed

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@
2525
"url": "https://github.com/bit-docs/bit-docs-html-toc/issues"
2626
},
2727
"homepage": "https://github.com/bit-docs/bit-docs-html-toc#readme",
28-
"system": {
29-
"npmAlgorithm": "flat"
30-
},
3128
"dependencies": {
32-
"can-control": "^3.0.3",
33-
"can-stache": "^3.0.7"
29+
"can-assign": "^1.3.1",
30+
"can-view-target": "^4.1.2"
3431
},
3532
"devDependencies": {
3633
"chai": "^3.5.0",

toc-container-control.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
var Control = require("can-control");
1+
var assign = require("can-assign");
22
var TableOfContents = require("./toc-control");
33

4-
module.exports = Control.extend({
5-
init: function(el) {
6-
el.style.display = "none";
4+
var TocControl = function(el){
5+
el.style.display = "none";
76

8-
var depth = this.getOutlineDepth();
9-
var tagName = this.getOutlineTagName();
10-
var selector = this.getHeadingsContainerSelector(el);
7+
var depth = this.getOutlineDepth();
8+
var tagName = this.getOutlineTagName();
9+
var selector = this.getHeadingsContainerSelector(el);
1110

12-
var toc = document.createElement(tagName);
13-
toc.className = "on-this-page";
14-
el.appendChild(toc);
11+
var toc = document.createElement(tagName);
12+
toc.className = "on-this-page";
13+
el.appendChild(toc);
1514

16-
new TableOfContents(toc, {
17-
depth: depth,
18-
tagName: tagName,
19-
headingsContainerSelector: selector
20-
});
21-
},
15+
new TableOfContents(toc, {
16+
depth: depth,
17+
tagName: tagName,
18+
headingsContainerSelector: selector
19+
});
20+
};
2221

22+
assign(TocControl.prototype, {
2323
getDocObject: function() {
2424
return window.docObject || {};
2525
},
@@ -44,3 +44,6 @@ module.exports = Control.extend({
4444
return selector ? selector : "article";
4545
}
4646
});
47+
48+
49+
module.exports = TocControl;

toc-control.js

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
var stache = require("can-stache");
2-
var Control = require("can-control");
1+
var viewTarget = require("can-view-target");
32
var makeTree = require("./make-tree");
3+
var assign = require("can-assign");
44

5-
var template = stache("{{#each nodes}}{{renderNode tagName .}}{{/each}}");
5+
// data { tagName: tagName, node: node }
6+
var renderNodeTarget = viewTarget([{
7+
tag: "li",
8+
children: [
9+
{
10+
tag: "a",
11+
attrs: {
12+
href: function(data){
13+
return this.setAttribute("href","#"+data.node.id);
14+
}
15+
},
16+
children: [function(data){ this.nodeValue = data.node.text; }]
17+
},
18+
function(data){
19+
var container = document.createElement(data.tagName);
20+
data.node.children.forEach(function(node){
21+
container.appendChild(renderNodeTarget.hydrate({node: node, tagName: data.tagName}));
22+
});
23+
if(data.node.children.length) {
24+
this.parentNode.replaceChild(container, this);
25+
}
626

27+
}
28+
]
29+
}]);
30+
/*
731
var renderNode = stache(
832
"<li>" +
933
"<a href='#{{node.id}}'>{{node.text}}</a>" +
@@ -16,34 +40,46 @@ var renderNode = stache(
1640
"{{/if}}" +
1741
"</li>"
1842
);
43+
*/
44+
45+
// data - { nodes: titles, tagName: this.tagName }
46+
var template = function(data){
47+
var container = document.createDocumentFragment();
48+
data.nodes.forEach(function(node){
49+
container.appendChild(renderNodeTarget.hydrate({node: node, tagName: data.tagName}));
50+
});
51+
return container;
52+
};
1953

20-
stache.registerSimpleHelper("renderNode", function(tagName, node) {
54+
55+
//var template = stache("{{#each nodes}}{{renderNode tagName .}}{{/each}}");
56+
/*stache.registerSimpleHelper("renderNode", function(tagName, node) {
2157
return renderNode({ tagName: tagName, node: node });
22-
});
58+
});*/
2359

24-
module.exports = Control.extend({
25-
init: function(el, options) {
26-
this.depth = options.depth;
27-
this.tagName = options.tagName;
28-
this.headingsContainerSelector = options.headingsContainerSelector;
60+
var TocControl = function(el, options){
61+
this.depth = options.depth;
62+
this.tagName = options.tagName;
63+
this.headingsContainerSelector = options.headingsContainerSelector;
2964

30-
var titles = this.collectTitles();
65+
var titles = this.collectTitles();
3166

32-
// If there are no titles, bail
33-
if (!titles.length) {
34-
el.parentNode.removeChild(el);
35-
return;
36-
} else {
37-
el.parentNode.style.display = 'block';
38-
}
67+
// If there are no titles, bail
68+
if (!titles.length) {
69+
el.parentNode.removeChild(el);
70+
return;
71+
} else {
72+
el.parentNode.style.display = 'block';
73+
}
3974

40-
// Append our template
41-
this.element.appendChild(template({
42-
nodes: titles,
43-
tagName: this.tagName
44-
}));
45-
},
75+
// Append our template
76+
el.appendChild(template({
77+
nodes: titles,
78+
tagName: this.tagName
79+
}));
80+
};
4681

82+
assign(TocControl.prototype, {
4783
makeSelector: function(tagName) {
4884
var container = this.headingsContainerSelector;
4985
return container + " " + tagName;
@@ -68,4 +104,4 @@ module.exports = Control.extend({
68104
return headings;
69105
}
70106
});
71-
107+
module.exports = TocControl;

0 commit comments

Comments
 (0)