main.js 7.2 KB

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