hmn/public/js/image_selector.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-07-03 03:11:41 +00:00
function ImageSelector(form, maxFileSize, container, {
defaultImageUrl = "",
onUpdate = (url) => {},
} = {}) {
2021-11-25 03:59:51 +00:00
this.form = form;
this.maxFileSize = maxFileSize;
this.fileInput = container.querySelector(".imginput");
this.removeImageInput = container.querySelector(".imginput-remove");
2021-11-25 03:59:51 +00:00
this.imageEl = container.querySelector("img");
this.container = container.querySelector(".imginput-container");
this.resetLink = container.querySelector(".imginput-reset-link");
this.removeLink = container.querySelector(".imginput-remove-link");
this.filenameText = container.querySelector(".imginput-filename");
this.originalImageUrl = this.imageEl.getAttribute("data-imginput-original");
this.originalImageFilename = this.imageEl.getAttribute("data-imginput-original-filename");
2021-11-25 03:59:51 +00:00
this.currentImageUrl = this.originalImageUrl;
2024-07-03 03:11:41 +00:00
this.defaultImageUrl = defaultImageUrl;
this.onUpdate = onUpdate;
2021-11-25 03:59:51 +00:00
this.fileInput.value = "";
this.removeImageInput.value = "";
2024-07-03 03:11:41 +00:00
this.setImageUrl(this.originalImageUrl, true);
this.updatePreview();
2021-11-25 03:59:51 +00:00
this.fileInput.addEventListener("change", function(ev) {
if (this.fileInput.files.length > 0) {
this.handleNewImageFile(this.fileInput.files[0]);
}
}.bind(this));
this.resetLink.addEventListener("click", function(ev) {
this.resetImage();
}.bind(this));
if (this.removeLink) {
this.removeLink.addEventListener("click", function(ev) {
this.removeImage();
}.bind(this));
}
}
ImageSelector.prototype.openFileInput = function() {
this.fileInput.click();
}
2021-11-25 03:59:51 +00:00
ImageSelector.prototype.handleNewImageFile = function(file) {
if (file) {
this.updateSizeLimit(file.size);
this.removeImageInput.value = "";
this.setImageUrl(URL.createObjectURL(file));
this.updatePreview(file);
2021-11-25 03:59:51 +00:00
}
};
ImageSelector.prototype.removeImage = function() {
this.updateSizeLimit(0);
this.fileInput.value = "";
this.removeImageInput.value = "true";
2021-12-29 14:38:23 +00:00
this.setImageUrl(this.defaultImageUrl);
this.updatePreview(null);
2021-11-25 03:59:51 +00:00
};
ImageSelector.prototype.resetImage = function() {
this.updateSizeLimit(0);
this.fileInput.value = "";
this.removeImageInput.value = "";
this.setImageUrl(this.originalImageUrl);
this.updatePreview(null);
2021-11-25 03:59:51 +00:00
};
ImageSelector.prototype.updateSizeLimit = function(size) {
this.fileTooBig = size > this.maxFileSize;
if (this.fileTooBig) {
this.setError("File too big. Max filesize is " + this.maxFileSize + " bytes.");
} else {
this.setError("");
}
};
ImageSelector.prototype.setError = function(error) {
this.fileInput.setCustomValidity(error);
this.fileInput.reportValidity();
}
2024-07-03 03:11:41 +00:00
ImageSelector.prototype.setImageUrl = function(url, initial = false) {
2021-11-25 03:59:51 +00:00
this.currentImageUrl = url;
this.imageEl.src = url;
if (url.length > 0) {
this.imageEl.style.display = "block";
} else {
this.imageEl.style.display = "none";
}
2024-07-03 03:11:41 +00:00
this.url = url;
if (!initial) {
this.onUpdate(url);
}
2021-11-25 03:59:51 +00:00
};
ImageSelector.prototype.updatePreview = function(file) {
const showReset = (
this.originalImageUrl
&& this.originalImageUrl != this.defaultImageUrl
&& this.originalImageUrl != this.currentImageUrl
);
const showRemove = (
!this.fileInput.required
&& this.currentImageUrl != this.defaultImageUrl
);
this.resetLink.hidden = !showReset;
this.removeLink.hidden = !showRemove;
2021-12-29 14:38:23 +00:00
if (this.currentImageUrl == this.originalImageUrl) {
this.filenameText.innerText = this.originalImageFilename;
2021-11-25 03:59:51 +00:00
} else {
this.filenameText.innerText = file ? file.name : "";
2021-11-25 03:59:51 +00:00
}
this.container.hidden = !this.currentImageUrl;
2021-11-25 03:59:51 +00:00
};