main.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. document.addEventListener('DOMContentLoaded', () => {
  2. // Dropdowns
  3. const $metalinks = getAll('#meta a');
  4. if ($metalinks.length > 0) {
  5. $metalinks.forEach($el => {
  6. $el.addEventListener('click', event => {
  7. event.preventDefault();
  8. const target = $el.getAttribute('href');
  9. const $target = document.getElementById(target.substring(1));
  10. $target.scrollIntoView(true);
  11. // window.history.replaceState(null, document.title, `${window.location.origin}${window.location.pathname}${target}`);
  12. return false;
  13. });
  14. });
  15. }
  16. // Dropdowns
  17. const $dropdowns = getAll('.dropdown:not(.is-hoverable)');
  18. if ($dropdowns.length > 0) {
  19. $dropdowns.forEach($el => {
  20. $el.addEventListener('click', event => {
  21. event.stopPropagation();
  22. $el.classList.toggle('is-active');
  23. });
  24. });
  25. document.addEventListener('click', event => {
  26. closeDropdowns();
  27. });
  28. }
  29. function closeDropdowns() {
  30. $dropdowns.forEach($el => {
  31. $el.classList.remove('is-active');
  32. });
  33. }
  34. // Toggles
  35. const $burgers = getAll('.burger');
  36. if ($burgers.length > 0) {
  37. $burgers.forEach($el => {
  38. $el.addEventListener('click', () => {
  39. const target = $el.dataset.target;
  40. const $target = document.getElementById(target);
  41. $el.classList.toggle('is-active');
  42. $target.classList.toggle('is-active');
  43. });
  44. });
  45. }
  46. // Modals
  47. const rootEl = document.documentElement;
  48. const $modals = getAll('.modal');
  49. const $modalButtons = getAll('.modal-button');
  50. const $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
  51. if ($modalButtons.length > 0) {
  52. $modalButtons.forEach($el => {
  53. $el.addEventListener('click', () => {
  54. const target = $el.dataset.target;
  55. const $target = document.getElementById(target);
  56. rootEl.classList.add('is-clipped');
  57. $target.classList.add('is-active');
  58. });
  59. });
  60. }
  61. if ($modalCloses.length > 0) {
  62. $modalCloses.forEach($el => {
  63. $el.addEventListener('click', () => {
  64. closeModals();
  65. });
  66. });
  67. }
  68. document.addEventListener('keydown', event => {
  69. const e = event || window.event;
  70. if (e.keyCode === 27) {
  71. closeModals();
  72. closeDropdowns();
  73. }
  74. });
  75. function closeModals() {
  76. rootEl.classList.remove('is-clipped');
  77. $modals.forEach($el => {
  78. $el.classList.remove('is-active');
  79. });
  80. }
  81. // Clipboard
  82. const $highlights = getAll('.highlight');
  83. let itemsProcessed = 0;
  84. if ($highlights.length > 0) {
  85. $highlights.forEach($el => {
  86. const copyEl = '<button class="button is-small bd-copy">Copy</button>';
  87. const expandEl = '<button class="button is-small bd-expand">Expand</button>';
  88. $el.insertAdjacentHTML('beforeend', copyEl);
  89. const $parent = $el.parentNode;
  90. if ($parent && $parent.classList.contains('bd-is-more')) {
  91. const showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  92. $el.insertAdjacentHTML('beforeend', showEl);
  93. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  94. $el.insertAdjacentHTML('beforeend', expandEl);
  95. }
  96. itemsProcessed++;
  97. if (itemsProcessed === $highlights.length) {
  98. addHighlightControls();
  99. }
  100. });
  101. }
  102. function addHighlightControls() {
  103. const $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
  104. $highlightButtons.forEach($el => {
  105. $el.addEventListener('mouseenter', () => {
  106. $el.parentNode.classList.add('bd-is-hovering');
  107. });
  108. $el.addEventListener('mouseleave', () => {
  109. $el.parentNode.classList.remove('bd-is-hovering');
  110. });
  111. });
  112. const $highlightExpands = getAll('.highlight .bd-expand');
  113. $highlightExpands.forEach($el => {
  114. $el.addEventListener('click', () => {
  115. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  116. });
  117. });
  118. const $highlightShows = getAll('.highlight .bd-show');
  119. $highlightShows.forEach($el => {
  120. $el.addEventListener('click', () => {
  121. $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
  122. });
  123. });
  124. }
  125. new Clipboard('.bd-copy', {
  126. target: function(trigger) {
  127. return trigger.previousSibling;
  128. }
  129. });
  130. // Functions
  131. function getAll(selector) {
  132. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  133. }
  134. // Scrolling
  135. const navbarEl = document.getElementById('navbar');
  136. const navbarBurger = document.getElementById('navbarBurger');
  137. const specialShadow = document.getElementById('specialShadow');
  138. const navbarHeight = 52;
  139. let navbarOpen = false;
  140. let pinned = false;
  141. let horizon = navbarHeight;
  142. let whereYouStoppedScrolling = 0;
  143. let threshold = 200;
  144. let scrollFactor = 0;
  145. navbarBurger.addEventListener('click', el => {
  146. navbarOpen = !navbarOpen;
  147. if (navbarOpen) {
  148. rootEl.classList.add('bd-is-clipped-touch');
  149. } else {
  150. rootEl.classList.remove('bd-is-clipped-touch');
  151. }
  152. });
  153. function upOrDown(lastY, currentY) {
  154. if (currentY >= lastY) {
  155. return goingDown(currentY);
  156. }
  157. return goingUp(currentY);
  158. }
  159. function goingDown(currentY) {
  160. const trigger = navbarHeight;
  161. whereYouStoppedScrolling = currentY;
  162. if (currentY > horizon) {
  163. horizon = currentY;
  164. }
  165. translateHeader(currentY);
  166. }
  167. function goingUp(currentY) {
  168. const trigger = 0;
  169. if (currentY < (whereYouStoppedScrolling - navbarHeight)) {
  170. horizon = currentY + navbarHeight;
  171. }
  172. translateHeader(currentY);
  173. }
  174. function constrainDelta(delta) {
  175. return Math.max(0, Math.min(delta, navbarHeight));
  176. }
  177. function translateHeader(currentY) {
  178. const delta = constrainDelta(Math.abs(currentY - horizon));
  179. const translateValue = delta - navbarHeight;
  180. const translateFactor = 1 + translateValue / navbarHeight;
  181. let navbarStyle = `
  182. transform: translateY(${translateValue}px);
  183. `;
  184. if (currentY > threshold) {
  185. scrollFactor = 1;
  186. } else {
  187. scrollFactor = currentY / threshold;
  188. }
  189. specialShadow.style.opacity = scrollFactor;
  190. specialShadow.style.transform = 'scaleY(' + translateFactor + ')';
  191. navbarEl.setAttribute('style', navbarStyle);
  192. }
  193. translateHeader(window.scrollY);
  194. let ticking = false;
  195. let lastY = 0;
  196. window.addEventListener('scroll', function() {
  197. const currentY = window.scrollY;
  198. if (!ticking) {
  199. window.requestAnimationFrame(function() {
  200. upOrDown(lastY, currentY);
  201. ticking = false;
  202. lastY = currentY;
  203. });
  204. }
  205. ticking = true;
  206. });
  207. });