1
- var stache = require ( "can-stache" ) ;
2
- var Control = require ( "can-control" ) ;
1
+ var viewTarget = require ( "can-view-target" ) ;
3
2
var makeTree = require ( "./make-tree" ) ;
3
+ var assign = require ( "can-assign" ) ;
4
4
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
+ }
6
26
27
+ }
28
+ ]
29
+ } ] ) ;
30
+ /*
7
31
var renderNode = stache(
8
32
"<li>" +
9
33
"<a href='#{{node.id}}'>{{node.text}}</a>" +
@@ -16,34 +40,46 @@ var renderNode = stache(
16
40
"{{/if}}" +
17
41
"</li>"
18
42
);
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
+ } ;
19
53
20
- stache . registerSimpleHelper ( "renderNode" , function ( tagName , node ) {
54
+
55
+ //var template = stache("{{#each nodes}}{{renderNode tagName .}}{{/each}}");
56
+ /*stache.registerSimpleHelper("renderNode", function(tagName, node) {
21
57
return renderNode({ tagName: tagName, node: node });
22
- } ) ;
58
+ });*/
23
59
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 ;
29
64
30
- var titles = this . collectTitles ( ) ;
65
+ var titles = this . collectTitles ( ) ;
31
66
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
+ }
39
74
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
+ } ;
46
81
82
+ assign ( TocControl . prototype , {
47
83
makeSelector : function ( tagName ) {
48
84
var container = this . headingsContainerSelector ;
49
85
return container + " " + tagName ;
@@ -68,4 +104,4 @@ module.exports = Control.extend({
68
104
return headings ;
69
105
}
70
106
} ) ;
71
-
107
+ module . exports = TocControl ;
0 commit comments