forked from Matt-Esch/virtual-dom
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpatch.js
executable file
·200 lines (184 loc) · 4.92 KB
/
patch.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
199
200
// TODO in the rough
var OPERATE = require('./lib/operate')
, DEAL = {};
function isText(vd) {
return vd.tag === 'text';
}
function getVDByKey(vds, key, cache) {
cache = cache || {};
if (cache[key]) return cache[key];
for (var i = 0, l = vds.length; i < l; i++) {
if (vds[i].key === key) return (cache[key] = vds[i]);
}
}
function find(vd, vds) {
var i = vds.indexOf(vd)
, res, tmp;
if (!(~i)) {
vds.every(function (item, j) {
if (!item.children || !item.children.length) return true;
res = find(vd, item.children);
if (res) {
tmp = res;
res = [j];
[].push.apply(res, tmp);
return false;
} else {
return true;
}
});
return res;
}
return [i];
}
function getRef(vd, root, a) {
var ref = vd.ref;
if (ref) return ref;
if (!root || !a) throw new Error('Should bind the reference node.');
var indexes = find(vd, [a]);
if (!indexes) throw new Error('Couldn\'t find the reference node.');
ref = root.childNodes[0];
for (var i = 1, l = indexes.length; i < l; i++) {
ref = ref.childNodes[indexes[i]];
}
vd.ref = ref;
return ref;
}
function setProps(to, from, node) {
for (key in from) {
// style
if (key === 'style') {
style = node.style;
value = from[key];
if (!value) {
style.cssText = '';
to.style = {};
} else {
value = from[key];
for (i in value) {
if (value[i] === undefined) {
style[i] = '';
delete to.style[i];
} else {
style[i] = value[i];
to.style[i] = value[i];
}
}
}
// property
} else if (key in node) {
value = from[key];
type = typeof node[key];
if (value === undefined) {
type === 'string' ?
(node[key] = '') :
(node[key] = null);
delete to[key];
} else {
node[key] = to[key] = value;
}
// attribute
} else {
value = from[key];
if (value === undefined) {
node.removeAttribute(key);
delete to[key];
} else {
node.setAttribute(key, value);
to[key] = value;
}
}
}
}
function create(vds, parent) {
!Array.isArray(vds) && (vds = [vds]);
parent = parent || document.createDocumentFragment();
var node;
vds.forEach(function (vd) {
if (isText(vd)) {
node = document.createTextNode(vd.text);
} else {
node = document.createElement(vd.tag);
}
parent.appendChild(node);
vd.children && vd.children.length &&
create(vd.children, node);
vd.props &&
setProps({ style: {} }, vd.props, node);
});
return parent;
}
function splice(vd, a, replacement) {
var indexes, i, l;
indexes = find(vd, [a]);
for (i = 1, l = indexes.length - 1; i < l; i++) {
a = a.children[indexes[i]];
}
replacement ?
a.children.splice(indexes[l], 1, replacement) :
a.children.splice(indexes[l], 1);
}
DEAL[OPERATE.REMOVE] = function (item, root, a) {
var node = getRef(item.from, root, a);
node.parentNode.removeChild(node);
item.from.ref = null;
splice(item.from, a);
};
DEAL[OPERATE.INSERT] = function (item, root, a) {
var node = create(item.from)
, parent = getRef(item.to, root, a);
parent.appendChild(node);
item.to.children.push(item.from);
};
DEAL[OPERATE.REPLACE] = function (item, root, a) {
var node = create(item.from)
, target = getRef(item.to, root, a)
, indexes
, i
, l;
target.parentNode.replaceChild(node, target);
item.to.ref = null;
splice(item.to, a, item.from);
};
DEAL[OPERATE.ORDER] = function (item, root, a) {
var to = item.to
, from = item.from
, children = to.children
, parent = getRef(to, root, a)
, grandpa = parent.parentNode
, removes = from.removes
, inserts = from.inserts
, cache = {};
removes.forEach(function (order) {
var vd = getVDByKey(children, order.key, cache)
, node = getRef(vd, grandpa, to);
parent.removeChild(node);
splice(vd, to);
});
inserts.forEach(function (order) {
var vd = getVDByKey(children, order.key, cache)
, node = getRef(vd, grandpa, to)
, before = getRef(children[order.to], grandpa, to);
parent.insertBefore(node, before);
children.splice(order.to, 0, vd);
});
cache = null;
};
DEAL[OPERATE.PROPS] = function (item, root, a) {
var props = item.from.props
, node = getRef(item.from, root, a)
, diff = item.diff
, key, value, i, l, style, type;
setProps(props, diff, node);
};
function patch(patches, root) {
var i = 0, l = patches.length
, a = patches.a;
root = root || a.hook;
for (; i < l; i++) {
patches[i].forEach(function (item) {
DEAL[item.operate](item, root, a);
});
}
}
module.exports = patch;