-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseComponent.js
198 lines (166 loc) · 4.71 KB
/
baseComponent.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// TODO: better errors to show if a signal was used in the wrong way
function isCoyComponent(component) {
if (component instanceof BaseComponent) {
return true;
}
return false;
}
class BaseComponent {
element;
props = {};
children = [];
tag;
parent;
nodeText = "";
constructor(tag, args = []) {
let refFn = null;
const { children, propsObjs } = args.reduce(
(acc, arg) => {
if (arg instanceof Props) {
const { ref, ...rest } = arg.props;
refFn = ref;
acc.propsObjs.push(rest);
return acc;
}
if (Array.isArray(arg)) {
acc.children.push(...arg);
return acc;
}
acc.children.push(arg);
return acc;
},
{ children: [], propsObjs: [] }
);
Object.assign(this.props, ...propsObjs);
this.children = children;
this.tag = tag;
this.create(refFn);
}
destroy() {
if (this.tag !== "void") {
this.element.remove();
}
}
create(refFn) {
const hasProps = Object.keys(this.props).length > 0;
switch (this.tag) {
case "void":
if (hasProps) console.warn("Props are ignored on void components");
break;
case "text":
if (hasProps) console.warn("Props are ignored on text components");
this.element = document.createTextNode("");
break;
case "fragment":
this.element = document.createComment("Coy.Fragment");
break;
case "reference":
const { DOMNode, ...rest } = this.props;
if (DOMNode) {
if (
!(DOMNode instanceof Element) &&
!(DOMNode instanceof BaseComponent)
) {
throw new Error(
"DOMNode node must be either a Dom Element or a BaseComponent"
);
}
this.element = DOMNode;
} else {
throw new Error(
"In reference component you need to pass the DOMNode prop which"
);
}
Object.entries(rest).forEach(([key, value]) => {
setPropertiesAndListenToSignals(this.element, key, value);
});
break;
default:
this.element = document.createElement(this.tag);
Object.entries(this.props).forEach(([key, value]) => {
setPropertiesAndListenToSignals(this.element, key, value);
});
break;
}
if (refFn && typeof refFn === "function") {
if (this.element.tag !== "void" && this.element.tag !== "fragment") {
refFn(this.element);
}
}
this.#createChildren();
}
appendChild(element) {
if (!isCoyComponent(element)) {
throw new Error("Element must be a BaseComponent instance");
}
if (element.tag === "void") {
return;
}
if (this.tag === "fragment") {
if (this.parent && this.parent.element.contains(this.element)) {
this.parent.element.insertBefore(element.element, this.element);
}
} else {
this.element.appendChild(element.element);
}
}
swapChild(from, to) {
if (from === to) return;
if (typeof from !== "number" || from < 0 || from >= this.children.length) {
throw new Error(
`Invalid from position ${from} at BaseComponent.swapChild`
);
}
if (typeof to !== "number" || to < 0 || to >= this.children.length) {
throw new Error(`Invalid to position ${to} at BaseComponent.swapChild`);
}
const fromNode = this.children[from];
const toNode = this.children[to];
swipeItemsOnArray(this.children, from, to);
if (!fromNode || !toNode) {
throw new Error("Nodes at specified positions do not exist.");
}
const parentNode = fromNode.parent.element;
if (parentNode) {
const temp = document.createComment("");
toNode.element.replaceWith(temp);
fromNode.element.replaceWith(toNode.element);
temp.replaceWith(fromNode.element);
}
}
removeChildAt(position) {
if (
typeof position !== "number" ||
position < 0 ||
position > this.children.length
) {
throw new Error(
`Invalid position ${position} at BaseComponent.removeChildAt`
);
}
this.children[position].destroy();
this.children.splice(position, 1);
}
removeAllChildren() {
if (this.tag === "fragment") {
this.children.forEach((c) => c.destroy());
} else {
this.element.textContent = "";
}
this.children = [];
}
#createChildren() {
if (this.tag === "text") {
this.nodeText = this.children.join("");
this.element.textContent = this.nodeText;
this.children = [];
} else {
this.children = this.children.map((c) => createElement(c));
}
}
}
class Props {
constructor(props) {
this.props = props;
}
}