main.js 8.1 KB

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