summaryrefslogtreecommitdiffstats
path: root/web/react/components/file_preview.jsx
blob: b8c86ed6732bf7f96cd112132ed99ac6c4c7dcb2 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

var Utils = require('../utils/utils.jsx');

export default class FilePreview extends React.Component {
    constructor(props) {
        super(props);

        this.handleRemove = this.handleRemove.bind(this);
    }

    componentDidUpdate() {
        if (this.props.uploadsInProgress.length > 0) {
            ReactDOM.findDOMNode(this.refs[this.props.uploadsInProgress[0]]).scrollIntoView();
        }
    }

    handleRemove(e) {
        var previewDiv = e.target.parentNode.parentNode;

        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'));
        }
    }

    render() {
        var previews = [];
        this.props.files.forEach((fullFilename) => {
            var filename = fullFilename;
            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 = Utils.getWindowLocationOrigin() + '/api/v1/files/get' + filename + '?' + Utils.getSessionIndex();

            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>
                );
            }
        });

        this.props.uploadsInProgress.forEach((clientId) => {
            previews.push(
                <div
                    ref={clientId}
                    key={clientId}
                    className='preview-div'
                    data-client-id={clientId}
                >
                    <img
                        className='spinner'
                        src='/static/images/load.gif'
                    />
                    <a
                        className='remove-preview'
                        onClick={this.handleRemove}
                    >
                        <i className='glyphicon glyphicon-remove'/>
                    </a>
                </div>
            );
        });

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

FilePreview.defaultProps = {
    files: [],
    uploadsInProgress: []
};
FilePreview.propTypes = {
    onRemove: React.PropTypes.func.isRequired,
    files: React.PropTypes.array,
    uploadsInProgress: React.PropTypes.array
};