-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpload.js
More file actions
203 lines (178 loc) · 4.64 KB
/
Upload.js
File metadata and controls
203 lines (178 loc) · 4.64 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict'
import classnames from 'classnames'
import React from 'react'
import Events from './utils/events'
import { nextUid, format } from './utils/strings'
import getGrid from './higherorder/grid'
import Message from './Message'
import upload from './utils/upload'
import { requireCss } from './themes'
requireCss('upload')
import { getLang, setLang } from './lang'
setLang('validation', 'buttons')
@getGrid
export default class Upload extends React.Component {
static displayName = 'Upload'
static propTypes = {
accept: React.PropTypes.string,
action: React.PropTypes.string.isRequired,
autoUpload: React.PropTypes.bool,
className: React.PropTypes.string,
content: React.PropTypes.object,
cors: React.PropTypes.bool,
disabled: React.PropTypes.bool,
fileSize: React.PropTypes.number,
limit: React.PropTypes.number,
name: React.PropTypes.string.isRequired,
readOnly: React.PropTypes.bool,
style: React.PropTypes.object,
withCredentials: React.PropTypes.bool
}
static defaultProps = {
autoUpload: false,
cors: true,
fileSize: 4096,
limit: 1,
withCredentials: false
}
state = {
files: {}
}
isCompleted () {
let completed = true,
files = this.state.files
Object.keys(files).forEach(id => {
if (files[id].status !== 2) {
completed = false
}
})
return completed
}
getValue () {
let values = [],
files = this.state.files
Object.keys(files).forEach(id => {
if (this.props.autoUpload) {
values.push(files[id].value)
} else {
values.push(files[id].file.files[0])
}
})
return values
}
// nope
setValue() {}
addFile () {
if (this.props.disabled || this.props.readOnly) {
return
}
let files = this.state.files,
file = document.createElement('input'),
autoUpload = this.props.autoUpload
file.type = 'file'
file.accept = this.props.accept
file.click()
Events.on(file, 'change', () => {
let blob = file.files[0]
if (blob.size / 1024 > this.props.fileSize) {
Message.show(format(getLang('validation.tips.fileSize'), this.props.fileSize), 'error')
return
}
let id = nextUid()
files[id] = {
file,
name: file.files[0].name,
status: autoUpload ? 1 : 0
}
if (autoUpload) {
files[id].xhr = this.uploadFile(file, id)
}
this.setState({ files })
})
}
removeFile (id) {
if (this.props.disabled || this.props.readOnly) {
return
}
let files = this.state.files
let file = files[id]
if (file.xhr) {
file.xhr.abort()
}
delete files[id]
this.setState({ files })
}
uploadFile (file, id) {
return upload({
url: this.props.action,
name: this.props.name,
cors: this.props.cors,
withCredentials: this.props.withCredentials,
file: file.files[0],
onProgress: (e) => {
let progress = this.refs[id]
progress.style.width = (e.loaded / e.total) * 100 + '%'
},
onLoad: (e) => {
let files = this.state.files
files[id].status = 2
files[id].value = e.currentTarget.responseText
this.setState({ files })
},
onError: () => {
let files = this.state.files
files[id].status = 3
this.setState({ files })
}
})
}
start () {
let files = this.state.files
Object.keys(files).forEach(id => {
this.uploadFile(files[id].file, id)
})
}
renderFiles () {
let files = this.state.files
return Object.keys(files).map((id, i) => {
let file = this.state.files[id]
let className = classnames(
`rct-file`,
{
'uploaded': file.status === 2,
'has-error': file.status === 3
}
)
return (
<div key={i}>
<div className={className}>
<span>{file.name}</span>
<a className="remove" onClick={this.removeFile.bind(this, id)}>× {getLang('buttons.cancel')}</a>
</div>
<div ref={id} className={`rct-upload-progress`}></div>
</div>
)
})
}
render () {
let className = classnames(
this.getGrid(),
`rct-upload-container`,
this.props.className
)
return (
<div className={className} style={this.props.style}>
{ Object.keys(this.state.files).length < this.props.limit && <div onClick={this.addFile.bind(this)}>{this.props.content}</div> }
{ this.renderFiles() }
</div>
)
}
}
require('./FormControl').register(
'upload',
function (props) {
return <Upload {...props} />
},
Upload,
'array'
)