main.js 7.6 KB

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