-
Notifications
You must be signed in to change notification settings - Fork 151
/
index.js
144 lines (129 loc) · 3.71 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
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
import React from 'react';
import ReactDOM from 'react-dom';
import styleParser from 'style-attr';
import {getEventMappings} from './eventUtils.js';
/**
* <a-entity>
*/
export class Entity extends React.Component {
static propTypes = {
children: React.PropTypes.any,
events: React.PropTypes.object,
mixin: React.PropTypes.string,
primitive: React.PropTypes.string
};
attachEvents = el => {
if (!el) { return; }
attachEventsToElement(el, Object.assign(
{},
this.props.events,
getEventMappings(this.props)
));
};
render() {
// Allow through normal attributes..
const otherProps = {};
['id', 'mixin'].forEach(propName => {
if (this.props[propName]) { otherProps[propName] = this.props[propName]; }
});
return React.createElement(
this.props.primitive || 'a-entity',
Object.assign(
{ref: this.attachEvents},
otherProps,
serializeComponents(this.props)
),
this.props.children
);
}
}
/**
* <a-scene>
*/
export class Scene extends React.Component {
static propTypes = {
events: React.PropTypes.object
};
attachEvents = el => {
if (!el) { return; }
attachEventsToElement(el, Object.assign(
{},
this.props.events,
getEventMappings(this.props)
));
};
render() {
// Allow through normal attributes..
const otherProps = {};
['id', 'mixin'].forEach(propName => {
if (this.props[propName]) { otherProps[propName] = this.props[propName]; }
});
return (
<a-scene
ref={this.attachEvents}
{...otherProps}
{...serializeComponents(this.props)}>
{this.props.children}
</a-scene>
);
}
}
/**
* Serialize React props to A-Frame components.
*
* {primitive: box; width: 10} to 'primitive: box; width: 10'
*/
export function serializeComponents (props) {
var components = AFRAME.components;
let serialProps = {};
Object.keys(props).forEach(component => {
// Allow these.
if (['class', 'children', 'id', 'mixin'].indexOf(component) !== -1) {
return;
}
// className to class.
if (component === 'className') {
serialProps.class = props[component];
serialProps.className = props[component];
return;
}
if (props[component].constructor === Function) { return; }
var ind = Object.keys(components).indexOf(component.split('__')[0]);
// Discards props that aren't components.
if (ind === -1) { return; }
if (props[component].constructor === Array) {
// Stringify components passed as array.
serialProps[component] = props[component].join(' ');
} else if (props[component].constructor === Object) {
// Stringify components passed as object.
serialProps[component] = styleParser.stringify(props[component]);
} else if (props[component].constructor === Boolean) {
if (components[component].schema.type === 'boolean') {
// If the component takes one property and it is Boolean
// just passes in the prop.
serialProps[component] = props[component];
} else if (props[component] === true) {
// Otherwise if it is true, assumes component is blank.
serialProps[component] = "";
} else {
// Otherwise if false lets aframe coerce.
serialProps[component] = props[component];
}
} else {
// Do nothing for components otherwise.
serialProps[component] = props[component];
}
});
return serialProps;
};
/**
* Register event handlers to ref.
*/
function attachEventsToElement(el, eventMap) {
if (!eventMap) { return; }
Object.keys(eventMap).forEach(eventName => {
el.addEventListener(eventName, event => {
eventMap[eventName](event);
});
});
}