maputnik/src/components/App.jsx

160 lines
4.3 KiB
React
Raw Normal View History

import React from 'react'
2016-12-20 11:44:22 +01:00
import { saveAs } from 'file-saver'
2016-09-15 09:13:23 +02:00
import Drawer from 'rebass/dist/Drawer'
import Container from 'rebass/dist/Container'
import Block from 'rebass/dist/Block'
import Fixed from 'rebass/dist/Fixed'
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'
import Layout from './Layout'
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 })
}
2016-12-17 14:53:16 +01:00
onLayersChanged(changedLayers) {
2016-12-20 16:08:49 +01:00
const changedStyle = {
...this.state.mapStyle,
layers: [changedLayers]
}
this.setState({ mapStyle: newStyle })
2016-12-17 21:25:00 +01:00
}
onLayerChanged(layer) {
2016-12-20 16:08:49 +01:00
const changedStyle = {
...this.state.mapStyle,
layers: {
...this.state.mapStyle.layers,
[layer.id]: layer
}
}
this.setState({ mapStyle: changedStyle })
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} />
}
}
2016-12-17 16:09:37 +01:00
onLayerSelected(layerId) {
2016-12-20 16:08:49 +01:00
const layers = this.state.mapStyle.layers
for (let i = 0; i < layers.length; i++) {
if(layers[i].id === layerId) {
this.setState({ selectedLayerIndex: i })
break
}
}
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
onLayersChanged={this.onLayersChanged.bind(this)}
onLayerSelected={this.onLayerSelected.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}
onLayerChanged={this.onLayerChanged.bind(this)}
sources={this.layerWatcher.sources}
vectorLayers={this.layerWatcher.vectorLayers}
/> : null
return <Layout
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