From 29cfb58a564fe114285a01d414da27c37065f7f7 Mon Sep 17 00:00:00 2001 From: Lukas Martinelli Date: Thu, 29 Dec 2016 15:22:47 +0100 Subject: [PATCH] Update sources if they change --- package.json | 1 + src/components/App.jsx | 17 ++++++++++++----- src/components/fields/SpecField.jsx | 1 - src/components/layers/LayerEditor.jsx | 1 - src/components/modals/AddModal.jsx | 18 ++++++++++++++---- src/libs/layerwatcher.js | 20 +++++++++++++++++++- src/libs/style.js | 1 - 7 files changed, 46 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index ebeca85..991d4fc 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "file-saver": "^1.3.2", "lodash.capitalize": "^4.2.1", "lodash.clonedeep": "^4.5.0", + "lodash.isequal": "^4.4.0", "lodash.throttle": "^4.1.1", "lodash.topairs": "^4.3.0", "mapbox-gl": "^0.29.0", diff --git a/src/components/App.jsx b/src/components/App.jsx index 8747acb..45d604d 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -20,9 +20,9 @@ import LayerWatcher from '../libs/layerwatcher' export default class App extends React.Component { constructor(props) { super(props) - this.layerWatcher = new LayerWatcher() this.styleStore = new ApiStyleStore() this.revisionStore = new RevisionStore() + this.styleStore.supported(isSupported => { if(!isSupported) { console.log('Falling back to local storage for storing styles') @@ -34,7 +34,14 @@ export default class App extends React.Component { this.state = { mapStyle: style.emptyStyle, selectedLayerIndex: 0, + sources: {}, + vectorLayers: {}, } + + this.layerWatcher = new LayerWatcher({ + onSourcesChange: v => this.setState({ sources: v }), + onVectorLayersChange: v => this.setState({ vectorLayers: v }) + }) } componentDidMount() { @@ -146,7 +153,7 @@ export default class App extends React.Component { if(renderer === 'ol3') { return } else if(renderer === 'inspection') { - return + return } else { return } @@ -164,7 +171,7 @@ export default class App extends React.Component { const toolbar = this.props.onChange(this.props.fieldName, newValue) } - console.log(this.props.fieldName, this.props.fieldSpec.type) switch(this.props.fieldSpec.type) { case 'number': return ( { return !(this.props.layer.type === 'background' && group.type === 'source') diff --git a/src/components/modals/AddModal.jsx b/src/components/modals/AddModal.jsx index 02b6931..b709fbe 100644 --- a/src/components/modals/AddModal.jsx +++ b/src/components/modals/AddModal.jsx @@ -24,16 +24,26 @@ class AddModal extends React.Component { constructor(props) { super(props) - - console.log('sources', this.props.sources) this.state = { type: 'fill', id: '', - //source: Object.keys(this.props.sources)[0], - //'source-layer': this.props.sources[0][0] + } + + if(props.sources.length > 0) { + this.state.source = Object.keys(this.props.sources)[0] } } + componentWillReceiveProps(nextProps) { + const sourceIds = Object.keys(nextProps.sources) + if(!this.state.source && sourceIds.length > 0) { + this.setState({ + source: sourceIds[0], + }) + } + } + + render() { return {}) + this.onVectorLayersChange = opts.onVectorLayersChange || (() => {}) + this._sources = {} this._vectorLayers = {} @@ -14,15 +18,24 @@ export default class LayerWatcher { } analyzeMap(map) { + const previousSources = { ...this._sources } + Object.keys(map.style.sourceCaches).forEach(sourceId => { //NOTE: This heavily depends on the internal API of Mapbox GL //so this breaks between Mapbox GL JS releases this._sources[sourceId] = map.style.sourceCaches[sourceId]._source.vectorLayerIds }) + + if(!isEqual(previousSources, this._sources)) { + this.onSourcesChange(this._sources) + } + this.throttledAnalyzeVectorLayerFields(map) } analyzeVectorLayerFields(map) { + const previousVectorLayers = { ...this._vectorLayers } + Object.keys(this._sources).forEach(sourceId => { this._sources[sourceId].forEach(vectorLayerId => { const knownProperties = this._vectorLayers[vectorLayerId] || {} @@ -38,6 +51,11 @@ export default class LayerWatcher { this._vectorLayers[vectorLayerId] = knownProperties }) }) + + if(!isEqual(previousVectorLayers, this._vectorLayers)) { + this.onVectorLayersChange(this._vectorLayers) + } + } /** Access all known sources and their vector tile ids */ diff --git a/src/libs/style.js b/src/libs/style.js index 7bb16e9..0cf7618 100644 --- a/src/libs/style.js +++ b/src/libs/style.js @@ -30,7 +30,6 @@ function ensureHasNoRefs(style) { ...style, layers: derefLayers(style.layers) } - console.log(derefedStyle) return derefedStyle }