@@ -6,8 +6,6 @@ var template = stache("{{#each titles}}" +
6
6
"<li><a ($click)='scrollTo(., %event)' href='#{{id}}'>{{text}}</a></li>" +
7
7
"{{/each}}" ) ;
8
8
9
- var toc = document . getElementsByClassName ( "on-this-page" ) [ 0 ] ;
10
-
11
9
function throttle ( fn , ms ) {
12
10
var wait = false ;
13
11
return function ( ) {
@@ -22,19 +20,30 @@ function throttle(fn, ms){
22
20
} ;
23
21
}
24
22
23
+ function outerHeight ( el ) {
24
+ var height = el . offsetHeight ;
25
+ var style = getComputedStyle ( el ) ;
26
+
27
+ height += parseInt ( style . marginTop ) + parseInt ( style . marginBottom ) ;
28
+ return height ;
29
+ }
30
+
25
31
var TableOfContents = Control . extend ( {
26
32
init : function ( el , options ) {
27
33
this . scroller = document . body ;
28
- this . titleSelector = options . titleSelector || ".signature-title" ;
34
+ this . container = this . element . parentNode ;
29
35
30
- this . navHeight = this . getNavHeight ( ) ;
36
+ this . depth = window . docObject . outline && window . docObject . outline . depth || 1 ;
31
37
38
+ this . navHeight = this . getNavHeight ( ) ;
32
39
this . titles = this . collectTitles ( ) ;
33
40
34
41
// If there are no titles, bail
35
42
if ( ! this . titles . length ) {
36
43
el . parentNode . removeChild ( el ) ;
37
44
return ;
45
+ } else {
46
+ el . parentNode . style . display = 'block' ;
38
47
}
39
48
this . titleIndex = 0 ;
40
49
this . calculateActive ( ) ;
@@ -46,7 +55,8 @@ var TableOfContents = Control.extend({
46
55
scrollTo : function ( item , ev ) {
47
56
ev . preventDefault ( ) ;
48
57
toc . disabled ( true ) ;
49
- window . scrollTo ( 0 , item . pos + 1 ) ;
58
+ var pos = item . pos + toc . TOCHeight ;
59
+ window . scrollTo ( 0 , pos + 1 ) ;
50
60
toc . calculateActive ( ) ;
51
61
52
62
requestAnimationFrame ( function ( ) {
@@ -55,6 +65,8 @@ var TableOfContents = Control.extend({
55
65
}
56
66
} ) ) ;
57
67
this . setActive ( this . titleIndex ) ;
68
+ this . TOCHeight = this . getTOCHeight ( ) ;
69
+ this . container . style . height = this . TOCHeight + 'px' ;
58
70
59
71
// Wait until we've appended the TOC so it can be part of the calculation
60
72
this . fixed ( ! this . isFirstTitleVisible ( ) ) ;
@@ -67,14 +79,22 @@ var TableOfContents = Control.extend({
67
79
return nav . clientHeight ;
68
80
} ,
69
81
82
+ getTOCHeight : function ( ) {
83
+ return outerHeight ( this . element ) ;
84
+ } ,
85
+
70
86
isFirstTitleVisible : function ( ) {
71
87
var firstPosition = this . titles [ 0 ] . pos + this . element . clientHeight +
72
88
this . navHeight ;
73
89
return firstPosition > this . scroller . scrollTop ;
74
90
} ,
75
91
76
92
collectTitles : function ( ) {
77
- var titles = document . querySelectorAll ( "article " + this . titleSelector ) ;
93
+ var selector = this . getHeadings ( ) . map ( function ( h ) {
94
+ return "article " + h ;
95
+ } ) . join ( "," ) ;
96
+
97
+ var titles = document . querySelectorAll ( selector ) ;
78
98
var curScroll = this . scroller . scrollTop ;
79
99
var navHeight = this . navHeight ;
80
100
return [ ] . map . call ( titles , function ( title , idx ) {
@@ -89,6 +109,14 @@ var TableOfContents = Control.extend({
89
109
} ) ;
90
110
} ,
91
111
112
+ getHeadings : function ( ) {
113
+ var headings = [ ] ;
114
+ for ( var i = 0 ; i < this . depth ; i ++ ) {
115
+ headings . push ( "h" + ( i + 2 ) ) ;
116
+ }
117
+ return headings ;
118
+ } ,
119
+
92
120
fixed : function ( fixed ) {
93
121
if ( fixed === this . _fixed ) {
94
122
return ;
@@ -131,35 +159,48 @@ var TableOfContents = Control.extend({
131
159
132
160
calculateActive : function ( ) {
133
161
var scrollTop = this . scroller . scrollTop ;
162
+ var TOCHeight = this . TOCHeight ;
134
163
135
164
// Determine which h2 should be showing
136
165
var prev = this . getTitle ( this . titleIndex ) ;
137
166
var next = this . getTitle ( this . titleIndex + 1 ) ;
138
167
139
168
// See if we need to jump to the next when scrolling down
140
- var cur ;
141
- while ( scrollTop > next . pos ) {
169
+ var cur , nextPos = next . pos + TOCHeight ;
170
+ while ( scrollTop > nextPos ) {
142
171
cur = next ;
143
172
next = this . getTitle ( cur . index + 1 ) ;
173
+ nextPos = next . pos + TOCHeight ;
144
174
}
145
175
146
176
// See if we need to move to the previous when scrolling up
147
177
if ( ! cur ) {
178
+ var curPos ;
148
179
do {
149
180
cur = prev ;
181
+ curPos = cur . pos + TOCHeight ;
150
182
prev = this . getTitle ( prev . index - 1 ) ;
151
- } while ( scrollTop < cur . pos ) ;
183
+ } while ( scrollTop < curPos ) ;
152
184
}
153
185
154
186
if ( typeof cur . pos !== "undefined" ) {
155
187
this . setActive ( cur . index ) ;
156
188
}
157
- } ,
189
+ }
190
+ } ) ;
158
191
192
+ var TOCContainer = Control . extend ( {
193
+ init : function ( el ) {
194
+ el . style . display = 'none' ;
159
195
196
+ var toc = document . createElement ( "ul" ) ;
197
+ toc . className = "on-this-page" ;
198
+ el . appendChild ( toc ) ;
160
199
200
+ new TableOfContents ( toc ) ;
201
+ }
161
202
} ) ;
162
203
163
- if ( toc ) {
164
- new TableOfContents ( toc ) ;
165
- }
204
+ new TOCContainer (
205
+ document . getElementsByClassName ( "on-this-page-container" ) [ 0 ]
206
+ ) ;
0 commit comments