|
31 | 31 | modal.setAttribute('aria-label', 'image view dialog'); |
32 | 32 | modal.tabIndex = -1; |
33 | 33 |
|
34 | | - // Force the dialog into light mode. This is needed for a webwork2 page in dark mode since the dialog is outside |
35 | | - // of the problem content. At least until PG is updated to honor dark mode. Further discussion on this will |
36 | | - // also be needed at that time since many images have transparent backgrounds that will not work with a dark |
37 | | - // background. |
38 | | - modal.dataset.bsTheme = 'light'; |
39 | | - |
40 | 34 | const dialog = document.createElement('div'); |
41 | 35 | dialog.classList.add('modal-dialog'); |
42 | 36 |
|
|
118 | 112 |
|
119 | 113 | const body = document.createElement('div'); |
120 | 114 | body.classList.add('modal-body'); |
| 115 | + body.dataset.bsTheme = 'light'; |
121 | 116 |
|
122 | 117 | let graphDiv = null; |
123 | 118 | if (imgType == 'div') { |
|
335 | 330 | } |
336 | 331 | }; |
337 | 332 |
|
338 | | - // Set up images that are already in the page. |
339 | | - document.querySelectorAll('.image-view-elt').forEach((elt) => { |
340 | | - elt.addEventListener('click', imageViewDialog); |
341 | | - elt.addEventListener('keydown', keyHandler); |
342 | | - }); |
| 333 | + const handleBrokenImage = (img) => { |
| 334 | + img.classList.add('broken'); |
| 335 | + img.removeAttribute('role'); |
| 336 | + }; |
| 337 | + |
| 338 | + const attachListeners = (img) => { |
| 339 | + img.removeEventListener('click', imageViewDialog); |
| 340 | + img.removeEventListener('keydown', keyHandler); |
| 341 | + img.addEventListener('click', imageViewDialog); |
| 342 | + img.addEventListener('keydown', keyHandler); |
| 343 | + }; |
343 | 344 |
|
344 | | - const attachListeners = (node) => { |
345 | | - node.removeEventListener('click', imageViewDialog); |
346 | | - node.removeEventListener('keydown', keyHandler); |
347 | | - node.addEventListener('click', imageViewDialog); |
348 | | - node.addEventListener('keydown', keyHandler); |
| 345 | + const initializeImgViewElt = (img) => { |
| 346 | + if (img instanceof HTMLImageElement) { |
| 347 | + if (img.complete) { |
| 348 | + if (img.naturalWidth === 0) handleBrokenImage(img); |
| 349 | + else attachListeners(img); |
| 350 | + } else { |
| 351 | + img.addEventListener('error', () => handleBrokenImage(img)); |
| 352 | + img.addEventListener('load', () => attachListeners(img)); |
| 353 | + } |
| 354 | + } else { |
| 355 | + attachListeners(img); |
| 356 | + } |
349 | 357 | }; |
350 | 358 |
|
| 359 | + // Set up images that are already in the page. |
| 360 | + for (const elt of document.querySelectorAll('.image-view-elt')) { |
| 361 | + initializeImgViewElt(elt); |
| 362 | + } |
| 363 | + |
351 | 364 | // Deal with images that are added to the page later. |
352 | 365 | const observer = new MutationObserver((mutationsList) => { |
353 | 366 | mutationsList.forEach((mutation) => { |
354 | 367 | mutation.addedNodes.forEach((node) => { |
355 | 368 | if (node instanceof Element) { |
356 | | - if (node.classList.contains('image-view-elt')) attachListeners(node); |
357 | | - else node.querySelectorAll('.image-view-elt').forEach(attachListeners); |
| 369 | + if (node.classList.contains('image-view-elt')) initializeImgViewElt(node); |
| 370 | + else node.querySelectorAll('.image-view-elt').forEach(initializeImgViewElt); |
358 | 371 | } |
359 | 372 | }); |
360 | 373 | }); |
|
0 commit comments