main.js 5.8 KB

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