1 |
|
2 | import { DOMObserver } from "./DOMObserver";
|
3 |
|
4 | export const Component = {
|
5 |
|
6 | tagName: 'component',
|
7 |
|
8 | attributes: {},
|
9 |
|
10 | |
11 |
|
12 |
|
13 | init(component) {
|
14 | const prop = '__component_' + this.tagName;
|
15 | if (component[prop] !== undefined) {
|
16 | return false;
|
17 | }
|
18 | component[prop] = true;
|
19 |
|
20 | if (this.role !== undefined) {
|
21 | component.setAttribute('role', this.role);
|
22 | }
|
23 |
|
24 | if (this.tabIndex !== undefined) {
|
25 | component.setAttribute('tabIndex', this.tabIndex);
|
26 | }
|
27 |
|
28 | for (let attrName in this.attributes) {
|
29 | const attrDefVal = this.attributes[attrName];
|
30 | DOMObserver.registerAttribute(component, attrName, attrDefVal, newVal => {
|
31 | this['__' + attrName](component, newVal);
|
32 | });
|
33 | }
|
34 |
|
35 | if (this.addElements !== undefined) {
|
36 | this.addElements(component);
|
37 | }
|
38 |
|
39 | this.addEvents(component);
|
40 | return true;
|
41 | },
|
42 |
|
43 | addEvents(component) {
|
44 |
|
45 | },
|
46 |
|
47 | initAll(container = document.body) {
|
48 | [].forEach.call(this.getAll(container), btn => {
|
49 | this.init(btn);
|
50 | });
|
51 | },
|
52 |
|
53 | getAll(container = document.body) {
|
54 | return container.getElementsByTagName(this.tagName);
|
55 | },
|
56 |
|
57 | register() {
|
58 | document.addEventListener('DOMContentLoaded', () => {
|
59 | DOMObserver.init();
|
60 | this.initAll();
|
61 | DOMObserver.onInsert(this.tagName, component => {
|
62 | this.init(component);
|
63 | });
|
64 | if (this.uninit !== undefined) {
|
65 | DOMObserver.onRemove(this.tagName, component => {
|
66 | this.uninit(component);
|
67 | });
|
68 | }
|
69 | });
|
70 | }
|
71 |
|
72 | };
|