2016-09-09 00:10:54 +02:00
|
|
|
import React from 'react'
|
2016-12-23 17:17:02 +01:00
|
|
|
import Mousetrap from 'mousetrap'
|
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-12-31 14:32:04 +01:00
|
|
|
import MessagePanel from './MessagePanel'
|
2016-09-09 00:10:54 +02:00
|
|
|
|
2017-01-10 11:13:53 +01:00
|
|
|
import { downloadGlyphsMetadata, downloadSpriteMetadata } from '../libs/metadata'
|
2017-03-22 11:36:20 +01:00
|
|
|
import GlSpec from 'mapbox-gl/src/style-spec/reference/latest'
|
|
|
|
import validateStyleMin from 'mapbox-gl/src/style-spec/validate_style.min'
|
|
|
|
import formatStyle from 'mapbox-gl/src/style-spec/format'
|
2016-12-20 11:44:22 +01:00
|
|
|
import style from '../libs/style.js'
|
2017-01-05 19:34:32 +01:00
|
|
|
import { initialStyleUrl, loadStyleUrl } from '../libs/urlopen'
|
2016-12-31 14:32:04 +01:00
|
|
|
import { undoMessages, redoMessages } from '../libs/diffmessage'
|
2016-12-28 21:50:47 +01:00
|
|
|
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'
|
2017-01-13 15:31:28 +01:00
|
|
|
import tokens from '../config/tokens.json'
|
2016-12-20 11:44:22 +01:00
|
|
|
|
2017-01-10 11:13:53 +01:00
|
|
|
function updateRootSpec(spec, fieldName, newValues) {
|
|
|
|
return {
|
|
|
|
...spec,
|
|
|
|
$root: {
|
|
|
|
...spec.$root,
|
|
|
|
[fieldName]: {
|
|
|
|
...spec.$root[fieldName],
|
|
|
|
values: newValues
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-23 17:17:02 +01:00
|
|
|
this.revisionStore = new RevisionStore()
|
2017-01-01 14:49:32 +01:00
|
|
|
this.styleStore = new ApiStyleStore({
|
|
|
|
onLocalStyleChange: mapStyle => this.onStyleChanged(mapStyle, false)
|
|
|
|
})
|
2016-12-29 15:22:47 +01:00
|
|
|
|
2017-01-05 19:34:32 +01:00
|
|
|
const styleUrl = initialStyleUrl()
|
|
|
|
if(styleUrl) {
|
|
|
|
this.styleStore = new StyleStore()
|
|
|
|
loadStyleUrl(styleUrl, mapStyle => this.onStyleChanged(mapStyle))
|
|
|
|
} else {
|
|
|
|
this.styleStore.init(err => {
|
|
|
|
if(err) {
|
|
|
|
console.log('Falling back to local storage for storing styles')
|
|
|
|
this.styleStore = new StyleStore()
|
|
|
|
}
|
|
|
|
this.styleStore.latestStyle(mapStyle => this.onStyleChanged(mapStyle))
|
|
|
|
})
|
|
|
|
}
|
2016-12-16 14:49:25 +01:00
|
|
|
|
|
|
|
this.state = {
|
2016-12-30 13:21:03 +01:00
|
|
|
errors: [],
|
2016-12-31 14:32:04 +01:00
|
|
|
infos: [],
|
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: {},
|
2017-01-08 22:03:21 +01:00
|
|
|
inspectModeEnabled: false,
|
2017-01-10 11:13:53 +01:00
|
|
|
spec: GlSpec,
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
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-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-22 21:06:32 +01:00
|
|
|
saveStyle(snapshotStyle) {
|
2016-12-16 14:49:25 +01:00
|
|
|
this.styleStore.save(snapshotStyle)
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:13:53 +01:00
|
|
|
updateFonts(urlTemplate) {
|
2017-01-13 15:31:28 +01:00
|
|
|
const metadata = this.state.mapStyle.metadata || {}
|
|
|
|
const accessToken = metadata['maputnik:openmaptiles_access_token'] || tokens.openmaptiles
|
|
|
|
downloadGlyphsMetadata(urlTemplate.replace('{key}', accessToken), fonts => {
|
2017-01-10 11:13:53 +01:00
|
|
|
this.setState({ spec: updateRootSpec(this.state.spec, 'glyphs', fonts)})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
updateIcons(baseUrl) {
|
|
|
|
downloadSpriteMetadata(baseUrl, icons => {
|
|
|
|
this.setState({ spec: updateRootSpec(this.state.spec, 'sprite', icons)})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-01 14:49:32 +01:00
|
|
|
onStyleChanged(newStyle, save=true) {
|
2017-01-10 11:13:53 +01:00
|
|
|
if(newStyle.glyphs !== this.state.mapStyle.glyphs) {
|
|
|
|
this.updateFonts(newStyle.glyphs)
|
|
|
|
}
|
|
|
|
if(newStyle.sprite !== this.state.mapStyle.sprite) {
|
|
|
|
this.updateIcons(newStyle.sprite)
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:00:49 +01:00
|
|
|
const errors = validateStyleMin(newStyle, GlSpec)
|
|
|
|
if(errors.length === 0) {
|
|
|
|
this.revisionStore.addRevision(newStyle)
|
2017-01-01 14:49:32 +01:00
|
|
|
if(save) this.saveStyle(newStyle)
|
2016-12-30 13:21:03 +01:00
|
|
|
this.setState({
|
|
|
|
mapStyle: newStyle,
|
|
|
|
errors: [],
|
|
|
|
})
|
2016-12-29 22:00:49 +01:00
|
|
|
} else {
|
2016-12-30 13:21:03 +01:00
|
|
|
this.setState({
|
|
|
|
errors: errors.map(err => err.message)
|
|
|
|
})
|
2016-12-29 22:00:49 +01:00
|
|
|
}
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-23 17:17:02 +01:00
|
|
|
onUndo() {
|
|
|
|
const activeStyle = this.revisionStore.undo()
|
2016-12-31 14:32:04 +01:00
|
|
|
const messages = undoMessages(this.state.mapStyle, activeStyle)
|
2016-12-23 17:17:02 +01:00
|
|
|
this.saveStyle(activeStyle)
|
2016-12-31 14:32:04 +01:00
|
|
|
this.setState({
|
|
|
|
mapStyle: activeStyle,
|
|
|
|
infos: messages,
|
|
|
|
})
|
2016-12-23 17:17:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onRedo() {
|
|
|
|
const activeStyle = this.revisionStore.redo()
|
2016-12-31 14:32:04 +01:00
|
|
|
const messages = redoMessages(this.state.mapStyle, activeStyle)
|
2016-12-23 17:17:02 +01:00
|
|
|
this.saveStyle(activeStyle)
|
2016-12-31 14:32:04 +01:00
|
|
|
this.setState({
|
|
|
|
mapStyle: activeStyle,
|
|
|
|
infos: messages,
|
|
|
|
})
|
2016-12-23 17:17:02 +01:00
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2017-01-08 22:03:21 +01:00
|
|
|
changeInspectMode() {
|
|
|
|
this.setState({
|
|
|
|
inspectModeEnabled: !this.state.inspectModeEnabled
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-17 14:53:16 +01:00
|
|
|
mapRenderer() {
|
2016-12-16 15:14:20 +01:00
|
|
|
const mapProps = {
|
2017-01-13 15:31:28 +01:00
|
|
|
mapStyle: style.replaceAccessToken(this.state.mapStyle),
|
2016-12-24 14:42:57 +01:00
|
|
|
onDataChange: (e) => {
|
|
|
|
this.layerWatcher.analyzeMap(e.map)
|
2016-12-24 15:14:31 +01:00
|
|
|
},
|
2016-12-16 15:14:20 +01:00
|
|
|
}
|
2016-12-20 16:08:49 +01:00
|
|
|
|
2017-01-13 15:31:28 +01:00
|
|
|
const metadata = this.state.mapStyle.metadata || {}
|
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-17 14:53:16 +01:00
|
|
|
} else {
|
2017-01-08 23:19:21 +01:00
|
|
|
return <MapboxGlMap {...mapProps}
|
|
|
|
inspectModeEnabled={this.state.inspectModeEnabled}
|
|
|
|
highlightedLayer={this.state.mapStyle.layers[this.state.selectedLayerIndex]} />
|
2016-12-17 14:53:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-28 17:08:42 +01:00
|
|
|
const metadata = this.state.mapStyle.metadata || {}
|
2016-12-20 14:50:38 +01:00
|
|
|
|
2016-12-20 16:37:35 +01:00
|
|
|
const toolbar = <Toolbar
|
|
|
|
mapStyle={this.state.mapStyle}
|
2017-01-11 16:23:33 +01:00
|
|
|
inspectModeEnabled={this.state.inspectModeEnabled}
|
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)}
|
2017-01-08 22:03:21 +01:00
|
|
|
onInspectModeToggle={this.changeInspectMode.bind(this)}
|
2016-12-20 16:37:35 +01:00
|
|
|
/>
|
|
|
|
|
|
|
|
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}
|
2017-01-11 15:59:51 +01:00
|
|
|
sources={this.state.sources}
|
2016-12-20 16:37:35 +01:00
|
|
|
/>
|
|
|
|
|
|
|
|
const layerEditor = selectedLayer ? <LayerEditor
|
|
|
|
layer={selectedLayer}
|
2016-12-29 15:22:47 +01:00
|
|
|
sources={this.state.sources}
|
|
|
|
vectorLayers={this.state.vectorLayers}
|
2017-01-10 11:13:53 +01:00
|
|
|
spec={this.state.spec}
|
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-31 14:32:04 +01:00
|
|
|
const bottomPanel = (this.state.errors.length + this.state.infos.length) > 0 ? <MessagePanel
|
|
|
|
errors={this.state.errors}
|
|
|
|
infos={this.state.infos}
|
2016-12-30 13:21:03 +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-30 13:21:03 +01:00
|
|
|
bottom={bottomPanel}
|
2016-12-20 16:37:35 +01:00
|
|
|
/>
|
2016-12-16 14:49:25 +01:00
|
|
|
}
|
2016-09-08 19:47:29 +02:00
|
|
|
}
|