forked from vuejs/v2.vuejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
216 lines (194 loc) · 5.79 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function () {
initSearch()
initMobileMenu()
if (PAGE_TYPE) {
initVersionSelect()
initSubHeaders()
}
/**
* Swiftype search box
*/
function initSearch () {
[
'#search-query-nav',
'#search-query-sidebar'
].forEach(function (selector) {
if (!document.querySelector(selector)) return
docsearch({
appId: 'BH4D9OD16A',
apiKey: '85cc3221c9f23bfbaa4e3913dd7625ea',
indexName: 'vuejs',
inputSelector: selector
})
})
}
/**
* Mobile burger menu button for toggling sidebar
*/
function initMobileMenu () {
var mobileBar = document.getElementById('mobile-bar')
var sidebar = document.querySelector('.sidebar')
var menuButton = mobileBar.querySelector('.menu-button')
menuButton.addEventListener('click', function () {
sidebar.classList.toggle('open')
})
document.body.addEventListener('click', function (e) {
if (e.target !== menuButton && !sidebar.contains(e.target)) {
sidebar.classList.remove('open')
}
})
}
/**
* Doc version select
*/
function initVersionSelect () {
// version select
document.querySelector('.version-select').addEventListener('change', function (e) {
var version = e.target.value
var section = window.location.pathname.match(/\/(\w+?)\//)[1]
if (version === 'SELF') return
window.location.assign(
'http://' +
version +
(version && '.') +
'vuejs.org/' + section + '/'
)
})
}
/**
* Sub headers in sidebar
*/
function initSubHeaders () {
var each = [].forEach
var main = document.getElementById('main')
var header = document.getElementById('header')
var sidebar = document.querySelector('.sidebar')
var content = document.querySelector('.content')
// build sidebar
var currentPageAnchor = sidebar.querySelector('.sidebar-link.current')
var isAPI = document.querySelector('.content').classList.contains('api')
if (currentPageAnchor || isAPI) {
var allHeaders = []
var sectionContainer
if (isAPI) {
sectionContainer = document.querySelector('.menu-root')
} else {
sectionContainer = document.createElement('ul')
sectionContainer.className = 'menu-sub'
currentPageAnchor.parentNode.appendChild(sectionContainer)
}
var headers = content.querySelectorAll('h2')
if (headers.length) {
each.call(headers, function (h) {
sectionContainer.appendChild(makeLink(h))
var h3s = collectH3s(h)
allHeaders.push(h)
allHeaders.push.apply(allHeaders, h3s)
if (h3s.length) {
sectionContainer.appendChild(makeSubLinks(h3s, isAPI))
}
})
} else {
headers = content.querySelectorAll('h3')
each.call(headers, function (h) {
sectionContainer.appendChild(makeLink(h))
allHeaders.push(h)
})
}
var animating = false
sectionContainer.addEventListener('click', function (e) {
e.preventDefault()
if (e.target.classList.contains('section-link')) {
sidebar.classList.remove('open')
setActive(e.target)
animating = true
setTimeout(function () {
animating = false
}, 400)
}
}, true)
// make links clickable
allHeaders.forEach(makeHeaderClickable)
// init smooth scroll
smoothScroll.init({
speed: 400,
offset: window.innerWidth > 720
? 40
: 58
})
}
// listen for scroll event to do positioning & highlights
window.addEventListener('scroll', updateSidebar)
window.addEventListener('resize', updateSidebar)
function updateSidebar () {
var doc = document.documentElement
var top = doc && doc.scrollTop || document.body.scrollTop
var headerHeight = header.offsetHeight
if (top > headerHeight) {
main.className = 'fix-sidebar'
} else {
main.className = ''
}
if (animating || !allHeaders) return
var last
for (var i = 0; i < allHeaders.length; i++) {
var link = allHeaders[i]
if (link.offsetTop > top) {
if (!last) last = link
break
} else {
last = link
}
}
if (last)
setActive(last.id)
}
function makeLink (h) {
var link = document.createElement('li')
var text = h.textContent.replace(/\(.*\)$/, '')
link.innerHTML =
'<a class="section-link" data-scroll href="#' + h.id + '">' +
text +
'</a>'
return link
}
function collectH3s (h) {
var h3s = []
var next = h.nextSibling
while (next && next.tagName !== 'H2') {
if (next.tagName === 'H3') {
h3s.push(next)
}
next = next.nextSibling
}
return h3s
}
function makeSubLinks (h3s, small) {
var container = document.createElement('ul')
if (small) {
container.className = 'menu-sub'
}
h3s.forEach(function (h) {
container.appendChild(makeLink(h))
})
return container
}
function setActive (id) {
var previousActive = sidebar.querySelector('.section-link.active')
var currentActive = typeof id === 'string'
? sidebar.querySelector('.section-link[href="#' + id + '"]')
: id
if (currentActive !== previousActive) {
if (previousActive) previousActive.classList.remove('active')
currentActive.classList.add('active')
}
}
function makeHeaderClickable (link) {
var wrapper = document.createElement('a')
wrapper.href = '#' + link.id
wrapper.setAttribute('data-scroll', '')
link.parentNode.insertBefore(wrapper, link)
wrapper.appendChild(link)
}
}
})()