maputnik/src/toolbar.jsx

134 lines
3.6 KiB
React
Raw Normal View History

2016-09-08 19:47:29 +02:00
import React from 'react';
import FileReaderInput from 'react-file-reader-input';
2016-09-08 19:47:29 +02:00
2016-09-15 09:13:23 +02:00
import Button from 'rebass/dist/Button'
import Text from 'rebass/dist/Text'
import Menu from 'rebass/dist/Menu'
import NavItem from 'rebass/dist/NavItem'
import Tooltip from 'rebass/dist/Tooltip'
import Container from 'rebass/dist/Container'
import Block from 'rebass/dist/Block'
import Fixed from 'rebass/dist/Fixed'
2016-09-10 14:47:06 +02:00
import MdFileDownload from 'react-icons/lib/md/file-download'
import MdFileUpload from 'react-icons/lib/md/file-upload'
import MdSettings from 'react-icons/lib/md/settings'
import MdInfo from 'react-icons/lib/md/info'
2016-09-10 14:47:06 +02:00
import MdLayers from 'react-icons/lib/md/layers'
import MdSave from 'react-icons/lib/md/save'
2016-09-10 22:08:26 +02:00
import MdMap from 'react-icons/lib/md/map'
2016-09-14 20:22:32 +02:00
import style from './style.js'
2016-09-10 16:11:43 +02:00
import { fullHeight } from './theme.js'
import theme from './theme.js';
2016-09-08 19:47:29 +02:00
export class Toolbar extends React.Component {
2016-09-09 16:58:48 +02:00
static propTypes = {
2016-09-10 15:15:17 +02:00
// A new style has been uploaded
onStyleUpload: React.PropTypes.func.isRequired,
// Current style is requested for download
onStyleDownload: React.PropTypes.func.isRequired,
// Style is explicitely saved to local cache
onStyleSave: React.PropTypes.func,
// Open settings drawer
onOpenSettings: React.PropTypes.func,
// Open about page
onOpenAbout: React.PropTypes.func,
2016-09-10 22:08:26 +02:00
// Open sources drawer
onOpenSources: React.PropTypes.func,
2016-09-10 15:15:17 +02:00
// Open layers drawer
onOpenLayers: React.PropTypes.func,
2016-09-10 14:56:59 +02:00
// Whether a style is available for download or saving
// A style with no layers should not be available
2016-09-10 15:15:17 +02:00
styleAvailable: React.PropTypes.bool,
}
2016-09-09 16:58:48 +02:00
2016-09-08 20:35:21 +02:00
onUpload(_, files) {
const [e, file] = files[0];
const reader = new FileReader();
2016-09-10 15:15:17 +02:00
reader.readAsText(file, "UTF-8");
reader.onload = e => {
2016-09-21 08:25:10 +02:00
let mapStyle = style.fromJSON(JSON.parse(e.target.result))
mapStyle = style.ensureMetadataExists(mapStyle)
this.props.onStyleUpload(mapStyle);
2016-09-08 20:35:21 +02:00
}
2016-09-10 15:15:17 +02:00
reader.onerror = e => console.log(e.target);
2016-09-08 20:35:21 +02:00
}
2016-09-10 14:56:59 +02:00
saveButton() {
if(this.props.styleAvailable) {
return <Block>
<Button onClick={this.props.onStyleSave} big={true}>
<Tooltip inverted rounded title="Save style">
<MdSave />
</Tooltip>
</Button>
</Block>
}
return null
}
downloadButton() {
if(this.props.styleAvailable) {
return <Block>
2016-09-09 16:58:48 +02:00
<Button onClick={this.props.onStyleDownload} big={true}>
<Tooltip inverted rounded title="Download style">
<MdFileDownload />
</Tooltip>
</Button>
</Block>
}
2016-09-10 14:56:59 +02:00
return null
}
2016-09-09 16:58:48 +02:00
2016-09-10 14:56:59 +02:00
render() {
2016-09-08 19:47:29 +02:00
return <Container style={{
2016-09-10 16:11:43 +02:00
...fullHeight,
2016-09-08 19:47:29 +02:00
zIndex: 100,
2016-09-10 16:11:43 +02:00
left: 0,
top: 0,
2016-09-08 19:47:29 +02:00
backgroundColor: theme.colors.black }
}>
<Block>
2016-09-10 15:15:17 +02:00
<FileReaderInput onChange={this.onUpload.bind(this)}>
2016-09-10 14:56:59 +02:00
<Button big={true} theme={this.props.styleAvailable ? "default" : "success"}>
2016-09-09 16:58:48 +02:00
<Tooltip inverted rounded title="Upload style">
2016-09-10 15:15:17 +02:00
<MdFileUpload />
2016-09-08 20:35:21 +02:00
</Tooltip>
</Button>
2016-09-10 15:15:17 +02:00
</FileReaderInput>
2016-09-08 19:47:29 +02:00
</Block>
2016-09-10 14:56:59 +02:00
{this.downloadButton()}
{this.saveButton()}
2016-09-08 19:47:29 +02:00
<Block>
2016-09-09 17:09:13 +02:00
<Button big={true} onClick={this.props.onOpenLayers}>
2016-09-08 19:47:29 +02:00
<Tooltip inverted rounded title="Layers">
<MdLayers />
2016-09-10 15:15:17 +02:00
</Tooltip>
2016-09-09 17:09:13 +02:00
</Button>
</Block>
2016-09-10 22:08:26 +02:00
<Block>
<Button big={true} onClick={this.props.onOpenSources}>
<Tooltip inverted rounded title="Sources">
<MdMap />
</Tooltip>
</Button>
</Block>
2016-09-09 17:09:13 +02:00
<Block>
<Button big={true} onClick={this.props.onOpenSettings}>
<Tooltip inverted rounded title="Settings">
<MdSettings />
2016-09-10 15:15:17 +02:00
</Tooltip>
2016-09-08 19:47:29 +02:00
</Button>
</Block>
<Block>
<Button big={true} onClick={this.props.onOpenAbout}>
<Tooltip inverted rounded title="About">
<MdInfo />
</Tooltip>
</Button>
</Block>
2016-09-08 19:47:29 +02:00
</Container>
}
}