aboutsummaryrefslogtreecommitdiff
path: root/static/js/pager.ts
blob: ebaf67a6e1def800d69b22d22b4c4715f9ec5f43 (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
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
(function () {
  const cookiesDisabled = !navigator.cookieEnabled;

  if (cookiesDisabled) {
    document.cookie = "disabled";
    document.cookie.indexOf("disabled");
    return console.warn("WARNING: Pager disabled due to cookie restrictions");
  }

  let seek;
  let pager = {};

  const state = "on";
  const key = "config.scroll.pager.urls";

  if (!localStorage[key]) localStorage[key] = JSON.stringify(pager);

  let url = function () { return self.location.href.split("#")[0].split("?")[0]; };

  const scrollHash = function (url) {
    const hash = self.location.hash;
    const fragment = hash.slice(1) && document.getElementById(hash.slice(1));
    const fragmented = hash.length > 0;
    const hashed = fragmented && document.body.contains(fragment);
    if (hashed) {
      self.location.hash = hash;
      self.location.href = hash;
      if ("scroll-margin-top" in document.body.style === false && fragment.textContent !== "") {
        self.scrollBy(0, -6 * parseFloat(getComputedStyle(document.documentElement).fontSize));
      }
    }
    return hashed;
  };

  const scrollRestore = function (url) {
    if (history.scrollRestoration) history.scrollRestoration = "manual";
    if (scrollHash(url)) return;
    pager = JSON.parse(localStorage[key]);
    if (pager[url] > 0) {
      clearInterval(seek);
      self.scrollTo(0, pager[url]);
      let i = 0; return seek = setInterval(function (position) {
        i++; if (i > 100) clearInterval(seek);
        if (document.documentElement.scrollHeight >= position + document.documentElement.clientHeight) {
          clearInterval(seek); self.scrollTo(0, position);
        }
      }, 4, pager[url]);
    } else self.scrollTo(0, 0);
    pager[url] = self.pageYOffset;
    localStorage[key] = JSON.stringify(pager);
  };

  const scrollTrack = function (url) {
    const currentPosition = self.pageYOffset;
    pager = JSON.parse(localStorage[key]);
    pager[url] = currentPosition;
    localStorage[key] = JSON.stringify(pager);
  };

  const scrollReverse = function (back, up, event) {
    if (document.body.contains(up) && up.contains(event.target)) {
      event.preventDefault();
      window.scrollTo(0, 0);
    }
    if (document.body.contains(back) && back.contains(event.target)) {
      if (history.length < 2) return;
      event.preventDefault();
      history.go(-1);
    }
  };

  ["DOMContentLoaded", "pageshow", "hashchange", "URLChangedCustomEvent"].forEach(
    function (event) {
      self.addEventListener(event, function (event) {
        if (event.type === "pageshow") {
          return event.persisted && self.scrollTo(0, pager[url()]);
        }
        if (event.type === "DOMContentLoaded") {
          self.addEventListener("click", function (event) {
            const up = document.getElementById("top");
            const back = document.getElementById("back");
            scrollReverse(back, up, event);
          });
        }
        scrollRestore(url());
      });
    },
  );

  ["click", "touchstart", "scroll"].forEach(function (event) {
    self.addEventListener(event, function () {
      scrollTrack(url());
    });
  });
})();