main.js 3.0 KB

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