summaryrefslogtreecommitdiff
path: root/assets/static/js/image-viewer.js
blob: 28c44cbdf9f47a2c2d99af6973d75c11d8a8dcd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Enlarge content images in a native dialog; navigate within galleries. */
(() => {
  const dialog = document.querySelector("#image-viewer");
  if (!dialog || typeof dialog.showModal !== "function") return;

  const enlargedImage = dialog.querySelector("figure > img");
  const caption = dialog.querySelector("figcaption");
  const previousButton = dialog.querySelector('[data-action="previous"]');
  const nextButton = dialog.querySelector('[data-action="next"]');
  const closeButton = dialog.querySelector('[data-action="close"]');

  // Linked images and interactive maps must keep their existing behaviour.
  const images = [...document.querySelectorAll("main img")].filter(
    (image) =>
      image.getAttribute("src") &&
      !image.closest("a, button, .trail, [data-no-enlarge]"),
  );

  let group = [];
  let index = 0;

  function displayCurrentImage() {
    const image = group[index];
    enlargedImage.src = image.dataset.fullsize || image.currentSrc || image.src;
    enlargedImage.alt = image.alt;

    const sourceCaption = image
      .closest("figure:not(.trail)")
      ?.querySelector(":scope > figcaption");
    const captionContent = sourceCaption?.cloneNode(true);
    captionContent
      ?.querySelectorAll("details")
      .forEach((detail) => detail.remove());
    caption.textContent =
      captionContent?.textContent.replace(/\s+/g, " ").trim() || "";
    caption.hidden = !caption.textContent;

    previousButton.hidden = nextButton.hidden = group.length < 2;
  }

  function openViewer(image) {
    const gallery = image.closest(".gallery");
    group = gallery
      ? images.filter((candidate) => candidate.closest(".gallery") === gallery)
      : [image];
    index = group.indexOf(image);
    displayCurrentImage();
    dialog.showModal();
  }

  function navigate(direction) {
    if (group.length < 2) return;
    index = (index + direction + group.length) % group.length;
    displayCurrentImage();
  }

  for (const image of images) {
    image.dataset.enlargeable = "";
    image.tabIndex = 0;
    image.setAttribute("role", "button");
    image.setAttribute("aria-haspopup", "dialog");
    image.setAttribute(
      "aria-label",
      image.alt ? `Enlarge image: ${image.alt}` : "Enlarge image",
    );

    image.addEventListener("click", () => openViewer(image));
    image.addEventListener("keydown", (event) => {
      if (event.key === "Enter" || event.key === " ") {
        event.preventDefault();
        openViewer(image);
      }
    });
  }

  previousButton.addEventListener("click", () => navigate(-1));
  nextButton.addEventListener("click", () => navigate(1));
  closeButton.addEventListener("click", () => dialog.close());

  dialog.addEventListener("keydown", (event) => {
    if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
      event.preventDefault();
      navigate(event.key === "ArrowLeft" ? -1 : 1);
    }
  });

  // Click the backdrop to close, but not the spacing around the image.
  dialog.addEventListener("click", (event) => {
    if (event.target !== dialog) return;
    const rect = dialog.getBoundingClientRect();
    if (
      event.clientX < rect.left ||
      event.clientX > rect.right ||
      event.clientY < rect.top ||
      event.clientY > rect.bottom
    ) {
      dialog.close();
    }
  });
})();