maputnik/src/app.jsx

100 lines
2.6 KiB
React
Raw Normal View History

import React from 'react'
2016-09-09 16:58:48 +02:00
import {saveAs} from 'file-saver'
2016-09-08 19:47:29 +02:00
import { Drawer, Container, Block, Fixed } from 'rebass'
import {Map} from './map.jsx'
import {Toolbar} from './toolbar.jsx'
2016-09-09 15:49:23 +02:00
import { StyleManager } from './style.js'
2016-09-10 16:37:41 +02:00
import { loadDefaultStyle, SettingsStore, StyleStore } from './stylestore.js'
2016-09-09 17:09:13 +02:00
import { WorkspaceDrawer } from './workspace.jsx'
import theme from './theme.js'
2015-06-15 15:21:19 +02:00
export default class App extends React.Component {
2016-09-10 15:15:17 +02:00
static childContextTypes = {
rebass: React.PropTypes.object,
2016-09-08 19:47:29 +02:00
reactIconBase: React.PropTypes.object
2016-09-10 15:15:17 +02:00
}
2016-09-08 19:47:29 +02:00
constructor(props) {
super(props)
this.styleStore = new StyleStore()
2016-09-10 16:26:39 +02:00
this.settingsStore = new SettingsStore()
this.state = {
2016-09-10 16:26:39 +02:00
accessToken: this.settingsStore.accessToken,
2016-09-09 17:09:13 +02:00
workContext: "layers",
currentStyle: this.styleStore.latestStyle(),
}
2016-09-10 16:37:41 +02:00
loadDefaultStyle(mapStyle => {
this.onStyleUpload(mapStyle)
})
}
2016-09-10 15:15:17 +02:00
getChildContext() {
return {
2016-09-09 23:25:06 +02:00
rebass: theme,
reactIconBase: { size: 20 }
}
}
2016-09-09 16:58:48 +02:00
onStyleDownload() {
2016-09-10 15:15:17 +02:00
const mapStyle = JSON.stringify(this.state.currentStyle.toJS(), null, 4)
2016-09-09 16:58:48 +02:00
const blob = new Blob([mapStyle], {type: "application/json;charset=utf-8"});
2016-09-09 23:25:06 +02:00
saveAs(blob, mapStyle.id + ".json");
2016-09-10 16:37:41 +02:00
this.onStyleSave(mapStyle)
2016-09-09 16:58:48 +02:00
}
2016-09-09 15:49:23 +02:00
onStyleUpload(newStyle) {
const savedStyle = this.styleStore.save(newStyle)
this.setState({ currentStyle: savedStyle })
}
2016-09-10 14:47:06 +02:00
onStyleSave() {
const snapshotStyle = this.state.currentStyle.set('modified', new Date().toJSON())
const savedStyle = this.styleStore.save(snapshotStyle)
this.setState({ currentStyle: savedStyle })
}
2016-09-09 23:25:06 +02:00
onStyleChanged(newStyle) {
this.setState({ currentStyle: newStyle })
2016-09-09 17:09:13 +02:00
}
2016-09-09 23:25:06 +02:00
onOpenSettings() {
2016-09-10 15:15:17 +02:00
this.setState({ workContext: "settings" })
2016-09-09 17:09:13 +02:00
}
2016-09-09 23:25:06 +02:00
onOpenLayers() {
this.setState({ workContext: "layers", })
2016-09-08 19:47:29 +02:00
}
2016-09-10 16:26:39 +02:00
onAccessTokenChanged(newToken) {
this.settingsStore.accessToken = newToken
this.setState({ accessToken: newToken })
}
2016-09-10 15:15:17 +02:00
render() {
return <div style={{ fontFamily: theme.fontFamily, color: theme.color, fontWeight: 300 }}>
2016-09-09 17:09:13 +02:00
<Toolbar
2016-09-10 14:56:59 +02:00
styleAvailable={this.state.currentStyle.get('layers').size > 0}
2016-09-10 14:47:06 +02:00
onStyleSave={this.onStyleSave.bind(this)}
2016-09-09 17:09:13 +02:00
onStyleUpload={this.onStyleUpload.bind(this)}
onStyleDownload={this.onStyleDownload.bind(this)}
onOpenSettings={this.onOpenSettings.bind(this)}
onOpenLayers={this.onOpenLayers.bind(this)}
/>
2016-09-09 23:25:06 +02:00
<WorkspaceDrawer
onStyleChanged={this.onStyleChanged.bind(this)}
workContext={this.state.workContext}
mapStyle={this.state.currentStyle}
2016-09-10 16:26:39 +02:00
accessToken={this.state.accessToken}
onAccessTokenChanged={this.onAccessTokenChanged.bind(this)}
/>
<Map
mapStyle={this.state.currentStyle}
accessToken={this.state.accessToken}
2016-09-09 23:25:06 +02:00
/>
</div>
2016-09-10 15:15:17 +02:00
}
2016-09-08 19:47:29 +02:00
}