2016-05-30 16:55:32 -07:00
|
|
|
import React from 'react';
|
2016-06-07 21:27:34 -03:00
|
|
|
import { truncateMiddle } from '../../lib/textHelper';
|
2016-06-06 21:53:22 -03:00
|
|
|
import ImageProxy from '../../valueObjects/ImageProxy';
|
2016-05-30 16:55:32 -07:00
|
|
|
|
2016-06-07 20:15:28 -03:00
|
|
|
const MAX_DISPLAY_LENGTH = 50;
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
export default class ImageControl extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-06-06 10:40:11 -03:00
|
|
|
|
|
|
|
this.state = {
|
2016-06-07 21:04:40 -03:00
|
|
|
currentImage: props.value ? new ImageProxy(props.value, null, true) : null
|
2016-06-06 10:40:11 -03:00
|
|
|
};
|
|
|
|
|
2016-06-07 11:38:19 -03:00
|
|
|
this.revokeCurrentImage = this.revokeCurrentImage.bind(this);
|
2016-05-30 16:55:32 -07:00
|
|
|
this.handleChange = this.handleChange.bind(this);
|
2016-06-05 23:22:12 -03:00
|
|
|
this.handleFileInputRef = this.handleFileInputRef.bind(this);
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleDragEnter = this.handleDragEnter.bind(this);
|
|
|
|
this.handleDragOver = this.handleDragOver.bind(this);
|
2016-06-06 10:40:11 -03:00
|
|
|
this.renderImageName = this.renderImageName.bind(this);
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:38:19 -03:00
|
|
|
revokeCurrentImage() {
|
2016-06-07 20:15:28 -03:00
|
|
|
if (this.state.currentImage && !this.state.currentImage.uploaded) {
|
|
|
|
this.props.onRemoveMedia(this.state.currentImage);
|
2016-06-06 10:40:11 -03:00
|
|
|
}
|
2016-06-05 23:22:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleFileInputRef(el) {
|
|
|
|
this._fileInput = el;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(e) {
|
|
|
|
this._fileInput.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDragEnter(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDragOver(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(e) {
|
2016-06-05 23:22:12 -03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-06-07 11:38:19 -03:00
|
|
|
this.revokeCurrentImage();
|
2016-06-06 21:53:22 -03:00
|
|
|
let imageRef = null;
|
2016-06-05 23:22:12 -03:00
|
|
|
const fileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
|
|
|
|
const files = [...fileList];
|
|
|
|
const imageType = /^image\//;
|
|
|
|
|
2016-06-05 23:37:19 -03:00
|
|
|
// Iterate through the list of files and return the first image on the list
|
2016-06-05 23:22:12 -03:00
|
|
|
const file = files.find((currentFile) => {
|
|
|
|
if (imageType.test(currentFile.type)) {
|
|
|
|
return currentFile;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-06 10:40:11 -03:00
|
|
|
if (file) {
|
2016-06-08 04:42:24 -03:00
|
|
|
imageRef = new ImageProxy(file.name, window.URL.createObjectURL(file, {oneTimeOnly: true}));
|
|
|
|
this.props.onAddMedia(file);
|
2016-06-06 10:40:11 -03:00
|
|
|
}
|
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
this.props.onChange(imageRef);
|
2016-06-07 20:15:28 -03:00
|
|
|
this.setState({currentImage: imageRef});
|
2016-06-06 10:40:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderImageName() {
|
2016-06-07 20:15:28 -03:00
|
|
|
if (!this.state.currentImage) return null;
|
2016-06-07 21:33:12 -03:00
|
|
|
return truncateMiddle(this.state.currentImage.uri, MAX_DISPLAY_LENGTH);
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-06-06 10:40:11 -03:00
|
|
|
const imageName = this.renderImageName();
|
2016-06-05 23:22:12 -03:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
onDragEnter={this.handleDragEnter}
|
|
|
|
onDragOver={this.handleDragOver}
|
|
|
|
onDrop={this.handleChange}
|
|
|
|
>
|
2016-06-06 10:40:11 -03:00
|
|
|
<span onClick={this.handleClick}>
|
|
|
|
{imageName ? imageName : 'Click or drop imag here.'}
|
|
|
|
</span>
|
2016-06-05 23:22:12 -03:00
|
|
|
<input
|
|
|
|
type="file"
|
|
|
|
accept="image/*"
|
|
|
|
onChange={this.handleChange}
|
|
|
|
style={styles.input}
|
|
|
|
ref={this.handleFileInputRef}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 23:22:12 -03:00
|
|
|
|
|
|
|
const styles = {
|
|
|
|
input: {
|
|
|
|
display: 'none'
|
|
|
|
}
|
|
|
|
};
|