maputnik/src/components/App.jsx

190 lines
5.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-12-23 17:17:02 +01:00
import Mousetrap from 'mousetrap'
2016-12-24 14:42:57 +01:00
import InspectionMap from './map/InspectionMap'
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'
import { loadDefaultStyle, StyleStore } from '../libs/stylestore'
2016-12-20 16:37:35 +01:00
import { ApiStyleStore } from '../libs/apistore'
2016-12-23 17:17:02 +01:00
import { RevisionStore } from '../libs/revisions'
2016-12-20 16:37:35 +01:00
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)
this.styleStore = new ApiStyleStore()
2016-12-23 17:17:02 +01:00
this.revisionStore = new RevisionStore()
2016-12-29 15:22:47 +01:00
this.styleStore.supported(isSupported => {
if(!isSupported) {
console.log('Falling back to local storage for storing styles')
this.styleStore = new StyleStore()
}
2016-12-22 21:06:32 +01:00
this.styleStore.latestStyle(mapStyle => this.onStyleChanged(mapStyle))
2016-12-03 23:28:43 +01:00
})
this.state = {
2016-12-17 15:44:42 +01:00
mapStyle: style.emptyStyle,
2016-12-20 16:08:49 +01:00
selectedLayerIndex: 0,
2016-12-29 15:22:47 +01:00
sources: {},
vectorLayers: {},
}
2016-12-29 15:22:47 +01:00
this.layerWatcher = new LayerWatcher({
onSourcesChange: v => this.setState({ sources: v }),
onVectorLayersChange: v => this.setState({ vectorLayers: v })
})
}
2016-12-23 17:17:02 +01:00
componentDidMount() {
Mousetrap.bind(['ctrl+z'], this.onUndo.bind(this));
Mousetrap.bind(['ctrl+y'], this.onRedo.bind(this));
}
componentWillUnmount() {
Mousetrap.unbind(['ctrl+z'], this.onUndo.bind(this));
Mousetrap.unbind(['ctrl+y'], this.onRedo.bind(this));
}
onReset() {
this.styleStore.purge()
2016-12-22 16:51:18 +01:00
loadDefaultStyle(mapStyle => this.onStyleOpen(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");
}
2016-12-22 21:06:32 +01:00
saveStyle(snapshotStyle) {
snapshotStyle.modified = new Date().toJSON()
this.styleStore.save(snapshotStyle)
}
onStyleChanged(newStyle) {
2016-12-23 17:17:02 +01:00
this.revisionStore.addRevision(newStyle)
2016-12-22 21:06:32 +01:00
this.saveStyle(newStyle)
2016-12-17 15:44:42 +01:00
this.setState({ mapStyle: newStyle })
}
2016-12-23 17:17:02 +01:00
onUndo() {
const activeStyle = this.revisionStore.undo()
console.log('Undo revision', this.revisionStore.currentIdx)
this.saveStyle(activeStyle)
this.setState({ mapStyle: activeStyle })
}
onRedo() {
const activeStyle = this.revisionStore.redo()
console.log('Redo revision', this.revisionStore.currentIdx)
this.saveStyle(activeStyle)
this.setState({ mapStyle: activeStyle })
}
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
}
2016-12-22 21:06:32 +01:00
this.onStyleChanged(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-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() {
const metadata = this.state.mapStyle.metadata || {}
2016-12-16 15:14:20 +01:00
const mapProps = {
2016-12-17 15:44:42 +01:00
mapStyle: this.state.mapStyle,
accessToken: metadata['maputnik:access_token'],
2016-12-24 14:42:57 +01:00
onDataChange: (e) => {
this.layerWatcher.analyzeMap(e.map)
2016-12-24 15:14:31 +01:00
},
//TODO: This would actually belong to the layout component
style:{
top: 40,
//left: 500,
}
2016-12-16 15:14:20 +01:00
}
2016-12-20 16:08:49 +01:00
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-24 14:42:57 +01:00
} else if(renderer === 'inspection') {
2016-12-29 15:22:47 +01:00
return <InspectionMap {...mapProps} sources={this.state.sources} />
2016-12-17 14:53:16 +01:00
} 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-28 17:08:42 +01:00
const metadata = this.state.mapStyle.metadata || {}
2016-12-20 16:37:35 +01:00
const toolbar = <Toolbar
mapStyle={this.state.mapStyle}
2016-12-29 15:22:47 +01:00
sources={this.state.sources}
2016-12-20 16:37:35 +01:00
onStyleChanged={this.onStyleChanged.bind(this)}
2016-12-22 21:06:32 +01:00
onStyleOpen={this.onStyleChanged.bind(this)}
2016-12-20 16:37:35 +01:00
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}
2016-12-29 15:22:47 +01:00
sources={this.state.sources}
vectorLayers={this.state.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