-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.js
More file actions
321 lines (242 loc) · 8.28 KB
/
Component.js
File metadata and controls
321 lines (242 loc) · 8.28 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
var error = require('./error.js');
var parse = require('./parse.js');
var h = require('./helpers.js');
var extend = require('./extend.js');
var defaults = require('./defaults.js');
var Base = require('./Base.js');
var compile = require('./compile.js');
var rafQueue = require('./rafQueue.js');
var Component = Base.extend({
name: '',
assignEvents: h.noop,
unassignEvents: h.noop,
componentWillMount: h.noop,
componentDidMount: h.noop,
componentWillUpdate: h.noop,
componentDidUpdate: h.noop,
componentWillUnmount: h.noop,
componentDidUnmount: h.noop,
componentWillReceiveProps: h.noop,
getChildContext: h.noop,
propTypes: {},
dataAttrs: {},
childrens: {},
__phase: 'MOUNTING',
__isInitedRafForceUpdate: false,
__isMounted: false,
constructor: function (vWidget) {
this.__uid = h.getUniqueId();
this.__events = {};
this.__vWidget = vWidget || {};
this.__initContext();
this.constructor.defaultProps = this.constructor.defaultProps || this.getDefaultProps();
this.constructor.childrens = this.constructor.childrens || this.__defineChildrens();
this.state = this.getInitialState();
this.props = this.__createProps();
this.selectors = this.__getInintialSelectors();
this.__onMount = this.__onMount.bind(this);
this.__onUnmount = this.__onUnmount.bind(this);
},
__initContext: function () {
var ownerVWidget = this.__vWidget.ownerVWidget;
if (!ownerVWidget) {
return this;
}
var com = ownerVWidget.com;
this.context = com.getChildContext() || com.context;
return this;
},
__defineChildrens: function () {
var childrens = {};
for (var name in this.childrens) {
childrens[name.toUpperCase()] = Component.extend(this.childrens[name]);
}
return childrens;
},
__createProps: function (props) {
props = props || this.__createPropsFromVNode();
defaults(props, this.constructor.defaultProps);
for (var prop in this.propTypes) {
props[prop] = this.__valueToType(props[prop], this.propTypes[prop]);
}
props.children = props.children || [];
return props;
},
__createPropsFromVNode: function () {
var originalVNode = this.__vWidget.originalVNode;
var ownerVWidget = this.__vWidget.ownerVWidget;
var props = {}, snakeProp;
if (!ownerVWidget || !originalVNode || !originalVNode.properties) {
return props;
}
var potentialProps = originalVNode.properties.attributes;
var additionalProps = originalVNode.properties;
for (var prop in this.propTypes) {
snakeProp = h.camelToSnake(prop);
props[prop] = this.__parseValueByType(
potentialProps.hasOwnProperty(snakeProp) ? potentialProps[snakeProp] : additionalProps[prop],
ownerVWidget.com,
this.propTypes[prop]
);
}
props.children = originalVNode.children;
return props;
},
__parseValueByType: function (value, context, type) {
switch (type) {
case Function: return parse(value)(context);
case Object: return parse(value)(context);
case Array: return parse(value)(context);
}
return value;
},
__valueToType: function (value, type) {
return this.__toTypes[type] ? this.__toTypes[type](value) : value;
},
__getInintialSelectors: function () {
var selectors = {};
for (var key in this.dataAttrs) {
selectors[key] = '[' + this.dataAttrs[key] + ']';
}
return selectors;
},
__compile: function () {
return compile(this.__vWidget, this.render(), this.constructor.childrens);
},
__mount: function () {
this.componentWillMount();
this.state = this.__nextState || this.state;
this.props = this.__nextProps || this.props;
delete this.__nextProps;
delete this.__nextState;
this.vNode = this.__compile();
this.node = h.toDOM(this.vNode);
this.node.com = this;
this.assignEvents();
rafQueue(this.__onMount);
return this;
},
__onMount: function () {
this.componentDidMount();
this.__phase = 'READY';
this.__isMounted = true;
},
__onUnmount: function () {
this.componentDidUnmount();
},
__update: function (isInitiator, state, props) {
if (!this.isMounted()) {
return this;
}
this.__phase = 'UPDATING';
var nextProps = this.__createProps(isInitiator ? (props || this.props) : null);
var nextState = state || this.state;
if (nextProps !== this.props) {
this.componentWillReceiveProps(nextProps, nextState);
}
if (!this.shouldComponentUpdate(nextProps, nextState)) {
this.state = nextState;
this.props = nextProps;
return this;
}
this.componentWillUpdate(nextProps, nextState);
prevProps = this.props;
prevState = this.state;
this.state = nextState;
this.props = nextProps;
var newVNode = this.__compile();
var patch = h.getDiff(this.vNode, newVNode);
this.node = h.applyPatch(this.node, patch);
this.vNode = newVNode;
this.__unmountList(h.findLostComs(patch, 7));
this.componentDidUpdate(prevProps, prevState);
this.__phase = 'READY';
return this;
},
__unmountList: function (coms) {
for (var i = 0, length = coms.length; i < length; i++) {
coms[i].unmount();
};
return this;
},
__toTypes: (function () {
var map = {};
map[Function] = function (value) { return typeof value === 'function' ? value : h.noop; };
map[Object] = function (value) { return typeof value === 'object' ? value : {}; };
map[Array] = function (value) { return value instanceof Array ? value : []; };
map[Boolean] = function (value) { return value !== 'false' && !!value; };
map[Number] = Number;
map[String] = String;
return map;
})(),
getInitialState: function () {
return {};
},
getDefaultProps: function () {
return {};
},
shouldComponentUpdate: function () {
return true;
},
render: function () {
throw error('No render function', this.name + '\'s render function is not defined!');
},
getDOMNode: function () {
return this.node;
},
isReady: function () {
return this.__phase === 'READY';
},
setState: function (state) {
if (typeof state === 'function') {
state = state(this.state, this.props);
}
return this.replaceState(extend({}, this.state, this.__nextState, state));
},
setProps: function (props) {
if (typeof props === 'function') {
props = props(this.state, this.props);
}
return this.replaceProps(extend({}, this.props, this.__nextProps, props));
},
replaceProps: function (props) {
this.__nextProps = props;
return this.forceUpdate();
},
replaceState: function (state) {
this.__nextState = state;
return this.forceUpdate();
},
forceUpdate: function () {
if (this.__isInitedRafForceUpdate) {
return this;
}
this.__isInitedRafForceUpdate = true;
rafQueue(function () {
this.__isInitedRafForceUpdate = false;
this.__update(true, this.__nextState, this.__nextProps);
}.bind(this));
return this;
},
unmount: function () {
if (!this.isMounted()) {
return;
}
this.__unmountList(h.findComs(this.vNode));
this.componentWillUnmount();
this.unassignEvents();
this.__isMounted = false;
if (this.node.remove) {
this.node.remove();
} else if (this.node.removeNode) {
this.node.removeNode(true);
}
delete this.node;
rafQueue(this.__onUnmount);
return this;
},
isMounted: function () {
return this.__isMounted;
}
});
module.exports = Component;