main.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. // 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. document.addEventListener('keydown', event => {
  97. const e = event || window.event;
  98. if (e.keyCode === 27) {
  99. closeModals();
  100. closeDropdowns();
  101. }
  102. });
  103. // Clipboard
  104. const $highlights = getAll('.highlight');
  105. let itemsProcessed = 0;
  106. if ($highlights.length > 0) {
  107. $highlights.forEach($el => {
  108. const copyEl = '<button class="button is-small bd-copy">Copy</button>';
  109. const expandEl = '<button class="button is-small bd-expand">Expand</button>';
  110. $el.insertAdjacentHTML('beforeend', copyEl);
  111. const $parent = $el.parentNode;
  112. if ($parent && $parent.classList.contains('bd-is-more')) {
  113. const showEl = '<button class="bd-show"><div><span class="icon"><i class="fas fa-code"></i></span> <strong>Show code</strong></div></button>';
  114. $el.insertAdjacentHTML('beforeend', showEl);
  115. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  116. $el.insertAdjacentHTML('beforeend', expandEl);
  117. }
  118. itemsProcessed++;
  119. if (itemsProcessed === $highlights.length) {
  120. addHighlightControls();
  121. }
  122. });
  123. }
  124. function addHighlightControls() {
  125. const $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
  126. $highlightButtons.forEach($el => {
  127. $el.addEventListener('mouseenter', () => {
  128. $el.parentNode.classList.add('bd-is-hovering');
  129. });
  130. $el.addEventListener('mouseleave', () => {
  131. $el.parentNode.classList.remove('bd-is-hovering');
  132. });
  133. });
  134. const $highlightExpands = getAll('.highlight .bd-expand');
  135. $highlightExpands.forEach($el => {
  136. $el.addEventListener('click', () => {
  137. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  138. });
  139. });
  140. const $highlightShows = getAll('.highlight .bd-show');
  141. $highlightShows.forEach($el => {
  142. $el.addEventListener('click', () => {
  143. $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
  144. });
  145. });
  146. }
  147. setTimeout(() => {
  148. new Clipboard('.bd-copy', {
  149. target: trigger => {
  150. return trigger.previousElementSibling.firstElementChild;
  151. }
  152. });
  153. }, 100);
  154. // Functions
  155. function getAll(selector) {
  156. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  157. }
  158. // Scrolling
  159. const navbarEl = document.getElementById('navbar');
  160. const navbarBurger = document.getElementById('navbarBurger');
  161. const specialShadow = document.getElementById('specialShadow');
  162. const NAVBAR_HEIGHT = 52;
  163. const THRESHOLD = 160;
  164. let navbarOpen = false;
  165. let horizon = NAVBAR_HEIGHT;
  166. let whereYouStoppedScrolling = 0;
  167. let scrollFactor = 0;
  168. let currentTranslate = 0;
  169. navbarBurger.addEventListener('click', el => {
  170. navbarOpen = !navbarOpen;
  171. if (navbarOpen) {
  172. rootEl.classList.add('bd-is-clipped-touch');
  173. } else {
  174. rootEl.classList.remove('bd-is-clipped-touch');
  175. }
  176. });
  177. function upOrDown(lastY, currentY) {
  178. if (currentY >= lastY) {
  179. return goingDown(currentY);
  180. }
  181. return goingUp(currentY);
  182. }
  183. function goingDown(currentY) {
  184. const trigger = NAVBAR_HEIGHT;
  185. whereYouStoppedScrolling = currentY;
  186. if (currentY > horizon) {
  187. horizon = currentY;
  188. }
  189. translateHeader(currentY, false);
  190. }
  191. function goingUp(currentY) {
  192. const trigger = 0;
  193. if (currentY < (whereYouStoppedScrolling - NAVBAR_HEIGHT)) {
  194. horizon = currentY + NAVBAR_HEIGHT;
  195. }
  196. translateHeader(currentY, true);
  197. }
  198. function constrainDelta(delta) {
  199. return Math.max(0, Math.min(delta, NAVBAR_HEIGHT));
  200. }
  201. function translateHeader(currentY, upwards) {
  202. // let topTranslateValue;
  203. let translateValue;
  204. if (upwards && currentTranslate == 0) {
  205. translateValue = 0;
  206. } else if (currentY <= NAVBAR_HEIGHT) {
  207. translateValue = currentY * -1;
  208. } else {
  209. const delta = constrainDelta(Math.abs(currentY - horizon));
  210. translateValue = delta - NAVBAR_HEIGHT;
  211. }
  212. if (translateValue != currentTranslate) {
  213. const navbarStyle = `
  214. transform: translateY(${translateValue}px);
  215. `;
  216. currentTranslate = translateValue;
  217. navbarEl.setAttribute('style', navbarStyle);
  218. }
  219. if (currentY > THRESHOLD * 2) {
  220. scrollFactor = 1;
  221. } else if (currentY > THRESHOLD) {
  222. scrollFactor = (currentY - THRESHOLD) / THRESHOLD;
  223. } else {
  224. scrollFactor = 0;
  225. }
  226. const translateFactor = 1 + translateValue / NAVBAR_HEIGHT;
  227. if (specialShadow) {
  228. specialShadow.style.opacity = scrollFactor;
  229. specialShadow.style.transform = 'scaleY(' + translateFactor + ')';
  230. }
  231. }
  232. translateHeader(window.scrollY, false);
  233. let ticking = false;
  234. let lastY = 0;
  235. window.addEventListener('scroll', function() {
  236. const currentY = window.scrollY;
  237. if (!ticking) {
  238. window.requestAnimationFrame(function() {
  239. upOrDown(lastY, currentY);
  240. ticking = false;
  241. lastY = currentY;
  242. });
  243. }
  244. ticking = true;
  245. });
  246. });