main.js 3.4 KB

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