-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.min.js
1 lines (1 loc) · 995 Bytes
/
Vector.min.js
1
export class Component{constructor(e){this.$parent=this.parseParent(e)[0]?this.parseParent(e)[0]:this.parseParent(e)}render(){this.destroy(),this.parseTemplate(),this.addEventListeners(),Array.from(this.parsedTemplate.children[0].children).forEach(e=>{this.$parent.appendChild(e)})}updateComponent(e){Object.keys(e).forEach(t=>{this.state[t]=e[t]}),this.render()}destroy(){this.$parent.innerHTML=""}addEventListeners(){if(!this.eventListeners)return this.parsedTemplate;this.eventListeners.forEach(e=>{Array.isArray(e)&&(e={selector:e[0],type:e[1],callback:e[2]});let t=this.parsedTemplate.querySelector(e.selector);t&&t.addEventListener(e.type,e.callback)})}parseParent(e){if("string"==typeof e)return document.querySelector(e);if("object"==typeof e){if(e.name)return document.getElementsByName(e.name)}else if(e instanceof NodeList)return e[0];return e}parseTemplate(){let e=`<div name="_Temp">${this.template()}</div>`;this.parsedTemplate=(new DOMParser).parseFromString(e,"text/html").body}}