-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathUploadTool.js
60 lines (52 loc) · 1.36 KB
/
UploadTool.js
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
import { domHelpers } from 'substance'
import { Tool } from '../../kit'
export default class UploadTool extends Tool {
// In addition to the regular button a file input is rendered
// which is used to trigger the browser's file dialog.
render ($$) {
let el = super.render($$)
const isMultiple = this.canUploadMultiple
const input = $$('input').attr({
'type': 'file'
}).ref('input')
.on('change', this.onFileSelect)
// ATTENTION: it is important to stop click events on the input
// as otherwise Tools click handler will be triggered again
.on('click', domHelpers.stop)
if (!this.doesAcceptAllFileTypes) {
const fileType = this.getFileType()
input.attr({ 'accept': fileType })
}
if (isMultiple) {
input.attr({
'multiple': 'multiple'
})
}
el.append(input)
return el
}
getClassNames () {
return 'sc-upload-tool'
}
getFileType () {
throw new Error('This method is abstract')
}
get canUploadMultiple () {
return false
}
get doesAcceptAllFileTypes () {
return false
}
_onClick (e) {
e.stopPropagation()
e.preventDefault()
this.refs.input.el.val(null)
this.refs.input.el.click()
}
onFileSelect (e) {
let files = e.currentTarget.files
this.executeCommand({
files: Array.prototype.slice.call(files)
})
}
}