main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 copyEl = '<button class="button is-small bd-copy">Copy</button>';
  87. const expandEl = '<button class="button is-small bd-expand">Expand</button>';
  88. $el.insertAdjacentHTML('beforeend', copyEl);
  89. const $parent = $el.parentNode;
  90. if ($parent && $parent.classList.contains('bd-is-more')) {
  91. const showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  92. $el.insertAdjacentHTML('beforeend', showEl);
  93. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  94. $el.insertAdjacentHTML('beforeend', expandEl);
  95. }
  96. itemsProcessed++;
  97. if (itemsProcessed === $highlights.length) {
  98. addHighlightControls();
  99. }
  100. });
  101. }
  102. function addHighlightControls() {
  103. const $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
  104. $highlightButtons.forEach($el => {
  105. $el.addEventListener('mouseenter', () => {
  106. $el.parentNode.classList.add('bd-is-hovering');
  107. });
  108. $el.addEventListener('mouseleave', () => {
  109. $el.parentNode.classList.remove('bd-is-hovering');
  110. });
  111. });
  112. const $highlightExpands = getAll('.highlight .bd-expand');
  113. $highlightExpands.forEach($el => {
  114. $el.addEventListener('click', () => {
  115. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  116. });
  117. });
  118. const $highlightShows = getAll('.highlight .bd-show');
  119. $highlightShows.forEach($el => {
  120. $el.addEventListener('click', () => {
  121. $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
  122. });
  123. });
  124. }
  125. new Clipboard('.bd-copy', {
  126. target: function(trigger) {
  127. return trigger.previousSibling;
  128. }
  129. });
  130. var npmClipboard = new Clipboard('#npmCopy');
  131. npmClipboard.on('onclick', function(e) {
  132. console.log('CLICK');
  133. });
  134. npmClipboard.on('success', function(e) {
  135. e.trigger.innerText = 'copied!';
  136. e.trigger.classList.add('is-success');
  137. setTimeout(() => {
  138. e.trigger.innerText = 'copy';
  139. e.trigger.classList.remove('is-success');
  140. }, 500);
  141. e.clearSelection();
  142. });
  143. npmClipboard.on('error', function(e) {
  144. e.trigger.innerText = 'error!';
  145. e.trigger.classList.add('is-error');
  146. setTimeout(() => {
  147. e.trigger.innerText = 'copy';
  148. e.trigger.classList.remove('is-error');
  149. }, 500);
  150. });
  151. // Functions
  152. function getAll(selector) {
  153. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  154. }
  155. let latestKnownScrollY = 0;
  156. let ticking = false;
  157. function scrollUpdate() {
  158. ticking = false;
  159. // do stuff
  160. }
  161. function onScroll() {
  162. latestKnownScrollY = window.scrollY;
  163. scrollRequestTick();
  164. }
  165. function scrollRequestTick() {
  166. if(!ticking) {
  167. requestAnimationFrame(scrollUpdate);
  168. }
  169. ticking = true;
  170. }
  171. window.addEventListener('scroll', onScroll, false);
  172. });