-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Select from 'react-select'; | ||
|
||
export default class AddComponent extends React.Component { | ||
static propTypes = { | ||
entity: PropTypes.object | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { value: null }; | ||
} | ||
|
||
/** | ||
* Add blank component. | ||
* If component is instanced, generate an ID. | ||
*/ | ||
addComponent = (value) => { | ||
this.setState({ value: null }); | ||
|
||
let componentName = value.value; | ||
|
||
var entity = this.props.entity; | ||
|
||
if (AFRAME.components[componentName].multiple) { | ||
const id = prompt( | ||
`Provide an ID for this component (e.g., 'foo' for ${componentName}__foo).` | ||
); | ||
componentName = id ? `${componentName}__${id}` : componentName; | ||
} | ||
|
||
AFRAME.INSPECTOR.execute('componentadd', { | ||
entity, | ||
component: componentName, | ||
value: '' | ||
}); | ||
}; | ||
|
||
/** | ||
* Component dropdown options. | ||
*/ | ||
getComponentsOptions() { | ||
const usedComponents = Object.keys(this.props.entity.components); | ||
var commonOptions = Object.keys(AFRAME.components) | ||
.filter(function (componentName) { | ||
return ( | ||
componentName.startsWith('street-generated-') && // Added filter for street-generated- prefix | ||
(AFRAME.components[componentName].multiple || | ||
usedComponents.indexOf(componentName) === -1) | ||
); | ||
}) | ||
.sort() | ||
.map(function (value) { | ||
return { value: value, label: value, origin: 'loaded' }; | ||
}); | ||
|
||
this.options = commonOptions; | ||
this.options = this.options.sort(function (a, b) { | ||
return a.label === b.label ? 0 : a.label < b.label ? -1 : 1; | ||
}); | ||
} | ||
|
||
renderOption(option) { | ||
var bullet = ( | ||
<span title="Component already loaded in the scene">●</span> | ||
); | ||
return ( | ||
<strong className="option"> | ||
{option.label} {option.origin === 'loaded' ? bullet : ''} | ||
</strong> | ||
); | ||
} | ||
|
||
render() { | ||
const entity = this.props.entity; | ||
if (!entity) { | ||
return <div />; | ||
} | ||
|
||
this.getComponentsOptions(); | ||
|
||
return ( | ||
<div id="addComponentContainer"> | ||
<p id="addComponentHeader">COMPONENTS</p> | ||
<Select | ||
id="addComponent" | ||
className="addComponent" | ||
classNamePrefix="select" | ||
options={this.options} | ||
isClearable={false} | ||
isSearchable | ||
placeholder="Add component..." | ||
noOptionsMessage={() => 'No components found'} | ||
onChange={this.addComponent} | ||
optionRenderer={this.renderOption} | ||
value={this.state.value} | ||
/> | ||
</div> | ||
); | ||
} | ||
} |