-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 3.83 KB
/
index.js
1
!function(t){var e={};function a(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,a),r.l=!0,r.exports}a.m=t,a.c=e,a.d=function(t,e,n){a.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},a.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},a.t=function(t,e){if(1&e&&(t=a(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(a.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)a.d(n,r,function(e){return t[e]}.bind(null,r));return n},a.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return a.d(e,"a",e),e},a.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},a.p="",a(a.s=2)}([function(t,e){let a=new Map;t.exports=class{static add(t,e){a.set(t,e)}static loop(t){a.forEach(t)}static get(t){return a.get(t)}}},function(t,e){t.exports=class{constructor({name:t,parentEl:e="div",data:a,template:n,actions:r}){this.name=t,this.data=a,this.parentEl=e,this.template=n,this.actions=r}getDOM(){return this.DOM=document.createElement(this.parentEl),this.DOM.innerHTML=this.template(this.data),this.DOM}}},function(t,e,a){const n=a(1),r=a(0);class i{constructor(t,e=document){this.data=t,this.parentDOM=e,this.data.updateState=this.updateState.bind(this),this.actions,this.templates}updateState(t,e){this.data[t]=e,this.updateVars(t),this.updateTemplates(t),this.updateIf(t),this.updateChildren()}updateIf(t){this.parentDOM.querySelectorAll(`*[if=${t}]`).forEach(e=>{1==this.data[t]?e.style.display="":e.style.display="none"})}updateVars(t){this.parentDOM.querySelectorAll(`*[var=${t}]`).forEach(e=>{e.innerHTML=this.data[t]})}updateTemplates(t){this.parentDOM.querySelectorAll(`*[loop=${t}]`).forEach(e=>{let a=e.getAttribute("template"),n=this.data[t].map((t,e)=>this.templates[a](t,e));e.innerHTML=n.join("")})}updateChildren(){let t=this.parentDOM.getAttribute("th-component-name"),e=r.get(t);this.parentDOM.innerHTML=e.template(e.data);do{r.loop(t=>{let{name:e,data:a,actions:n,templates:r}=t;this.parentDOM.querySelectorAll(`template[th-component=${e}]`).forEach(e=>{for(const t in a)if(a.hasOwnProperty(t)){let n=e.getAttribute(t);null!=n&&(a[t]=n)}let l=t.getDOM();e.parentNode.replaceChild(l,e);let o=new i(a,l);o.setActions(n),o.setTemplates(r),o.init()})})}while(this.parentDOM.querySelectorAll("template").length>0)}init(){let t=this.parentDOM.querySelectorAll("*[var]");t.forEach(t=>{let e=t.getAttribute("var");t.innerHTML=this.data[e]}),(t=this.parentDOM.querySelectorAll("*[if]")).forEach(t=>{let e=t.getAttribute("if");1==this.data[e]?t.style.display="":t.style.display="none"})}setActions(t){this.actions=t,this.parentDOM.querySelectorAll("*[on]").forEach(t=>{let e=t.getAttribute("on").split(" "),a=t.getAttribute("action").split(" ");e.forEach((e,n)=>{let r=a[n];t.addEventListener(e,this.actions[r].bind(this))})})}setTemplates(t){if(this.templates=t,void 0!=this.templates){this.parentDOM.querySelectorAll("*[loop]").forEach(t=>{let e=t.getAttribute("loop"),a=t.getAttribute("template"),n=this.data[e].map((t,e)=>this.templates[a](t,e));t.innerHTML=n.join("")})}}}i.createComponent=(({name:t,data:e,template:a,actions:i,parentEl:l})=>{let o=new n({name:t,data:e,template:a,parentEl:l,actions:i});r.add(t,o)}),i.start=(()=>{for(;document.querySelectorAll("template").length>0;)r.loop(t=>{let{name:e,data:a,actions:n,template:r}=t;document.querySelectorAll(`template[th-component=${e}]`).forEach(l=>{for(const t in a)if(a.hasOwnProperty(t)){let e=l.getAttribute(t);null!=e&&(a[t]=e)}let o=t.getDOM();l.parentNode.replaceChild(o,l),o.setAttribute("th-component-name",e);let s=new i(a,o);s.setActions(n),s.setTemplates(r),s.init()})})}),window&&(window.TrollHunter=i),t.exports=i}]);