-
Notifications
You must be signed in to change notification settings - Fork 2
/
UIModal.min.js
1 lines (1 loc) · 2.5 KB
/
UIModal.min.js
1
!function(t){"function"==typeof define&&define.amd?define(t):"undefined"!=typeof module&&void 0!==module.exports?module.exports=t():window.UIModal=t()}(function(){var t=function(t){t=t||{},this.el=null,this.onshow=t.onshow||function(){},this.onshown=t.onshown||function(){},this.onhide=t.onhide||function(){},this.onhidden=t.onhidden||function(){},this.style={position:"fixed",top:0,left:0,right:0,bottom:0,overflow:"auto",zIndex:t.zIndex||1e3,background:t.background||"rgba(0,0,0,0)"},this.backClose=t.backClose||!1};t.prototype={open:function(){if(!this.el&&!1!==this.onshow.apply(this,arguments)){this._setBodyScroll(),this.el=document.createElement("div"),this.el.className="ui-modal";for(var t in this.style)this.el.style[t]=this.style[t];if(document.body.appendChild(this.el),this.backClose){var e=this;this.el.addEventListener("click",function(t){t.stopPropagation(),t.target===this&&e.close()})}return this.onshown.apply(this,arguments)}},close:function(t){if(this.el&&!1!==this.onhide.apply(this,t)){this.el.remove(),this.el=null;var e=!1;return[].slice.call(document.querySelectorAll(".ui-modal"),function(t,o){if("none"!=getComputedStyle(t).display)return e=!0,!1}),e||this._resetBodyScroll(),this.onhidden.apply(this,t)}},_setBodyScroll:function(){var t=document.body,e=parseInt(getComputedStyle(document.body).marginRight||0)+parseInt(getComputedStyle(document.body).marginLeft||0);if(-1==t.classList.value.indexOf("ui-modal-open")&&t.clientWidth+e<window.innerWidth){var o,i,n=t.getAttribute("data-scrollInfo"),d=parseInt(getComputedStyle(document.body).paddingRight||0);if(n)i=parseInt(n.split(",")[1]);else{var l=document.createElement("style");l.type="text/css",l.appendChild(document.createTextNode(".ui-modal-open{overflow:hidden;}")),document.querySelector("head").appendChild(l),o=t.style.paddingRight||"",i=function(){var t=document.createElement("div");t.style.cssText="position: absolute;top: -9999px;margin: 0;padding: 0;width: 50px;height: 50px;overflow: scroll;",document.body.appendChild(t);var e=t.offsetWidth-t.clientWidth;return document.body.removeChild(t),e}(),t.setAttribute("data-scrollInfo",o+","+i)}t.classList.add("ui-modal-open"),t.style.paddingRight=d+i+"px"}},_resetBodyScroll:function(){var t=document.body;if(-1!=t.classList.value.indexOf("ui-modal-open")){var e=t.getAttribute("data-scrollInfo").split(",");t.classList.remove("ui-modal-open"),t.style.paddingRight=e[0]}}};var e=function(e,o){return new t(e,o)};return e.setBodyScroll=t.prototype._setBodyScroll,e.resetBodyScroll=t.prototype._resetBodyScroll,e});