-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualDOM.js
More file actions
163 lines (136 loc) · 4.67 KB
/
VirtualDOM.js
File metadata and controls
163 lines (136 loc) · 4.67 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
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
export const vDom = {
Create: (
Layout
) => {
const { tag, props = {}, lang = "", children = [] } = Layout;
let LanguageObj = window.globalValues.translateData;
return vDom.CreateElement(
tag,
props,
lang,
children.length === 1 && typeof children[0] === "string"
? [LanguageObj[lang] ? LanguageObj[lang][window.globalValues.language][children[0]] : children[0]]
: children.map(child => vDom.Create(child))
);
},
CreateElement: (
tag,
props,
lang,
children
) => {
return {
tag,
props: props || {},
lang: lang || "",
children: children || []
}
},
Render: (
vNode
) => {
if (typeof vNode === "string") {
return document.createTextNode(vNode);
}
const el = document.createElement(vNode.tag);
for (const [ key, value ] of Object.entries(vNode.props)) {
el.setAttribute(key, value);
}
vNode.children.forEach(child => {
if (typeof child === "string") {
let LanguageObj = window.globalValues.translateData;
el.innerHTML += LanguageObj[vNode.lang] ? LanguageObj[vNode.lang][window.globalValues.language][child] || child : child;
} else {
el.appendChild(vDom.Render(child));
}
});
return el;
},
Diff: (
oldNode,
newNode,
lang
) => {
const patches = [];
if (oldNode === undefined || newNode === undefined) {
if (newNode !== undefined) {
patches.push({ type: "ADD", newNode });
} else if (oldNode !== undefined) {
patches.push({ type: "REMOVE" });
}
}
else if (typeof oldNode === "string" && typeof newNode === "string") {
let LanguageObj = window.globalValues.translateData;
let newText = LanguageObj[lang] ? LanguageObj[lang][window.globalValues.language][newNode] || newNode : newNode;
if (oldNode !== newNode) {
patches.push({ type: "TEXT", text: newText });
}
}
else if (oldNode.tag !== newNode.tag) {
patches.push({ type: "REPLACE", newNode });
}
else {
const propPatches = [];
for (const [ key, value ] of Object.entries(newNode.props)) {
if (oldNode.props[key] !== value) {
propPatches.push({ key, value });
}
}
for (const key in oldNode.props) {
if (!(key in newNode.props)) {
propPatches.push({ key });
}
}
if (propPatches.length > 0) {
patches.push({ type: "PROPS", props: propPatches })
}
const childPatch = []
const maxChildrenLength = Math.max(oldNode.children.length, newNode.children.length);
for (let i = 0; i < maxChildrenLength; i++) {
childPatch.push(vDom.Diff(oldNode.children[i], newNode.children[i], newNode.lang));
}
patches.push({ type: "CHILDREN", children: childPatch });
}
return patches;
},
Patch: (
parent,
patches,
index = 0
) => {
const el = parent.children[index];
patches.forEach(patch => {
switch(patch.type) {
case "ADD":
parent.appendChild(vDom.Render(patch.newNode));
break;
case "REMOVE":
window.globalValues.nodeToRemove.push({
parent: parent.id,
el
});
break;
case "TEXT":
parent.innerHTML = patch.text;
break;
case "REPLACE":
parent.replaceChild(vDom.Render(patch.newNode), el);
break;
case "PROPS":
patch.props.forEach(({ key, value }) => {
if (value === undefined){
el.removeAttribute(key);
} else {
el.setAttribute(key, value);
}
});
break;
case "CHILDREN":
patch.children.forEach((childPatch, i) => {
vDom.Patch(el, childPatch, i);
});
break;
}
});
}
}