main.js 3.5 KB

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