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)}var Component=function(){function t(t,e,n){var i=this;this.template="",this.id=null,this.eventHandlers=[],this.registeredElements=[],this.name=t;var r="string"==typeof n.element?document.querySelector(n.element):n.element,s={};if(r){var o=Util.Selector.attrConfig(r);o&&(s=o)}this.defaultProps=e,this.props=Object.assign(e,s,n,{element:r}),this.id=this.uid(),this.elementListener=function(t){return i.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 i=this;void 0===e&&(e={}),void 0===n&&(n=!1);var r=t.split(".").reduce(function(t,e,n){return 0===n?e:t+e.charAt(0).toUpperCase()+e.slice(1)}),s="on"+r.charAt(0).toUpperCase()+r.slice(1),o=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[r]&&t[r].apply(i,[e]),"function"==typeof t[s]&&o[s].apply(i,[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}(),Alert=function(i){function n(t){void 0===t&&(t={fade:!0});var e=i.call(this,"alert",{fade:!0},t)||this;if(e.onTransition=!1,0!==e.getOpacity()){var n=e.getElement().querySelector('[data-dismiss="alert"]');n&&e.registerElement({target:n,event:Util.Event.CLICK})}return e}return __extends(n,i),n.attachDOM=function(){Util.Observer.subscribe({componentClass:"alert",onAdded:function(t,e){e(new n({element:t}))},onRemoved:function(t,e){e("Alert",t)}})},n.prototype.show=function(){var e=this;if(this.onTransition)return!1;var n=this.getElement();if(n.classList.contains("show")&&0!==this.getOpacity())return!1;this.onTransition=!0,this.triggerEvent(Util.Event.SHOW);var i=function(){e.triggerEvent(Util.Event.SHOWN),n.classList.contains("fade")&&n.classList.remove("fade");var t=Util.Selector.closest(e.getElement(),'[data-dismiss="alert"]');t&&e.registerElement({target:t,event:Util.Event.CLICK}),n.removeEventListener(Util.Event.TRANSITION_END,i),e.onTransition=!1},t=this.getProp("fade");return t&&!n.classList.contains("fade")&&n.classList.add("fade"),n.classList.add("show"),n.addEventListener(Util.Event.TRANSITION_END,i),n.classList.contains("hide")&&n.classList.remove("hide"),t||i(),!0},n.prototype.hide=function(t){var e=this;if(this.onTransition||0===this.getOpacity())return!1;this.onTransition=!0;var n=t||this.getElement();this.triggerEvent(Util.Event.HIDE);var i=function(){e.triggerEvent(Util.Event.HIDDEN),n.removeEventListener(Util.Event.TRANSITION_END,i),e.onTransition=!1},r=this.getProp("fade");return r&&!n.classList.contains("fade")&&n.classList.add("fade"),n.addEventListener(Util.Event.TRANSITION_END,i),n.classList.contains("hide")||n.classList.add("hide"),n.classList.contains("show")&&n.classList.remove("show"),r||i(),!0},n.prototype.onElementEvent=function(t){t.type===Util.Event.CLICK&&this.hide()},n.prototype.destroy=function(){this.unregisterElements(),this.hide()},n.prototype.getOpacity=function(){var t=this.getElement(),e=window.getComputedStyle(t).opacity;return parseFloat(e||"")},n}(Component);Alert.attachDOM(),module.exports=Alert;
|