-
Notifications
You must be signed in to change notification settings - Fork 424
/
index.js
82 lines (67 loc) · 1.9 KB
/
index.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
/* global AFRAME */
var styleParser = AFRAME.utils.styleParser;
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
AFRAME.registerComponent('event-set', {
schema: {
default: '',
parse: function (value) {
return styleParser.parse(value);
}
},
multiple: true,
init: function () {
this.eventHandler = null;
this.eventName = null;
},
update: function (oldData) {
this.removeEventListener();
this.updateEventListener();
this.addEventListener();
},
pause: function () {
this.removeEventListener();
},
play: function () {
this.addEventListener();
},
/**
* Update source-of-truth event listener registry.
* Does not actually attach event listeners yet.
*/
updateEventListener: function () {
var data = this.data;
var el = this.el;
var event;
var target;
var targetEl;
// Set event listener using `_event`.
event = data._event || this.id;
target = data._target;
// Decide the target to `setAttribute` on.
targetEl = target ? el.sceneEl.querySelector(target) : el;
this.eventName = event;
const handler = () => {
var propName;
// Set attributes.
for (propName in data) {
if (propName === '_event' || propName === '_target') { continue; }
AFRAME.utils.entity.setComponentProperty.call(this, targetEl, propName,
data[propName]);
}
};
if (!isNaN(data._delay)) {
// Delay.
this.eventHandler = () => { setTimeout(handler, parseFloat(data._delay)); };
} else {
this.eventHandler = handler;
}
},
addEventListener: function () {
this.el.addEventListener(this.eventName, this.eventHandler);
},
removeEventListener: function () {
this.el.removeEventListener(this.eventName, this.eventHandler);
}
});