-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
82 lines (67 loc) · 2.21 KB
/
node.js
File metadata and controls
82 lines (67 loc) · 2.21 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
export default class VirtualNode {
constructor(props) {
this.tag = props.tag || 'div';
this.attrs = props.attrs || {};
this.listeners = props.listeners || {};
this.children = props.children || [];
}
// Start by creating the element.
// Then sets the attributes and the event Listeners.
// And finally appends children whether it is a virtual node,
// a properties object or just a text as content.
render() {
this.elem = document.createElement(this.tag);
Object.entries(this.attrs).forEach(([name, value]) => {
this.elem.setAttribute(name, value);
});
Object.entries(this.listeners).forEach(([event, callback]) => {
this.elem[event] = callback;
});
this.children.forEach(child => {
this.add(child);
})
return this.elem;
}
// Get an 'non-string' element from the children array
// using the index. The method then virtualises it if needed.
// It finally returns the element as a virtual node.
// Useful for internal manipulation.
select(index) {
let child = this.children[index]
if (!child) {
return
}
if (!(child instanceof VirtualNode) && typeof child !== 'string') {
child = new VirtualNode(child)
}
return child
}
// If the component has already been rendered,
// it appends the child to the existing element.
// Otherwise, it will add it to the children array
// before it get rendered.
add(child) {
if (!this.elem) {
this.children.push(child);
return
}
if (typeof child === 'string') {
this.elem.textContent += child;
return
}
if (!(child instanceof VirtualNode)) {
child = new VirtualNode(child)
}
this.elem.appendChild(child.elem || child.render())
}
// Empty the element first,
// then add the new child.
replace(...children) {
if (this.elem) {
this.elem.innerHTML = '';
children.forEach(child => this.add(child));
return
}
this.children = [...children];
}
}