2016-09-09 00:10:54 +02:00
|
|
|
import React from 'react'
|
2016-12-20 11:44:22 +01:00
|
|
|
import { saveAs } from 'file-saver'
|
2016-09-09 00:10:54 +02:00
|
|
|
|
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-09-09 00:10:54 +02:00
|
|
|
|
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 {
|
2016-12-16 14:49:25 +01:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2016-12-19 21:21:10 +01:00
|
|
|
this.layerWatcher = new LayerWatcher()
|
2016-12-16 14:49:25 +01:00
|
|
|
this.styleStore = new ApiStyleStore()
|
|
|
|
this.styleStore.supported(isSupported => {
|
|
|
|
if(!isSupported) {
|
|
|
|
console.log('Falling back to local storage for storing styles')
|
|
|
|
this.styleStore = new StyleStore()
|
|
|
|
}
|
2016-12-22 16:51:18 +01:00
|
|
|
this.styleStore.latestStyle(mapStyle => this.onStyleOpen(mapStyle))
|
2016-12-03 23:28:43 +01:00
|
|
|
})
|
2016-12-16 14:49:25 +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,
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onReset() {
|
|
|
|
this.styleStore.purge()
|
2016-12-22 16:51:18 +01:00
|
|
|
loadDefaultStyle(mapStyle => this.onStyleOpen(mapStyle))
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onStyleDownload() {
|
2016-12-20 16:08:49 +01:00
|
|
|
const mapStyle = this.state.mapStyle
|
2016-12-16 14:49:25 +01:00
|
|
|
const blob = new Blob([JSON.stringify(mapStyle, null, 4)], {type: "application/json;charset=utf-8"});
|
|
|
|
saveAs(blob, mapStyle.id + ".json");
|
|
|
|
this.onStyleSave()
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:51:18 +01:00
|
|
|
onStyleOpen(newStyle) {
|
2016-12-20 16:08:49 +01:00
|
|
|
console.log('upload', newStyle)
|
2016-12-16 14:49:25 +01:00
|
|
|
const savedStyle = this.styleStore.save(newStyle)
|
2016-12-17 15:44:42 +01:00
|
|
|
this.setState({ mapStyle: savedStyle })
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
2016-12-16 14:49:25 +01:00
|
|
|
this.styleStore.save(snapshotStyle)
|
|
|
|
}
|
|
|
|
|
|
|
|
onStyleChanged(newStyle) {
|
2016-12-17 15:44:42 +01:00
|
|
|
this.setState({ mapStyle: newStyle })
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onAccessTokenChanged(newToken) {
|
|
|
|
this.settingsStore.accessToken = newToken
|
|
|
|
this.setState({ accessToken: newToken })
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:21:35 +01:00
|
|
|
onLayersChange(changedLayers) {
|
2016-12-20 16:08:49 +01:00
|
|
|
const changedStyle = {
|
|
|
|
...this.state.mapStyle,
|
2016-12-20 20:21:35 +01:00
|
|
|
layers: changedLayers
|
2016-12-20 16:08:49 +01:00
|
|
|
}
|
2016-12-20 20:21:35 +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-22 18:08:42 +01:00
|
|
|
|
|
|
|
// Check if OL3 code has been loaded?
|
2016-12-17 14:53:16 +01:00
|
|
|
if(renderer === 'ol3') {
|
2016-12-22 18:08:42 +01:00
|
|
|
return <OpenLayers3Map {...mapProps} />
|
2016-12-17 14:53:16 +01:00
|
|
|
} else {
|
|
|
|
return <MapboxGlMap {...mapProps} />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:21:35 +01:00
|
|
|
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 14:50:38 +01:00
|
|
|
|
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)}
|
2016-12-22 16:51:18 +01:00
|
|
|
onStyleOpen={this.onStyleOpen.bind(this)}
|
2016-12-20 16:37:35 +01:00
|
|
|
onStyleDownload={this.onStyleDownload.bind(this)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
const layerList = <LayerList
|
2016-12-20 20:21:35 +01:00
|
|
|
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}
|
2016-12-20 20:21:35 +01:00
|
|
|
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-12-16 14:49:25 +01:00
|
|
|
}
|
2016-09-08 19:47:29 +02:00
|
|
|
}
|
2016-09-21 08:41:02 +02:00
|
|
|
|