aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortdro <tdro@noreply.example.com>2024-08-04 00:00:00 +0000
committertdro <tdro@noreply.example.com>2024-08-04 00:00:00 +0000
commitdb0e69fb286d03d72e73b79e10a682980a5c7429 (patch)
treee04cf8f2802fb5d79444701a4b07dcc5ef611217
parent443652fe6cd393eba0ca89739fd481a0bb671550 (diff)
downloadcanory-db0e69fb286d03d72e73b79e10a682980a5c7429.tar.gz
canory-db0e69fb286d03d72e73b79e10a682980a5c7429.tar.bz2
canory-db0e69fb286d03d72e73b79e10a682980a5c7429.zip
static/js/fixedsearch: Formatting
-rw-r--r--assets/js/index.js12
-rw-r--r--static/js/fixedsearch.ts420
2 files changed, 208 insertions, 224 deletions
diff --git a/assets/js/index.js b/assets/js/index.js
index e05fa01..2b61453 100644
--- a/assets/js/index.js
+++ b/assets/js/index.js
@@ -315,9 +315,7 @@
};
const submit = Submit();
function first(element) {
- if (element.firstChild.nextElementSibling) {
- return element.firstChild.nextElementSibling.firstChild.nextElementSibling;
- }
+ if (element.firstChild.nextElementSibling) return element.firstChild.nextElementSibling.firstChild.nextElementSibling;
}
function last(element) {
return element.lastElementChild.firstChild.nextElementSibling;
@@ -329,12 +327,12 @@
return element.activeElement.parentElement.previousElementSibling.firstChild.nextElementSibling.focus();
}
function press(keyname) {
- document.dispatchEvent(new KeyboardEvent("keydown", {
+ document.dispatchevent(new keyboardevent("keydown", {
"key": keyname
}));
}
- let selected;
if (submit === null) return;
+ let selected;
submit.addEventListener("click", function(event) {
if (selected) {
event.preventDefault();
@@ -421,9 +419,7 @@
function fetch(url, callback) {
const http = new XMLHttpRequest();
http.onreadystatechange = function() {
- if (http.readyState === 4 && http.status === 200 && callback) {
- callback(http);
- }
+ if (http.readyState === 4 && http.status === 200 && callback) callback(http);
};
http.open("GET", url);
http.send();
diff --git a/static/js/fixedsearch.ts b/static/js/fixedsearch.ts
index fc2dee1..9c56784 100644
--- a/static/js/fixedsearch.ts
+++ b/static/js/fixedsearch.ts
@@ -7,260 +7,248 @@
(function () {
["DOMContentLoaded", "URLChangedCustomEvent"].forEach(function (event) {
- self.addEventListener(event, function () {
-
- const Container = function () { return document.querySelector("search-box") }; const container = Container();
- const Dropdown = function () { return document.querySelector("search-box ul") }; const dropdown = Dropdown();
- const Form = function () { return document.querySelector("search-box form") }; const form = Form();
- const Query = function () { return document.querySelector("search-box input") }; const query = Query();
- const Submit = function () { return document.querySelector("search-box button") }; const submit = Submit();
-
- function first(element) {
- if (element.firstChild.nextElementSibling) {
- return element.firstChild.nextElementSibling.firstChild
- .nextElementSibling;
- }
- }
+ self.addEventListener(event, function () {
- function last(element) {
- return element.lastElementChild.firstChild.nextElementSibling;
- }
+ const Container = function () { return document.querySelector("search-box") }; const container = Container();
+ const Dropdown = function () { return document.querySelector("search-box ul") }; const dropdown = Dropdown();
+ const Form = function () { return document.querySelector("search-box form") }; const form = Form();
+ const Query = function () { return document.querySelector("search-box input") }; const query = Query();
+ const Submit = function () { return document.querySelector("search-box button") }; const submit = Submit();
- function next(element) {
- return element.activeElement.parentElement.nextElementSibling.firstChild
- .nextElementSibling.focus();
- }
+ function first(element) {
+ if (element.firstChild.nextElementSibling) return element.firstChild.nextElementSibling.firstChild.nextElementSibling;
+ }
- function previous(element) {
- return element.activeElement.parentElement.previousElementSibling
- .firstChild
- .nextElementSibling.focus();
- }
+ function last(element) {
+ return element.lastElementChild.firstChild.nextElementSibling;
+ }
- function press(keyname) {
- document.dispatchEvent(new KeyboardEvent("keydown", { "key": keyname }));
- }
+ function next(element) {
+ return element.activeElement.parentElement.nextElementSibling.firstChild.nextElementSibling.focus();
+ }
- let selected;
+ function previous(element) {
+ return element.activeElement.parentElement.previousElementSibling.firstChild.nextElementSibling.focus();
+ }
- if (submit === null) return;
+ function press(keyname) { document.dispatchevent(new keyboardevent("keydown", { "key": keyname })); }
- submit.addEventListener("click", function (event) {
- if (selected) {
- event.preventDefault();
- selected.focus();
- return selected.click();
- }
- first(dropdown).focus();
- press("ArrowDown");
- document.activeElement.click();
- });
+ if (submit === null) return;
- ["keyup", "click"].forEach(function (event) {
- form.addEventListener(event, function () {
- if (document.activeElement.nodeName === "A") {
- return selected = document.activeElement;
+ let selected;
+
+ submit.addEventListener("click", function (event) {
+ if (selected) {
+ event.preventDefault();
+ selected.focus();
+ return selected.click();
}
- selected = undefined;
+ first(dropdown).focus();
+ press("ArrowDown");
+ document.activeElement.click();
});
- });
- form.addEventListener("focusin", function () {
- container.setAttribute("data-focus", "");
- initialize();
- });
+ ["keyup", "click"].forEach(function (event) {
+ form.addEventListener(event, function () {
+ if (document.activeElement.nodeName === "A") {
+ return selected = document.activeElement;
+ }
+ selected = undefined;
+ });
+ });
- form.addEventListener("submit", function (event) {
- event.preventDefault();
- return false;
- });
+ form.addEventListener("focusin", function () {
+ container.setAttribute("data-focus", "");
+ initialize();
+ });
- form.addEventListener("keydown", function (event) {
- // ESC (27)
- if (form.contains(event.target)) {
- if (event.keyCode == 27) {
- document.activeElement.blur();
- dropdown.setAttribute("hidden", "");
- container.removeAttribute("data-focus");
- }
- }
+ form.addEventListener("submit", function (event) {
+ event.preventDefault();
+ return false;
+ });
- // BACKSPACE (8)
- if (event.keyCode == 8) {
- if (document.activeElement != query) {
- event.preventDefault();
- query.focus();
+ form.addEventListener("keydown", function (event) {
+ // ESC (27)
+ if (form.contains(event.target)) {
+ if (event.keyCode == 27) {
+ document.activeElement.blur();
+ dropdown.setAttribute("hidden", "");
+ container.removeAttribute("data-focus");
+ }
}
- }
-
- const head = first(dropdown);
- const tail = last(dropdown);
- // DOWN (40)
- if (event.keyCode == 40) {
- event.preventDefault();
- if (document.activeElement == query) head.focus();
- else if (document.activeElement == tail) query.focus();
- else next(document);
- }
+ // BACKSPACE (8)
+ if (event.keyCode == 8) {
+ if (document.activeElement != query) {
+ event.preventDefault();
+ query.focus();
+ }
+ }
- // UP (38)
- if (event.keyCode == 38) {
- event.preventDefault();
- if (document.activeElement == query) tail.focus();
- else if (document.activeElement == head) query.focus();
- else previous(document);
- }
+ const head = first(dropdown);
+ const tail = last(dropdown);
- // ENTER (13)
- if (event.keyCode == 13) {
- if (dropdown && document.activeElement == query) {
+ // DOWN (40)
+ if (event.keyCode == 40) {
event.preventDefault();
- head.focus();
- head.click();
+ if (document.activeElement == query) head.focus();
+ else if (document.activeElement == tail) query.focus();
+ else next(document);
}
- }
- });
- if (event === "DOMContentLoaded") {
- let scrolls = 0;
- self.addEventListener("scroll", function () {
- if (scrolls > 3) {
- scrolls = 0;
- document.activeElement.blur();
- Dropdown().setAttribute("hidden", "");
- Container().removeAttribute("data-focus");
+ // UP (38)
+ if (event.keyCode == 38) {
+ event.preventDefault();
+ if (document.activeElement == query) tail.focus();
+ else if (document.activeElement == head) query.focus();
+ else previous(document);
}
- scrolls++;
- });
- self.addEventListener("click", function (event) {
- if (
- !Form().contains(event.target) &&
- !(document.activeElement === Query()) &&
- !(document.activeElement === Submit())
- ) {
- Dropdown().setAttribute("hidden", "");
- Container().removeAttribute("data-focus");
+ // ENTER (13)
+ if (event.keyCode == 13) {
+ if (dropdown && document.activeElement == query) {
+ event.preventDefault();
+ head.focus();
+ head.click();
+ }
}
});
- }
- function fetch(url, callback) {
- const http = new XMLHttpRequest();
- http.onreadystatechange = function () {
- if (http.readyState === 4 && http.status === 200 && callback) {
- callback(http);
- }
- };
- http.open("GET", url);
- http.send();
- }
-
- /* Load script based on https://stackoverflow.com/a/55451823 */
-
- function 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;
- });
- }
+ if (event === "DOMContentLoaded") {
+ let scrolls = 0;
+ self.addEventListener("scroll", function () {
+ if (scrolls > 3) {
+ scrolls = 0;
+ document.activeElement.blur();
+ Dropdown().setAttribute("hidden", "");
+ Container().removeAttribute("data-focus");
+ }
+ scrolls++;
+ });
+ self.addEventListener("click", function (event) {
+ if (
+ !Form().contains(event.target) &&
+ !(document.activeElement === Query()) &&
+ !(document.activeElement === Submit())
+ ) {
+ Dropdown().setAttribute("hidden", "");
+ Container().removeAttribute("data-focus");
+ }
+ });
+ }
+
+ function fetch(url, callback) {
+ const http = new XMLHttpRequest();
+ http.onreadystatechange = function () {
+ if (http.readyState === 4 && http.status === 200 && callback) callback(http);
+ };
+ http.open("GET", url);
+ http.send();
+ }
+
+ /* Load script based on https://stackoverflow.com/a/55451823 */
+
+ function 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 data = {};
- let boot = true;
+ let data = {};
+ let boot = true;
- const options = { key: ["title"] };
+ const options = { key: ["title"] };
- function isEmpty(obj) { return Object.keys(obj).length === 0; }
+ function isEmpty(obj) { return Object.keys(obj).length === 0; }
- function appendItemsTo(local, remote) {
- const paginated = Object.keys(remote).includes("next_url");
- if (isEmpty(local)) {
- local = remote;
- } else {
- local.items = local.items.concat(remote.items);
+ function appendItemsTo(local, remote) {
+ const paginated = Object.keys(remote).includes("next_url");
+ if (isEmpty(local)) {
+ local = remote;
+ } else {
+ local.items = local.items.concat(remote.items);
+ }
+ if (paginated) {
+ fetch(remote.next_url, function (request) {
+ appendItemsTo(local, JSON.parse(request.responseText));
+ });
+ } else search(query.value, data.items, options);
+ data = local;
}
- if (paginated) {
- fetch(remote.next_url, function (request) {
- appendItemsTo(local, JSON.parse(request.responseText));
- });
- } else search(query.value, data.items, options);
- data = local;
- }
-
- function initialize() {
- if (boot) {
- script(window.location.origin + "/js/fuzzysort.js")
- .then(function () {
-
- fetch("/index.json", function (request) {
- appendItemsTo({}, JSON.parse(request.responseText));
- search("", data.items, options);
- boot = false;
- });
+ function initialize() {
+ if (boot) {
+ script(window.location.origin + "/js/fuzzysort.js")
+ .then(function () {
- ["keyup", "focusin"].forEach(function (event) {
- query.addEventListener(event, function () {
- if (data.items) search(query.value, data.items, options);
- else { boot = true; initialize(); }
+ fetch("/index.json", function (request) {
+ appendItemsTo({}, JSON.parse(request.responseText));
+ search("", data.items, options);
+ boot = false;
});
+
+ ["keyup", "focusin"].forEach(function (event) {
+ query.addEventListener(event, function () {
+ if (data.items) search(query.value, data.items, options);
+ else { boot = true; initialize(); }
+ });
+ });
+
+ }).catch(function (error) {
+ console.error("ERROR: Failed to load fuzzy search", error);
});
+ }
+ }
- }).catch(function (error) {
- console.error("ERROR: Failed to load fuzzy search", error);
- });
+ function escape(text) {
+ const escaped = document.createElement("textarea");
+ escaped.textContent = text;
+ return escaped.innerHTML;
}
- }
-
- function escape(text) {
- const escaped = document.createElement("textarea");
- escaped.textContent = text;
- return escaped.innerHTML;
- }
-
- 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 tabindex="0">'.concat(
- escape(term),
- " ",
- ).concat(separator, " No Results Found</a></li>");
- dropdown.removeAttribute("hidden");
- container.setAttribute("data-focus", "");
- } else {
- dropdown.removeAttribute("hidden");
- for (var string in results.slice(0, 10)) {
- const title = results[string].obj.title;
- let highlight = fuzzysort.highlight(
- fuzzysort.single(escape(term), escape(title)),
- "<span>",
- "</span>",
- );
-
- if (highlight === null) highlight = title;
-
- items = items +
- '\n<li>\n<a href="'.concat(
- results[string].obj.url,
- '" tabindex="0">',
- ).concat(highlight, "</a>\n</li>\n");
+
+ 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 tabindex="0">'.concat(
+ escape(term),
+ " ",
+ ).concat(separator, " No Results Found</a></li>");
+ dropdown.removeAttribute("hidden");
+ container.setAttribute("data-focus", "");
+ } else {
+ dropdown.removeAttribute("hidden");
+ for (var string in results.slice(0, 10)) {
+ const title = results[string].obj.title;
+ let highlight = fuzzysort.highlight(
+ fuzzysort.single(escape(term), escape(title)),
+ "<span>",
+ "</span>",
+ );
+
+ if (highlight === null) highlight = title;
+
+ items = items +
+ '\n<li>\n<a href="'.concat(
+ results[string].obj.url,
+ '" tabindex="0">',
+ ).concat(highlight, "</a>\n</li>\n");
+ }
}
+ dropdown.innerHTML = items;
}
- dropdown.innerHTML = items;
- }
- });
+ });
});
})();