2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2016-03-14 08:50:46 -04:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
import * as Utils from 'utils/utils.jsx';
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
|
|
import loadingGif from 'images/load.gif';
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
export default class FilePreview extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.handleRemove = this.handleRemove.bind(this);
|
2015-11-18 17:13:38 -05:00
|
|
|
}
|
2015-08-31 09:35:31 -07:00
|
|
|
|
2015-11-18 17:13:38 -05:00
|
|
|
componentDidUpdate() {
|
|
|
|
|
if (this.props.uploadsInProgress.length > 0) {
|
|
|
|
|
ReactDOM.findDOMNode(this.refs[this.props.uploadsInProgress[0]]).scrollIntoView();
|
|
|
|
|
}
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-18 17:13:38 -05:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
handleRemove(e) {
|
2015-06-14 23:53:32 -08:00
|
|
|
var previewDiv = e.target.parentNode.parentNode;
|
2015-08-10 12:05:45 -04:00
|
|
|
|
|
|
|
|
if (previewDiv.hasAttribute('data-filename')) {
|
|
|
|
|
this.props.onRemove(previewDiv.getAttribute('data-filename'));
|
|
|
|
|
} else if (previewDiv.hasAttribute('data-client-id')) {
|
|
|
|
|
this.props.onRemove(previewDiv.getAttribute('data-client-id'));
|
|
|
|
|
}
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-18 17:13:38 -05:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
render() {
|
2015-06-14 23:53:32 -08:00
|
|
|
var previews = [];
|
2015-11-18 17:13:38 -05:00
|
|
|
this.props.files.forEach((fullFilename) => {
|
2015-08-31 09:35:31 -07:00
|
|
|
var filename = fullFilename;
|
2015-07-22 08:01:10 -04:00
|
|
|
var originalFilename = filename;
|
2015-06-14 23:53:32 -08:00
|
|
|
var filenameSplit = filename.split('.');
|
2015-08-31 09:35:31 -07:00
|
|
|
var ext = filenameSplit[filenameSplit.length - 1];
|
|
|
|
|
var type = Utils.getFileType(ext);
|
|
|
|
|
|
2016-01-06 09:54:05 -05:00
|
|
|
filename = Utils.getFileUrl(filename);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
if (type === 'image') {
|
2015-06-14 23:53:32 -08:00
|
|
|
previews.push(
|
2015-08-31 09:35:31 -07:00
|
|
|
<div
|
|
|
|
|
key={filename}
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview'
|
2015-08-31 09:35:31 -07:00
|
|
|
data-filename={originalFilename}
|
|
|
|
|
>
|
|
|
|
|
<img
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview__image'
|
2015-08-31 09:35:31 -07:00
|
|
|
src={filename}
|
|
|
|
|
/>
|
|
|
|
|
<a
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview__remove'
|
2015-08-31 09:35:31 -07:00
|
|
|
onClick={this.handleRemove}
|
|
|
|
|
>
|
|
|
|
|
<i className='glyphicon glyphicon-remove'/>
|
|
|
|
|
</a>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
previews.push(
|
2015-08-31 09:35:31 -07:00
|
|
|
<div
|
|
|
|
|
key={filename}
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview custom-file'
|
2015-08-31 09:35:31 -07:00
|
|
|
data-filename={originalFilename}
|
|
|
|
|
>
|
|
|
|
|
<div className={'file-icon ' + Utils.getIconClassName(type)}/>
|
|
|
|
|
<a
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview__remove'
|
2015-08-31 09:35:31 -07:00
|
|
|
onClick={this.handleRemove}
|
|
|
|
|
>
|
|
|
|
|
<i className='glyphicon glyphicon-remove'/>
|
|
|
|
|
</a>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-11-18 17:13:38 -05:00
|
|
|
});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-11-18 17:13:38 -05:00
|
|
|
this.props.uploadsInProgress.forEach((clientId) => {
|
2015-06-14 23:53:32 -08:00
|
|
|
previews.push(
|
2015-08-31 09:35:31 -07:00
|
|
|
<div
|
2015-11-18 17:13:38 -05:00
|
|
|
ref={clientId}
|
2015-08-31 09:35:31 -07:00
|
|
|
key={clientId}
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview'
|
2015-08-31 09:35:31 -07:00
|
|
|
data-client-id={clientId}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
className='spinner'
|
2016-03-14 08:50:46 -04:00
|
|
|
src={loadingGif}
|
2015-08-31 09:35:31 -07:00
|
|
|
/>
|
|
|
|
|
<a
|
2016-03-14 22:35:57 +05:00
|
|
|
className='file-preview__remove'
|
2015-08-31 09:35:31 -07:00
|
|
|
onClick={this.handleRemove}
|
|
|
|
|
>
|
|
|
|
|
<i className='glyphicon glyphicon-remove'/>
|
|
|
|
|
</a>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
);
|
2015-11-18 17:13:38 -05:00
|
|
|
});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
return (
|
2016-03-14 22:35:57 +05:00
|
|
|
<div className='file-preview__container'>
|
2015-06-14 23:53:32 -08:00
|
|
|
{previews}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePreview.defaultProps = {
|
2015-11-18 17:13:38 -05:00
|
|
|
files: [],
|
|
|
|
|
uploadsInProgress: []
|
2015-08-31 09:35:31 -07:00
|
|
|
};
|
|
|
|
|
FilePreview.propTypes = {
|
|
|
|
|
onRemove: React.PropTypes.func.isRequired,
|
|
|
|
|
files: React.PropTypes.array,
|
|
|
|
|
uploadsInProgress: React.PropTypes.array
|
|
|
|
|
};
|