main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. document.addEventListener('DOMContentLoaded', () => {
  2. // Dropdowns
  3. const $metalinks = getAll('#meta a');
  4. if ($metalinks.length > 0) {
  5. $metalinks.forEach($el => {
  6. $el.addEventListener('click', event => {
  7. event.preventDefault();
  8. const target = $el.getAttribute('href');
  9. const $target = document.getElementById(target.substring(1));
  10. $target.scrollIntoView(true);
  11. // window.history.replaceState(null, document.title, `${window.location.origin}${window.location.pathname}${target}`);
  12. return false;
  13. });
  14. });
  15. }
  16. // Dropdowns
  17. const $dropdowns = getAll('.dropdown:not(.is-hoverable)');
  18. if ($dropdowns.length > 0) {
  19. $dropdowns.forEach($el => {
  20. $el.addEventListener('click', event => {
  21. event.stopPropagation();
  22. $el.classList.toggle('is-active');
  23. });
  24. });
  25. document.addEventListener('click', event => {
  26. closeDropdowns();
  27. });
  28. }
  29. function closeDropdowns() {
  30. $dropdowns.forEach($el => {
  31. $el.classList.remove('is-active');
  32. });
  33. }
  34. // Toggles
  35. const $burgers = getAll('.burger');
  36. if ($burgers.length > 0) {
  37. $burgers.forEach($el => {
  38. $el.addEventListener('click', () => {
  39. const target = $el.dataset.target;
  40. const $target = document.getElementById(target);
  41. $el.classList.toggle('is-active');
  42. $target.classList.toggle('is-active');
  43. });
  44. });
  45. }
  46. // Modals
  47. const $html = document.documentElement;
  48. const $modals = getAll('.modal');
  49. const $modalButtons = getAll('.modal-button');
  50. const $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
  51. if ($modalButtons.length > 0) {
  52. $modalButtons.forEach($el => {
  53. $el.addEventListener('click', () => {
  54. const target = $el.dataset.target;
  55. const $target = document.getElementById(target);
  56. $html.classList.add('is-clipped');
  57. $target.classList.add('is-active');
  58. });
  59. });
  60. }
  61. if ($modalCloses.length > 0) {
  62. $modalCloses.forEach($el => {
  63. $el.addEventListener('click', () => {
  64. closeModals();
  65. });
  66. });
  67. }
  68. document.addEventListener('keydown', event => {
  69. const e = event || window.event;
  70. if (e.keyCode === 27) {
  71. closeModals();
  72. closeDropdowns();
  73. }
  74. });
  75. function closeModals() {
  76. $html.classList.remove('is-clipped');
  77. $modals.forEach($el => {
  78. $el.classList.remove('is-active');
  79. });
  80. }
  81. // Clipboard
  82. const $highlights = getAll('.highlight');
  83. let itemsProcessed = 0;
  84. if ($highlights.length > 0) {
  85. $highlights.forEach($el => {
  86. const copy = '<button class="copy">Copy</button>';
  87. const expand = '<button class="expand">Expand</button>';
  88. $el.insertAdjacentHTML('beforeend', copy);
  89. if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  90. $el.insertAdjacentHTML('beforeend', expand);
  91. }
  92. itemsProcessed++;
  93. if (itemsProcessed === $highlights.length) {
  94. addHighlightControls();
  95. }
  96. });
  97. }
  98. function addHighlightControls() {
  99. const $highlightButtons = getAll('.highlight .copy, .highlight .expand');
  100. $highlightButtons.forEach($el => {
  101. $el.addEventListener('mouseenter', () => {
  102. $el.parentNode.style.boxShadow = '0 0 0 1px #ed6c63';
  103. });
  104. $el.addEventListener('mouseleave', () => {
  105. $el.parentNode.style.boxShadow = 'none';
  106. });
  107. });
  108. const $highlightExpands = getAll('.highlight .expand');
  109. $highlightExpands.forEach($el => {
  110. $el.addEventListener('click', () => {
  111. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  112. });
  113. });
  114. }
  115. new Clipboard('.copy', {
  116. target: function(trigger) {
  117. return trigger.previousSibling;
  118. }
  119. });
  120. // Functions
  121. function getAll(selector) {
  122. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  123. }
  124. let latestKnownScrollY = 0;
  125. let ticking = false;
  126. function scrollUpdate() {
  127. ticking = false;
  128. // do stuff
  129. }
  130. function onScroll() {
  131. latestKnownScrollY = window.scrollY;
  132. scrollRequestTick();
  133. }
  134. function scrollRequestTick() {
  135. if(!ticking) {
  136. requestAnimationFrame(scrollUpdate);
  137. }
  138. ticking = true;
  139. }
  140. window.addEventListener('scroll', onScroll, false);
  141. });