UNPKG

7.17 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(o,s,a,l){return new(a||(a=Promise))(function(t,e){function n(t){try{i(l.next(t))}catch(t){e(t)}}function r(t){try{i(l.throw(t))}catch(t){e(t)}}function i(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(n,r)}i((l=l.apply(o,s||[])).next())})}function __generator(n,r){var i,o,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(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,o&&(s=2&e[0]?o.return:e[0]?o.throw||((s=o.return)&&s.call(o),0):o.next)&&!(s=s.call(o,e[1])).done)return s;switch(o=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++,o=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],o=0}finally{i=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var Component=function(){function t(t,e,n){var r=this;this.template="",this.id=null,this.eventHandlers=[],this.registeredElements=[],this.name=t;var i="string"==typeof n.element?document.querySelector(n.element):n.element,o={};if(i){var s=Util.Selector.attrConfig(i);s&&(o=s)}this.defaultProps=e,this.props=Object.assign(e,o,n,{element:i}),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 i=t.split(".").reduce(function(t,e,n){return 0===n?e:t+e.charAt(0).toUpperCase()+e.slice(1)}),o="on"+i.charAt(0).toUpperCase()+i.slice(1),s=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[i]&&t[i].apply(r,[e]),"function"==typeof t[o]&&s[o].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}(),Tab=function(n){function r(t){var e=n.call(this,"tab",{},t)||this;return e.tabSelector="tab",e.tabItemSelector="tab-item",e.tabContentSelector="tab-pane",e.onAnimation=!1,e.registerElement({target:e.getElement(),event:Util.Event.CLICK}),e}return __extends(r,n),r.attachDOM=function(){Util.Observer.subscribe({componentClass:"tabs",onAdded:function(t,e){e(new r({element:t}))},onRemoved:function(t,e){e("Tab",t)}})},r.prototype.onElementEvent=function(t){var e=t.target;if(e&&e.getAttribute("data-toggle")){if(!(e.getAttribute("href")||e.getAttribute("data-target")))return;t.preventDefault(),this.show(e)}},r.prototype.show=function(t){var e=this;if(this.onAnimation)return!1;var n=Util.Selector.closest(t,"."+this.tabItemSelector);if(!n||n.classList.contains("active"))return!1;var r=t.getAttribute("href")||t.getAttribute("data-target");if(!r)return!1;var i=this.getElement();Array.from(i.querySelectorAll(".tab-item")||[]).forEach(function(t){t.classList.contains("active")&&t.classList.remove("active");var e=t.querySelector(".tab-link");e&&e.setAttribute("aria-selected","false")}),this.onAnimation=!0,n.classList.add("active"),t.setAttribute("aria-selected","true");var o=document.querySelector(r);if(!o)return!1;var s=o.parentNode;if(!s)return!1;var a=Array.from(s.querySelectorAll("."+this.tabContentSelector)||[]),l=i.querySelector(".tab-item.active");a.forEach(function(t){t.classList.contains("active")&&t.classList.remove("active")}),o.classList.add("showing"),this.triggerEvent(Util.Event.SHOW,this.getTabEvent(t)),l&&this.triggerEvent(Util.Event.HIDE,this.getTabEvent(l));var c=function(){o.classList.remove("animate"),o.classList.add("active"),o.classList.remove("showing"),e.triggerEvent(Util.Event.SHOWN,e.getTabEvent(t)),l&&e.triggerEvent(Util.Event.HIDDEN,e.getTabEvent(l)),e.onAnimation=!1,o.removeEventListener(Util.Event.TRANSITION_END,c)};return __awaiter(e,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return[4,Util.sleep(20)];case 1:return t.sent(),o.addEventListener(Util.Event.TRANSITION_END,c),o.classList.add("animate"),[2]}})}),!0},r.prototype.getTabEvent=function(t){return{id:t.getAttribute("href")||t.getAttribute("data-target"),target:t}},r.prototype.destroy=function(){this.registerElement({target:this.getElement(),event:Util.Event.CLICK})},r}(Component);Tab.attachDOM(),module.exports=Tab;
2//# sourceMappingURL=/home/qathom/Workspaces/Web/phonon/dist/js/components/tab.min.js.map
\No newline at end of file