-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathDOMElement.js
695 lines (616 loc) · 20.6 KB
/
DOMElement.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Famous Industries Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var CallbackStore = require('../utilities/CallbackStore');
var TransformSystem = require('../core/TransformSystem');
var OpacitySystem = require('../core/OpacitySystem');
var Commands = require('../core/Commands');
var Size = require('../core/Size');
/**
* A DOMElement is a component that can be added to a Node with the
* purpose of sending draw commands to the renderer. Renderables send draw commands
* to through their Nodes to the Compositor where they are acted upon.
*
* @class DOMElement
*
* @param {Node} node The Node to which the `DOMElement`
* renderable should be attached to.
* @param {Object} options Initial options used for instantiating
* the Node.
* @param {Object} options.properties CSS properties that should be added to
* the actual DOMElement on the initial draw.
* @param {Object} options.attributes Element attributes that should be added to
* the actual DOMElement.
* @param {String} options.id String to be applied as 'id' of the actual
* DOMElement.
* @param {String} options.content String to be applied as the content of the
* actual DOMElement.
* @param {Boolean} options.cutout Specifies the presence of a 'cutout' in the
* WebGL canvas over this element which allows
* for DOM and WebGL layering. On by default.
*/
function DOMElement(node, options) {
if (!node) throw new Error('DOMElement must be instantiated on a node');
this._changeQueue = [];
this._requestingUpdate = false;
this._renderSized = false;
this._requestRenderSize = false;
this._UIEvents = node.getUIEvents().slice(0);
this._classes = ['famous-dom-element'];
this._requestingEventListeners = [];
this._styles = {};
this._attributes = {};
this._content = '';
this._tagName = options && options.tagName ? options.tagName : 'div';
this._renderSize = [0, 0, 0];
this._node = node;
if (node) node.addComponent(this);
this._callbacks = new CallbackStore();
this.setProperty('display', node.isShown() ? 'block' : 'none');
if (!options) return;
var i;
var key;
if (options.classes)
for (i = 0; i < options.classes.length; i++)
this.addClass(options.classes[i]);
if (options.attributes)
for (key in options.attributes)
this.setAttribute(key, options.attributes[key]);
if (options.properties)
for (key in options.properties)
this.setProperty(key, options.properties[key]);
if (options.id) this.setId(options.id);
if (options.content) this.setContent(options.content);
if (options.cutout === false) this.setCutoutState(options.cutout);
}
/**
* Serializes the state of the DOMElement.
*
* @method
*
* @return {Object} serialized interal state
*/
DOMElement.prototype.getValue = function getValue() {
return {
classes: this._classes,
styles: this._styles,
attributes: this._attributes,
content: this._content,
id: this._attributes.id,
tagName: this._tagName
};
};
/**
* Method to be invoked by the node as soon as an update occurs. This allows
* the DOMElement renderable to dynamically react to state changes on the Node.
*
* This flushes the internal draw command queue by sending individual commands
* to the node using `sendDrawCommand`.
*
* @method
*
* @return {undefined} undefined
*/
DOMElement.prototype.onUpdate = function onUpdate () {
var node = this._node;
var queue = this._changeQueue;
var len = queue.length;
if (len && node) {
node.sendDrawCommand(Commands.WITH);
node.sendDrawCommand(node.getLocation());
while (len--) node.sendDrawCommand(queue.shift());
if (this._requestRenderSize) {
node.sendDrawCommand(Commands.DOM_RENDER_SIZE);
node.sendDrawCommand(node.getLocation());
this._requestRenderSize = false;
}
}
this._requestingUpdate = false;
};
/**
* Method to be invoked by the Node as soon as the node (or any of its
* ancestors) is being mounted.
*
* @method onMount
*
* @param {Node} node Parent node to which the component should be added.
* @param {String} id Path at which the component (or node) is being
* attached. The path is being set on the actual
* DOMElement as a `data-fa-path`-attribute.
*
* @return {undefined} undefined
*/
DOMElement.prototype.onMount = function onMount(node, id) {
this._node = node;
this._id = id;
this._UIEvents = node.getUIEvents().slice(0);
TransformSystem.makeBreakPointAt(node.getLocation());
this.onSizeModeChange.apply(this, node.getSizeMode());
OpacitySystem.makeBreakPointAt(node.getLocation());
this.draw();
this.setAttribute('data-fa-path', node.getLocation());
};
/**
* Method to be invoked by the Node as soon as the node is being dismounted
* either directly or by dismounting one of its ancestors.
*
* @method
*
* @return {undefined} undefined
*/
DOMElement.prototype.onDismount = function onDismount() {
this.setProperty('display', 'none');
this.setAttribute('data-fa-path', '');
this.setCutoutState(false);
this.onUpdate();
this._initialized = false;
};
/**
* Method to be invoked by the node as soon as the DOMElement is being shown.
* This results into the DOMElement setting the `display` property to `block`
* and therefore visually showing the corresponding DOMElement (again).
*
* @method
*
* @return {undefined} undefined
*/
DOMElement.prototype.onShow = function onShow() {
if (this._node.isShown()) this.setProperty('display', 'block');
};
/**
* Method to be invoked by the node as soon as the DOMElement is being hidden.
* This results into the DOMElement setting the `display` property to `none`
* and therefore visually hiding the corresponding DOMElement (again).
*
* @method
*
* @return {undefined} undefined
*/
DOMElement.prototype.onHide = function onHide() {
this.setProperty('display', 'none');
};
/**
* Enables or disables WebGL 'cutout' for this element, which affects
* how the element is layered with WebGL objects in the scene.
*
* @method
*
* @param {Boolean} usesCutout The presence of a WebGL 'cutout' for this element.
*
* @return {DOMElement} this
*/
DOMElement.prototype.setCutoutState = function setCutoutState (usesCutout) {
if (this._initialized)
this._changeQueue.push(Commands.GL_CUTOUT_STATE, usesCutout);
if (!this._requestingUpdate) this._requestUpdate();
return this;
};
/**
* Method to be invoked by the node as soon as the transform matrix associated
* with the node changes. The DOMElement will react to transform changes by sending
* `CHANGE_TRANSFORM` commands to the `DOMRenderer`.
*
* @method
*
* @param {Float32Array} transform The final transform matrix
*
* @return {undefined} undefined
*/
DOMElement.prototype.onTransformChange = function onTransformChange (transform) {
this._changeQueue.push(Commands.CHANGE_TRANSFORM);
transform = transform.getLocalTransform();
for (var i = 0, len = transform.length ; i < len ; i++)
this._changeQueue.push(transform[i]);
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* Method to be invoked by the node as soon as its computed size changes.
*
* @method
*
* @param {Number} x width of the Node the DOMElement is attached to
* @param {Number} y height of the Node the DOMElement is attached to
*
* @return {DOMElement} this
*/
DOMElement.prototype.onSizeChange = function onSizeChange(x, y) {
var sizeMode = this._node.getSizeMode();
var sizedX = sizeMode[0] !== Size.RENDER;
var sizedY = sizeMode[1] !== Size.RENDER;
if (this._initialized)
this._changeQueue.push(Commands.CHANGE_SIZE,
sizedX ? x : sizedX,
sizedY ? y : sizedY);
if (!this._requestingUpdate) this._requestUpdate();
return this;
};
/**
* Method to be invoked by the node as soon as its opacity changes
*
* @method
*
* @param {Number} opacity The new opacity, as a scalar from 0 to 1
*
* @return {DOMElement} this
*/
DOMElement.prototype.onOpacityChange = function onOpacityChange(opacity) {
opacity = opacity.getLocalOpacity();
return this.setProperty('opacity', opacity);
};
/**
* Method to be invoked by the node as soon as a new UIEvent is being added.
* This results into an `ADD_EVENT_LISTENER` command being sent.
*
* @param {String} uiEvent uiEvent to be subscribed to (e.g. `click`)
*
* @return {undefined} undefined
*/
DOMElement.prototype.onAddUIEvent = function onAddUIEvent(uiEvent) {
if (this._UIEvents.indexOf(uiEvent) === -1) {
this._subscribe(uiEvent);
this._UIEvents.push(uiEvent);
}
else if (this._inDraw) {
this._subscribe(uiEvent);
}
return this;
};
/**
* Method to be invoked by the node as soon as a UIEvent is removed from
* the node. This results into an `UNSUBSCRIBE` command being sent.
*
* @param {String} UIEvent UIEvent to be removed (e.g. `mousedown`)
*
* @return {undefined} undefined
*/
DOMElement.prototype.onRemoveUIEvent = function onRemoveUIEvent(UIEvent) {
var index = this._UIEvents.indexOf(UIEvent);
if (index !== -1) {
this._unsubscribe(UIEvent);
this._UIEvents.splice(index, 1);
}
else if (this._inDraw) {
this._unsubscribe(UIEvent);
}
return this;
};
/**
* Appends an `SUBSCRIBE` command to the command queue.
*
* @method
* @private
*
* @param {String} uiEvent Event type (e.g. `click`)
*
* @return {undefined} undefined
*/
DOMElement.prototype._subscribe = function _subscribe (uiEvent) {
if (this._initialized) {
this._changeQueue.push(Commands.SUBSCRIBE, uiEvent);
}
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* When running in a worker, the browser's default action for specific events
* can't be prevented on a case by case basis (via `e.preventDefault()`).
* Instead this function should be used to register an event to be prevented by
* default.
*
* @method
*
* @param {String} uiEvent UI Event (e.g. wheel) for which to prevent the
* browser's default action (e.g. form submission,
* scrolling)
* @return {undefined} undefined
*/
DOMElement.prototype.preventDefault = function preventDefault (uiEvent) {
if (this._initialized) {
this._changeQueue.push(Commands.PREVENT_DEFAULT, uiEvent);
}
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* Opposite of {@link DOMElement#preventDefault}. No longer prevent the
* browser's default action on subsequent events of this type.
*
* @method
*
* @param {type} uiEvent UI Event previously registered using
* {@link DOMElement#preventDefault}.
* @return {undefined} undefined
*/
DOMElement.prototype.allowDefault = function allowDefault (uiEvent) {
if (this._initialized) {
this._changeQueue.push(Commands.ALLOW_DEFAULT, uiEvent);
}
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* Appends an `UNSUBSCRIBE` command to the command queue.
*
* @method
* @private
*
* @param {String} UIEvent Event type (e.g. `click`)
*
* @return {undefined} undefined
*/
DOMElement.prototype._unsubscribe = function _unsubscribe (UIEvent) {
if (this._initialized) {
this._changeQueue.push(Commands.UNSUBSCRIBE, UIEvent);
}
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* Method to be invoked by the node as soon as the underlying size mode
* changes. This results into the size being fetched from the node in
* order to update the actual, rendered size.
*
* @method
*
* @param {Number} x the sizing mode in use for determining size in the x direction
* @param {Number} y the sizing mode in use for determining size in the y direction
* @param {Number} z the sizing mode in use for determining size in the z direction
*
* @return {undefined} undefined
*/
DOMElement.prototype.onSizeModeChange = function onSizeModeChange(x, y, z) {
if (x === Size.RENDER || y === Size.RENDER || z === Size.RENDER) {
this._renderSized = true;
this._requestRenderSize = true;
}
var size = this._node.getSize();
this.onSizeChange(size[0], size[1]);
};
/**
* Method to be retrieve the rendered size of the DOM element that is
* drawn for this node.
*
* @method
*
* @return {Array} size of the rendered DOM element in pixels
*/
DOMElement.prototype.getRenderSize = function getRenderSize() {
return this._renderSize;
};
/**
* Method to have the component request an update from its Node
*
* @method
* @private
*
* @return {undefined} undefined
*/
DOMElement.prototype._requestUpdate = function _requestUpdate() {
if (!this._requestingUpdate && this._id) {
this._node.requestUpdate(this._id);
this._requestingUpdate = true;
}
};
/**
* Initializes the DOMElement by sending the `INIT_DOM` command. This creates
* or reallocates a new Element in the actual DOM hierarchy.
*
* @method
*
* @return {undefined} undefined
*/
DOMElement.prototype.init = function init () {
this._changeQueue.push(Commands.INIT_DOM, this._tagName);
this._initialized = true;
this.onTransformChange(TransformSystem.get(this._node.getLocation()));
this.onOpacityChange(OpacitySystem.get(this._node.getLocation()));
var size = this._node.getSize();
this.onSizeChange(size[0], size[1]);
if (!this._requestingUpdate) this._requestUpdate();
};
/**
* Sets the id attribute of the DOMElement.
*
* @method
*
* @param {String} id New id to be set
*
* @return {DOMElement} this
*/
DOMElement.prototype.setId = function setId (id) {
this.setAttribute('id', id);
return this;
};
/**
* Adds a new class to the internal class list of the underlying Element in the
* DOM.
*
* @method
*
* @param {String} value New class name to be added
*
* @return {DOMElement} this
*/
DOMElement.prototype.addClass = function addClass (value) {
if (this._classes.indexOf(value) < 0) {
if (this._initialized) this._changeQueue.push(Commands.ADD_CLASS, value);
this._classes.push(value);
if (!this._requestingUpdate) this._requestUpdate();
if (this._renderSized) this._requestRenderSize = true;
return this;
}
if (this._inDraw) {
if (this._initialized) this._changeQueue.push(Commands.ADD_CLASS, value);
if (!this._requestingUpdate) this._requestUpdate();
}
return this;
};
/**
* Removes a class from the DOMElement's classList.
*
* @method
*
* @param {String} value Class name to be removed
*
* @return {DOMElement} this
*/
DOMElement.prototype.removeClass = function removeClass (value) {
var index = this._classes.indexOf(value);
if (index < 0) return this;
this._changeQueue.push(Commands.REMOVE_CLASS, value);
this._classes.splice(index, 1);
if (!this._requestingUpdate) this._requestUpdate();
return this;
};
/**
* Checks if the DOMElement has the passed in class.
*
* @method
*
* @param {String} value The class name
*
* @return {Boolean} Boolean value indicating whether the passed in class name is in the DOMElement's class list.
*/
DOMElement.prototype.hasClass = function hasClass (value) {
return this._classes.indexOf(value) !== -1;
};
/**
* Sets an attribute of the DOMElement.
*
* @method
*
* @param {String} name Attribute key (e.g. `src`)
* @param {String} value Attribute value (e.g. `http://famo.us`)
*
* @return {DOMElement} this
*/
DOMElement.prototype.setAttribute = function setAttribute (name, value) {
if (this._attributes[name] !== value || this._inDraw) {
this._attributes[name] = value;
if (this._initialized) this._changeQueue.push(Commands.CHANGE_ATTRIBUTE, name, value);
if (!this._requestUpdate) this._requestUpdate();
}
return this;
};
/**
* Sets a CSS property
*
* @chainable
*
* @param {String} name Name of the CSS rule (e.g. `background-color`)
* @param {String} value Value of CSS property (e.g. `red`)
*
* @return {DOMElement} this
*/
DOMElement.prototype.setProperty = function setProperty (name, value) {
if (this._styles[name] !== value || this._inDraw) {
this._styles[name] = value;
if (this._initialized) this._changeQueue.push(Commands.CHANGE_PROPERTY, name, value);
if (!this._requestingUpdate) this._requestUpdate();
if (this._renderSized) this._requestRenderSize = true;
}
return this;
};
/**
* Sets the content of the DOMElement. This is using `innerHTML`, escaping user
* generated content is therefore essential for security purposes.
*
* @method
*
* @param {String} content Content to be set using `.innerHTML = ...`
*
* @return {DOMElement} this
*/
DOMElement.prototype.setContent = function setContent (content) {
if (this._content !== content || this._inDraw) {
this._content = content;
if (this._initialized) this._changeQueue.push(Commands.CHANGE_CONTENT, content);
if (!this._requestingUpdate) this._requestUpdate();
if (this._renderSized) this._requestRenderSize = true;
}
return this;
};
/**
* Subscribes to a DOMElement using.
*
* @method on
*
* @param {String} event The event type (e.g. `click`).
* @param {Function} listener Handler function for the specified event type
* in which the payload event object will be
* passed into.
*
* @return {Function} A function to call if you want to remove the callback
*/
DOMElement.prototype.on = function on (event, listener) {
return this._callbacks.on(event, listener);
};
/**
* Function to be invoked by the Node whenever an event is being received.
* There are two different ways to subscribe for those events:
*
* 1. By overriding the onReceive method (and possibly using `switch` in order
* to differentiate between the different event types).
* 2. By using DOMElement and using the built-in CallbackStore.
*
* @method
*
* @param {String} event Event type (e.g. `click`)
* @param {Object} payload Event object.
*
* @return {undefined} undefined
*/
DOMElement.prototype.onReceive = function onReceive (event, payload) {
if (event === 'resize') {
this._renderSize[0] = payload.val[0];
this._renderSize[1] = payload.val[1];
if (!this._requestingUpdate) this._requestUpdate();
}
this._callbacks.trigger(event, payload);
};
/**
* The draw function is being used in order to allow mutating the DOMElement
* before actually mounting the corresponding node.
*
* @method
* @private
*
* @return {undefined} undefined
*/
DOMElement.prototype.draw = function draw() {
var key;
var i;
var len;
this._inDraw = true;
this.init();
for (i = 0, len = this._classes.length ; i < len ; i++)
this.addClass(this._classes[i]);
if (this._content) this.setContent(this._content);
for (key in this._styles)
if (this._styles[key] != null)
this.setProperty(key, this._styles[key]);
for (key in this._attributes)
if (this._attributes[key] != null)
this.setAttribute(key, this._attributes[key]);
for (i = 0, len = this._UIEvents.length ; i < len ; i++)
this.onAddUIEvent(this._UIEvents[i]);
this._inDraw = false;
};
module.exports = DOMElement;