|
44 | 44 | }
|
45 | 45 | }
|
46 | 46 | // When load the document, update AttachmentBox in preview
|
47 |
| -document.addEventListener ("DOMContentLoaded", () => {imcgerShowAttachImage();}); |
| 47 | +document.addEventListener("DOMContentLoaded", () => {imcgerShowAttachImage();}); |
48 | 48 |
|
49 | 49 | (function($) { // Avoid conflicts with other libraries
|
50 | 50 |
|
|
217 | 217 | */
|
218 | 218 | const imcgerImgUpload = {
|
219 | 219 | initialImage: function() {
|
220 |
| - const maxImageWidth = '{{ IUL_IMG_MAXWIDTH }}'; |
221 |
| - |
222 | 220 | // Return if no content is displayed
|
223 | 221 | if (!document.getElementsByClassName("content").length) {
|
224 | 222 | return;
|
225 | 223 | }
|
226 | 224 |
|
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"); |
229 | 226 |
|
230 | 227 | 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); |
239 | 229 | });
|
240 | 230 | },
|
| 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 | + }, |
241 | 244 | }
|
242 | 245 |
|
243 | 246 | // Adjust the size of images when loaded
|
244 | 247 | let postImageArray = document.querySelectorAll(".content img.postimage");
|
245 | 248 | 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,}); |
258 | 250 | });
|
259 | 251 |
|
260 | 252 | // When load the document, adjust the size of images
|
261 |
| -document.addEventListener ("DOMContentLoaded", () => {imcgerImgUpload.initialImage();}); |
| 253 | +document.addEventListener("DOMContentLoaded", () => {imcgerImgUpload.initialImage();}); |
262 | 254 |
|
263 | 255 | // When resize the window, adjust the size of images
|
264 |
| -window.onresize = (event) => {imcgerImgUpload.initialImage();}; |
| 256 | +window.addEventListener("resize", () => {imcgerImgUpload.initialImage();}); |
265 | 257 | </script>
|
0 commit comments