summaryrefslogtreecommitdiffstats
path: root/web/react/components/file_preview.jsx
blob: fdd12feec437f98cd7a0954914d9d7b78addcb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var UserStore = require('../stores/user_store.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var client = require('../utils/client.jsx');
var utils = require('../utils/utils.jsx');
var Constants = require('../utils/constants.jsx');

module.exports = React.createClass({
    handleRemove: function(e) {
        var previewDiv = e.target.parentNode.parentNode;
        this.props.onRemove(previewDiv.getAttribute('data-filename'));
    },
    render: function() {
        var previews = [];
        this.props.files.forEach(function(filename) {

            var originalFilename = filename;
            var filenameSplit = filename.split('.');
            var ext = filenameSplit[filenameSplit.length-1];
            var type = utils.getFileType(ext);
            // This is a temporary patch to fix issue with old files using absolute paths
            if (filename.indexOf("/api/v1/files/get") != -1) {
                filename = filename.split("/api/v1/files/get")[1];
            }
            filename = window.location.origin + "/api/v1/files/get" + filename;

            if (type === "image") {
                previews.push(
                    <div key={filename} className="preview-div" data-filename={originalFilename}>
                        <img className="preview-img" src={filename}/>
                        <a className="remove-preview" onClick={this.handleRemove}><i className="glyphicon glyphicon-remove"/></a>
                    </div>
                );
            } else {
                previews.push(
                    <div key={filename} className="preview-div custom-file" data-filename={originalFilename}>
                        <div className={"file-icon "+utils.getIconClassName(type)}/>
                        <a className="remove-preview" onClick={this.handleRemove}><i className="glyphicon glyphicon-remove"/></a>
                    </div>
                );
            }
        }.bind(this));

        for (var i = 0; i < this.props.uploadsInProgress; i++) {
            previews.push(
                <div className="preview-div">
                    <img className="spinner" src="/static/images/load.gif"/>
                </div>
            );
        }

        return (
            <div className="preview-container">
                {previews}
            </div>
        );
    }
});