This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/static/scripts/enhancements/check.ts

44 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2023-11-30 09:14:32 +00:00
import { check_compose } from "/static/scripts/compose.js";
2023-11-24 23:45:54 +00:00
/**
* Makes the form require the stack to be checked before submitting
2023-11-30 09:14:32 +00:00
* @param form
* @param editor
2023-11-24 23:45:54 +00:00
*/
2023-11-30 09:14:32 +00:00
export function add_check(form: HTMLFormElement, editor: AceAjax.Editor) {
2023-11-24 23:45:54 +00:00
form.addEventListener("submit", (ev) => {
ev.preventDefault();
check_stack(editor).then((result) => {
if (result) {
form.submit();
}
})
return false;
});
}
/**
* Runs the check function and updates some DOM elements
2023-11-30 09:14:32 +00:00
* @param editor Editor instance
2023-11-24 23:45:54 +00:00
* @returns true if the stack is valid, false otherwise
*/
2023-11-30 09:14:32 +00:00
export async function check_stack(editor: AceAjax.Editor) {
const source = editor.getValue();
2023-11-24 23:45:54 +00:00
const check_result = await check_compose(source);
2023-11-30 09:14:32 +00:00
const editorEl = document.querySelector(".ace_editor")!;
const editorErrorEl = document.querySelector<HTMLDivElement>("#editor-form div.error")!;
2023-11-24 23:45:54 +00:00
editorEl.classList.remove("err", "checked");
editorErrorEl.style.display = "block";
editorErrorEl.classList.add("pending");
editorErrorEl.innerHTML = "Checking...";
if (check_result.ok) {
editorEl.classList.add("checked");
editorErrorEl.style.display = "";
} else {
editorEl.classList.add("err");
editorErrorEl.classList.remove("pending");
editorErrorEl.innerHTML = check_result.error.message;
}
return check_result.ok;
}