2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
export class ImagePicker extends Component {
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
setup() {
|
|
|
|
this.imageElem = this.$refs.image;
|
|
|
|
this.imageInput = this.$refs.imageInput;
|
|
|
|
this.resetInput = this.$refs.resetInput;
|
|
|
|
this.removeInput = this.$refs.removeInput;
|
|
|
|
this.resetButton = this.$refs.resetButton;
|
|
|
|
this.removeButton = this.$refs.removeButton || null;
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
this.defaultImage = this.$opts.defaultImage;
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
this.resetButton.addEventListener('click', this.reset.bind(this));
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
if (this.removeButton) {
|
|
|
|
this.removeButton.addEventListener('click', this.removeImage.bind(this));
|
2017-12-07 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:48:15 +08:00
|
|
|
this.imageInput.addEventListener('change', this.fileInputChange.bind(this));
|
|
|
|
}
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2019-05-04 22:48:15 +08:00
|
|
|
fileInputChange() {
|
|
|
|
this.resetInput.setAttribute('disabled', 'disabled');
|
|
|
|
if (this.removeInput) {
|
|
|
|
this.removeInput.setAttribute('disabled', 'disabled');
|
|
|
|
}
|
2017-12-07 01:32:29 +08:00
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
for (const file of this.imageInput.files) {
|
2019-05-04 22:48:15 +08:00
|
|
|
this.imageElem.src = window.URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
this.imageElem.classList.remove('none');
|
2017-12-07 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2019-05-04 22:48:15 +08:00
|
|
|
this.imageInput.value = '';
|
|
|
|
this.imageElem.src = this.defaultImage;
|
|
|
|
this.resetInput.removeAttribute('disabled');
|
|
|
|
if (this.removeInput) {
|
|
|
|
this.removeInput.setAttribute('disabled', 'disabled');
|
|
|
|
}
|
2017-12-07 01:32:29 +08:00
|
|
|
this.imageElem.classList.remove('none');
|
|
|
|
}
|
|
|
|
|
|
|
|
removeImage() {
|
2019-05-04 22:48:15 +08:00
|
|
|
this.imageInput.value = '';
|
2017-12-07 01:32:29 +08:00
|
|
|
this.imageElem.classList.add('none');
|
2019-05-04 22:48:15 +08:00
|
|
|
this.removeInput.removeAttribute('disabled');
|
|
|
|
this.resetInput.setAttribute('disabled', 'disabled');
|
2017-12-07 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|