style: format static/htmx.ts
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
export {};
|
|
||||||
|
|
||||||
import { onReady } from "./utils";
|
import { onReady } from "./utils";
|
||||||
|
|
||||||
type ToastFn = (opts: {
|
type ToastFn = (opts: {
|
||||||
@@ -10,10 +8,7 @@ type ToastFn = (opts: {
|
|||||||
|
|
||||||
type HtmxParameters = Record<string, string | string[]>;
|
type HtmxParameters = Record<string, string | string[]>;
|
||||||
|
|
||||||
type HtmxConfigRequestDetail = {
|
type HtmxConfigRequestDetail = { elt?: Element; parameters?: HtmxParameters };
|
||||||
elt?: Element;
|
|
||||||
parameters?: HtmxParameters;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getToast = (): ToastFn | null => {
|
const getToast = (): ToastFn | null => {
|
||||||
const anyWindow = window as unknown as { showToast?: ToastFn };
|
const anyWindow = window as unknown as { showToast?: ToastFn };
|
||||||
@@ -25,7 +20,9 @@ const toast = (message: string): void => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const setBusy = (el: Element | null, busy: boolean): void => {
|
const setBusy = (el: Element | null, busy: boolean): void => {
|
||||||
if (!(el instanceof HTMLElement)) return;
|
if (!(el instanceof HTMLElement)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
el.toggleAttribute("aria-busy", busy);
|
el.toggleAttribute("aria-busy", busy);
|
||||||
el.dataset.htmxLoading = busy ? "true" : "false";
|
el.dataset.htmxLoading = busy ? "true" : "false";
|
||||||
|
|
||||||
@@ -50,17 +47,23 @@ const isBrowseForm = (form: HTMLFormElement): boolean =>
|
|||||||
form.getAttribute("action") === "/browse" || form.getAttribute("hx-get") === "/browse";
|
form.getAttribute("action") === "/browse" || form.getAttribute("hx-get") === "/browse";
|
||||||
|
|
||||||
const selectedGenreValues = (): string[] =>
|
const selectedGenreValues = (): string[] =>
|
||||||
Array.from(document.querySelectorAll<HTMLInputElement>("[data-genre-visual]:checked"))
|
[...document.querySelectorAll<HTMLInputElement>("[data-genre-visual]:checked")]
|
||||||
.map((input) => input.value)
|
.map((input) => input.value)
|
||||||
.filter((value) => value !== "");
|
.filter((value) => value !== "");
|
||||||
|
|
||||||
const syncBrowseRequestParameters = (event: Event): void => {
|
const syncBrowseRequestParameters = (event: Event): void => {
|
||||||
const detail = (event as CustomEvent<HtmxConfigRequestDetail>).detail;
|
const { detail } = event as CustomEvent<HtmxConfigRequestDetail>;
|
||||||
if (!detail.parameters) return;
|
if (!detail.parameters) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const form = detail.elt instanceof HTMLFormElement ? detail.elt : detail.elt?.closest("form");
|
const form = detail.elt instanceof HTMLFormElement ? detail.elt : detail.elt?.closest("form");
|
||||||
if (!(form instanceof HTMLFormElement)) return;
|
if (!(form instanceof HTMLFormElement)) {
|
||||||
if (!isBrowseForm(form)) return;
|
return;
|
||||||
|
}
|
||||||
|
if (!isBrowseForm(form)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const checkbox = document.querySelector<HTMLInputElement>("[data-sfw-checkbox]");
|
const checkbox = document.querySelector<HTMLInputElement>("[data-sfw-checkbox]");
|
||||||
if (checkbox) {
|
if (checkbox) {
|
||||||
@@ -87,9 +90,11 @@ onReady(() => {
|
|||||||
setBusy(getTriggerFromHtmxEvent(event), false);
|
setBusy(getTriggerFromHtmxEvent(event), false);
|
||||||
|
|
||||||
const remaining = document.querySelectorAll(".continue-watching-item").length;
|
const remaining = document.querySelectorAll(".continue-watching-item").length;
|
||||||
if (remaining !== 0) return;
|
if (remaining !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const section = document.getElementById("continue-watching-section");
|
const section = document.querySelector("#continue-watching-section");
|
||||||
section?.remove();
|
section?.remove();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -100,8 +105,12 @@ onReady(() => {
|
|||||||
document.addEventListener("htmx:afterSwap", (event) => {
|
document.addEventListener("htmx:afterSwap", (event) => {
|
||||||
const detail = event as CustomEvent<{ target?: EventTarget | null }>;
|
const detail = event as CustomEvent<{ target?: EventTarget | null }>;
|
||||||
const target = detail.detail?.target;
|
const target = detail.detail?.target;
|
||||||
if (!(target instanceof HTMLElement)) return;
|
if (!(target instanceof HTMLElement)) {
|
||||||
if (!target.classList.contains("error")) return;
|
return;
|
||||||
|
}
|
||||||
|
if (!target.classList.contains("error")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
toast("Failed to load content");
|
toast("Failed to load content");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user