This repository was archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachmentsForm.js
More file actions
162 lines (139 loc) · 4.3 KB
/
AttachmentsForm.js
File metadata and controls
162 lines (139 loc) · 4.3 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use strict";
var _ = require("lodash");
var React = require("react");
var classNames = require("classnames");
var filesize = require("filesize");
var FileItem = require("./FileItem");
var EditableList = require("./EditableList");
/**
* AttachmentsForm
*
* @namespace components
* @class AttachmentsForm
* @constructor
* @param {Object} props
* @param {Number} [props] maxSize Max size for a single file to be uploaded
*/
var AttachmentsForm = React.createClass({
propTypes: {
maxSize: React.PropTypes.number
},
getDefaultProps: function() {
// 50mb
return { maxSize: 5e+7 };
},
getInitialState: function() {
return {
files: []
};
},
/**
* @method isUnderSizeLimit
* @param {HTML5 File Object} file
* @return Boolean
*/
isUnderSizeLimit: function(file) {
return file.size < this.props.maxSize;
},
/**
* Get files selected by the user
*
* @method getFiles
* @return {Array} of HTML 5 File objects
*/
getFiles: function() {
return this.state.files.filter(this.isUnderSizeLimit);
},
/**
* Clear selected files
*
* @method clear
*/
clear: function() {
this.setState({ files: [] });
},
/**
* Copy HTML 5 file objects from the file input to the component state
*
* @method handleFileChange
*/
handleFileChange: function(e) {
this.setState({
files: _.uniq(this.state.files.concat(_.toArray(e.target.files)), toUniqueId)
});
// http://stackoverflow.com/a/13351234/153718
this.refs.form.reset();
},
/**
* @method removeFile
* @param {HTML5 File Object} file Remove given file from the component
* state
*/
removeFile: function(file) {
this.setState({
files: this.state.files.filter(function(current) {
return current !== file;
})
});
},
/**
* Open file section dialog
*
* @method openFileDialog
*/
openFileDialog: function(e) {
if (e) e.preventDefault();
this.refs.file.click();
},
render: function() {
var self = this;
var files = this.state.files;
var totalSize = files.reduce(function(total, f) {
return total + f.size;
}, 0);
return (
<div className="AttachmentsForm" style={{ display: "block" }}>
<form ref="form" className="AttachmentsForm-form" style={{ display: "none" }}>
<input className="AttachmentsForm-file-input" type="file" ref="file" multiple onChange={self.handleFileChange} />
</form>
<EditableList>
{files.map(function(f) {
var isTooLarge = !self.isUnderSizeLimit(f);
var classes = classNames({
"too-large": isTooLarge
});
return <EditableList.Item key={toUniqueId(f)} onRemove={self.removeFile.bind(self, f)} >
<span className={classes} >
<FileItem mime={f.type} name={f.name} size={f.size} />
</span>
{isTooLarge &&
<div className="size-error-message">
Tiedosto on liian suuri ladattavaksi.
Suurin mahdollinen koko on {filesize(self.props.maxSize)}
</div>}
</EditableList.Item>;
})}
{files.length > 1 && <EditableList.Item permanent className="total">
Yhteensä {filesize(totalSize)}
</EditableList.Item>}
</EditableList>
<a className="select-button" href="#" onClick={this.openFileDialog}>Liitä tiedosto...</a>
</div>
);
}
});
/**
* Figure out an unique id for a HTML 5 File Object
*
* Not perfect...
*
* @static
* @private
* @method toUniqueId
* @param {HTML5 File Object} file
* @return {String}
*/
function toUniqueId(file) {
return file.name + (file.lastModifiedDate || "") + String(file.size);
}
module.exports = AttachmentsForm;