forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
57 lines (55 loc) · 2.21 KB
/
Copy pathcontroller.js
File metadata and controls
57 lines (55 loc) · 2.21 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
import { Controller } from '@hotwired/stimulus';
class default_1 extends Controller {
connect() {
this.clear();
this.previewClearButtonTarget.addEventListener('click', () => this.clear());
this.inputTarget.addEventListener('change', (event) => this.onInputChange(event));
this.dispatchEvent('connect');
}
clear() {
this.inputTarget.value = '';
this.inputTarget.style.display = 'block';
this.placeholderTarget.style.display = 'block';
this.previewTarget.style.display = 'none';
this.previewImageTarget.style.display = 'none';
this.previewImageTarget.style.backgroundImage = 'none';
this.previewFilenameTarget.textContent = '';
document.querySelectorAll('.dropzone-preview-image-container').forEach((e) => e.remove());
this.dispatchEvent('clear');
}
onInputChange(event) {
for (const fileItem in event.target.files) {
const file = event.target.files[fileItem];
if (typeof file === 'undefined') {
return;
}
this.placeholderTarget.style.display = 'none';
this.previewFilenameTarget.textContent = file.name;
this.previewTarget.style.display = 'flex';
this.previewImageTarget.style.display = 'none';
if (file.type && file.type.indexOf('image') !== -1) {
this._populateImagePreview(file);
}
}
this.dispatchEvent('change', event.target.files);
}
_populateImagePreview(file) {
if (typeof FileReader === 'undefined') {
return;
}
const reader = new FileReader();
reader.addEventListener('load', (event) => {
this.previewImageTarget.style.display = 'block';
this.previewImageTarget.style.backgroundImage = 'url("' + event.target.result + '")';
});
reader.readAsDataURL(file);
}
dispatchEvent(name, payload = {}) {
this.dispatch(name, { detail: payload, prefix: 'dropzone' });
}
}
default_1.values = {
numberOfFiles: Number
};
default_1.targets = ['input', 'placeholder', 'preview', 'previewClearButton', 'previewFilename', 'previewImage'];
export { default_1 as default };