common.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. (function () {
  2. var each = [].forEach
  3. var main = document.querySelector('.js-MainContent')
  4. var doc = document.documentElement
  5. var body = document.body
  6. var header = document.querySelector('.js-MainHeader')
  7. var menu = document.querySelector('.js-Sidebar')
  8. var content = document.querySelector('.js-Content')
  9. var menuButton = document.querySelector('.js-MenuBtn')
  10. menuButton.addEventListener('click', function () {
  11. menu.classList.toggle('is-open')
  12. })
  13. body.addEventListener('click', function (e) {
  14. if (e.target !== menuButton && !menu.contains(e.target)) {
  15. menu.classList.remove('is-open')
  16. }
  17. })
  18. // build sidebar
  19. var currentPageAnchor = menu.querySelector('.sidebar-link.current')
  20. var isAPI = document.querySelector('.Content').classList.contains('api')
  21. if (currentPageAnchor || isAPI) {
  22. var allLinks = []
  23. var sectionContainer
  24. if (false && isAPI) {
  25. sectionContainer = document.querySelector('.menu-root')
  26. } else {
  27. sectionContainer = document.createElement('ul')
  28. sectionContainer.className = 'menu-sub'
  29. currentPageAnchor.parentNode.appendChild(sectionContainer)
  30. }
  31. var h2s = content.querySelectorAll('h2')
  32. if (h2s.length) {
  33. each.call(h2s, function (h) {
  34. sectionContainer.appendChild(makeLink(h))
  35. var h3s = collectH3s(h)
  36. allLinks.push(h)
  37. allLinks.push.apply(allLinks, h3s)
  38. if (h3s.length) {
  39. sectionContainer.appendChild(makeSubLinks(h3s, isAPI))
  40. }
  41. })
  42. } else {
  43. h2s = content.querySelectorAll('h3')
  44. each.call(h2s, function (h) {
  45. sectionContainer.appendChild(makeLink(h))
  46. allLinks.push(h)
  47. })
  48. }
  49. var animating = false
  50. sectionContainer.addEventListener('click', function (e) {
  51. e.preventDefault()
  52. if (e.target.classList.contains('section-link')) {
  53. menu.classList.remove('open')
  54. setActive(e.target)
  55. animating = true
  56. setTimeout(function () {
  57. animating = false
  58. }, 400)
  59. }
  60. }, true)
  61. // make links clickable
  62. allLinks.forEach(makeLinkClickable)
  63. // init smooth scroll
  64. smoothScroll.init({
  65. speed: 400,
  66. offset: window.innerWidth > 720
  67. ? 40
  68. : 58
  69. })
  70. }
  71. // listen for scroll event to do positioning & highlights
  72. window.addEventListener('scroll', updateSidebar)
  73. window.addEventListener('resize', updateSidebar)
  74. function updateSidebar () {
  75. var top = doc && doc.scrollTop || body.scrollTop
  76. var headerHeight = header.offsetHeight
  77. if (top > headerHeight) {
  78. addClass(main, 'fix-sidebar');
  79. } else {
  80. removeClass(main, 'fix-sidebar');
  81. }
  82. if (animating || !allLinks) return
  83. var last
  84. for (var i = 0; i < allLinks.length; i++) {
  85. var link = allLinks[i]
  86. if (link.offsetTop > top) {
  87. if (!last) last = link
  88. break
  89. } else {
  90. last = link
  91. }
  92. }
  93. if (last)
  94. setActive(last.id)
  95. }
  96. function makeLink (h) {
  97. var link = document.createElement('li')
  98. var text = h.textContent.replace(/\(.*\)$/, '')
  99. // make sure the ids are link-able...
  100. h.id = h.id
  101. .replace(/\(.*\)$/, '')
  102. .replace(/\$/, '')
  103. link.innerHTML =
  104. '<a class="section-link" data-scroll href="#' + h.id + '">' +
  105. text +
  106. '</a>'
  107. return link
  108. }
  109. function collectH3s (h) {
  110. var h3s = []
  111. var next = h.nextSibling
  112. while (next && next.tagName !== 'H2') {
  113. if (next.tagName === 'H3') {
  114. h3s.push(next)
  115. }
  116. next = next.nextSibling
  117. }
  118. return h3s
  119. }
  120. function makeSubLinks (h3s, small) {
  121. var container = document.createElement('ul')
  122. if (small) {
  123. container.className = 'menu-sub'
  124. }
  125. h3s.forEach(function (h) {
  126. container.appendChild(makeLink(h))
  127. })
  128. return container
  129. }
  130. function setActive (id) {
  131. var previousActive = menu.querySelector('.section-link.active')
  132. var currentActive = typeof id === 'string'
  133. ? menu.querySelector('.section-link[href="#' + id + '"]')
  134. : id
  135. if (currentActive !== previousActive) {
  136. if (previousActive) previousActive.classList.remove('active')
  137. currentActive.classList.add('active')
  138. }
  139. }
  140. function makeLinkClickable (link) {
  141. var wrapper = document.createElement('a')
  142. wrapper.href = '#' + link.id
  143. wrapper.setAttribute('data-scroll', '')
  144. link.parentNode.insertBefore(wrapper, link)
  145. wrapper.appendChild(link)
  146. }
  147. function addClass(el, className) {
  148. if (el.classList) {
  149. el.classList.add(className);
  150. } else {
  151. el.className += ' ' + className;
  152. }
  153. }
  154. function removeClass(el, className) {
  155. if (el.classList) {
  156. el.classList.remove(className);
  157. } else {
  158. el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
  159. }
  160. }
  161. // Search with SwiftType
  162. // @todo get our own swifttype
  163. // (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
  164. // (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
  165. // e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
  166. // })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');
  167. // _st('install','HgpxvBc7pUaPUWmG9sgv','2.0.0');
  168. // version select
  169. // document.querySelector('.version-select').addEventListener('change', function (e) {
  170. // var version = e.target.value
  171. // if (version.indexOf('1.') !== 0) {
  172. // version = version.replace('.', '')
  173. // var section = window.location.pathname.match(/\/(\w+?)\//)[1]
  174. // window.location.assign('http://' + version + '.uppyjs.io/' + section + '/')
  175. // } else {
  176. // // TODO when 1.x is out
  177. // }
  178. // })
  179. })();