diff options
author | tdro <tdro@noreply.example.com> | 2022-05-06 09:07:31 -0400 |
---|---|---|
committer | tdro <tdro@noreply.example.com> | 2022-05-06 09:07:31 -0400 |
commit | 6caee9fe72293ef957bb9461e489d85f65edbc41 (patch) | |
tree | 798f5f2622e36c55fe0907731a2700f16d724ed4 /assets/js | |
parent | 8f2e61d3095b9951c19619a145f94a30b743fb75 (diff) | |
download | canory-6caee9fe72293ef957bb9461e489d85f65edbc41.tar.gz canory-6caee9fe72293ef957bb9461e489d85f65edbc41.tar.bz2 canory-6caee9fe72293ef957bb9461e489d85f65edbc41.zip |
assets: Replicate static assets
Diffstat (limited to 'assets/js')
-rw-r--r-- | assets/js/index.js | 426 |
1 files changed, 426 insertions, 0 deletions
diff --git a/assets/js/index.js b/assets/js/index.js new file mode 100644 index 0000000..62615e5 --- /dev/null +++ b/assets/js/index.js @@ -0,0 +1,426 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + +(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"); + self.addEventListener("click", function(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)) { + event.preventDefault(); + history.go(-1); + } + }); + }); +})(); +(function() { + const options = "targetWindow,width=700,height=500,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes"; + self.addEventListener("keydown", function(event) { + if (event.repeat && event.key === "d") { + selection(dictionary); + } + if (event.repeat && event.key === "s") { + selection(search); + } + if (event.repeat && event.key === "m") { + selection(manual); + } + }); + function selection(execute) { + let phrase = "" + window.getSelection(); + phrase = phrase.replace(/[!.:?,;"]/g, ""); + phrase = phrase.replace(/^\s*(\S*?)\s*$/g, "$1"); + if (phrase && phrase > "" && phrase.length > 1) { + execute(phrase); + } + } + function dictionary(word) { + window.open("https://www.merriam-webster.com/dictionary/" + encodeURIComponent(word), "Definitions", options); + } + function search(phrase) { + window.open("https://lite.duckduckgo.com/lite/?q=" + encodeURIComponent(phrase), "Search", options); + } + function manual(program) { + window.open("https://man.archlinux.org/search?q=" + encodeURIComponent(program), "Manual", options); + } +})(); +(function() { + self.addEventListener("DOMContentLoaded", function() { + let mouseoverTimer; + let lastTouchTimestamp; + const prefetches = new Set(); + const prefetchElement = document.createElement("link"); + const isSupported = prefetchElement.relList && prefetchElement.relList.supports && prefetchElement.relList.supports("prefetch") && window.IntersectionObserver && "isIntersecting" in IntersectionObserverEntry.prototype; + const allowQueryString = "instantAllowQueryString" in document.body.dataset; + const allowExternalLinks = "instantAllowExternalLinks" in document.body.dataset; + const useWhitelist = "instantWhitelist" in document.body.dataset; + const mousedownShortcut = "instantMousedownShortcut" in document.body.dataset; + let delayOnHover = 65; + let useMousedown = false; + let useMousedownOnly = false; + let useViewport = false; + if ("instantIntensity" in document.body.dataset) { + const intensity = document.body.dataset.instantIntensity; + if (intensity.substr(0, "mousedown".length) == "mousedown") { + useMousedown = true; + if (intensity == "mousedown-only") { + useMousedownOnly = true; + } + } else if (intensity.substr(0, "viewport".length) == "viewport") { + if (!(navigator.connection && (navigator.connection.saveData || navigator.connection.effectiveType && navigator.connection.effectiveType.includes("2g")))) { + if (intensity == "viewport") { + if (document.documentElement.clientWidth * document.documentElement.clientHeight < 450000) { + useViewport = true; + } + } else if (intensity == "viewport-all") { + useViewport = true; + } + } + } else { + const milliseconds = parseInt(intensity); + if (!isNaN(milliseconds)) { + delayOnHover = milliseconds; + } + } + } + if (isSupported) { + const eventListenersOptions = { + capture: true, + passive: true + }; + if (!useMousedownOnly) { + document.addEventListener("touchstart", touchstartListener, eventListenersOptions); + } + if (!useMousedown) { + document.addEventListener("mouseover", mouseoverListener, eventListenersOptions); + } else if (!mousedownShortcut) { + document.addEventListener("mousedown", mousedownListener, eventListenersOptions); + } + if (mousedownShortcut) { + document.addEventListener("mousedown", mousedownShortcutListener, eventListenersOptions); + } + if (useViewport) { + let triggeringFunction; + if (window.requestIdleCallback) { + triggeringFunction = function(callback) { + requestIdleCallback(callback, { + timeout: 1500 + }); + }; + } else { + triggeringFunction = function(callback) { + callback(); + }; + } + triggeringFunction(function() { + const intersectionObserver = new IntersectionObserver(function(entries) { + entries.forEach(function(entry) { + if (entry.isIntersecting) { + const linkElement = entry.target; + intersectionObserver.unobserve(linkElement); + preload(linkElement.href); + } + }); + }); + document.querySelectorAll("a").forEach(function(linkElement) { + if (isPreloadable(linkElement)) { + intersectionObserver.observe(linkElement); + } + }); + }); + } + } + function touchstartListener(event) { + lastTouchTimestamp = performance.now(); + const linkElement = event.target.closest("a"); + if (!isPreloadable(linkElement)) { + return; + } + preload(linkElement.href); + } + function mouseoverListener(event) { + if (performance.now() - lastTouchTimestamp < 1111) { + return; + } + const linkElement = event.target.closest("a"); + if (!isPreloadable(linkElement)) { + return; + } + linkElement.addEventListener("mouseout", mouseoutListener, { + passive: true + }); + mouseoverTimer = setTimeout(function() { + preload(linkElement.href); + mouseoverTimer = undefined; + }, delayOnHover); + } + function mousedownListener(event) { + const linkElement = event.target.closest("a"); + if (!isPreloadable(linkElement)) { + return; + } + preload(linkElement.href); + } + function mouseoutListener(event) { + if (event.relatedTarget && event.target.closest("a") == event.relatedTarget.closest("a")) { + return; + } + if (mouseoverTimer) { + clearTimeout(mouseoverTimer); + mouseoverTimer = undefined; + } + } + function mousedownShortcutListener(event1) { + if (performance.now() - lastTouchTimestamp < 1111) { + return; + } + const linkElement = event1.target.closest("a"); + if (event1.which > 1 || event1.metaKey || event1.ctrlKey) { + return; + } + if (!linkElement) { + return; + } + linkElement.addEventListener("click", function(event) { + if (event.detail == 1337) { + return; + } + event.preventDefault(); + }, { + capture: true, + passive: false, + once: true + }); + const customEvent = new MouseEvent("click", { + view: window, + bubbles: true, + cancelable: false, + detail: 1337 + }); + linkElement.dispatchEvent(customEvent); + } + function isPreloadable(linkElement) { + if (!linkElement || !linkElement.href) { + return; + } + if (useWhitelist && !("instant" in linkElement.dataset)) { + return; + } + if (!allowExternalLinks && linkElement.origin != location.origin && !("instant" in linkElement.dataset)) { + return; + } + if (![ + "http:", + "https:" + ].includes(linkElement.protocol)) { + return; + } + if (linkElement.protocol == "http:" && location.protocol == "https:") { + return; + } + if (!allowQueryString && linkElement.search && !("instant" in linkElement.dataset)) { + return; + } + if (linkElement.hash && linkElement.pathname + linkElement.search == location.pathname + location.search) { + return; + } + if ("noInstant" in linkElement.dataset) { + return; + } + return true; + } + function preload(url) { + if (prefetches.has(url)) { + return; + } + const prefetcher = document.createElement("link"); + prefetcher.rel = "prefetch"; + prefetcher.href = url; + document.head.appendChild(prefetcher); + prefetches.add(url); + } + }); +})(); +(function() { + self.addEventListener("DOMContentLoaded", function() { + const form = document.getElementById("search-form"); + const query = document.getElementById("search-input"); + document.getElementById("search-submit"); + const dropdown = document.getElementById("search-results"); + const container = document.getElementById("search-frame"); + form.addEventListener("focusin", function() { + initialize(); + }); + form.addEventListener("submit", function(event) { + event.preventDefault(); + return false; + }); + form.addEventListener("keydown", function(event) { + const head = dropdown.firstChild.nextElementSibling.firstChild.nextElementSibling; + const tail = dropdown.lastElementChild.firstChild.nextElementSibling; + if (query.contains(event.target)) { + if (event.keyCode == 27) { + document.activeElement.blur(); + dropdown.setAttribute("hidden", ""); + } + } + if (event.keyCode == 40) { + event.preventDefault(); + if (document.activeElement == query) head.focus(); + else if (document.activeElement == tail) query.focus(); + else { + document.activeElement.parentElement.nextElementSibling.firstChild.nextElementSibling.focus(); + } + } + if (event.keyCode == 38) { + event.preventDefault(); + if (document.activeElement == query) tail.focus(); + else if (document.activeElement == head) query.focus(); + else { + document.activeElement.parentElement.previousElementSibling.firstChild.nextElementSibling.focus(); + } + } + if (event.keyCode == 8) { + if (document.activeElement != query) { + event.preventDefault(); + query.focus(); + } + } + if (event.keyCode == 13) { + if (dropdown && document.activeElement == query) { + event.preventDefault(); + head.focus(); + self.window.location = document.activeElement.href; + } + } + }); + let scrolls = 0; + self.addEventListener("scroll", function() { + if (scrolls > 3) { + scrolls = 0; + document.activeElement.blur(); + dropdown.setAttribute("hidden", ""); + container.removeAttribute("data-focus"); + } + scrolls++; + }); + document.addEventListener("click", function(event) { + if (!form.contains(event.target)) { + dropdown.setAttribute("hidden", ""); + container.removeAttribute("data-focus"); + } + }); + function fetch_JSON(path, callback) { + const httpRequest = new XMLHttpRequest(); + httpRequest.onreadystatechange = function() { + if (httpRequest.readyState === 4) { + if (httpRequest.status === 200) { + const data = JSON.parse(httpRequest.responseText); + if (callback) callback(data); + } + } + }; + httpRequest.open("GET", path); + httpRequest.send(); + } + function load_script(url) { + return new Promise(function(resolve, reject) { + const script = document.createElement("script"); + script.onerror = reject; + script.onload = resolve; + if (document.currentScript) { + document.currentScript.parentNode.insertBefore(script, document.currentScript); + } else { + document.head.appendChild(script); + } + script.src = url; + }); + } + let first_run = true; + function initialize() { + if (first_run) { + load_script(window.location.origin + "/js/fuzzysort.js").then(()=>{ + first_run = false; + fetch_JSON("/index.json", function(data) { + const options = { + key: [ + "title" + ] + }; + query.addEventListener("keyup", function() { + search(query.value, data.items, options); + }); + query.addEventListener("focusin", function() { + search(query.value, data.items, options); + }); + search(query.value, data.items, options); + }); + }).catch((error)=>{ + console.log("Error failed to load fuzzy sort: " + error); + }); + } + } + function search(term, data, options) { + const results = fuzzysort.go(term, data, options); + let items = ""; + if (results.length === 0 && term.length >= 0) { + let separator = "—"; + if (term.length === 0) separator = ""; + items = ` + <li> + <a href="javascript: void(0)" tabindex="0">${term} ${separator} No Results Found</a> + </li> + `; + dropdown.removeAttribute("hidden"); + container.setAttribute("data-focus", ""); + } else { + dropdown.removeAttribute("hidden"); + for(const string in results.slice(0, 10)){ + let highlight = fuzzysort.highlight(fuzzysort.single(term, results[string].obj.title), "<span>", "</span>"); + if (highlight === null) { + highlight = results[string].obj.title; + } + items = items + ` + <li> + <a href="${results[string].obj.url}" tabindex="0">${highlight}</a> + </li> + `; + } + } + dropdown.innerHTML = items; + } + }); +})(); +console.log("Surface Control: OK"); |