main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. document.addEventListener('DOMContentLoaded', () => {
  2. // Toggles
  3. const $burgers = getAll('.burger');
  4. const $fries = getAll('.fries');
  5. if ($burgers.length > 0) {
  6. $burgers.forEach($el => {
  7. $el.addEventListener('click', () => {
  8. const target = $el.dataset.target;
  9. const $target = document.getElementById(target);
  10. $el.classList.toggle('is-active');
  11. $target.classList.toggle('is-active');
  12. });
  13. });
  14. }
  15. // Modals
  16. const $html = document.documentElement;
  17. const $modals = getAll('.modal');
  18. const $modalButtons = getAll('.modal-button');
  19. const $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
  20. if ($modalButtons.length > 0) {
  21. $modalButtons.forEach($el => {
  22. $el.addEventListener('click', () => {
  23. const target = $el.dataset.target;
  24. const $target = document.getElementById(target);
  25. $html.classList.add('is-clipped');
  26. $target.classList.add('is-active');
  27. });
  28. });
  29. }
  30. if ($modalCloses.length > 0) {
  31. $modalCloses.forEach($el => {
  32. $el.addEventListener('click', () => {
  33. $html.classList.remove('is-clipped');
  34. closeModals();
  35. });
  36. });
  37. }
  38. document.addEventListener('keydown', e => {
  39. if (e.keyCode === 27) {
  40. $html.classList.remove('is-clipped');
  41. closeModals();
  42. }
  43. });
  44. function closeModals() {
  45. $modals.forEach($el => {
  46. $el.classList.remove('is-active');
  47. });
  48. }
  49. // Clipboard
  50. const $highlights = getAll('.highlight');
  51. let itemsProcessed = 0;
  52. if ($highlights.length > 0) {
  53. $highlights.forEach($el => {
  54. const copy = '<button class="copy">Copy</button>';
  55. const expand = '<button class="expand">Expand</button>';
  56. $el.insertAdjacentHTML('beforeend', copy);
  57. if ($el.firstElementChild.scrollHeight > 320) {
  58. $el.insertAdjacentHTML('beforeend', expand);
  59. }
  60. itemsProcessed++;
  61. if (itemsProcessed === $highlights.length) {
  62. addHighlightControls();
  63. }
  64. });
  65. }
  66. function addHighlightControls() {
  67. const $highlightButtons = getAll('.highlight .copy, .highlight .expand');
  68. $highlightButtons.forEach($el => {
  69. $el.addEventListener('mouseenter', () => {
  70. $el.parentNode.style.boxShadow = '0 0 0 1px #ed6c63';
  71. });
  72. $el.addEventListener('mouseleave', () => {
  73. $el.parentNode.style.boxShadow = 'none';
  74. });
  75. });
  76. const $highlightExpands = getAll('.highlight .expand');
  77. $highlightExpands.forEach($el => {
  78. $el.addEventListener('click', () => {
  79. $el.parentNode.firstElementChild.style.maxHeight = 'none';
  80. });
  81. });
  82. }
  83. new Clipboard('.copy', {
  84. target: function(trigger) {
  85. return trigger.previousSibling;
  86. }
  87. });
  88. // Functions
  89. function getAll(selector) {
  90. return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
  91. }
  92. });