Skip to content

Commit 793411f

Browse files
committed
v1.2.0-beta3
- JS code optimized
1 parent cd0c694 commit 793411f

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

imcger/imgupload/styles/all/template/event/overall_footer_body_after.html

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545
}
4646
// When load the document, update AttachmentBox in preview
47-
document.addEventListener ("DOMContentLoaded", () => {imcgerShowAttachImage();});
47+
document.addEventListener("DOMContentLoaded", () => {imcgerShowAttachImage();});
4848

4949
(function($) { // Avoid conflicts with other libraries
5050

@@ -217,49 +217,41 @@
217217
*/
218218
const imcgerImgUpload = {
219219
initialImage: function() {
220-
const maxImageWidth = '{{ IUL_IMG_MAXWIDTH }}';
221-
222220
// Return if no content is displayed
223221
if (!document.getElementsByClassName("content").length) {
224222
return;
225223
}
226224

227-
let postImageArray = document.querySelectorAll(".content img.postimage"),
228-
postbodyWidth = window.getComputedStyle(document.getElementsByClassName("content")[0]).width;
225+
let postImageArray = document.querySelectorAll(".content img.postimage");
229226

230227
postImageArray.forEach((image) => {
231-
if (parseInt(image.naturalWidth) > parseInt(postbodyWidth) || parseInt(image.naturalWidth) > parseInt(maxImageWidth)) {
232-
image.style.width = '100%';
233-
image.style.maxWidth = maxImageWidth;
234-
} else {
235-
image.style.width = 'auto';
236-
image.style.maxWidth = 'none';
237-
}
238-
228+
this.setImageStyle(image);
239229
});
240230
},
231+
setImageStyle: function(img) {
232+
const maxImageWidth = '{{ IUL_IMG_MAXWIDTH }}';
233+
let postbodyWidth = window.getComputedStyle(document.getElementsByClassName("content")[0]).width,
234+
maxWidth = maxImageWidth == 'none' ? parseInt(postbodyWidth) : Math.min(parseInt(postbodyWidth), parseInt(maxImageWidth));
235+
236+
if (parseInt(img.naturalWidth) > maxWidth) {
237+
img.style.width = '100%';
238+
img.style.maxWidth = maxImageWidth;
239+
} else {
240+
img.style.width = 'auto';
241+
img.style.maxWidth = 'none';
242+
}
243+
},
241244
}
242245

243246
// Adjust the size of images when loaded
244247
let postImageArray = document.querySelectorAll(".content img.postimage");
245248
postImageArray.forEach((image) => {
246-
image.onload = (event) => {
247-
const maxImageWidth = '{{ IUL_IMG_MAXWIDTH }}';
248-
let postbodyWidth = window.getComputedStyle(document.getElementsByClassName("content")[0]).width;
249-
250-
if (parseInt(image.naturalWidth) > parseInt(postbodyWidth) || parseInt(image.naturalWidth) > parseInt(maxImageWidth)) {
251-
image.style.width = '100%';
252-
image.style.maxWidth = maxImageWidth;
253-
} else {
254-
image.style.width = 'auto';
255-
image.style.maxWidth = 'none';
256-
}
257-
};
249+
image.addEventListener("load", () => {imcgerImgUpload.setImageStyle(image)}, {once: true,});
258250
});
259251

260252
// When load the document, adjust the size of images
261-
document.addEventListener ("DOMContentLoaded", () => {imcgerImgUpload.initialImage();});
253+
document.addEventListener("DOMContentLoaded", () => {imcgerImgUpload.initialImage();});
262254

263255
// When resize the window, adjust the size of images
264-
window.onresize = (event) => {imcgerImgUpload.initialImage();};
256+
window.addEventListener("resize", () => {imcgerImgUpload.initialImage();});
265257
</script>

0 commit comments

Comments
 (0)