-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.js
More file actions
104 lines (77 loc) · 3.16 KB
/
section.js
File metadata and controls
104 lines (77 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Section {
constructor(element) {
this.constructor.elements[this.constructor.elements.length] = this;
this.element = element || {};
this.element.controller = this;
}
static defer(delegate) {
if(!Section.deferred) Section.deferred = [];
Section.deferred.push(delegate);
}
static create() {
if (this.tag) {
let el = new this.tag();
return this.creator.create(el);
}
throw new Error("Create called before registration");
}
static register(tag, template) {
const Type = this;
Type.tagName = tag;
Type.importDocument = document.currentScript.ownerDocument;
Type.elements = [];
Type.creator = {};
Type.base = Object.create(HTMLElement.prototype);
Type.base.createdCallback = () => {
Type.template = null;
if (!template) {
let templateElements = Type.importDocument.getElementsByTagName("template");
if (!templateElements || templateElements.length !== 1) throw new Error("requires exactly one template");
Type.template = templateElements[0];
} else if (typeof template === "string") {
Type.template = Type.importDocument.getElementById(template);
if (!Type.template) throw new Error(`template by id ${template} undefined`);
} else {
Type.template = template;
}
Type.creator.createWithElement = (element) => {
let clone = document.importNode(Type.template.content, true);
let children = [];
let hasContentElement = !!(clone.querySelector("content"));
if (hasContentElement) {
while (element.firstChild) {
children.push(element.removeChild(element.firstChild));
}
}
element.appendChild(clone);
if (hasContentElement) {
let contentElement = element.getElementsByTagName("content");
if (contentElement && contentElement.length > 0) {
children.forEach((child) => contentElement[0].appendChild(child));
} else {
throw new Error("somehow we lost the content element");
}
}
return new Type(element);
};
Type.creator.create = (el) => {
return Type.creator.createWithElement(el || document.createElement(Type.tagName));
};
if (!Section.loaded) {
Section.defer(() => {
Type.creator.createWithElement(document.getElementsByTagName(Type.tagName)[Type.elements.length]);
}, false);
}
};
Type.tag = document.registerElement(Type.tagName, {prototype: Type.base });
return Type.tag;
}
}
window.addEventListener("load", () => {
let delegate;
if (!Section.deferred) Section.deferred = [];
while(delegate = Section.deferred.pop()) {
delegate();
}
Section.loaded = true;
});