aboutsummaryrefslogtreecommitdiff
path: root/static/js/pager.ts
blob: e818d178b93dc46578e757c4f0836b15e1284ed7 (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
(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 url;
  let seek;
  let pager = {};

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

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

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

  const scrollHash = function (url, load = false) {
    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 (load) fragment.scrollIntoView();
    }
    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);
          });
        }
        link(); scrollRestore(url);
      });
    },
  );

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