UNPKG

13.2 kBJavaScriptView Raw
1"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var Util=_interopDefault(require("../util.js")),extendStatics=function(t,e){return(extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function __extends(t,e){function n(){this.constructor=t}extendStatics(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function __awaiter(i,s,a,l){return new(a||(a=Promise))(function(t,e){function n(t){try{o(l.next(t))}catch(t){e(t)}}function r(t){try{o(l.throw(t))}catch(t){e(t)}}function o(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(n,r)}o((l=l.apply(i,s||[])).next())})}function __generator(n,r){var o,i,s,t,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,i&&(s=2&e[0]?i.return:e[0]?i.throw||((s=i.return)&&s.call(i),0):i.next)&&!(s=s.call(i,e[1])).done)return s;switch(i=0,s&&(e=[2&e[0],s.value]),e[0]){case 0:case 1:s=e;break;case 4:return a.label++,{value:e[1],done:!1};case 5:a.label++,i=e[1],e=[0];continue;case 7:e=a.ops.pop(),a.trys.pop();continue;default:if(!(s=0<(s=a.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){a=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3])){a.label=e[1];break}if(6===e[0]&&a.label<s[1]){a.label=s[1],s=e;break}if(s&&a.label<s[2]){a.label=s[2],a.ops.push(e);break}s[2]&&a.ops.pop(),a.trys.pop();continue}e=r.call(n,a)}catch(t){e=[6,t],i=0}finally{o=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var Size,Color,Direction,Component=function(){function t(t,e,n){var r=this;this.template="",this.id=null,this.eventHandlers=[],this.registeredElements=[],this.name=t;var o="string"==typeof n.element?document.querySelector(n.element):n.element,i={};if(o){var s=Util.Selector.attrConfig(o);s&&(i=s)}this.defaultProps=e,this.props=Object.assign(e,i,n,{element:o}),this.id=this.uid(),this.elementListener=function(t){return r.onBeforeElementEvent(t)},this.setEventsHandler()}return t.prototype.setTemplate=function(t){this.template=t},t.prototype.getTemplate=function(){return this.template},t.prototype.getElement=function(){return this.getProp("element")||null},t.prototype.setElement=function(t){this.props.element=t},t.prototype.getId=function(){return this.id},t.prototype.uid=function(){return Math.random().toString(36).substr(2,10)},t.prototype.getName=function(){return this.name},t.prototype.getProps=function(){return this.props},t.prototype.getProp=function(t){var e=this.defaultProps[t];return void 0!==this.props[t]?this.props[t]:e},t.prototype.setProps=function(t){var e=Object.assign({},t);this.props=Object.assign(this.props,e)},t.prototype.setProp=function(t,e){if(void 0===this.props[t])throw new Error("Cannot set an invalid prop");this.props[t]=e},t.prototype.registerElements=function(t){var e=this;t.forEach(function(t){return e.registerElement(t)})},t.prototype.registerElement=function(t){t.target.addEventListener(t.event,this.elementListener),this.registeredElements.push(t)},t.prototype.unregisterElements=function(){var e=this;this.registeredElements.forEach(function(t){e.unregisterElement(t)})},t.prototype.unregisterElement=function(e){var t=this.registeredElements.findIndex(function(t){return t.target===e.target&&t.event===e.event});-1<t?(e.target.removeEventListener(e.event,this.elementListener),this.registeredElements.splice(t,1)):console.error("Warning! Could not remove element: "+e.target+" with event: "+e.event+".")},t.prototype.triggerEvent=function(t,e,n){var r=this;void 0===e&&(e={}),void 0===n&&(n=!1);var o=t.split(".").reduce(function(t,e,n){return 0===n?e:t+e.charAt(0).toUpperCase()+e.slice(1)}),i="on"+o.charAt(0).toUpperCase()+o.slice(1),s=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[o]&&t[o].apply(r,[e]),"function"==typeof t[i]&&s[i].apply(r,[e])}),!n){var a=this.getElement();a?Util.Dispatch.elementEvent(a,t,this.name,e):Util.Dispatch.winDocEvent(t,this.name,e)}},t.prototype.preventClosable=function(){return!1},t.prototype.destroy=function(){this.unregisterElements()},t.prototype.onElementEvent=function(t){},t.prototype.setEventsHandler=function(){var n=this.getProps(),t=Object.keys(n).reduce(function(t,e){return"function"==typeof n[e]&&(t[e]=n[e]),t},{});0<Object.keys(t).length&&this.eventHandlers.push(t)},t.prototype.onBeforeElementEvent=function(t){this.preventClosable()||this.onElementEvent(t)},t}();!function(t){t.sm="sm",t.md="md",t.lg="lg",t.xl="xl"}(Size||(Size={})),function(t){t.primary="primary",t.secondary="secondary",t.success="success",t.warning="warning",t.danger="danger"}(Color||(Color={})),function(t){t.top="top",t.right="right",t.bottom="bottom",t.left="left"}(Direction||(Direction={}));var Loader=function(e){function t(t){return void 0===t&&(t={color:Color.primary,size:Size.md}),e.call(this,"loader",{fade:!0,size:Size.md,color:Color.primary},t)||this}return __extends(t,e),t.prototype.show=function(){var t=this.getElement();t.classList.contains("hide")&&t.classList.remove("hide"),this.triggerEvent(Util.Event.SHOW);var e=this.getProp("size");Util.Selector.removeClasses(t,Object.values(Size),"loader"),t.classList.add("loader-"+e);var n=this.getProp("color"),r=this.getSpinner();return Util.Selector.removeClasses(r,Object.values(Color),"loader"),r.classList.add("loader-"+n),this.triggerEvent(Util.Event.SHOWN),!0},t.prototype.animate=function(t){void 0===t&&(t=!0),t?this.show():this.hide();var e=this.getSpinner();return t&&!e.classList.contains("loader-spinner-animated")?e.classList.add("loader-spinner-animated"):!t&&e.classList.contains("loader-spinner-animated")&&e.classList.remove("loader-spinner-animated"),!0},t.prototype.hide=function(){var t=this.getElement();return t.classList.contains("hide")||t.classList.add("hide"),this.triggerEvent(Util.Event.HIDE),this.triggerEvent(Util.Event.HIDDEN),!0},t.prototype.getSpinner=function(){return this.getElement().querySelector(".loader-spinner")},t}(Component),Modal=function(r){function n(t,e){void 0===e&&(e=!0);var n=r.call(this,"modal",{title:null,message:null,cancelable:!0,background:null,cancelableKeyCodes:[27,13],buttons:[{event:"confirm",text:"Ok",dismiss:!0,class:"btn btn-primary"}],center:!0},t)||this;return n.backdropSelector="modal-backdrop",n.elementGenerated=!1,n.setTemplate('<div class="modal" tabindex="-1" role="modal" data-no-boot><div class="modal-inner" role="document"><div class="modal-content"><div class="modal-header"><h5 class="modal-title"></h5><button type="button" class="icon-close" data-dismiss="modal" aria-label="Close"><span class="icon" aria-hidden="true"></span></button></div><div class="modal-body"><p></p></div><div class="modal-footer"></div></div></div></div>'),e&&null===n.getElement()&&n.build(),n}return __extends(n,r),n.attachDOM=function(){var s="modal";Util.Observer.subscribe({componentClass:s,onAdded:function(t,e){e(new n({element:t}))},onRemoved:function(t,e){e("Modal",t)}}),document.addEventListener(Util.Event.CLICK,function(t){var e=t.target;if(e){var n=Util.Selector.closest(e,'[data-toggle="modal"]');if(n){var r=n.getAttribute("data-target");if(!r)return;var o=document.querySelector(r);if(!o)return;var i=Util.Observer.getComponent(s,{element:o});if(!i)return;e.blur(),i.show()}}})},n.prototype.build=function(){var e=this;this.elementGenerated=!0;var t=document.createElement("div");t.innerHTML=this.getTemplate(),this.setElement(t.firstChild);var n=this.getElement(),r=this.getProp("title");null!==r&&(n.querySelector(".modal-title").innerHTML=r);var o=this.getProp("message");null!==o?n.querySelector(".modal-body").firstChild.innerHTML=o:this.removeTextBody(),this.getProp("cancelable")||(n.querySelector(".close").style.display="none");var i=this.getProp("buttons");Array.isArray(i)&&0<i.length?i.forEach(function(t){n.querySelector(".modal-footer").appendChild(e.buildButton(t))}):this.removeFooter(),document.body.appendChild(n)},n.prototype.show=function(){var t=this,r=this.getElement();return null===r&&this.build(),!r.classList.contains("show")&&(document.body.style.overflow="hidden",__awaiter(t,void 0,void 0,function(){var e,n=this;return __generator(this,function(t){switch(t.label){case 0:return[4,Util.sleep(20)];case 1:return t.sent(),this.triggerEvent(Util.Event.SHOW),this.buildBackdrop(),this.attachEvents(),e=function(){n.triggerEvent(Util.Event.SHOWN),r.removeEventListener(Util.Event.TRANSITION_END,e)},r.addEventListener(Util.Event.TRANSITION_END,e),this.getProp("center")&&this.center(),r.classList.add("show"),[2]}})}),!0)},n.prototype.hide=function(){var t=this,e=this.getElement();if(!e.classList.contains("show"))return!1;document.body.style.overflow="visible",this.triggerEvent(Util.Event.HIDE),this.detachEvents(),e.classList.add("hide"),e.classList.remove("show");var n=this.getBackdrop(),r=function(){n&&(document.body.removeChild(n),n.removeEventListener(Util.Event.TRANSITION_END,r)),e.classList.remove("hide"),t.triggerEvent(Util.Event.HIDDEN),t.elementGenerated&&document.body.removeChild(e)};return n&&(n.addEventListener(Util.Event.TRANSITION_END,r),n.classList.add("fadeout")),!0},n.prototype.onElementEvent=function(e){if("keyup"!==e.type)if(e.type!==Util.Event.START){if(e.type===Util.Event.CLICK){var t=e.target,n=t.getAttribute("data-event");n&&this.triggerEvent(n);var r=Util.Selector.closest(t,"[data-dismiss]");r&&"modal"===r.getAttribute("data-dismiss")&&this.hide()}}else this.hide();else this.getProp("cancelableKeyCodes").find(function(t){return t===e.keyCode})&&this.hide()},n.prototype.setBackgroud=function(){var t=this.getElement(),e=this.getProp("background");e&&(t.classList.contains("modal-"+e)||t.classList.add("modal-"+e),t.classList.contains("text-white")||t.classList.add("text-white"))},n.prototype.buildButton=function(t){var e=document.createElement("button");return e.setAttribute("type","button"),e.setAttribute("class",t.class||"btn"),e.setAttribute("data-event",t.event),e.innerHTML=t.text,t.dismiss&&e.setAttribute("data-dismiss","modal"),e},n.prototype.buildBackdrop=function(){var t=document.createElement("div");t.setAttribute("data-id",this.getId()),t.classList.add(this.backdropSelector),document.body.appendChild(t)},n.prototype.getBackdrop=function(){return document.querySelector("."+this.backdropSelector+'[data-id="'+this.getId()+'"]')},n.prototype.removeTextBody=function(){var t=this.getElement();t.querySelector(".modal-body").removeChild(t.querySelector(".modal-body").firstChild)},n.prototype.removeFooter=function(){var t=this.getElement(),e=t.querySelector(".modal-footer");t.querySelector(".modal-content").removeChild(e)},n.prototype.center=function(){var t=this.getElement(),e=window.getComputedStyle(t);if(e&&e.height){var n=e.height.slice(0,e.height.length-2),r=window.innerHeight/2-parseFloat(n)/2;t.style.top=r+"px"}},n.prototype.attachEvents=function(){var e=this,t=this.getElement();Array.from(t.querySelectorAll("[data-dismiss], .modal-footer button")||[]).forEach(function(t){return e.registerElement({target:t,event:Util.Event.CLICK})});var n=this.getProp("cancelable"),r=this.getBackdrop();n&&r&&(this.registerElement({target:r,event:Util.Event.START}),this.registerElement({target:document,event:"keyup"}))},n.prototype.detachEvents=function(){var e=this,t=this.getElement();if(Array.from(t.querySelectorAll("[data-dismiss], .modal-footer button")||[]).forEach(function(t){return e.unregisterElement({target:t,event:Util.Event.CLICK})}),this.getProp("cancelable")){var n=this.getBackdrop();this.unregisterElement({target:n,event:Util.Event.START}),this.unregisterElement({target:document,event:"keyup"})}},n}(Component);Modal.attachDOM();var ModalLoader=function(n){function t(t){var e=n.call(this,Object.assign({buttons:[{event:"cancel",text:"Cancel",dismiss:!0,class:"btn btn-secondary"},{event:"confirm",text:"Ok",dismiss:!0,class:"btn btn-primary"}]},t),!1)||this;return e.loader=null,e.setTemplate('<div class="modal" tabindex="-1" role="modal" data-no-boot><div class="modal-inner" role="document"><div class="modal-content"><div class="modal-header"><h5 class="modal-title"></h5><button type="button" class="icon-close" data-dismiss="modal" aria-label="Close"><span class="icon" aria-hidden="true"></span></button></div><div class="modal-body"><p></p><div class="mx-auto text-center"><div class="loader mx-auto d-block"><div class="loader-spinner"></div></div></div></div><div class="modal-footer"></div></div></div></div>'),null===e.getElement()&&e.build(),e}return __extends(t,n),t.prototype.show=function(){return n.prototype.show.call(this),this.loader=new Loader({element:this.getElement().querySelector(".loader")}),this.loader.animate(!0),!0},t.prototype.hide=function(){return n.prototype.hide.call(this),this.loader&&this.loader.animate(!1),!(this.loader=null)},t}(Modal);module.exports=ModalLoader;
2//# sourceMappingURL=/home/qathom/Workspaces/Web/phonon/dist/js/components/modalloader.min.js.map
\No newline at end of file