maputnik/src/components/App.jsx

161 lines
4.5 KiB
React
Raw Normal View History

import React from 'react'
2016-12-20 11:44:22 +01:00
import { saveAs } from 'file-saver'
2016-12-20 16:37:35 +01:00
import MapboxGlMap from './map/MapboxGlMap'
import OpenLayers3Map from './map/OpenLayers3Map'
import LayerList from './layers/LayerList'
import LayerEditor from './layers/LayerEditor'
import Toolbar from './Toolbar'
2016-12-22 16:39:09 +01:00
import AppLayout from './AppLayout'
2016-12-20 11:44:22 +01:00
import style from '../libs/style.js'
2016-12-20 16:37:35 +01:00
import { loadDefaultStyle, SettingsStore, StyleStore } from '../libs/stylestore'
import { ApiStyleStore } from '../libs/apistore'
import LayerWatcher from '../libs/layerwatcher'
2016-12-20 11:44:22 +01:00
2015-06-15 15:21:19 +02:00
export default class App extends React.Component {
constructor(props) {
super(props)
2016-12-19 21:21:10 +01:00
this.layerWatcher = new LayerWatcher()
this.styleStore = new ApiStyleStore()
this.styleStore.supported(isSupported => {
if(!isSupported) {
console.log('Falling back to local storage for storing styles')
this.styleStore = new StyleStore()
}
this.styleStore.latestStyle(mapStyle => this.onStyleUpload(mapStyle))
2016-12-03 23:28:43 +01:00
})
this.settingsStore = new SettingsStore()
this.state = {
accessToken: this.settingsStore.accessToken,
2016-12-17 15:44:42 +01:00
mapStyle: style.emptyStyle,
2016-12-20 16:08:49 +01:00
selectedLayerIndex: 0,
}
}
onReset() {
this.styleStore.purge()
loadDefaultStyle(mapStyle => this.onStyleUpload(mapStyle))
}
onStyleDownload() {
2016-12-20 16:08:49 +01:00
const mapStyle = this.state.mapStyle
const blob = new Blob([JSON.stringify(mapStyle, null, 4)], {type: "application/json;charset=utf-8"});
saveAs(blob, mapStyle.id + ".json");
this.onStyleSave()
}
onStyleUpload(newStyle) {
2016-12-20 16:08:49 +01:00
console.log('upload', newStyle)
const savedStyle = this.styleStore.save(newStyle)
2016-12-17 15:44:42 +01:00
this.setState({ mapStyle: savedStyle })
}
onStyleSave() {
2016-12-20 16:08:49 +01:00
const snapshotStyle = this.state.mapStyle.modified = new Date().toJSON()
2016-12-17 15:44:42 +01:00
this.setState({ mapStyle: snapshotStyle })
2016-12-03 23:28:43 +01:00
console.log('Save')
this.styleStore.save(snapshotStyle)
}
onStyleChanged(newStyle) {
2016-12-17 15:44:42 +01:00
this.setState({ mapStyle: newStyle })
}
onAccessTokenChanged(newToken) {
this.settingsStore.accessToken = newToken
this.setState({ accessToken: newToken })
}
onLayersChange(changedLayers) {
2016-12-20 16:08:49 +01:00
const changedStyle = {
...this.state.mapStyle,
layers: changedLayers
2016-12-20 16:08:49 +01:00
}
this.setState({ mapStyle: changedStyle })
2016-12-17 21:25:00 +01:00
}
2016-12-22 15:49:36 +01:00
onLayerIdChange(oldId, newId) {
const changedLayers = this.state.mapStyle.layers.slice(0)
const idx = style.indexOfLayer(changedLayers, oldId)
changedLayers[idx] = {
...changedLayers[idx],
id: newId
}
this.onLayersChange(changedLayers)
}
2016-12-17 21:25:00 +01:00
onLayerChanged(layer) {
2016-12-22 15:49:36 +01:00
console.log('layer changed', layer)
2016-12-20 20:50:08 +01:00
const changedLayers = this.state.mapStyle.layers.slice(0)
const idx = style.indexOfLayer(changedLayers, layer.id)
changedLayers[idx] = layer
this.onLayersChange(changedLayers)
2016-12-17 21:25:00 +01:00
}
2016-12-17 14:53:16 +01:00
mapRenderer() {
2016-12-16 15:14:20 +01:00
const mapProps = {
2016-12-17 15:44:42 +01:00
mapStyle: this.state.mapStyle,
2016-12-16 15:14:20 +01:00
accessToken: this.state.accessToken,
2016-12-19 21:21:10 +01:00
onMapLoaded: (map) => {
this.layerWatcher.map = map
}
2016-12-16 15:14:20 +01:00
}
2016-12-20 16:08:49 +01:00
const metadata = this.state.mapStyle.metadata || {}
const renderer = metadata['maputnik:renderer'] || 'mbgljs'
2016-12-17 14:53:16 +01:00
if(renderer === 'ol3') {
return <OpenLayers3Map {...mapProps} />
} else {
return <MapboxGlMap {...mapProps} />
}
}
onLayerSelect(layerId) {
const idx = style.indexOfLayer(this.state.mapStyle.layers, layerId)
this.setState({ selectedLayerIndex: idx })
2016-12-17 16:09:37 +01:00
}
2016-12-17 14:53:16 +01:00
render() {
2016-12-20 16:08:49 +01:00
const layers = this.state.mapStyle.layers || []
const selectedLayer = layers.length > 0 ? layers[this.state.selectedLayerIndex] : null
2016-12-20 16:37:35 +01:00
const toolbar = <Toolbar
mapStyle={this.state.mapStyle}
onStyleChanged={this.onStyleChanged.bind(this)}
onStyleSave={this.onStyleSave.bind(this)}
onStyleUpload={this.onStyleUpload.bind(this)}
onStyleDownload={this.onStyleDownload.bind(this)}
/>
const layerList = <LayerList
onLayersChange={this.onLayersChange.bind(this)}
onLayerSelect={this.onLayerSelect.bind(this)}
2016-12-20 19:20:56 +01:00
selectedLayerIndex={this.state.selectedLayerIndex}
2016-12-20 16:37:35 +01:00
layers={layers}
/>
const layerEditor = selectedLayer ? <LayerEditor
layer={selectedLayer}
sources={this.layerWatcher.sources}
vectorLayers={this.layerWatcher.vectorLayers}
onLayerChanged={this.onLayerChanged.bind(this)}
2016-12-22 15:49:36 +01:00
onLayerIdChange={this.onLayerIdChange.bind(this)}
2016-12-20 16:37:35 +01:00
/> : null
2016-12-22 16:39:09 +01:00
return <AppLayout
2016-12-20 16:37:35 +01:00
toolbar={toolbar}
layerList={layerList}
layerEditor={layerEditor}
map={this.mapRenderer()}
/>
}
2016-09-08 19:47:29 +02:00
}
2016-09-21 08:41:02 +02:00