maputnik/src/components/modals/OpenModal.jsx

137 lines
3.7 KiB
React
Raw Normal View History

2016-12-22 11:27:53 +01:00
import React from 'react'
import Modal from './Modal'
import Heading from '../Heading'
import Button from '../Button'
import Paragraph from '../Paragraph'
import FileReaderInput from 'react-file-reader-input'
2016-12-22 16:35:31 +01:00
import request from 'request'
2016-12-22 11:27:53 +01:00
import FileUploadIcon from 'react-icons/lib/md/file-upload'
import AddIcon from 'react-icons/lib/md/add-circle-outline'
2016-12-22 20:06:48 +01:00
import style from '../../libs/style.js'
2016-12-22 11:27:53 +01:00
import colors from '../../config/colors'
import { margins, fontSizes } from '../../config/scales'
import publicStyles from '../../config/styles.json'
class PublicStyle extends React.Component {
static propTypes = {
url: React.PropTypes.string.isRequired,
thumbnailUrl: React.PropTypes.string.isRequired,
title: React.PropTypes.string.isRequired,
onSelect: React.PropTypes.func.isRequired,
}
render() {
return <div style={{
verticalAlign: 'top',
marginTop: margins[2],
marginRight: margins[2],
backgroundColor: colors.gray,
display: 'inline-block',
width: 180,
fontSize: fontSizes[4],
color: colors.lowgray,
}}>
2016-12-22 16:35:31 +01:00
<Button
onClick={() => this.props.onSelect(this.props.url)}
style={{
backgroundColor: 'transparent',
padding: margins[2],
display: 'block',
}}
>
2016-12-22 11:27:53 +01:00
<div style={{
display: 'flex',
flexDirection: 'row',
}}>
<span style={{fontWeight: 700}}>{this.props.title}</span>
<span style={{flexGrow: 1}} />
<AddIcon />
</div>
<img
style={{
display: 'block',
marginTop: margins[1],
maxWidth: '100%',
}}
src={this.props.thumbnailUrl}
alt={this.props.title}
/>
</Button>
</div>
}
}
class OpenModal extends React.Component {
static propTypes = {
isOpen: React.PropTypes.bool.isRequired,
2016-12-22 16:35:31 +01:00
onOpenToggle: React.PropTypes.func.isRequired,
2016-12-22 11:27:53 +01:00
onStyleOpen: React.PropTypes.func.isRequired,
}
2016-12-22 16:35:31 +01:00
onStyleSelect(styleUrl) {
request({
url: styleUrl,
withCredentials: false,
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
2016-12-22 20:06:48 +01:00
const mapStyle = style.ensureMetadataExists(JSON.parse(body))
2016-12-22 16:35:31 +01:00
console.log('Loaded style ', mapStyle.id)
this.props.onStyleOpen(mapStyle)
} else {
console.warn('Could not open the style URL', styleUrl)
}
})
}
2016-12-22 11:27:53 +01:00
onUpload(_, files) {
const [e, file] = files[0];
const reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = e => {
let mapStyle = JSON.parse(e.target.result)
mapStyle = style.ensureMetadataExists(mapStyle)
this.props.onStyleOpen(mapStyle);
}
reader.onerror = e => console.log(e.target);
}
render() {
const styleOptions = publicStyles.map(style => {
return <PublicStyle
2016-12-22 16:35:31 +01:00
key={style.id}
2016-12-22 11:27:53 +01:00
url={style.url}
title={style.title}
thumbnailUrl={style.thumbnail}
2016-12-22 16:35:31 +01:00
onSelect={this.onStyleSelect.bind(this)}
2016-12-22 11:27:53 +01:00
/>
})
return <Modal
isOpen={this.props.isOpen}
2016-12-22 16:35:31 +01:00
onOpenToggle={this.props.onOpenToggle}
2016-12-22 11:27:53 +01:00
title={'Open Style'}
>
<Heading level={4}>Upload Style</Heading>
<Paragraph>
Upload a JSON style from your computer.
</Paragraph>
<FileReaderInput onChange={this.onUpload.bind(this)}>
<Button>
2016-12-22 16:35:31 +01:00
<FileUploadIcon />
Upload
2016-12-22 11:27:53 +01:00
</Button>
</FileReaderInput>
<Heading level={4}>Gallery Styles</Heading>
<Paragraph>
Open one of the publicly available styles to start from.
</Paragraph>
{styleOptions}
</Modal>
}
}
export default OpenModal