main.js 8.4 KB

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