-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
360 lines (323 loc) · 9.85 KB
/
renderer.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import query from "dom-query";
import Tree from "dom-layer/src/tree/tree";
import Tag from 'dom-layer/src/node/tag';
import Text from 'dom-layer/src/node/text';
import Components from "./components";
import _ from "./util";
import _if from "./modifiers/if";
import _each from "./modifiers/each";
class Renderer {
/**
* Renderer constructor.
*/
constructor(options) {
options = options || {};
this._modifiers = {};
this._ordered = [];
this._tree = options.tree || new Tree();
this._components = options.components || new Components();
this.register('if', _if);
this.register('each', _each);
}
/**
* Gets/sets the tree instance.
*
* @return object The tree instance.
*/
tree(tree) {
if (tree) {
this._tree = tree;
}
return this._tree;
}
/**
* Gets/sets the components instance.
*
* @return Object The components instance.
*/
components(components) {
if (components) {
this._components = components;
}
return this._components;
}
/**
* Registers a modifier.
*
* @param String name The modifier name to register.
* @param Function className The class to use for this modifier name.
*/
register(name, className) {
this._ordered.push(name);
this._modifiers[name] = className;
}
/**
* Unregisters a modifier.
*
* @param String name The modifier name to unregister.
*/
unregister(name) {
var index = this._ordered.indexOf(name);
if (index > -1) {
this._ordered.splice(index, 1);
this._ordered = this._ordered.map(function(v) {
return this._ordered[v];
}.bind(this));
}
delete this._modifiers[name];
}
/**
* Lists registered modifiers.
*
* @return Array The array of registered widget names.
*/
registered() {
return this._ordered;
}
/**
* Checks if a modifier exists.
*
* @param String name The modifier name to check.
* @return Boolean Returns `true` is the modifier exists `false` otherwise.
*/
exists(name) {
return !!this._modifiers[name];
}
/**
* Gets the priority of a modifier.
*
* @param String name The modifier name to unregister.
* @return Integer Returns the modifier priority.
*/
priority(name) {
return this._ordered.indexOf(name);
}
/**
* Builds a virtual dom tree using the virtual dom DSL.
*
* @param Object vdef The virtual node definition. Possible values are:
* -`'config'` _Object_ : An object that describe the virtual node
* (e.g. `{ tagName: "span", props: { className: "bar" } }`).
* -`'children'` _Function_ : A function which returns an array of children
* -`'<data>'` _mixed_ : Some custom data to attach to the virtual node.
* @param Function children A function which return an array of any combination of virtual nodes.
* @return Array A flattened array of virtual nodes.
*/
h(tagName, config, children, scope, component) {
var modifiers = [];
if (!tagName) {
return _.isFunction(config) ? new Text(config(children)) : new Text(config);
}
config.data = config.data || {};
config.data.scope = scope;
config.data.component = component;
_.each(config.attrs, function(value, name, attrs) {
if (name === "component") {
tagName = value(scope);
delete attrs[name];
} else if (this.exists(name)) {
modifiers.push({ name: name, value: value, p: this.priority(name) });
delete attrs[name];
}
}.bind(this));
modifiers = modifiers.sort(function(a, b){ return a.p - b.p; });
var nodes = this._transform(modifiers, tagName, config, children);
return _.isArray(nodes) ? nodes : [nodes];
}
/**
* Transforms an hyperscript node into a nested array of virtual dom definition using
* the virtual dom DSL (i.e. using `Tag`)
*
* @param Object vdef See the `h()` function for details.
* @return Array A flattened array of virtual nodes.
*/
_transform(modifiers, tagName, config, children) {
if (modifiers.length) {
var statement = modifiers.shift();
var modifier = this._modifiers[statement.name];
var params = modifier(statement.value, config);
return params.map(function(param) {
return this._transform(modifiers, tagName, param, children);
}.bind(this));
}
var scope = config.data.scope;
var component = config.data.component;
if (tagName === 'content') {
var content = component.content();
var parent = component.parent();
var contentScope;
if (parent) {
contentScope = _.extend({}, scope);
contentScope.__proto__ = component.parent().get();
} else {
contentScope = scope;
}
return content ? content(contentScope, component) : [];
}
_.each(config.attrs, function(value, name, attrs) {
if (_.isFunction(value)) {
attrs[name] = value(scope);
}
});
_.each(config.data, function(value, name, data) {
if (_.isFunction(value)) {
data[name] = value(scope);
}
});
if (this._components.exists(tagName)) {
return this._createComponent(tagName, config, children);
}
return new Tag(tagName, config, children ? children(scope, component) : []);
}
/**
* Returns a the virtual tag node.
*
* @param Object config See the `h()` function for details.
* @return Object A virtual tag node.
*/
_createComponent(tagName, config, children) {
config.hooks = {
created: function(node, element) {
var isolated = node.attrs && node.attrs.isolated;
if (!isolated || isolated.trim() !== "false") {
if (node.data.scope.__proto__ === Object.prototype) {
node.data.scope = {};
} else {
node.data.scope.__proto__ = Object.prototype;
}
}
var component = this.mount(element, node.tagName, node.data, node.data.scope, node.data.component, children);
}.bind(this),
remove: function(node, element) {
this.tree().unmount(element.domLayerTreeId);
}.bind(this)
};
return new Tag(tagName, config);
}
/**
* Shallow flattens a one level nested array of virtual nodes into a flat array.
*
* @param Array input The nested array of virtual nodes.
* @return Array A flattened array of virtual nodes.
*/
flatten(input) {
var output = [], idx = 0;
for (var i = 0, length = input.length; i < length; i++) {
var value = input[i];
if (_.isArray(value)) {
var j = 0, len = value.length;
output.length += len;
while (j < len) {
output[idx++] = value[j++];
}
} else {
output[idx++] = value;
}
}
return output;
}
/**
* Replaces undefined value by an empty string.
* If the passed value is an object, it'll casted into a string based
* on its keys where all keys with a `false` values will be removed.
*
* Example:
* { a: true, b: false, c: "hello" } => "a c"
*
*
* @param mixed value The value to beautify.
* @return mixed The beautified value.
*/
beautify(value) {
if (value === undefined) {
return '';
}
if (!_.isPlainObject(value)) {
return value;
}
var values = [];
_.each(value, function(v, k) {
if (v) {
values.push(k);
}
});
return values.join(" ");
}
/**
* Casts a JS value.
* It casts a value according a casting function.
*
* @param mixed value The value to cast.
* @param Function cast A casting function.
* @return mixed The casted value.
*/
cast(value, cast) {
return cast ? cast(value) : value;
}
/**
* Mounts a DOM node from a Tag name & emits `'unmount'`/`'umounted'` events.
*
* @param mixed selector The DOM container node or a string selector.
* @param String tagName The tag name to instanciate.
* @param Object data The data to embbed.
* @param Object scope The scope to use for initializing the tag.
* @param Object parent An optionnal parent tag reference.
* @return Object A payload with the following keys:
* -`'component'` _Object_ : The created tag instance.
* -`'container'` _DOMElement_: The container.
*/
mount(selector, tagName, data, scope, parent, content) {
var containers = query.all(selector);
if (!containers.length) {
return;
}
if (containers.length > 1) {
for(var i = 0, len = containers.length; i < len; i++) {
this.mount(containers[i]);
}
return;
}
var container = containers[0];
var component = this._components.create(tagName, {
renderer: this,
scope: scope,
data: data,
parent: parent,
content: content
});
component.emit('mount');
component.domLayerTreeId = this._tree.mount(container, component.render.bind(component), { component: component });
component.emit('refresh');
return component;
}
/**
* Unmounts a single component.
*
* @param mixed component The component.
*/
unmount(component) {
this._tree.unmount(component.domLayerTreeId);
}
/**
* Refreshes components or a single component.
*/
refresh(component) {
if (arguments.length) {
if (!component.dirty() || component.onrefresh) {
return;
}
component.onrefresh = true;
component.emit('update');
this._tree.update(component.domLayerTreeId);
component.emit('updated');
component.onrefresh = false;
component.dirty(false);
return;
}
var mountId, mounted = this._tree.mounted();
for (mountId in mounted) {
this.refresh(mounted[mountId].component);
}
}
}
export default Renderer;