|
1 | 1 | (function () {
|
2 | 2 |
|
| 3 | + initSearch() |
| 4 | + initMobileMenu() |
3 | 5 | if (PAGE_TYPE) {
|
4 |
| - initSubHeaders() |
5 | 6 | initVersionSelect()
|
| 7 | + initSubHeaders() |
6 | 8 | }
|
7 |
| - initSearch() |
8 | 9 |
|
9 |
| - function initSubHeaders () { |
10 |
| - var each = [].forEach |
11 |
| - var main = document.getElementById('main') |
12 |
| - var doc = document.documentElement |
13 |
| - var body = document.body |
14 |
| - var header = document.getElementById('header') |
15 |
| - var menu = document.querySelector('.sidebar') |
16 |
| - var content = document.querySelector('.content') |
17 |
| - var mobileBar = document.getElementById('mobile-bar') |
| 10 | + /** |
| 11 | + * Swiftype search box |
| 12 | + */ |
| 13 | + |
| 14 | + function initSearch () { |
| 15 | + (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){ |
| 16 | + (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t); |
| 17 | + e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e); |
| 18 | + })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st'); |
| 19 | + |
| 20 | + _st('install','HgpxvBc7pUaPUWmG9sgv','2.0.0'); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Mobile burger menu button for toggling sidebar |
| 25 | + */ |
18 | 26 |
|
| 27 | + function initMobileMenu () { |
| 28 | + var mobileBar = document.getElementById('mobile-bar') |
| 29 | + var sidebar = document.querySelector('.sidebar') |
19 | 30 | var menuButton = mobileBar.querySelector('.menu-button')
|
| 31 | + |
20 | 32 | menuButton.addEventListener('click', function () {
|
21 |
| - menu.classList.toggle('open') |
| 33 | + sidebar.classList.toggle('open') |
22 | 34 | })
|
23 | 35 |
|
24 |
| - body.addEventListener('click', function (e) { |
25 |
| - if (e.target !== menuButton && !menu.contains(e.target)) { |
26 |
| - menu.classList.remove('open') |
| 36 | + document.body.addEventListener('click', function (e) { |
| 37 | + if (e.target !== menuButton && !sidebar.contains(e.target)) { |
| 38 | + sidebar.classList.remove('open') |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Doc version select |
| 45 | + */ |
| 46 | + |
| 47 | + function initVersionSelect () { |
| 48 | + // version select |
| 49 | + document.querySelector('.version-select').addEventListener('change', function (e) { |
| 50 | + var version = e.target.value |
| 51 | + if (version.indexOf('1.') !== 0) { |
| 52 | + version = version.replace('.', '') |
| 53 | + var section = window.location.pathname.match(/\/(\w+?)\//)[1] |
| 54 | + window.location.assign('http://' + version + '.vuejs.org/' + section + '/') |
| 55 | + } else { |
| 56 | + // TODO when 1.x is out |
27 | 57 | }
|
28 | 58 | })
|
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sub headers in sidebar |
| 63 | + */ |
| 64 | + |
| 65 | + function initSubHeaders () { |
| 66 | + var each = [].forEach |
| 67 | + var main = document.getElementById('main') |
| 68 | + var header = document.getElementById('header') |
| 69 | + var sidebar = document.querySelector('.sidebar') |
| 70 | + var content = document.querySelector('.content') |
29 | 71 |
|
30 | 72 | // build sidebar
|
31 |
| - var currentPageAnchor = menu.querySelector('.sidebar-link.current') |
| 73 | + var currentPageAnchor = sidebar.querySelector('.sidebar-link.current') |
32 | 74 | var isAPI = document.querySelector('.content').classList.contains('api')
|
33 | 75 | if (currentPageAnchor || isAPI) {
|
34 | 76 | var allHeaders = []
|
|
63 | 105 | sectionContainer.addEventListener('click', function (e) {
|
64 | 106 | e.preventDefault()
|
65 | 107 | if (e.target.classList.contains('section-link')) {
|
66 |
| - menu.classList.remove('open') |
| 108 | + sidebar.classList.remove('open') |
67 | 109 | setActive(e.target)
|
68 | 110 | animating = true
|
69 | 111 | setTimeout(function () {
|
|
89 | 131 | window.addEventListener('resize', updateSidebar)
|
90 | 132 |
|
91 | 133 | function updateSidebar () {
|
92 |
| - var top = doc && doc.scrollTop || body.scrollTop |
| 134 | + var doc = document.documentElement |
| 135 | + var top = doc && doc.scrollTop || document.body.scrollTop |
93 | 136 | var headerHeight = header.offsetHeight
|
94 | 137 | if (top > headerHeight) {
|
95 | 138 | main.className = 'fix-sidebar'
|
|
149 | 192 | }
|
150 | 193 |
|
151 | 194 | function setActive (id) {
|
152 |
| - var previousActive = menu.querySelector('.section-link.active') |
| 195 | + var previousActive = sidebar.querySelector('.section-link.active') |
153 | 196 | var currentActive = typeof id === 'string'
|
154 |
| - ? menu.querySelector('.section-link[href="#' + id + '"]') |
| 197 | + ? sidebar.querySelector('.section-link[href="#' + id + '"]') |
155 | 198 | : id
|
156 | 199 | if (currentActive !== previousActive) {
|
157 | 200 | if (previousActive) previousActive.classList.remove('active')
|
|
167 | 210 | wrapper.appendChild(link)
|
168 | 211 | }
|
169 | 212 | }
|
170 |
| - |
171 |
| - function initVersionSelect () { |
172 |
| - // version select |
173 |
| - document.querySelector('.version-select').addEventListener('change', function (e) { |
174 |
| - var version = e.target.value |
175 |
| - if (version.indexOf('1.') !== 0) { |
176 |
| - version = version.replace('.', '') |
177 |
| - var section = window.location.pathname.match(/\/(\w+?)\//)[1] |
178 |
| - window.location.assign('http://' + version + '.vuejs.org/' + section + '/') |
179 |
| - } else { |
180 |
| - // TODO when 1.x is out |
181 |
| - } |
182 |
| - }) |
183 |
| - } |
184 |
| - |
185 |
| - function initSearch () { |
186 |
| - // Search with SwiftType |
187 |
| - (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){ |
188 |
| - (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t); |
189 |
| - e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e); |
190 |
| - })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st'); |
191 |
| - |
192 |
| - _st('install','HgpxvBc7pUaPUWmG9sgv','2.0.0'); |
193 |
| - } |
194 | 213 | })()
|
0 commit comments