-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-input.js
109 lines (85 loc) · 2.18 KB
/
file-input.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
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
import { define, Component } from '@xinix/xin';
import { FilePool } from './file-pool';
const { File } = window;
export class FileInput extends Component {
get template () {
return require('./file-input.html');
}
get props () {
return Object.assign({}, super.props, {
value: {
type: Array,
value: () => ([]),
notify: true,
},
pool: {
type: Object,
},
bucket: {
type: String,
value: '/',
},
});
}
attached () {
super.attached();
this.fileInputChanged = this.fileInputChanged.bind(this);
this.fileInput = document.createElement('input');
this.fileInput.type = 'file';
this.fileInput.setAttribute('multiple', 'multiple');
this.fileInput.addEventListener('change', this.fileInputChanged);
}
detached () {
this.fileInput.removeEventListener('change', this.fileInputChanged);
this.fileInput = null;
}
getPool () {
return this.pool || FilePool.default;
}
fileInputChanged (evt) {
let target = evt.target;
this.set('value', this.value || []);
let files = Array.from(target.files);
files.forEach(file => {
if (this.value.find(f => f.name === file.name)) {
return;
}
this.push('value', file);
});
target.value = '';
this.async(async () => {
try {
let resultFiles = await this.getPool().upload(files, this);
let value = this.value.map(file => {
if (file instanceof File) {
let resultFile = resultFiles.find(rf => rf.name === file.name);
if (resultFile) {
file = resultFile;
}
}
return file;
});
this.set('value', value);
} catch (err) {
this.set('error', err);
this.fire('error', err);
}
}, 10);
}
computeUploadingText (hash) {
if (!hash) {
return '(uploading)';
}
}
selectClicked (evt) {
evt.preventDefault();
this.fileInput.click();
}
deleteClicked (evt, index) {
evt.preventDefault();
let value = this.value.map(f => f);
value.splice(index, 1);
this.set('value', value);
}
}
define('file-input', FileInput);