-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathindex.js
More file actions
311 lines (247 loc) · 8.76 KB
/
index.js
File metadata and controls
311 lines (247 loc) · 8.76 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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Scene = exports.Entity = exports.options = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var nonEntityPropNames = ['children', 'events', 'primitive'];
var filterNonEntityPropNames = function filterNonEntityPropNames(propName) {
return nonEntityPropNames.indexOf(propName) === -1;
};
var options = {
// React needs this because React serializes.
// Preact does not because Preact runs `.setAttribute` on its own.
runSetAttributeOnUpdates: true
};
exports.options = options;
/**
* Call `.setAttribute()` on the `ref`, passing prop data directly to A-Frame.
*/
function doSetAttribute(el, props, propName) {
if (propName === 'className') {
el.setAttribute('class', props.className);
} else if (props[propName] && props[propName].constructor === Function) {
return;
} else {
var val = props[propName];
if (val === undefined || val === null) {
val = "";
}
el.setAttribute(propName, val);
}
}
/**
* Handle diffing of previous and current attributes.
*
* @param {Element} el
* @param {Object|null} prevProps - Previous props map.
* @param {Object} props - Current props map.
*/
function updateAttributes(el, prevProps, props) {
var propName;
if (!props || prevProps === props) {
return;
}
// Set attributes.
for (propName in props) {
if (!filterNonEntityPropNames(propName)) {
continue;
}
doSetAttribute(el, props, propName);
}
// See if attributes were removed.
if (prevProps) {
for (propName in prevProps) {
if (!filterNonEntityPropNames(propName)) {
continue;
}
if (Object.keys(props).indexOf(propName) === -1) {
el.removeAttribute(propName);
}
}
}
}
/**
* Render <a-entity>.
* Tell React to use A-Frame's .setAttribute() on the DOM element for all prop initializations
* and updates.
*/
var Entity = exports.Entity = function (_React$Component) {
_inherits(Entity, _React$Component);
function Entity() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Entity);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Entity.__proto__ || Object.getPrototypeOf(Entity)).call.apply(_ref, [this].concat(args))), _this), _this.initEntity = function (el) {
var props = _this.props;
var eventName;
if (!el) {
return;
}
// Store.
_this.el = el;
// Attach events.
if (props.events) {
for (eventName in props.events) {
addEventListeners(el, eventName, props.events[eventName]);
}
}
// Update entity.
updateAttributes(el, null, props);
// Allow ref.
if (props._ref) {
props._ref(el);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
/**
* In response to initial `ref` callback.
*/
_createClass(Entity, [{
key: 'componentDidUpdate',
/**
* Handle updates after the initial render.
*/
value: function componentDidUpdate(prevProps, prevState) {
var el = this.el;
var props = this.props;
// Update events.
updateEventListeners(el, prevProps.events, props.events);
// Update entity.
if (options.runSetAttributeOnUpdates) {
updateAttributes(el, prevProps, props);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var el = this.el;
var props = this.props;
var eventName;
if (props.events) {
// Remove events.
for (eventName in props.events) {
removeEventListeners(el, eventName, props.events[eventName]);
}
}
}
/**
* Render A-Frame DOM with ref: https://facebook.github.io/react/docs/refs-and-the-dom.html
*/
}, {
key: 'render',
value: function render() {
var props = this.props;
var elementName = this.isScene ? 'a-scene' : props.primitive || 'a-entity';
var propName;
// Let through props that are OK to render initially.
var reactProps = {};
for (propName in props) {
if (['className', 'id', 'mixin', 'loading-screen'].indexOf(propName) !== -1 || propName.indexOf('data-') === 0) {
reactProps[propName] = props[propName];
}
}
return _react2.default.createElement(elementName, _extends({ ref: this.initEntity }, reactProps), props.children);
}
}]);
return Entity;
}(_react2.default.Component);
/**
* Render <a-scene>.
* <a-scene> extends from <a-entity> in A-Frame so we reuse <Entity/>.
*/
var Scene = exports.Scene = function (_Entity) {
_inherits(Scene, _Entity);
function Scene(props) {
_classCallCheck(this, Scene);
var _this2 = _possibleConstructorReturn(this, (Scene.__proto__ || Object.getPrototypeOf(Scene)).call(this, props));
_this2.isScene = true;
return _this2;
}
return Scene;
}(Entity);
/**
* Handle diffing of previous and current event maps.
*
* @param {Element} el
* @param {Object} prevEvents - Previous event map.
* @param {Object} events - Current event map.
*/
function updateEventListeners(el, prevEvents, events) {
var eventName;
if (!prevEvents || !events || prevEvents === events) {
return;
}
for (eventName in events) {
// Didn't change.
if (prevEvents[eventName] === events[eventName]) {
continue;
}
// If changed, remove old previous event listeners.
if (prevEvents[eventName]) {
removeEventListeners(el, eventName, prevEvents[eventName]);
}
// Add new event listeners.
addEventListeners(el, eventName, events[eventName]);
}
// See if event handlers were removed.
for (eventName in prevEvents) {
if (!events[eventName]) {
removeEventListeners(el, eventName, prevEvents[eventName]);
}
}
}
/**
* Register event handlers for an event name to ref.
*
* @param {Element} el - DOM element.
* @param {string} eventName
* @param {array|function} eventHandlers - Handler function or array of handler functions.
*/
function addEventListeners(el, eventName, handlers) {
var handler;
var i;
if (!handlers) {
return;
}
// Convert to array.
if (handlers.constructor === Function) {
handlers = [handlers];
}
// Register.
for (i = 0; i < handlers.length; i++) {
el.addEventListener(eventName, handlers[i]);
}
}
/**
* Unregister event handlers for an event name to ref.
*
* @param {Element} el - DOM element.
* @param {string} eventName
* @param {array|function} eventHandlers - Handler function or array of handler functions.
*/
function removeEventListeners(el, eventName, handlers) {
var handler;
var i;
if (!handlers) {
return;
}
// Convert to array.
if (handlers.constructor === Function) {
handlers = [handlers];
}
// Unregister.
for (i = 0; i < handlers.length; i++) {
el.removeEventListener(eventName, handlers[i]);
}
}