|
| 1 | +(function () { |
| 2 | + var skip = |
| 3 | + ".platform, .platform-vendors, .partner-logos, .video-preview, .video-previews, table.docutils"; |
| 4 | + |
| 5 | + function eligible(img) { |
| 6 | + return img.closest(".rst-content") && !img.closest(skip) && !img.closest("a"); |
| 7 | + } |
| 8 | + |
| 9 | + function init() { |
| 10 | + var images = document.querySelectorAll(".rst-content img"); |
| 11 | + var targets = []; |
| 12 | + images.forEach(function (img) { |
| 13 | + if (eligible(img)) { |
| 14 | + targets.push(img); |
| 15 | + } |
| 16 | + }); |
| 17 | + if (!targets.length) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + var overlay = document.createElement("div"); |
| 22 | + overlay.className = "image-lightbox"; |
| 23 | + overlay.hidden = true; |
| 24 | + overlay.setAttribute("role", "dialog"); |
| 25 | + overlay.setAttribute("aria-modal", "true"); |
| 26 | + overlay.setAttribute("aria-label", "Enlarged image"); |
| 27 | + |
| 28 | + var enlarged = document.createElement("img"); |
| 29 | + enlarged.alt = ""; |
| 30 | + overlay.appendChild(enlarged); |
| 31 | + document.body.appendChild(overlay); |
| 32 | + |
| 33 | + var lastFocus = null; |
| 34 | + |
| 35 | + function open(img) { |
| 36 | + lastFocus = document.activeElement; |
| 37 | + enlarged.src = img.currentSrc || img.src; |
| 38 | + enlarged.alt = img.alt || ""; |
| 39 | + overlay.hidden = false; |
| 40 | + document.body.classList.add("image-lightbox-open"); |
| 41 | + overlay.focus(); |
| 42 | + } |
| 43 | + |
| 44 | + function close() { |
| 45 | + overlay.hidden = true; |
| 46 | + enlarged.removeAttribute("src"); |
| 47 | + enlarged.alt = ""; |
| 48 | + document.body.classList.remove("image-lightbox-open"); |
| 49 | + if (lastFocus && lastFocus.focus) { |
| 50 | + lastFocus.focus(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + overlay.tabIndex = -1; |
| 55 | + overlay.addEventListener("click", close); |
| 56 | + document.addEventListener("keydown", function (event) { |
| 57 | + if (event.key === "Escape" && !overlay.hidden) { |
| 58 | + close(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + targets.forEach(function (img) { |
| 63 | + img.classList.add("lightbox-enabled"); |
| 64 | + img.tabIndex = 0; |
| 65 | + img.setAttribute("role", "button"); |
| 66 | + img.setAttribute("aria-haspopup", "dialog"); |
| 67 | + img.addEventListener("click", function () { |
| 68 | + open(img); |
| 69 | + }); |
| 70 | + img.addEventListener("keydown", function (event) { |
| 71 | + if (event.key === "Enter" || event.key === " ") { |
| 72 | + event.preventDefault(); |
| 73 | + open(img); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + if (document.readyState === "loading") { |
| 80 | + document.addEventListener("DOMContentLoaded", init); |
| 81 | + } else { |
| 82 | + init(); |
| 83 | + } |
| 84 | +})(); |
0 commit comments