UNPKG

sevejs

Version:

🌳 Simple and light JS components manager

1 lines • 7.56 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Sevejs=e():t.Sevejs=e()}(self,(function(){return(()=>{"use strict";var t={d:(e,n)=>{for(var o in n)t.o(n,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:n[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function n(t){return"string"!=typeof t?"":t.charAt(0).toUpperCase()+t.slice(1)}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function r(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}t.r(e),t.d(e,{App:()=>f,Component:()=>a,Module:()=>c});var i=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.DOM={el:e},this._compEvents=[],this.componentsId=0,this.components,this.app,this.binded={}}var e,i;return e=t,(i=[{key:"_compUpdate",value:function(t){this.components=t,this.app=this.components.get("App").get("global")}},{key:"_compResize",value:function(t){this.resize(t)}},{key:"_compDestroy",value:function(){for(var t=this._compEvents.length;t>0;)t-=1,this.off(t,this._compEvents[t],!0);this.binded={},this.destroy()}},{key:"call",value:function(t,e,r,i){if(e&&!r&&(r=e,e=!1),r=n(r),this.components.has(r)){i=i||this.components.get(r).entries().next().value[0];var a=this.components.get(r).get(i);if(null!=a&&a[t])return e=Array.isArray(e)?e:[e],a[t].apply(a,function(t){if(Array.isArray(t))return o(t)}(s=e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(s)||function(t,e){if(t){if("string"==typeof t)return o(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(t,e):void 0}}(s)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}())}var s}},{key:"on",value:function(t){var e=t.e,n=t.target,o=t.cb,r=t.opts,i=void 0!==r&&r;return n.addEventListener(e,o,i),this._compEvents.push(Object.assign(t,{id:this.componentsId})),this.componentsId++}},{key:"off",value:function(t,e){var n,o=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!e)for(var r=0;r<this._compEvents.length;r++)if(this._compEvents[r].id===t){e=this._compEvents[r],n=r;break}n=o?t:n;var i=e,a=i.e,s=i.target,c=i.cb,p=i.opts,u=void 0!==p&&p;s.removeEventListener(a,c,u),this._compEvents.splice(n,1)}},{key:"init",value:function(){}},{key:"resize",value:function(){}},{key:"destroy",value:function(){}}])&&r(e.prototype,i),t}();i.prototype.emitter={},i.prototype.trigger=function(t){for(var e=arguments.length,n=new Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];0!=!!i.prototype.emitter[t]&&i.prototype.emitter[t].slice().forEach((function(t){t.apply(void 0,n)}))},i.prototype.subscribe=function(t,e){0==!!i.prototype.emitter[t]&&(i.prototype.emitter[t]=[]),i.prototype.emitter[t].push(e)},i.prototype.unsubscribe=function(t,e){var n=i.prototype.emitter[t];if(0!=!!n){var o=n.indexOf(e);n.splice(o)}};const a=i;function s(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}const c=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.components=e}var e,n;return e=t,(n=[{key:"getComponent",value:function(t,e){var n=this;try{var o=function(t){var e=n.components.get(t);if(e)return e;throw'Component "'.concat(t,"\" doesn't exist.")}(t);return e?null==o?void 0:o.get(e):null==o?void 0:o.values().next().value}catch(t){console.error(t)}}}])&&s(e.prototype,n),t}();function p(t){return function(t){if(Array.isArray(t))return u(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return u(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function l(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}var f=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.app,this.components=e.components||[],this.activeComponents=new Map,this.newComponents=new Map,this.componentsId=0,this._globalEventsFuncs={},this._resize()}var e,o;return e=t,(o=[{key:"init",value:function(t,e){var o=this;t&&!this.app&&(this.app=t),this.activeComponents.set("App",new Map([["global",this.app]])),p((e||document).querySelectorAll("[data-component]")).forEach((function(t){var e=t.dataset.component;if(""!==(e=o.components[n(e)]||o.components[e]?n(e):"")){var r=new o.components[e](t),i=t.dataset.componentId;i||(o.componentsId++,i=t.dataset.componentId="comp-".concat(o.componentsId)),o.newComponents.set("".concat(e,"-").concat(i),r),o.activeComponents.has(e)?o.activeComponents.get(e).set(i,r):o.activeComponents.set(e,new Map([[i,r]]))}})),this._listAllComponents((function(t,e,r){t._compUpdate(o.activeComponents),o.newComponents.has("".concat(n(e),"-").concat(r))&&t.init({})})),this.newComponents.clear()}},{key:"_listAllComponents",value:function(t){this.activeComponents.forEach((function(e,o){return e.forEach((function(e,r){"App"!==n(o)&&t(e,o,r)}))}))}},{key:"update",value:function(t){var e=this;this.init(this.app,t),this._listAllComponents((function(t){return t._compUpdate(e.activeComponents)}))}},{key:"windowSizeInfos",get:function(){return{windowSizes:{width:window.innerWidth,height:window.innerHeight}}}},{key:"_resize",value:function(){var t,e,n,o,r,i=this;null!==(e=(t=this._globalEventsFuncs).resize)&&void 0!==e||(t.resize=(n=function(){i._listAllComponents((function(t){var e=i.windowSizeInfos.windowSizes;t._compResize(e)}))},350,function(){var t=this,e=arguments,i=function(){r=null,n.apply(t,e)},a=o;clearTimeout(r),r=setTimeout(i,350),a&&n.apply(t,e)})),window.addEventListener("resize",this._globalEventsFuncs.resize)}},{key:"destroy",value:function(t){var e=this;t?p(t.querySelectorAll("[data-component]")).forEach((function(t){var o=n(t.dataset.component),r=t.dataset.componentId;if(e.activeComponents.has(o)){var i=e.activeComponents.get(o);i.get(r)._compDestroy(),i.delete(r),i.size<=0&&e.activeComponents.delete(o)}})):(this.activeComponents.forEach((function(t,e){"App"!==n(e)&&t.forEach((function(t){return t._compDestroy()}))})),this.activeComponents.clear(),window.removeEventListener("resize",this._globalEventsFuncs.resize))}}])&&l(e.prototype,o),t}();return e})()}));