import { query, next, prev, hasType, parent, queryAll, onWeak, isChrome, isMac, isIOS, on, hasClass, rootSizeInPixels } from "../utils"; function updatePlaceholder(element: HTMLInputElement | HTMLTextAreaElement) { if (!element.placeholder) element.placeholder = " "; } function onClickLabel(e: Event) { const label = e.currentTarget as HTMLLabelElement; const field = parent(label); const input = query("input:not([type=file], [type=checkbox], [type=radio]), select, textarea", field) as HTMLElement; if (input) input.focus(); } function onFocusInput(e: Event) { const input = e.currentTarget as HTMLInputElement; updateInput(input); } function onBlurInput(e: Event) { const input = e.currentTarget as HTMLInputElement; updateInput(input); } function onChangeFile(e: Event) { const input = e.currentTarget as HTMLInputElement; updateFile(input); } function onChangeColor(e: Event) { const input = e.currentTarget as HTMLInputElement; updateColor(input); } function onKeydownFile(e: KeyboardEvent) { const input = e.currentTarget as HTMLInputElement; updateFile(input, e); } function onKeydownColor(e: KeyboardEvent) { const input = e.currentTarget as HTMLInputElement; updateColor(input, e); } function onPasswordIconClick(e: Event) { const icon = e.currentTarget as HTMLElement; const input = query("input", parent(icon)) as HTMLInputElement; if (input && icon.textContent?.includes("visibility")) input.type = input.type === "password" ? "text" : "password"; } function onInputTextarea(e: Event) { const textarea = e.currentTarget as HTMLTextAreaElement; updateTextarea(textarea); } function updateAllLabels() { const labels = queryAll(".field > label"); for (let i=0; i input:not([type=file], [type=color], [type=range])") as NodeListOf; for (let i=0; i select") as NodeListOf; for (let i=0; i input[type=file]") as NodeListOf; for (let i=0; i input[type=color]") as NodeListOf; for (let i=0; i textarea") as NodeListOf; for (let i=0; i x.name).join(", ") : ""; nextInput.readOnly = true; onWeak(nextInput, "keydown", onKeydownFile, false); updateInput(nextInput); } function updateColor(input: HTMLInputElement, e?: KeyboardEvent) { if (e?.key === "Enter") { const previousInput = prev(input) as HTMLInputElement; if (!hasType(previousInput, "color")) return; previousInput.click(); return; } const nextInput = next(input) as HTMLInputElement; if (!hasType(nextInput, "text")) return; nextInput.readOnly = true; nextInput.value = input.value; onWeak(nextInput, "keydown", onKeydownColor, false); updateInput(nextInput); } function updateTextarea(textarea: HTMLTextAreaElement) { updatePlaceholder(textarea); if (textarea.hasAttribute("rows")) return; const rootSize = rootSizeInPixels(); textarea.style.blockSize = "auto"; textarea.style.blockSize = `${textarea.scrollHeight - rootSize}px`; } export function updateAllFields() { updateAllLabels(); updateAllInputs(); updateAllSelects(); updateAllFiles(); updateAllColors(); updateAllTextareas(); updateAllPasswordIcons(); }