main.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. <<<<<<< HEAD
  92. <<<<<<< HEAD
  93. <<<<<<< HEAD
  94. <<<<<<< HEAD
  95. <<<<<<< HEAD
  96. =======
  97. >>>>>>> Add show code
  98. =======
  99. >>>>>>> Improve snippet
  100. =======
  101. >>>>>>> Improve snippet
  102. =======
  103. =======
  104. >>>>>>> Add show code
  105. >>>>>>> Add show code
  106. =======
  107. >>>>>>> Improve snippet
  108. var $parent = $el.parentNode;
  109. if ($parent && $parent.classList.contains('bd-is-more')) {
  110. var showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  111. $el.insertAdjacentHTML('beforeend', showEl);
  112. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  113. $el.insertAdjacentHTML('beforeend', expandEl);
  114. =======
  115. if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  116. $el.insertAdjacentHTML('beforeend', expandEl);
  117. }
  118. <<<<<<< HEAD
  119. <<<<<<< HEAD
  120. <<<<<<< HEAD
  121. <<<<<<< HEAD
  122. =======
  123. >>>>>>> Add show code
  124. =======
  125. >>>>>>> Improve snippet
  126. var $parent = $el.parentNode;
  127. if ($parent && $parent.classList.contains('bd-is-more')) {
  128. var showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  129. $el.insertAdjacentHTML('beforeend', showEl);
  130. <<<<<<< HEAD
  131. >>>>>>> Add show code
  132. =======
  133. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  134. $el.insertAdjacentHTML('beforeend', expandEl);
  135. >>>>>>> Improve snippet
  136. =======
  137. <<<<<<< HEAD
  138. =======
  139. >>>>>>> Improve snippet
  140. =======
  141. =======
  142. >>>>>>> Improve snippet
  143. >>>>>>> Improve snippet
  144. var $parent = $el.parentNode;
  145. if ($parent && $parent.classList.contains('bd-is-more')) {
  146. var showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  147. $el.insertAdjacentHTML('beforeend', showEl);
  148. <<<<<<< HEAD
  149. >>>>>>> Add show code
  150. <<<<<<< HEAD
  151. >>>>>>> Add show code
  152. =======
  153. =======
  154. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  155. $el.insertAdjacentHTML('beforeend', expandEl);
  156. >>>>>>> Improve snippet
  157. >>>>>>> Improve snippet
  158. =======
  159. =======
  160. =======
  161. >>>>>>> Improve snippet
  162. >>>>>>> Improve snippet
  163. var $parent = $el.parentNode;
  164. if ($parent && $parent.classList.contains('bd-is-more')) {
  165. var showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
  166. $el.insertAdjacentHTML('beforeend', showEl);
  167. <<<<<<< HEAD
  168. >>>>>>> Add show code
  169. <<<<<<< HEAD
  170. >>>>>>> Add show code
  171. <<<<<<< HEAD
  172. >>>>>>> Add show code
  173. =======
  174. =======
  175. =======
  176. } else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
  177. $el.insertAdjacentHTML('beforeend', expandEl);
  178. >>>>>>> Improve snippet
  179. >>>>>>> Improve snippet
  180. >>>>>>> Improve snippet
  181. }
  182. itemsProcessed++;
  183. if (itemsProcessed === $highlights.length) {
  184. addHighlightControls();
  185. }
  186. });
  187. }
  188. function addHighlightControls() {
  189. var $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
  190. $highlightButtons.forEach(function ($el) {
  191. $el.addEventListener('mouseenter', function () {
  192. $el.parentNode.classList.add('bd-is-hovering');
  193. });
  194. $el.addEventListener('mouseleave', function () {
  195. $el.parentNode.classList.remove('bd-is-hovering');
  196. });
  197. });
  198. var $highlightExpands = getAll('.highlight .bd-expand');
  199. $highlightExpands.forEach(function ($el) {
  200. $el.addEventListener('click', function () {
  201. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  202. });
  203. });
  204. <<<<<<< HEAD
  205. <<<<<<< HEAD
  206. <<<<<<< HEAD
  207. <<<<<<< HEAD
  208. <<<<<<< HEAD
  209. <<<<<<< HEAD
  210. =======
  211. >>>>>>> Improve snippet
  212. =======
  213. >>>>>>> Add show code
  214. =======
  215. =======
  216. >>>>>>> Improve snippet
  217. >>>>>>> Improve snippet
  218. var $highlightShows = getAll('.highlight .bd-show');
  219. $highlightShows.forEach(function ($el) {
  220. $el.addEventListener('click', function () {
  221. $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
  222. =======
  223. var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
  224. $highlightShows.forEach(function ($el) {
  225. $el.addEventListener('click', function () {
  226. $el.parentNode.classList.remove('bd-is-clipped');
  227. >>>>>>> Add show code
  228. =======
  229. <<<<<<< HEAD
  230. <<<<<<< HEAD
  231. <<<<<<< HEAD
  232. =======
  233. >>>>>>> Add show code
  234. =======
  235. >>>>>>> Improve snippet
  236. =======
  237. =======
  238. >>>>>>> Add show code
  239. >>>>>>> Add show code
  240. =======
  241. =======
  242. >>>>>>> Add show code
  243. =======
  244. >>>>>>> Improve snippet
  245. >>>>>>> Improve snippet
  246. var $highlightShows = getAll('.highlight .bd-show');
  247. $highlightShows.forEach(function ($el) {
  248. $el.addEventListener('click', function () {
  249. $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
  250. <<<<<<< HEAD
  251. <<<<<<< HEAD
  252. <<<<<<< HEAD
  253. <<<<<<< HEAD
  254. >>>>>>> Improve snippet
  255. =======
  256. =======
  257. var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
  258. $highlightShows.forEach(function ($el) {
  259. $el.addEventListener('click', function () {
  260. $el.parentNode.classList.remove('bd-is-clipped');
  261. >>>>>>> Add show code
  262. >>>>>>> Add show code
  263. =======
  264. >>>>>>> Improve snippet
  265. =======
  266. >>>>>>> Add show code
  267. =======
  268. >>>>>>> Improve snippet
  269. >>>>>>> Improve snippet
  270. =======
  271. =======
  272. var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
  273. $highlightShows.forEach(function ($el) {
  274. $el.addEventListener('click', function () {
  275. $el.parentNode.classList.remove('bd-is-clipped');
  276. >>>>>>> Add show code
  277. >>>>>>> Add show code
  278. =======
  279. >>>>>>> Improve snippet
  280. >>>>>>> Improve snippet
  281. });
  282. });
  283. }
  284. new Clipboard('.bd-copy', {
  285. target: function target(trigger) {
  286. return trigger.previousSibling;
  287. }
  288. });
  289. // Functions
  290. function getAll(selector) {
  291. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  292. }
  293. var latestKnownScrollY = 0;
  294. var ticking = false;
  295. function scrollUpdate() {
  296. ticking = false;
  297. // do stuff
  298. }
  299. function onScroll() {
  300. latestKnownScrollY = window.scrollY;
  301. scrollRequestTick();
  302. }
  303. function scrollRequestTick() {
  304. if (!ticking) {
  305. requestAnimationFrame(scrollUpdate);
  306. }
  307. ticking = true;
  308. }
  309. window.addEventListener('scroll', onScroll, false);
  310. });