Advertisement
Guest User

Untitled

a guest
Jul 29th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function e(e, t) {
  2.     for (var o = 0; o < t.length; o++) {
  3.         var n = t[o];
  4.         n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n)
  5.     }
  6. }
  7.  
  8. function t(e) {
  9.     return function(e) {
  10.         if (Array.isArray(e)) return o(e)
  11.     }(e) || function(e) {
  12.         if ("undefined" != typeof Symbol && Symbol.iterator in Object(e)) return Array.from(e)
  13.     }(e) || function(e, t) {
  14.         if (!e) return;
  15.         if ("string" == typeof e) return o(e, t);
  16.         var n = Object.prototype.toString.call(e).slice(8, -1);
  17.         "Object" === n && e.constructor && (n = e.constructor.name);
  18.         if ("Map" === n || "Set" === n) return Array.from(e);
  19.         if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return o(e, t)
  20.     }(e) || function() {
  21.         throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  22.     }()
  23. }
  24.  
  25. function o(e, t) {
  26.     (null == t || t > e.length) && (t = e.length);
  27.     for (var o = 0, n = new Array(t); o < t; o++) n[o] = e[o];
  28.     return n
  29. }
  30. var n, i, a, r, s, l = (n = ["a[href]", "area[href]", 'input:not([disabled]):not([type="hidden"]):not([aria-hidden])', "select:not([disabled]):not([aria-hidden])", "textarea:not([disabled]):not([aria-hidden])", "button:not([disabled]):not([aria-hidden])", "iframe", "object", "embed", "[contenteditable]", '[tabindex]:not([tabindex^="-"])'], i = function() {
  31.     function o(e) {
  32.         var n = e.targetModal,
  33.             i = e.triggers,
  34.             a = void 0 === i ? [] : i,
  35.             r = e.onShow,
  36.             s = void 0 === r ? function() {} : r,
  37.             l = e.onClose,
  38.             c = void 0 === l ? function() {} : l,
  39.             d = e.openTrigger,
  40.             u = void 0 === d ? "data-micromodal-trigger" : d,
  41.             f = e.closeTrigger,
  42.             h = void 0 === f ? "data-micromodal-close" : f,
  43.             v = e.openClass,
  44.             g = void 0 === v ? "is-open" : v,
  45.             m = e.disableScroll,
  46.             b = void 0 !== m && m,
  47.             y = e.disableFocus,
  48.             p = void 0 !== y && y,
  49.             w = e.awaitCloseAnimation,
  50.             E = void 0 !== w && w,
  51.             k = e.awaitOpenAnimation,
  52.             M = void 0 !== k && k,
  53.             A = e.debugMode,
  54.             C = void 0 !== A && A;
  55.         ! function(e, t) {
  56.             if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
  57.         }(this, o), this.modal = document.getElementById(n), this.config = {
  58.             debugMode: C,
  59.             disableScroll: b,
  60.             openTrigger: u,
  61.             closeTrigger: h,
  62.             openClass: g,
  63.             onShow: s,
  64.             onClose: c,
  65.             awaitCloseAnimation: E,
  66.             awaitOpenAnimation: M,
  67.             disableFocus: p
  68.         }, a.length > 0 && this.registerTriggers.apply(this, t(a)), this.onClick = this.onClick.bind(this), this.onKeydown = this.onKeydown.bind(this)
  69.     }
  70.     var i, a, r;
  71.     return i = o, (a = [{
  72.         key: "registerTriggers",
  73.         value: function() {
  74.             for (var e = this, t = arguments.length, o = new Array(t), n = 0; n < t; n++) o[n] = arguments[n];
  75.             o.filter(Boolean).forEach((function(t) {
  76.                 t.addEventListener("click", (function(t) {
  77.                     return e.showModal(t)
  78.                 }))
  79.             }))
  80.         }
  81.     }, {
  82.         key: "showModal",
  83.         value: function() {
  84.             var e = this,
  85.                 t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null;
  86.             if (this.activeElement = document.activeElement, this.modal.setAttribute("aria-hidden", "false"), this.modal.classList.add(this.config.openClass), this.scrollBehaviour("disable"), this.addEventListeners(), this.config.awaitOpenAnimation) {
  87.                 var o = function t() {
  88.                     e.modal.removeEventListener("animationend", t, !1), e.setFocusToFirstNode()
  89.                 };
  90.                 this.modal.addEventListener("animationend", o, !1)
  91.             } else this.setFocusToFirstNode();
  92.             this.config.onShow(this.modal, this.activeElement, t)
  93.         }
  94.     }, {
  95.         key: "closeModal",
  96.         value: function() {
  97.             var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null,
  98.                 t = this.modal;
  99.             if (this.modal.setAttribute("aria-hidden", "true"), this.removeEventListeners(), this.scrollBehaviour("enable"), this.activeElement && this.activeElement.focus && this.activeElement.focus(), this.config.onClose(this.modal, this.activeElement, e), this.config.awaitCloseAnimation) {
  100.                 var o = this.config.openClass;
  101.                 this.modal.addEventListener("animationend", (function e() {
  102.                     t.classList.remove(o), t.removeEventListener("animationend", e, !1)
  103.                 }), !1)
  104.             } else t.classList.remove(this.config.openClass)
  105.         }
  106.     }, {
  107.         key: "closeModalById",
  108.         value: function(e) {
  109.             this.modal = document.getElementById(e), this.modal && this.closeModal()
  110.         }
  111.     }, {
  112.         key: "scrollBehaviour",
  113.         value: function(e) {
  114.             if (this.config.disableScroll) {
  115.                 var t = document.querySelector("body");
  116.                 switch (e) {
  117.                     case "enable":
  118.                         Object.assign(t.style, {
  119.                             overflow: ""
  120.                         });
  121.                         break;
  122.                     case "disable":
  123.                         Object.assign(t.style, {
  124.                             overflow: "hidden"
  125.                         })
  126.                 }
  127.             }
  128.         }
  129.     }, {
  130.         key: "addEventListeners",
  131.         value: function() {
  132.             this.modal.addEventListener("touchstart", this.onClick), this.modal.addEventListener("click", this.onClick), document.addEventListener("keydown", this.onKeydown)
  133.         }
  134.     }, {
  135.         key: "removeEventListeners",
  136.         value: function() {
  137.             this.modal.removeEventListener("touchstart", this.onClick), this.modal.removeEventListener("click", this.onClick), document.removeEventListener("keydown", this.onKeydown)
  138.         }
  139.     }, {
  140.         key: "onClick",
  141.         value: function(e) {
  142.             (e.target.hasAttribute(this.config.closeTrigger) || e.target.parentNode.hasAttribute(this.config.closeTrigger)) && (e.preventDefault(), e.stopPropagation(), this.closeModal(e))
  143.         }
  144.     }, {
  145.         key: "onKeydown",
  146.         value: function(e) {
  147.             27 === e.keyCode && this.closeModal(e), 9 === e.keyCode && this.retainFocus(e)
  148.         }
  149.     }, {
  150.         key: "getFocusableNodes",
  151.         value: function() {
  152.             var e = this.modal.querySelectorAll(n);
  153.             return Array.apply(void 0, t(e))
  154.         }
  155.     }, {
  156.         key: "setFocusToFirstNode",
  157.         value: function() {
  158.             var e = this;
  159.             if (!this.config.disableFocus) {
  160.                 var t = this.getFocusableNodes();
  161.                 if (0 !== t.length) {
  162.                     var o = t.filter((function(t) {
  163.                         return !t.hasAttribute(e.config.closeTrigger)
  164.                     }));
  165.                     o.length > 0 && o[0].focus(), 0 === o.length && t[0].focus()
  166.                 }
  167.             }
  168.         }
  169.     }, {
  170.         key: "retainFocus",
  171.         value: function(e) {
  172.             var t = this.getFocusableNodes();
  173.             if (0 !== t.length)
  174.                 if (t = t.filter((function(e) {
  175.                         return null !== e.offsetParent
  176.                     })), this.modal.contains(document.activeElement)) {
  177.                     var o = t.indexOf(document.activeElement);
  178.                     e.shiftKey && 0 === o && (t[t.length - 1].focus(), e.preventDefault()), !e.shiftKey && t.length > 0 && o === t.length - 1 && (t[0].focus(), e.preventDefault())
  179.                 } else t[0].focus()
  180.         }
  181.     }]) && e(i.prototype, a), r && e(i, r), o
  182. }(), a = null, r = function(e) {
  183.     if (!document.getElementById(e)) return console.warn("MicroModal: ❗Seems like you have missed %c'".concat(e, "'"), "background-color: #f8f9fa;color: #50596c;font-weight: bold;", "ID somewhere in your code. Refer example below to resolve it."), console.warn("%cExample:", "background-color: #f8f9fa;color: #50596c;font-weight: bold;", '<div class="modal" id="'.concat(e, '"></div>')), !1
  184. }, s = function(e, t) {
  185.     if (function(e) {
  186.             e.length <= 0 && (console.warn("MicroModal: ❗Please specify at least one %c'micromodal-trigger'", "background-color: #f8f9fa;color: #50596c;font-weight: bold;", "data attribute."), console.warn("%cExample:", "background-color: #f8f9fa;color: #50596c;font-weight: bold;", '<a href="#" data-micromodal-trigger="my-modal"></a>'))
  187.         }(e), !t) return !0;
  188.     for (var o in t) r(o);
  189.     return !0
  190. }, {
  191.     init: function(e) {
  192.         var o = Object.assign({}, {
  193.                 openTrigger: "data-micromodal-trigger"
  194.             }, e),
  195.             n = t(document.querySelectorAll("[".concat(o.openTrigger, "]"))),
  196.             r = function(e, t) {
  197.                 var o = [];
  198.                 return e.forEach((function(e) {
  199.                     var n = e.attributes[t].value;
  200.                     void 0 === o[n] && (o[n] = []), o[n].push(e)
  201.                 })), o
  202.             }(n, o.openTrigger);
  203.         if (!0 !== o.debugMode || !1 !== s(n, r))
  204.             for (var l in r) {
  205.                 var c = r[l];
  206.                 o.targetModal = l, o.triggers = t(c), a = new i(o)
  207.             }
  208.     },
  209.     show: function(e, t) {
  210.         var o = t || {};
  211.         o.targetModal = e, !0 === o.debugMode && !1 === r(e) || (a && a.removeEventListeners(), (a = new i(o)).showModal())
  212.     },
  213.     close: function(e) {
  214.         e ? a.closeModalById(e) : a.closeModal()
  215.     }
  216. });
  217. "undefined" != typeof window && (window.MicroModal = l);
  218. export default l;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement