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,o={};if(r){var s=Util.Selector.attrConfig(r);s&&(o=s)}this.defaultProps=e,this.props=Object.assign(e,o,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)}),o="on"+r.charAt(0).toUpperCase()+r.slice(1),s=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[r]&&t[r].apply(i,[e]),"function"==typeof t[o]&&s[o].apply(i,[e])}),!n){var l=this.getElement();l?Util.Dispatch.elementEvent(l,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}(),Collapse=function(n){function i(t){void 0===t&&(t={toggle:!1});var e=n.call(this,"collapse",{toggle:!1},t)||this;return e.getProp("toggle")&&e.toggle(),e}return __extends(i,n),i.attachDOM=function(){var s="collapse";Util.Observer.subscribe({componentClass:s,onAdded:function(t,e){e(new i({element:t}))},onRemoved:function(t,e){e("Collapse",t)}}),document.addEventListener(Util.Event.CLICK,function(t){if(t.target){var e=Util.Selector.closest(t.target,"[data-toggle]");if(e){var n=e.getAttribute("data-toggle");if(n&&n===s){var i=e.getAttribute("data-target")||e.getAttribute("href");if(!i)return;t.preventDefault();var r=document.querySelector(i);if(!r)return;var o=Util.Observer.getComponent(s,{element:r});if(!o)return;o.toggle({element:r,toggle:!0})}}}})},i.prototype.getHeight=function(){return this.getElement().getBoundingClientRect(this.getElement()).height},i.prototype.toggle=function(){return this.isVisible()?this.hide():this.show()},i.prototype.show=function(){var t=this,e=this.getElement();if(e.classList.contains("collapsing")||this.isVisible())return!1;this.triggerEvent(Util.Event.SHOW);var n=function(){t.triggerEvent(Util.Event.SHOWN),e.classList.add("show"),e.classList.remove("collapsing"),e.removeEventListener(Util.Event.TRANSITION_END,n),e.setAttribute("aria-expanded",!0),e.style.height="auto"};e.classList.contains("collapsing")||e.classList.add("collapsing"),e.addEventListener(Util.Event.TRANSITION_END,n);var i=this.getHeight();return e.style.height="0px",setTimeout(function(){e.style.height=i+"px"},20),!0},i.prototype.hide=function(){var t=this,e=this.getElement();if(e.classList.contains("collapsing"))return!1;if(!e.classList.contains("show"))return!1;this.triggerEvent(Util.Event.HIDE);var n=function(){t.triggerEvent(Util.Event.HIDDEN),e.classList.remove("collapsing"),e.style.height="auto",e.removeEventListener(Util.Event.TRANSITION_END,n),e.setAttribute("aria-expanded",!1)};return e.style.height=e.offsetHeight+"px",setTimeout(function(){e.style.height="0px"},20),e.addEventListener(Util.Event.TRANSITION_END,n),e.classList.contains("collapsing")||e.classList.add("collapsing"),e.classList.remove("show"),!0},i.prototype.isVisible=function(){return this.getElement().classList.contains("show")},i}(Component);Collapse.attachDOM(),module.exports=Collapse;
|