-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1 lines (1 loc) · 9.12 KB
/
index.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).Popup=e()}(this,function(){"use strict";function i(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function e(e,t){var n,o=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),o.push.apply(o,n)),o}function s(i){for(var t=1;t<arguments.length;t++){var s=null!=arguments[t]?arguments[t]:{};t%2?e(Object(s),!0).forEach(function(t){var e,n,o;e=i,o=s[n=t],n in e?Object.defineProperty(e,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[n]=o}):Object.getOwnPropertyDescriptors?Object.defineProperties(i,Object.getOwnPropertyDescriptors(s)):e(Object(s)).forEach(function(t){Object.defineProperty(i,t,Object.getOwnPropertyDescriptor(s,t))})}return i}function r(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],o=!0,i=!1,s=void 0;try{for(var r,a=t[Symbol.iterator]();!(o=(r=a.next()).done)&&(n.push(r.value),!e||n.length!==e);o=!0);}catch(t){i=!0,s=t}finally{try{o||null==a.return||a.return()}finally{if(i)throw s}}return n}(t,e)||n(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t){return function(t){if(Array.isArray(t))return o(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||n(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){if(t){if("string"==typeof t)return o(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(t,e):void 0}}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}var f="popup",c="js-popup-open",b="js-popup",g="js-popup-close",v="active",l="js-btn-in-popup-open",y="#",m=function(e,n){return{name:n,block:e,addMod:function(t){e.classList.add("".concat(n,"--").concat(t))},toggleMod:function(t){e.classList.toggle("".concat(n,"--").concat(t))},removeMod:function(t){e.classList.remove("".concat(n,"--").concat(t))},containsMod:function(t){return e.classList.contains("".concat(n,"--").concat(t))}}};function h(){document.body.style.overflow="",document.body.style.marginRight=""}function p(){function t(){var t,e;o.name=i&&(i.dataset.popupTarget||i.getAttribute("href"))||s,-1<window.location.href.indexOf(y)?n=!1:0===o.name.indexOf(y)?(n=!0,o.href=o.name):(n=!1,s&&c()),o.popup=0===o.name.indexOf(y)?document.getElementById(o.name.slice(1)):document.querySelector(".".concat(b,'[data-popup="').concat(o.name,'"]')),o.popup&&(o.name&&n&&l(),m(o.popup,f).addMod(v),(t=i?i.dataset.toggleBtnClass||u:"")&&m(i,t).addMod(v),d&&(e=window.innerWidth-document.documentElement.clientWidth,document.body.style.overflow="hidden",0<e&&(document.body.style.marginRight="".concat(e,"px"))),p&&(a.filter(function(t){return t===o.popup})[0]||(r.observe(o.popup,{attributes:!0,attributeFilter:["class"],attributeOldValue:!0}),a.push(o.popup))),h&&h())}var n,o=this,i=this.btn,s=this.href,r=this.observer,a=this.observedPopups,c=this.removeUrl,l=this.pushUrl,h=this.onOpen,p=this.onClose,e=this.options,u=e.toggleBtnClass,d=e.preventScroll;i&&i.classList.contains(g)?(this.closeTrigger=i,this.closePopup(),setTimeout(t)):t()}var u={preventScroll:!0,escapeHandler:!0,closeOnOverlayClick:!0,toggleBtnClass:!1};return function(){function e(t){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),this.options=s(s({},u),t),this.closeAll=function(){var t,e,n,o;this.openPopups.length&&(t=this.removeUrl,e=this.options,n=e.toggleBtnClass,o=e.preventScroll,this.openPopups.forEach(function(t){m(t,f).removeMod(v)}),0<this.btns.length&&this.btns.forEach(function(t){var e=t.dataset.toggleBtnClass||n;e&&m(t,e).removeMod(v)}),0<this.hashStart&&t(),o&&h())}.bind(this),this.resetElements=function(){this.btn=null,this.popup=null,this.closeTrigger=null,this.href=""}.bind(this),this.pushUrl=function(){var t="".concat(window.location.href).concat(this.name);window.history.pushState({},"",t)}.bind(this),this.removeUrl=function(){var t=window.location.href.slice(0,this.hashStart);window.history.pushState({},"",t),this.href=""}.bind(this),this.handlePopState=function(){var t,e,n=this.href,o=this.btn,i=this.closePopup,s=this.openPopup;-1===this.hashStart&&this.popup&&(this.closeTrigger=this.openPopups[this.openPopups.length-1],i()),0<this.hashStart&&(e=r((t=window.location.href.split(y))[t.length-1],1)[0],this.popup&&!e&&(this.closeTrigger=this.openPopups[this.openPopups.length-1],this.closePopup()),e&&(n||o||(this.href=window.location.href.slice(this.hashStart)),s()))}.bind(this),this.handleEscClick=function(){var t,e,n;this.openPopups.length&&(t=this.closePopup,e=this.options.escapeHandler,((n=this.openPopups[this.openPopups.length-1]).dataset.escapeHandler?JSON.parse(n.dataset.escapeHandler):e)&&(this.closeTrigger=n,t()))}.bind(this),this.handleBtnClick=function(t){var e,n=t.target,o=this.closePopup,i=this.options.closeOnOverlayClick,s=this.popup,r=n.closest(".".concat(g));r&&r.classList.contains(c)||((s&&s.dataset.closeOnOverlayClick?JSON.parse(s.dataset.closeOnOverlayClick):i)?(e=n.classList&&n.classList.contains(b)?n:null,this.closeTrigger=r||e):this.closeTrigger=r,this.closeTrigger&&(t.preventDefault(),o()))}.bind(this),this.handleOpen=function(t){var e=t.target,n=this.openPopup;this.btn=e.closest(".".concat(c)),this.btn&&(e.closest(".".concat(l))||(n(),t.preventDefault()))}.bind(this),this.handleClose=function(t){var e=t.code,n=t.type,o=this.handleEscClick,i=this.handleBtnClick;"Escape"===e&&o(t),"click"===n&&i(t)}.bind(this),this.openPopup=p.bind(this),this.closePopup=function(){var t=this.closeTrigger,e=this.href,n=this.hashStart,o=this.removeUrl,i=this.options,s=i.toggleBtnClass,r=i.preventScroll;this.popup=t.closest(".".concat(b)),this.name=this.popup.dataset.popup||"#".concat(this.popup.id),this.btn=document.querySelector(".".concat(c,'[data-popup-target="').concat(this.name,'"]'))||document.querySelector(".".concat(c,'[href="').concat(this.name,'"]')),e&&e===this.name&&0<n&&o(),m(this.popup,f).removeMod(v);var a=this.btn?this.btn.dataset.toggleBtnClass||s:"";a&&m(this.btn,a).removeMod(v),r&&!this.openPopups.length&&h()}.bind(this),this.openTarget=function(t){var e=t.id,n=t.dataset.popup,o=this.openPopup;this.href=e?"#".concat(e):null,this.btn=this.href?document.querySelector(".".concat(c,'[href="').concat(this.href,'"]')):document.querySelector(".".concat(c,'[data-popup-target="').concat(n,'"]')),o()}.bind(this),this.closeTarget=function(t){this.closeTrigger=t,this.closePopup()}.bind(this),this.handleMutation=function(t){var e=this.onClose;e&&t.forEach(function(t){0<t.oldValue.indexOf("".concat(f,"--").concat(v))&&e()})}.bind(this),this.btn=null,this.popup=null,this.closeTrigger=null,this.observedPopups=[]}var t,n,o;return t=e,(n=[{key:"_addListeners",value:function(){this.openHandler=this.handleOpen.bind(this),this.closeHandler=this.handleClose.bind(this),this.popstateHandler=this.handlePopState.bind(this),this.onClose&&(this.observer=new MutationObserver(this.handleMutation.bind(this))),document.addEventListener("click",this.openHandler),document.addEventListener("click",this.closeHandler),this.shouldAddEscapeHandler&&document.addEventListener("keydown",this.closeHandler),this.shouldAddPopstate&&window.addEventListener("popstate",this.popstateHandler)}},{key:"_removeListeners",value:function(){document.removeEventListener("click",this.openHandler),document.removeEventListener("click",this.closeHandler),this.shouldAddEscapeHandler&&document.removeEventListener("keydown",this.closeHandler),this.shouldAddPopstate&&window.removeEventListener("popstate",this.popstateHandler)}},{key:"_onLoad",value:function(){0<this.hashStart&&(this.href=window.location.href.slice(this.hashStart),this.openPopup())}},{key:"init",value:function(){this._addListeners(),this._onLoad()}},{key:"destroy",value:function(){this.closeAll(),this._removeListeners(),this.observer&&this.observer.disconnect()}},{key:"popups",get:function(){return a(document.querySelectorAll(".".concat(b)))}},{key:"btns",get:function(){return a(document.querySelectorAll(".".concat(c)))}},{key:"openPopups",get:function(){return this.popups.filter(function(t){return m(t,f).containsMod(v)})}},{key:"hashStart",get:function(){return window.location.href.indexOf(y)}},{key:"shouldAddPopstate",get:function(){return 0<this.btns.filter(function(t){return t.getAttribute("href")&&2<t.getAttribute("href").length}).length}},{key:"shouldAddEscapeHandler",get:function(){return this.options.escapeHandler||0<this.popups.filter(function(t){return"true"===t.dataset.escapeHandler}).length}}])&&i(t.prototype,n),o&&i(t,o),e}()});