Merge pull request #196 from orangemug/fix/source-layer-autocomplete

Improved source/layer autocomplete
This commit is contained in:
Orange Mug 2017-11-28 08:57:24 +00:00 committed by GitHub
commit 7fe3137fd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 10 deletions

View file

@ -67,12 +67,12 @@ export default class App extends React.Component {
} }
this.layerWatcher = new LayerWatcher({ this.layerWatcher = new LayerWatcher({
onSourcesChange: v => this.setState({ sources: v }),
onVectorLayersChange: v => this.setState({ vectorLayers: v }) onVectorLayersChange: v => this.setState({ vectorLayers: v })
}) })
} }
componentDidMount() { componentDidMount() {
this.fetchSources();
Mousetrap.bind(['ctrl+z'], this.onUndo.bind(this)); Mousetrap.bind(['ctrl+z'], this.onUndo.bind(this));
Mousetrap.bind(['ctrl+y'], this.onRedo.bind(this)); Mousetrap.bind(['ctrl+y'], this.onRedo.bind(this));
} }
@ -126,6 +126,8 @@ export default class App extends React.Component {
errors: errors.map(err => err.message) errors: errors.map(err => err.message)
}) })
} }
this.fetchSources();
} }
onUndo() { onUndo() {
@ -182,11 +184,53 @@ export default class App extends React.Component {
}) })
} }
fetchSources() {
const sourceList = {};
for(let [key, val] of Object.entries(this.state.mapStyle.sources)) {
sourceList[key] = {
type: val.type,
layers: []
};
if(val.type === "vector") {
const url = val.url;
fetch(url)
.then((response) => {
return response.json();
})
.then((json) => {
// Create new objects before setState
const sourceList = {...this.state.sources};
sourceList[key] = {...sourceList[key]};
for(let layer of json.vector_layers) {
sourceList[key].layers.push(layer.id)
}
this.setState({
sources: sourceList
});
})
.catch((err) => {
console.error("Failed to process sources for '%s'", url, err);
})
}
}
// Note: Each source will be missing layers initially until the fetch is complete
this.setState({
sources: sourceList
})
}
mapRenderer() { mapRenderer() {
const mapProps = { const mapProps = {
mapStyle: style.replaceAccessToken(this.state.mapStyle), mapStyle: style.replaceAccessToken(this.state.mapStyle),
onDataChange: (e) => { onDataChange: (e) => {
this.layerWatcher.analyzeMap(e.map) this.layerWatcher.analyzeMap(e.map)
this.fetchSources();
}, },
} }

View file

@ -134,7 +134,7 @@ export default class LayerEditor extends React.Component {
/> />
} }
{this.props.layer.type !== 'raster' && this.props.layer.type !== 'background' && <LayerSourceLayerBlock {this.props.layer.type !== 'raster' && this.props.layer.type !== 'background' && <LayerSourceLayerBlock
sourceLayerIds={this.props.sources[this.props.layer.source]} sourceLayerIds={this.props.sources[this.props.layer.source].layers}
value={this.props.layer['source-layer']} value={this.props.layer['source-layer']}
onChange={v => this.changeProperty(null, 'source-layer', v)} onChange={v => this.changeProperty(null, 'source-layer', v)}
/> />

View file

@ -56,18 +56,54 @@ class AddModal extends React.Component {
} }
} }
componentWillReceiveProps(nextProps) { componentWillUpdate(nextProps, nextState) {
const sourceIds = Object.keys(nextProps.sources) // Check if source is valid for new type
if(!this.state.source && sourceIds.length > 0) { const availableSources = this.getSources(nextState.type);
if(
this.state.source !== ""
&& availableSources.indexOf(this.state.source) < 0
) {
this.setState({ this.setState({
source: sourceIds[0], source: ""
'source-layer': this.state['source-layer'] || (nextProps.sources[sourceIds[0]] || [])[0] });
})
} }
} }
getLayersForSource(source) {
const sourceObj = this.props.sources[source] || {};
return sourceObj.layers || [];
}
getSources(type) {
const sources = [];
const types = {
vector: [
"fill",
"line",
"symbol",
"circle",
"fill-extrusion"
],
raster: [
"raster"
]
}
for(let [key, val] of Object.entries(this.props.sources)) {
if(types[val.type].indexOf(type) > -1) {
sources.push(key);
}
}
return sources;
}
render() { render() {
const sources = this.getSources(this.state.type);
const layers = this.getLayersForSource(this.state.source);
return <Modal return <Modal
isOpen={this.props.isOpen} isOpen={this.props.isOpen}
onOpenToggle={this.props.onOpenToggle} onOpenToggle={this.props.onOpenToggle}
@ -84,14 +120,14 @@ class AddModal extends React.Component {
/> />
{this.state.type !== 'background' && {this.state.type !== 'background' &&
<LayerSourceBlock <LayerSourceBlock
sourceIds={Object.keys(this.props.sources)} sourceIds={sources}
value={this.state.source} value={this.state.source}
onChange={v => this.setState({ source: v })} onChange={v => this.setState({ source: v })}
/> />
} }
{this.state.type !== 'background' && this.state.type !== 'raster' && {this.state.type !== 'background' && this.state.type !== 'raster' &&
<LayerSourceLayerBlock <LayerSourceLayerBlock
sourceLayerIds={this.props.sources[this.state.source] || []} sourceLayerIds={layers}
value={this.state['source-layer']} value={this.state['source-layer']}
onChange={v => this.setState({ 'source-layer': v })} onChange={v => this.setState({ 'source-layer': v })}
/> />