diff options
Diffstat (limited to 'static/js/pager.ts')
-rw-r--r-- | static/js/pager.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/static/js/pager.ts b/static/js/pager.ts new file mode 100644 index 0000000..de60bcd --- /dev/null +++ b/static/js/pager.ts @@ -0,0 +1,46 @@ +(function () { + const url = self.location.href.split("#")[0]; + let settings = { pager: {} }; + + self.addEventListener("DOMContentLoaded", function () { + if (history.scrollRestoration) history.scrollRestoration = "manual"; + if (localStorage["settings"]) { + settings = JSON.parse(localStorage["settings"]); + } + if (self.location.href.indexOf("#") >= 0) { + settings["pager"][url] = self.pageYOffset; + localStorage["settings"] = JSON.stringify(settings); + document.getElementById(location.hash.slice(1)).scrollIntoView(); + return; + } + if (settings["pager"][url] > 0) { + self.scrollTo(0, settings["pager"][url]); + return; + } + settings["pager"][url] = self.pageYOffset; + localStorage["settings"] = JSON.stringify(settings); + }); + + self.addEventListener("scroll", function () { + const currentPosition = self.pageYOffset; + settings["pager"][url] = currentPosition; + localStorage["settings"] = JSON.stringify(settings); + }); + + self.addEventListener("DOMContentLoaded", function () { + const up = document.getElementById("top"); + const back = document.getElementById("back"); + + if (document.body.contains(up)) up.href = "javascript: void(0)"; + if (document.body.contains(back)) back.href = "javascript: void(0)"; + + self.addEventListener("click", function (event) { + if (document.body.contains(up) && up.contains(event.target)) { + window.scrollTo(0, 0); + } + if (document.body.contains(back) && back.contains(event.target)) { + history.go(-1); + } + }); + }); +})(); |