2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
export class ImagePicker extends Component {
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2022-11-15 11:04:46 -05: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-06 12:32:29 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
this.defaultImage = this.$opts.defaultImage;
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
this.resetButton.addEventListener('click', this.reset.bind(this));
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
if (this.removeButton) {
|
|
|
|
this.removeButton.addEventListener('click', this.removeImage.bind(this));
|
2017-12-06 12:32:29 -05:00
|
|
|
}
|
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
this.imageInput.addEventListener('change', this.fileInputChange.bind(this));
|
|
|
|
}
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
fileInputChange() {
|
|
|
|
this.resetInput.setAttribute('disabled', 'disabled');
|
|
|
|
if (this.removeInput) {
|
|
|
|
this.removeInput.setAttribute('disabled', 'disabled');
|
|
|
|
}
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const file of this.imageInput.files) {
|
2019-05-04 10:48:15 -04:00
|
|
|
this.imageElem.src = window.URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
this.imageElem.classList.remove('none');
|
2017-12-06 12:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2019-05-04 10:48:15 -04:00
|
|
|
this.imageInput.value = '';
|
|
|
|
this.imageElem.src = this.defaultImage;
|
|
|
|
this.resetInput.removeAttribute('disabled');
|
|
|
|
if (this.removeInput) {
|
|
|
|
this.removeInput.setAttribute('disabled', 'disabled');
|
|
|
|
}
|
2017-12-06 12:32:29 -05:00
|
|
|
this.imageElem.classList.remove('none');
|
|
|
|
}
|
|
|
|
|
|
|
|
removeImage() {
|
2019-05-04 10:48:15 -04:00
|
|
|
this.imageInput.value = '';
|
2017-12-06 12:32:29 -05:00
|
|
|
this.imageElem.classList.add('none');
|
2019-05-04 10:48:15 -04:00
|
|
|
this.removeInput.removeAttribute('disabled');
|
|
|
|
this.resetInput.setAttribute('disabled', 'disabled');
|
2017-12-06 12:32:29 -05:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|