UNPKG

8.11 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)}var Component=function(){function t(t,e,n){var o=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,i={};if(r){var s=Util.Selector.attrConfig(r);s&&(i=s)}this.defaultProps=e,this.props=Object.assign(e,i,n,{element:r}),this.id=this.uid(),this.elementListener=function(t){return o.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 o=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)}),i="on"+r.charAt(0).toUpperCase()+r.slice(1),s=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[r]&&t[r].apply(o,[e]),"function"==typeof t[i]&&s[i].apply(o,[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 o(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(o,n),o.attachDOM=function(){var s="collapse";Util.Observer.subscribe({componentClass:s,onAdded:function(t,e){e(new o({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 o=e.getAttribute("data-target")||e.getAttribute("href");if(!o)return;t.preventDefault();var r=document.querySelector(o);if(!r)return;var i=Util.Observer.getComponent(s,{element:r});if(!i)return;i.toggle({element:r,toggle:!0})}}}})},o.prototype.getHeight=function(){return this.getElement().getBoundingClientRect(this.getElement()).height},o.prototype.toggle=function(){return this.isVisible()?this.hide():this.show()},o.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 o=this.getHeight();return e.style.height="0px",setTimeout(function(){e.style.height=o+"px"},20),!0},o.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},o.prototype.isVisible=function(){return this.getElement().classList.contains("show")},o}(Component);Collapse.attachDOM();var Accordion=function(n){function o(t){var o=n.call(this,"accordion",{multiple:!1},t)||this;o.collapses=[];var e=o.getElement();return Array.from(e.querySelectorAll('[data-toggle="accordion"]')||[]).forEach(function(t){var e=t.getAttribute("href")||t.getAttribute("data-target");if(null===e)throw new Error("Accordion: collapse is missing href or data-target attribute");var n=document.querySelector(e);n&&o.addCollapse(n)}),o.registerElement({target:e,event:Util.Event.CLICK}),o}return __extends(o,n),o.attachDOM=function(){Util.Observer.subscribe({componentClass:"accordion",onAdded:function(t,e){e(new o({element:t}))},onRemoved:function(t,e){e("Accordion",t)}})},o.prototype.addCollapse=function(t){var e=new Collapse({element:t});return this.collapses.push(e),e},o.prototype.getCollapse=function(e){var n=this.getElement(),t=this.collapses.find(function(t){return n.getAttribute("id")===e.getAttribute("id")});return t||(t=this.addCollapse(e)),t},o.prototype.getCollapses=function(){return this.collapses},o.prototype.setCollapses=function(t){var e=this,n=(this.getElement(),this.getCollapse(t));this.getProp("multiple")||this.collapses.filter(function(t){return t.getElement()!==n.getElement()}).forEach(function(t){e.toggleIcon(t.getElement(),"icon-minus","icon-plus"),t.hide()});var o=n.isVisible();this.toggleIcon(n.getElement(),o?"icon-minus":"icon-plus",o?"icon-plus":"icon-minus"),n.toggle()},o.prototype.onElementEvent=function(t){var e=t.target,n=Util.Selector.closest(e,'[data-toggle="accordion"]');if(n){var o=n.getAttribute("data-target")||n.getAttribute("href");if(o){var r=document.querySelector(o);Util.Selector.closest(n,".accordion")&&r&&(t.preventDefault(),this.show(r))}}},o.prototype.toggleIcon=function(t,e,n){var o='[data-toggle="accordion"][href="#'+t.getAttribute("id")+'"] .collapse-toggle',r=document.querySelector(o);r&&r.classList.contains(e)&&(r.classList.remove(e),r.classList.add(n))},o.prototype.show=function(t){var e=t;if("string"==typeof t&&(e=document.querySelector(t)),!e)throw new Error("The collapsible "+t+" is an invalid HTMLElement.");return this.setCollapses(e),!0},o.prototype.hide=function(t){var e=t;if("string"==typeof t&&(e=document.querySelector(t)),!e)throw new Error("The collapsible "+t+" is an invalid HTMLElement.");return this.getCollapse(e).hide()},o}(Component);Accordion.attachDOM(),module.exports=Accordion;
2//# sourceMappingURL=/home/qathom/Workspaces/Web/phonon/dist/js/components/accordion.min.js.map
\No newline at end of file