Track known vector layer ids

This commit is contained in:
Lukas Martinelli 2016-12-19 21:21:10 +01:00
parent 965b2d6e05
commit ec39f03449
4 changed files with 52 additions and 6 deletions

View file

@ -15,6 +15,7 @@ import style from './style.js'
import { loadDefaultStyle, SettingsStore, StyleStore } from './stylestore.js' import { loadDefaultStyle, SettingsStore, StyleStore } from './stylestore.js'
import { ApiStyleStore } from './apistore.js' import { ApiStyleStore } from './apistore.js'
import LayerWatcher from './layerwatcher.js'
import theme from './theme.js' import theme from './theme.js'
import { colors, fullHeight } from './theme.js' import { colors, fullHeight } from './theme.js'
import './index.css' import './index.css'
@ -27,7 +28,7 @@ export default class App extends React.Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.layerWatcher = new LayerWatcher()
this.styleStore = new ApiStyleStore() this.styleStore = new ApiStyleStore()
this.styleStore.supported(isSupported => { this.styleStore.supported(isSupported => {
if(!isSupported) { if(!isSupported) {
@ -106,6 +107,9 @@ export default class App extends React.Component {
const mapProps = { const mapProps = {
mapStyle: this.state.mapStyle, mapStyle: this.state.mapStyle,
accessToken: this.state.accessToken, accessToken: this.state.accessToken,
onMapLoaded: (map) => {
this.layerWatcher.map = map
}
} }
const renderer = this.state.mapStyle.getIn(['metadata', 'maputnik:renderer'], 'mbgljs') const renderer = this.state.mapStyle.getIn(['metadata', 'maputnik:renderer'], 'mbgljs')
if(renderer === 'ol3') { if(renderer === 'ol3') {
@ -156,7 +160,7 @@ export default class App extends React.Component {
width: 300, width: 300,
backgroundColor: colors.gray} backgroundColor: colors.gray}
}> }>
{selectedLayer && <LayerEditor layer={selectedLayer} onLayerChanged={this.onLayerChanged.bind(this)} sources={this.state.mapStyle.get('sources')}/>} {selectedLayer && <LayerEditor layer={selectedLayer} onLayerChanged={this.onLayerChanged.bind(this)} sources={this.layerWatcher.sources}/>}
</div> </div>
{this.mapRenderer()} {this.mapRenderer()}
</div> </div>

View file

@ -7,6 +7,14 @@ import Immutable from 'immutable'
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color' import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
export class MapboxGlMap extends Map { export class MapboxGlMap extends Map {
static propTypes = {
onMapLoaded: React.PropTypes.func,
}
static defaultProps = {
onMapLoaded: () => {}
}
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { map: null } this.state = { map: null }
@ -51,6 +59,7 @@ export class MapboxGlMap extends Map {
}); });
map.on("style.load", (...args) => { map.on("style.load", (...args) => {
this.props.onMapLoaded(map)
this.setState({ map }); this.setState({ map });
}); });
} }

View file

@ -27,6 +27,12 @@ export default class SourceEditor extends React.Component {
const options = this.props.sources.map((source, sourceId)=> { const options = this.props.sources.map((source, sourceId)=> {
return <option key={sourceId} value={sourceId}>{sourceId}</option> return <option key={sourceId} value={sourceId}>{sourceId}</option>
}).toIndexedSeq() }).toIndexedSeq()
const layerOptions = this.props.sources.get(this.props.source, Immutable.Set()).map(vectorLayerId => {
const id = vectorLayerId
return <option key={id} value={id}>{id}</option>
}).toIndexedSeq()
console.log(this.props.sources) console.log(this.props.sources)
return <div> return <div>
<div style={inputStyle.property}> <div style={inputStyle.property}>
@ -41,12 +47,13 @@ export default class SourceEditor extends React.Component {
</div> </div>
<div style={inputStyle.property}> <div style={inputStyle.property}>
<label style={inputStyle.label}>Source Layer</label> <label style={inputStyle.label}>Source Layer</label>
<input <select
style={inputStyle.input} style={inputStyle.select}
name={"sourceLayer"}
value={this.props.sourceLayer} value={this.props.sourceLayer}
onChange={(e) => this.onSourceLayerChange(e.target.value)} onChange={(e) => this.onSourceLayerChange(e.target.value)}
/> >
{layerOptions}
</select>
</div> </div>
</div> </div>

26
src/layerwatcher.js Normal file
View file

@ -0,0 +1,26 @@
import Immutable from 'immutable'
/** Listens to map events to build up a store of available vector
* layers contained in the tiles */
export default class LayerWatcher {
constructor() {
this._sources = {}
}
/** Set the map as soon as the map is initialized */
set map(m) {
//TODO: At some point we need to unsubscribe when new map is set
m.on('data', (e) => {
if(e.dataType !== 'tile') return
this._sources[e.source.id] = e.source.vectorLayerIds
})
}
/** Access all known sources and their vector tile ids */
get sources() {
console.log(this._sources)
return Immutable.Map(Object.keys(this._sources).map(key => {
return [key, Immutable.Set(this._sources[key])]
}))
}
}