maputnik/src/app.jsx

119 lines
3.1 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-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-09-10 22:08:26 +02:00
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-21 08:25:10 +02:00
if(this.state.currentStyle.get('layers').size === 0) {
loadDefaultStyle(mapStyle => this.onStyleUpload(mapStyle))
}
}
2016-09-21 08:34:48 +02:00
onReset() {
this.styleStore.purge()
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() {
const mapStyle = JSON.stringify(this.state.currentStyle.toJSON(), 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");
this.onStyleSave()
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())
this.setState({ currentStyle: snapshotStyle })
2016-09-21 08:25:10 +02:00
this.styleStore.save(snapshotStyle)
2016-09-10 14:47:06 +02:00
}
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
}
onOpenAbout() {
this.setState({ workContext: "about" })
}
2016-09-09 23:25:06 +02:00
onOpenLayers() {
this.setState({ workContext: "layers", })
2016-09-08 19:47:29 +02:00
}
2016-09-10 22:08:26 +02:00
onOpenSources() {
this.setState({ workContext: "sources", })
}
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)}
onOpenAbout={this.onOpenAbout.bind(this)}
2016-09-09 17:09:13 +02:00
onOpenLayers={this.onOpenLayers.bind(this)}
2016-09-10 22:08:26 +02:00
onOpenSources={this.onOpenSources.bind(this)}
2016-09-09 17:09:13 +02:00
/>
2016-09-09 23:25:06 +02:00
<WorkspaceDrawer
onStyleChanged={this.onStyleChanged.bind(this)}
2016-09-21 08:34:48 +02:00
onReset={this.onReset.bind(this)}
2016-09-09 23:25:06 +02:00
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
}