main.js 3.4 KB

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