2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 19:39:07 +00:00
books/client/components/baseComponent.js
Faris Ansari 9878055ddf More functionality to Tree Component
- Make a basecomponent to encapsulate bootstrapping of WC
- Remove old tree.js
- Refactor Tree Components into JS and HTML
2018-04-12 02:04:45 +05:30

44 lines
961 B
JavaScript

let templates = {};
class BaseComponent extends HTMLElement {
constructor(name) {
super();
this._name = name;
this._shadowRoot = this.attachShadow({ mode: 'open' });
if (!templates[name]) {
makeTemplate(name, this.templateHTML);
}
let template = getTemplate(name);
this._shadowRoot.appendChild(template);
}
triggerEvent(eventName, detail = {}) {
const event = new CustomEvent(eventName, {
bubbles: true,
composed: true,
detail
});
this.dispatchEvent(event);
}
}
function makeTemplate(name, html) {
if (!templates[name]) {
let template = document.createElement('template');
template.id = name;
template.innerHTML = html;
templates[name] = template;
}
}
function getTemplate(name) {
return templates[name].content.cloneNode(true);
}
module.exports = BaseComponent;