2017-12-06 12:32:29 -05:00
|
|
|
|
|
|
|
class ImagePicker {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.imageElem = elem.querySelector('img');
|
2019-05-04 10:48:15 -04:00
|
|
|
this.imageInput = elem.querySelector('input[type=file]');
|
|
|
|
this.resetInput = elem.querySelector('input[data-reset-input]');
|
|
|
|
this.removeInput = elem.querySelector('input[data-remove-input]');
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
this.defaultImage = elem.getAttribute('data-default-image');
|
2017-12-06 12:32:29 -05:00
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
const resetButton = elem.querySelector('button[data-action="reset-image"]');
|
2017-12-06 12:32:29 -05:00
|
|
|
resetButton.addEventListener('click', this.reset.bind(this));
|
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
const removeButton = elem.querySelector('button[data-action="remove-image"]');
|
2017-12-06 12:32:29 -05:00
|
|
|
if (removeButton) {
|
|
|
|
removeButton.addEventListener('click', this.removeImage.bind(this));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
for (let file of this.imageInput.files) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default ImagePicker;
|