interlanguage-checker/src/main/js/Main.ts

128 lines
4.2 KiB
TypeScript

const {$, doAfterLoad} = (window as any).fwdekker;
const {
clearFormValidity, showInputInvalid, showMessageBusy, showMessageError, showMessageInfo, showMessageType
} = (window as any).fwdekker.validation;
import {InterlangTable} from "./InterlangTable";
import {discoverNetwork, InterlangNetwork, MediaWiki, MediaWikiManager, NetworkVerdict} from "./MediaWiki";
// Contains global functions for debugging
(window as any).ilc = {};
// Handle "About" toggle
doAfterLoad(() => {
const about = $("#about");
const key = "/tools/interlanguage-checker//toggle-about";
about.addEventListener("toggle", () => localStorage.setItem(key, "" + !!about.open));
const storedState = localStorage.getItem(key);
about.open = storedState === null || storedState === "true";
});
// Handle input
doAfterLoad(async () => {
const form = $("#inputs");
const urlInput = $("#url");
const articleInput = $("#article");
// Form submission
let previousUrl: string | undefined;
let mwm: MediaWikiManager | undefined;
const submit = async () => {
localStorage.setItem("/tools/interlanguage-checker//api-url", urlInput.value);
// Clean up
clearFormValidity(form);
$("#network-table")?.remove();
// Validate
const urlValue = urlInput.value;
const articleValue = articleInput.value;
if (urlValue.trim() === "")
return showInputInvalid(urlInput, "Enter the API URL.");
try {
new URL(urlValue); // Throws exception if invalid
} catch (error) {
try {
const url = `http://${urlValue}`;
new URL(url); // Throws exception if invalid
$("#url").value = url;
return "";
} catch {
return showInputInvalid(urlInput, (error as Error).message);
}
}
if (articleValue.trim() === "")
return showInputInvalid(articleInput, "Enter the name of the article to check.");
// Initialize
if (urlValue !== previousUrl) {
showMessageBusy(form, `Initializing <code>${urlValue}</code>`);
try {
const mw = await new MediaWiki(urlValue).init();
mwm = await new MediaWikiManager().init(mw);
} catch (error) {
return showMessageError(form, (error as Error).message);
}
previousUrl = urlValue;
}
// Discover
discoverNetwork(
mwm!,
articleInput.value,
(type, message) => showMessageType(form, message, type),
it => showMessageType(form, it, "busy")
)
.then(it => new InterlangNetwork(it.pages, it.redirects))
.then(network => {
showMessageType(form, "Creating table", "busy");
const tableForm = $("#network-table-form");
tableForm.textContent = "";
tableForm.appendChild((new InterlangTable()).render("network-table", network));
const props = NetworkVerdict.props[network.getNetworkVerdict()];
showMessageType(form, props.message, props.type);
})
.catch(error => showMessageError(form, error));
};
form.addEventListener("submit", (event: SubmitEvent) => {
event.preventDefault();
submit();
});
urlInput.focus(); // Default focus
// Read inputs from cookies
const apiUrl = localStorage.getItem("/tools/interlanguage-checker//api-url");
if (apiUrl !== null && apiUrl.trim() !== "") {
urlInput.value = apiUrl;
articleInput.focus();
}
// Read inputs from URL
const currentParams = new URLSearchParams(window.location.search);
if (currentParams.has("api")) {
urlInput.value = currentParams.get("api")!;
articleInput.focus();
}
if (currentParams.has("article")) articleInput.value = currentParams.get("article")!;
if (currentParams.has("api") && currentParams.has("article")) await submit();
// Set global debug function
(window as any).ilc.getCurrentInputAsUrl = () =>
location.href.split("?")[0] +
`?api=${encodeURI(urlInput.value)}` +
`&article=${encodeURI(articleInput.value)}`;
});