2016-06-16 19:20:36 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-06-07 21:27:34 -03:00
|
|
|
import { truncateMiddle } from '../../lib/textHelper';
|
2017-01-10 22:23:22 -02:00
|
|
|
import { Loader } from '../UI';
|
|
|
|
import AssetProxy, { createAssetProxy } from '../../valueObjects/AssetProxy';
|
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 {
|
2017-01-10 22:23:22 -02:00
|
|
|
state = {
|
|
|
|
processing: false,
|
|
|
|
};
|
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
handleFileInputRef = (el) => {
|
2016-06-05 23:22:12 -03:00
|
|
|
this._fileInput = el;
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-06-05 23:22:12 -03:00
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
handleClick = (e) => {
|
2016-06-05 23:22:12 -03:00
|
|
|
this._fileInput.click();
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-06-05 23:22:12 -03:00
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
handleDragEnter = (e) => {
|
2016-06-05 23:22:12 -03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-06-05 23:22:12 -03:00
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
handleDragOver = (e) => {
|
2016-06-05 23:22:12 -03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-05-30 16:55:32 -07:00
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
handleChange = (e) => {
|
2016-06-05 23:22:12 -03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-06-10 14:01:14 -03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
this.props.onRemoveAsset(this.props.value);
|
2016-06-06 10:40:11 -03:00
|
|
|
if (file) {
|
2017-01-10 22:23:22 -02:00
|
|
|
this.setState({ processing: true });
|
|
|
|
createAssetProxy(file.name, file)
|
|
|
|
.then((assetProxy) => {
|
|
|
|
this.setState({ processing: false });
|
|
|
|
this.props.onAddAsset(assetProxy);
|
|
|
|
this.props.onChange(assetProxy.public_path);
|
|
|
|
});
|
2016-06-10 00:16:01 -03:00
|
|
|
} else {
|
|
|
|
this.props.onChange(null);
|
2016-06-06 10:40:11 -03:00
|
|
|
}
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-06-06 10:40:11 -03:00
|
|
|
|
2016-10-03 14:25:27 +02:00
|
|
|
renderImageName = () => {
|
2016-06-10 14:01:14 -03:00
|
|
|
if (!this.props.value) return null;
|
2017-01-10 22:23:22 -02:00
|
|
|
if (this.value instanceof AssetProxy) {
|
2016-07-19 17:11:22 -03:00
|
|
|
return truncateMiddle(this.props.value.path, MAX_DISPLAY_LENGTH);
|
2016-06-10 14:01:14 -03:00
|
|
|
} else {
|
|
|
|
return truncateMiddle(this.props.value, MAX_DISPLAY_LENGTH);
|
|
|
|
}
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-05-30 16:55:32 -07:00
|
|
|
|
|
|
|
render() {
|
2017-01-10 22:23:22 -02:00
|
|
|
const { processing } = this.state;
|
2016-06-06 10:40:11 -03:00
|
|
|
const imageName = this.renderImageName();
|
2017-01-10 22:23:22 -02:00
|
|
|
if (processing) {
|
|
|
|
return (
|
|
|
|
<div style={styles.imageUpload}>
|
|
|
|
<span style={styles.message}>
|
|
|
|
<Loader active />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-06-05 23:22:12 -03:00
|
|
|
return (
|
|
|
|
<div
|
2017-01-10 22:23:22 -02:00
|
|
|
style={styles.imageUpload}
|
2016-10-03 16:57:48 +02:00
|
|
|
onDragEnter={this.handleDragEnter}
|
|
|
|
onDragOver={this.handleDragOver}
|
|
|
|
onDrop={this.handleChange}
|
2016-06-05 23:22:12 -03:00
|
|
|
>
|
2017-01-10 22:23:22 -02:00
|
|
|
<span style={styles.message} onClick={this.handleClick}>
|
2016-08-31 13:08:14 -07:00
|
|
|
{imageName ? imageName : 'Tip: Click here to upload an image from your file browser, or drag an image directly into this box from your desktop'}
|
2016-06-06 10:40:11 -03:00
|
|
|
</span>
|
2016-06-05 23:22:12 -03:00
|
|
|
<input
|
2016-10-03 16:57:48 +02:00
|
|
|
type="file"
|
|
|
|
accept="image/*"
|
|
|
|
onChange={this.handleChange}
|
|
|
|
style={styles.input}
|
|
|
|
ref={this.handleFileInputRef}
|
2016-06-05 23:22:12 -03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 23:22:12 -03:00
|
|
|
|
|
|
|
const styles = {
|
|
|
|
input: {
|
2016-10-18 12:32:39 -02:00
|
|
|
display: 'none',
|
2016-08-31 12:11:14 -07:00
|
|
|
},
|
2017-01-10 22:23:22 -02:00
|
|
|
message: {
|
|
|
|
padding: '20px',
|
|
|
|
display: 'block',
|
|
|
|
fontSize: '12px',
|
|
|
|
},
|
2016-08-31 12:11:14 -07:00
|
|
|
imageUpload: {
|
2016-08-31 13:06:23 -07:00
|
|
|
backgroundColor: '#fff',
|
2016-08-31 12:11:14 -07:00
|
|
|
textAlign: 'center',
|
2016-08-31 13:08:14 -07:00
|
|
|
color: '#999',
|
2016-08-31 13:06:23 -07:00
|
|
|
border: '1px dashed #eee',
|
|
|
|
cursor: 'pointer',
|
2016-10-18 12:32:39 -02:00
|
|
|
},
|
2016-06-05 23:22:12 -03:00
|
|
|
};
|
2016-06-16 19:20:36 -03:00
|
|
|
|
|
|
|
ImageControl.propTypes = {
|
2017-01-10 22:23:22 -02:00
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
2016-06-16 19:20:36 -03:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-01-10 22:23:22 -02:00
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
2016-06-16 19:20:36 -03:00
|
|
|
value: PropTypes.node,
|
|
|
|
};
|