main.js 3.5 KB

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