1
- ( function ( ) {
1
+ ( function ( ) {
2
2
3
3
initMobileMenu ( )
4
4
if ( PAGE_TYPE ) {
8
8
initLocationHashFuzzyMatching ( )
9
9
}
10
10
11
- function initApiSpecLinks ( ) {
11
+ function initApiSpecLinks ( ) {
12
12
var apiContent = document . querySelector ( '.content.api' )
13
13
if ( apiContent ) {
14
14
var apiTitles = [ ] . slice . call ( apiContent . querySelectorAll ( 'h3' ) )
15
- apiTitles . forEach ( function ( titleNode ) {
15
+ apiTitles . forEach ( function ( titleNode ) {
16
16
var ulNode = titleNode . parentNode . nextSibling
17
17
if ( ulNode . tagName !== 'UL' ) {
18
18
ulNode = ulNode . nextSibling
31
31
}
32
32
}
33
33
34
- function initLocationHashFuzzyMatching ( ) {
34
+ function initLocationHashFuzzyMatching ( ) {
35
35
var hash = window . location . hash
36
36
if ( ! hash ) return
37
37
var hashTarget = document . getElementById ( hash )
38
38
if ( ! hashTarget ) {
39
39
var normalizedHash = normalizeHash ( hash )
40
40
var possibleHashes = [ ] . slice . call ( document . querySelectorAll ( '[id]' ) )
41
- . map ( function ( el ) { return el . id } )
42
- possibleHashes . sort ( function ( hashA , hashB ) {
41
+ . map ( function ( el ) {
42
+ return el . id
43
+ } )
44
+ possibleHashes . sort ( function ( hashA , hashB ) {
43
45
var distanceA = levenshteinDistance ( normalizedHash , normalizeHash ( hashA ) )
44
46
var distanceB = levenshteinDistance ( normalizedHash , normalizeHash ( hashB ) )
45
47
if ( distanceA < distanceB ) return - 1
49
51
window . location . hash = possibleHashes [ 0 ]
50
52
}
51
53
52
- function normalizeHash ( rawHash ) {
54
+ function normalizeHash ( rawHash ) {
53
55
return rawHash
54
56
. toLowerCase ( )
55
57
. replace ( / \- (?: d e p r e c a t e d | r e m o v e d | r e p l a c e d | c h a n g e d | o b s o l e t e ) $ / , '' )
56
58
}
57
59
58
- function levenshteinDistance ( a , b ) {
60
+ function levenshteinDistance ( a , b ) {
59
61
var m = [ ]
60
62
if ( ! ( a && b ) ) return ( b || a ) . length
61
63
for ( let i = 0 ; i <= b . length ; m [ i ] = [ i ++ ] ) { }
62
64
for ( let j = 0 ; j <= a . length ; m [ 0 ] [ j ] = j ++ ) { }
63
65
for ( let i = 1 ; i <= b . length ; i ++ ) {
64
66
for ( let j = 1 ; j <= a . length ; j ++ ) {
65
- m [ i ] [ j ] = b . charAt ( i - 1 ) === a . charAt ( j - 1 )
66
- ? m [ i - 1 ] [ j - 1 ]
67
- : m [ i ] [ j ] = Math . min (
67
+ m [ i ] [ j ] = b . charAt ( i - 1 ) === a . charAt ( j - 1 ) ?
68
+ m [ i - 1 ] [ j - 1 ] :
69
+ m [ i ] [ j ] = Math . min (
68
70
m [ i - 1 ] [ j - 1 ] + 1 ,
69
71
Math . min ( m [ i ] [ j - 1 ] + 1 , m [ i - 1 ] [ j ] + 1 ) )
70
72
}
77
79
* Mobile burger menu button for toggling sidebar
78
80
*/
79
81
80
- function initMobileMenu ( ) {
82
+ function initMobileMenu ( ) {
81
83
var mobileBar = document . getElementById ( 'mobile-bar' )
82
84
var sidebar = document . querySelector ( '.sidebar' )
83
85
var menuButton = mobileBar . querySelector ( '.menu-button' )
84
86
85
- menuButton . addEventListener ( 'click' , function ( ) {
87
+ menuButton . addEventListener ( 'click' , function ( ) {
86
88
sidebar . classList . toggle ( 'open' )
87
89
} )
88
90
89
- document . body . addEventListener ( 'click' , function ( e ) {
91
+ document . body . addEventListener ( 'click' , function ( e ) {
90
92
if ( e . target !== menuButton && ! sidebar . contains ( e . target ) ) {
91
93
sidebar . classList . remove ( 'open' )
92
94
}
97
99
* Doc version select
98
100
*/
99
101
100
- function initVersionSelect ( ) {
102
+ function initVersionSelect ( ) {
101
103
// version select
102
- document . querySelector ( '.version-select' ) . addEventListener ( 'change' , function ( e ) {
104
+ document . querySelector ( '.version-select' ) . addEventListener ( 'change' , function ( e ) {
103
105
var version = e . target . value
104
106
var section = window . location . pathname . match ( / \/ v \d \/ ( \w + ?) \/ / ) [ 1 ]
105
107
if ( version === 'SELF' ) return
116
118
* Sub headers in sidebar
117
119
*/
118
120
119
- function initSubHeaders ( ) {
121
+ function initSubHeaders ( ) {
120
122
var each = [ ] . forEach
121
123
var main = document . getElementById ( 'main' )
122
124
var header = document . getElementById ( 'header' )
138
140
}
139
141
var headers = content . querySelectorAll ( 'h2' )
140
142
if ( headers . length ) {
141
- each . call ( headers , function ( h ) {
143
+ each . call ( headers , function ( h ) {
142
144
sectionContainer . appendChild ( makeLink ( h ) )
143
145
var h3s = collectH3s ( h )
144
146
allHeaders . push ( h )
149
151
} )
150
152
} else {
151
153
headers = content . querySelectorAll ( 'h3' )
152
- each . call ( headers , function ( h ) {
154
+ each . call ( headers , function ( h ) {
153
155
sectionContainer . appendChild ( makeLink ( h ) )
154
156
allHeaders . push ( h )
155
157
} )
156
158
}
157
159
158
160
var animating = false
159
- sectionContainer . addEventListener ( 'click' , function ( e ) {
161
+ sectionContainer . addEventListener ( 'click' , function ( e ) {
160
162
e . preventDefault ( )
161
163
if ( e . target . classList . contains ( 'section-link' ) ) {
162
164
sidebar . classList . remove ( 'open' )
163
165
setActive ( e . target )
164
166
animating = true
165
- setTimeout ( function ( ) {
167
+ setTimeout ( function ( ) {
166
168
animating = false
167
169
} , 400 )
168
170
}
178
180
}
179
181
180
182
var hoveredOverSidebar = false
181
- sidebar . addEventListener ( 'mouseover' , function ( ) {
183
+ sidebar . addEventListener ( 'mouseover' , function ( ) {
182
184
hoveredOverSidebar = true
183
185
} )
184
- sidebar . addEventListener ( 'mouseleave' , function ( ) {
186
+ sidebar . addEventListener ( 'mouseleave' , function ( ) {
185
187
hoveredOverSidebar = false
186
188
} )
187
189
188
190
// listen for scroll event to do positioning & highlights
189
191
window . addEventListener ( 'scroll' , updateSidebar )
190
192
window . addEventListener ( 'resize' , updateSidebar )
191
193
192
- function updateSidebar ( ) {
194
+ function updateSidebar ( ) {
193
195
var doc = document . documentElement
194
196
var top = doc && doc . scrollTop || document . body . scrollTop
195
197
if ( animating || ! allHeaders ) return
204
206
}
205
207
}
206
208
if ( last )
207
- setActive ( last . id , ! hoveredOverSidebar )
209
+ setActive ( last . id , ! hoveredOverSidebar )
208
210
}
209
211
210
- function makeLink ( h ) {
212
+ function makeLink ( h ) {
211
213
var link = document . createElement ( 'li' )
212
214
var text = h . textContent . replace ( / \( .* \) $ / , '' )
213
215
link . innerHTML =
214
216
'<a class="section-link" data-scroll href="#' + h . id + '">' +
215
- text +
217
+ text +
216
218
'</a>'
217
219
return link
218
220
}
219
221
220
- function collectH3s ( h ) {
222
+ function collectH3s ( h ) {
221
223
var h3s = [ ]
222
224
var next = h . nextSibling
223
225
while ( next && next . tagName !== 'H2' ) {
229
231
return h3s
230
232
}
231
233
232
- function makeSubLinks ( h3s , small ) {
234
+ function makeSubLinks ( h3s , small ) {
233
235
var container = document . createElement ( 'ul' )
234
236
if ( small ) {
235
237
container . className = 'menu-sub'
236
238
}
237
- h3s . forEach ( function ( h ) {
239
+ h3s . forEach ( function ( h ) {
238
240
container . appendChild ( makeLink ( h ) )
239
241
} )
240
242
return container
241
243
}
242
244
243
- function setActive ( id , shouldScrollIntoView ) {
245
+ function setActive ( id , shouldScrollIntoView ) {
244
246
var previousActive = sidebar . querySelector ( '.section-link.active' )
245
- var currentActive = typeof id === 'string'
246
- ? sidebar . querySelector ( '.section-link[href="#' + id + '"]' )
247
- : id
247
+ var currentActive = typeof id === 'string' ?
248
+ sidebar . querySelector ( '.section-link[href="#' + id + '"]' ) :
249
+ id
248
250
if ( currentActive !== previousActive ) {
249
251
if ( previousActive ) previousActive . classList . remove ( 'active' )
250
252
currentActive . classList . add ( 'active' )
251
253
if ( shouldScrollIntoView ) {
252
- var currentPageOffset = currentPageAnchor
253
- ? currentPageAnchor . offsetTop - 8
254
- : 0
254
+ var currentPageOffset = currentPageAnchor ?
255
+ currentPageAnchor . offsetTop - 8 :
256
+ 0
255
257
var currentActiveOffset = currentActive . offsetTop + currentActive . parentNode . clientHeight
256
258
var sidebarHeight = sidebar . clientHeight
257
259
var currentActiveIsInView = (
258
260
currentActive . offsetTop >= sidebar . scrollTop &&
259
261
currentActiveOffset <= sidebar . scrollTop + sidebarHeight
260
262
)
261
263
var linkNotFurtherThanSidebarHeight = currentActiveOffset - currentPageOffset < sidebarHeight
262
- var newScrollTop = currentActiveIsInView
263
- ? sidebar . scrollTop
264
- : linkNotFurtherThanSidebarHeight
265
- ? currentPageOffset
266
- : currentActiveOffset - sidebarHeight
264
+ var newScrollTop = currentActiveIsInView ?
265
+ sidebar . scrollTop :
266
+ linkNotFurtherThanSidebarHeight ?
267
+ currentPageOffset :
268
+ currentActiveOffset - sidebarHeight
267
269
sidebar . scrollTop = newScrollTop
268
270
}
269
271
}
270
272
}
271
273
272
- function makeHeaderClickable ( link ) {
274
+ function makeHeaderClickable ( link ) {
273
275
var wrapper = document . createElement ( 'a' )
274
276
wrapper . href = '#' + link . id
275
277
wrapper . setAttribute ( 'data-scroll' , '' )
276
278
link . parentNode . insertBefore ( wrapper , link )
277
279
wrapper . appendChild ( link )
278
280
}
279
281
}
280
- } ) ( )
282
+ } ) ( )
0 commit comments