UNPKG

rmodal

Version:

A simple modal dialog with no external dependencies. IE9+ supported.

3 lines (2 loc) 3.19 kB
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(t="undefined"!=typeof globalThis?globalThis:t||self).RModal=o()}(this,(function(){var t=function(t){return(""+(t||"")).split(" ").filter((function(t){return!!t}))},o=function(o,e){t(e).forEach((function(t){o.classList.add(t)}))},e=function(o,e){t(e).forEach((function(t){o.classList.remove(t)}))};return function(){function t(t,o){var e=this;void 0===o&&(o={}),this.version="1.1.0",this.focusOutElement=null,this.opened=!1,this.opts={bodyClass:"modal-open",dialogClass:"modal-dialog",dialogOpenClass:"bounceInDown",dialogCloseClass:"bounceOutUp",focus:!0,focusElements:["a[href]","area[href]","input:not([disabled]):not([type=hidden])","button:not([disabled])","select:not([disabled])","textarea:not([disabled])","iframe","object","embed","*[tabindex]","*[contenteditable]"],escapeClose:!0,content:void 0,closeTimeout:500},Object.keys(o).forEach((function(t){var s=t;void 0!==o[s]&&(e.opts[s]=o[s])})),this.overlay=t,this.dialog=t.querySelector("."+this.opts.dialogClass),this.opts.content&&this.content(this.opts.content)}return t.prototype.open=function(t){var o=this;if(this.content(t),"function"!=typeof this.opts.beforeOpen)return this._doOpen();this.opts.beforeOpen((function(){o._doOpen()}))},t.prototype._doOpen=function(){o(document.body,this.opts.bodyClass),e(this.dialog,this.opts.dialogCloseClass),o(this.dialog,this.opts.dialogOpenClass),this.overlay.style.display="block",this.opts.focus&&(this.focusOutElement=document.activeElement,this.focus()),"function"==typeof this.opts.afterOpen&&this.opts.afterOpen(),this.opened=!0},t.prototype.close=function(){var t=this;if("function"!=typeof this.opts.beforeClose)return this._doClose();this.opts.beforeClose((function(){t._doClose()}))},t.prototype._doClose=function(){var t=this;e(this.dialog,this.opts.dialogOpenClass),o(this.dialog,this.opts.dialogCloseClass),e(document.body,this.opts.bodyClass),this.opts.focus&&this.focus(this.focusOutElement),setTimeout((function(){t.overlay.style.display="none","function"==typeof t.opts.afterClose&&t.opts.afterClose(),t.opened=!1}),this.opts.closeTimeout)},t.prototype.content=function(t){if(void 0===t)return this.dialog.innerHTML;this.dialog.innerHTML=t},t.prototype.elements=function(t,o){return o=o||window.navigator.appVersion.indexOf("MSIE 9.0")>-1,t=Array.isArray(t)?t.join(","):t,[].filter.call(this.dialog.querySelectorAll(t),(function(t){if(o){var e=window.getComputedStyle(t);return"none"!==e.display&&"hidden"!==e.visibility}return null!==t.offsetParent}))},t.prototype.focus=function(t){(t=t||this.elements(this.opts.focusElements||"")[0]||this.dialog.firstChild)&&"function"==typeof t.focus&&t.focus()},t.prototype.keydown=function(t){this.opts.escapeClose&&27==t.which&&this.close();var o=function(){t.preventDefault(),t.stopPropagation()};if(this.opened&&9==t.which&&this.dialog.contains(t.target)){var e=this.elements(this.opts.focusElements||""),s=e[0],i=e[e.length-1];s==i?o():t.target==s&&t.shiftKey?(o(),i.focus()):t.target!=i||t.shiftKey||(o(),s.focus())}},t.version="1.1.0",t}()})); //# sourceMappingURL=rmodal.min.js.map