summaryrefslogtreecommitdiff
path: root/cgithub/public/static/refs.js
blob: ffff505f18aa486e0ada004ef5868c7f5eb6bfa5 (plain)
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
// Progressive enhancement for the bits of data GitHub itself loads lazily:
// the branch/tag dropdown, the latest-commit bar above a file listing, and the
// language breakdown. All degrade gracefully without JS: the button's
// command/commandfor still opens the (empty) dialog natively; the placeholders
// just never fill in.
document.addEventListener("DOMContentLoaded", () => {
  const dialog = document.querySelector(".ref-selector-menu");
  if (dialog) {
    const list = dialog.querySelector(".ref-selector-list");
    const { owner, repo } = dialog.dataset;
    const cache = {};

    async function load(type) {
      if (Object.prototype.hasOwnProperty.call(cache, type)) {
        list.innerHTML = cache[type];
        return;
      }
      list.textContent = "Loading…";
      try {
        const res = await fetch(`/api/${owner}/${repo}/refs/${type}`);
        const html = res.ok ? await res.text() : "";
        cache[type] = html || "Failed to load.";
        list.innerHTML = cache[type];
      } catch {
        list.textContent = "Failed to load.";
      }
    }

    let opened = false;
    dialog.addEventListener("toggle", () => {
      if (dialog.open && !opened) {
        opened = true;
        load("branches");
      }
    });

    for (const tab of dialog.querySelectorAll("[data-ref-type]")) {
      tab.addEventListener("click", () => {
        for (const other of dialog.querySelectorAll("[data-ref-type]")) {
          other.classList.toggle("active", other === tab);
        }
        load(tab.dataset.refType);
      });
    }
  }

  for (const placeholder of document.querySelectorAll(".placeholder[data-src]")) {
    fetch(placeholder.dataset.src)
      .then((res) => (res.ok ? res.text() : ""))
      .then((html) => {
        if (html.trim()) {
          placeholder.outerHTML = html;
        } else {
          placeholder.remove();
        }
      })
      .catch(() => placeholder.remove());
  }
});