main.js 9.5 KB

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