interlanguage-checker/src/main/js/DOM.js

428 lines
15 KiB
JavaScript

// noinspection JSUnresolvedVariable
const {stringToHtml} = window.fwdekker;
/**
* An input that can be validated.
*
* @property input {HTMLElement} the input that is validatable
*/
export class ValidatableInput {
/**
* Constructs a new validatable input.
*
* @param input {HTMLInputElement} the input that is validatable
* @param isValid {function(string): string} returns an empty string if the given input string is valid, and a
* string explaining why it is is invalid otherwise
*/
constructor(input, isValid) {
this.input = input;
this._isValid = isValid;
}
/**
* Returns the value of the underlying input element.
*
* @return {String} the value of the underlying input element
*/
getValue() {
return this.input.value;
}
/**
* Sets the value of the underlying input element.
*
* @param value {string} the value to set
*/
setValue(value) {
this.input.value = value;
}
/**
* Validates the input.
*
* @return {String} an empty string if the input string is valid, and a string explaining why it is is invalid
* otherwise
*/
validate() {
const validity = this._isValid(this.input.value);
if (validity.length === 0) this.showSuccess();
else this.showError();
return validity;
}
/**
* Marks the input as neither valid nor invalid.
*/
showBlank() {
this.input.dataset["entered"] = "false";
this.input.setCustomValidity("");
}
/**
* Marks the input as invalid and moves focus to it.
*/
showError() {
this.input.dataset["entered"] = "true";
this.input.setCustomValidity("Incorrect");
this.input.focus();
}
/**
* Marks the input as valid.
*/
showSuccess() {
this.input.dataset["entered"] = "true";
this.input.setCustomValidity("");
}
}
/**
* Interacts with the DOM to delegate errors to the user.
*/
export class ErrorHandler {
/**
* Constructs a new `ErrorHandler`, inserting relevant new elements into the DOM to interact with.
*
* @param parent {HTMLElement} the element to insert elements into
* @param [id] {string} the id of the div containing the errors
*/
constructor(parent, id) {
this._outerDiv = document.createElement("div");
this._outerDiv.classList.add("errorOuter", "hidden");
parent.appendChild(this._outerDiv);
}
/**
* Handles the displaying of the given error.
*
* @param [level] {"warning"|"error"|null} the level of message to display, determines the style of the text
* @param [message] {*} the message to display
* @returns {ErrorHandler} this `ErrorHandler`
*/
handle(level, message) {
this._outerDiv.classList.remove("hidden");
const errorInner = document.createElement("div");
errorInner.classList.add("errorInner");
if (level !== null) errorInner.classList.add(level);
errorInner.innerText = message;
this._outerDiv.appendChild(errorInner);
return this;
}
/**
* Clears all errors from the DOM.
*
* @returns {ErrorHandler} this `ErrorHandler`
*/
clear() {
this._outerDiv.classList.add("hidden");
this._outerDiv.innerHTML = "";
return this;
}
}
/**
* Interacts with the DOM to delegate messages to the user.
*/
export class MessageHandler {
/**
* Constructs a new `MessageHandler`, inserting relevant new elements into the DOM to interact with.
*
* @param parent {HTMLElement} the element to insert elements into
*/
constructor(parent) {
this._outerDiv = document.createElement("div");
this._outerDiv.classList.add("messageOuter", "hidden");
parent.appendChild(this._outerDiv);
this._innerDiv = document.createElement("div");
this._innerDiv.classList.add("messageInner");
this._outerDiv.appendChild(this._innerDiv);
this._loadingIcon = document.createElement("i");
this._loadingIcon.classList.add("fa", "fa-spinner", "fa-spin");
this._innerDiv.appendChild(this._loadingIcon);
this._spacing = document.createElement("span");
this._spacing.innerHTML = " ";
this._innerDiv.appendChild(this._spacing);
this._textSpan = document.createElement("span");
this._innerDiv.appendChild(this._textSpan);
this._currentLevel = undefined;
this._callback = undefined;
}
/**
* Handles the displaying of the given message.
*
* If no message is given, the current message and the loading icon are hidden. To display an empty message next to
* the loading icon, give an empty string.
*
* @param [level] {"complete"|"progress"|"warning"|"error"|"neutral"|undefined} the level of message to display, or
* `undefined` if the entire message handler should be hidden
* @param [message] {*} the message to display
* @returns {MessageHandler} this `MessageHandler`
*/
handle(level, message) {
this._displayLevel(level);
if (level === undefined) return this; // No need to handle the rest
if (this._callback !== undefined) this._callback(level, message);
this._textSpan.innerHTML = message;
return this;
}
/**
* Calls `#handle` without any arguments.
*
* @returns {MessageHandler} this `MessageHandler`
*/
clear() {
return this.handle();
}
/**
* Sets the callback to be executed whenever a message is handler by this handler.
*
* @param callback {function("complete"|"progress"|"warning"|"error"|"neutral", [*]): *} the function to execute
* whenever a message is handled
* @returns {MessageHandler} this `MessageHandler`
*/
setCallback(callback) {
this._callback = callback;
return this;
}
/**
* Changes the appearance of the message handler to that of the given level.
*
* @param level {"complete"|"progress"|"warning"|"error"|"neutral"} the level to change appearance to
* @private
*/
_displayLevel(level) {
if (level === this._currentLevel) return;
this._currentLevel = level;
this._outerDiv.classList.remove("hidden");
this._innerDiv.classList.remove("success", "warning", "error");
switch (level) {
case "complete":
this._innerDiv.classList.add("success");
this._toggleLoadingIcon(false);
break;
case "progress":
this._toggleLoadingIcon(true);
break;
case "warning":
this._innerDiv.classList.add("warning");
this._toggleLoadingIcon(false);
break;
case "error":
this._innerDiv.classList.add("error");
this._toggleLoadingIcon(false);
break;
case "neutral":
this._toggleLoadingIcon(false);
break;
default:
this._outerDiv.classList.add("hidden");
break;
}
}
/**
* Turns the loading icon on or off.
*
* @param state {boolean} `true` if and only if the loading icon should be on
* @returns {MessageHandler} this `MessageHandler`
* @private
*/
_toggleLoadingIcon(state) {
if (state) {
this._loadingIcon.classList.remove("hidden");
this._spacing.classList.remove("hidden");
} else {
this._loadingIcon.classList.add("hidden");
this._spacing.classList.add("hidden");
}
return this;
}
}
/**
* A network of interlanguage links.
*
* @property pages {Page[]} the pages in the network
*/
export class InterlangTable {
/**
* Generates an icon element with the given title and additional classes.
*
* @param icon {string} the name of the icon to display
* @param title {string} the title of the icon, used for the `title` attribute
* @param [classes] {string[]} the additional classes to apply to the icon
* @return {String} an icon element with the given title and additional classes
* @private
*/
_createIcon(icon, title, classes) {
return `<i class="fa fa-${icon} ${(classes || []).join(" ")}" title="${title}"></i>`;
}
/**
* Returns an appropriate label for the given page.
*
* The label contains a link to the page and a few buttons to help the user interact with that page. The label's
* appearance and contents depend both on the properties of the page (e.g. whether it exists and whether it's a
* redirect page) and on the other pages in this network (e.g. whether it's the only page in its language).
*
* @param pages {Page[]} a list of all pages
* @param page {Page} the page to generate a label of
* @return {String} an appropriate label with icons for the given page
* @private
*/
_generateLabel(pages, page) {
const labelText = pages.some(it => it.link.lang === page.link.lang && !it.link.equals(page.link))
? page.link.toString()
: page.link.lang;
return "" +
`<span class="${page.exists ? "" : "redLink"}">` +
/**/`<a href="${page.url}" target="_blank" title="${page.link}">${labelText}</a>` +
/**/`<span> </span>` +
/**/`<a href="${page.url}?action=edit" target="_blank" title="Edit"><i class="fa fa-pencil"></i></a>` +
/**/`<span> </span>` +
/**/`<a title="Copy"><i class="fa fa-clipboard copyIcon" data-clipboarddata="${page.link}"></i></a>` +
`</span>`;
}
/**
* Generates the head of the table generated by `#toTable`.
*
* @param network {InterlangNetwork} the network to generate the head for
* @return {String} the head of the table generated by `#toTable`
* @private
*/
_generateTableHead(network) {
return "" +
`<thead>` +
/**/`<tr>` +
/****/`<th rowspan="2"></th>` +
/****/`<th class="sourceLabel" rowspan="2">Source</th>` +
/****/`<th colspan="${network.pages.length}">Destination</th>` +
/**/`</tr>` +
/**/`<tr>${network.pages.map(page => `<th>${this._generateLabel(network.pages, page)}</th>`)}</tr>` +
`</thead>`;
}
/**
* Generates the body of the table generated by `#toTable`.
*
* @param network {InterlangNetwork} the network to generate the body for
* @return {String} the body of the table generated by `#toTable`
* @private
*/
_generateTableBody(network) {
const rows = network.pages.map(srcPage => {
const verdict = network.getPageVerdict(srcPage);
const icons = verdict.self
.map(state => {
switch (state) {
case "perfect":
return this._createIcon("check", "Perfect 🙂", ["success"]);
case "not-found":
return this._createIcon("search", "Article does not exist 😕", ["error"]);
case "wrongly-ordered":
return this._createIcon("sort-alpha-asc", "Links are in the wrong order 😕", ["warning"]);
case "doubly-linked":
return this._createIcon("clone", "Links to the same wiki multiple times 😕", ["warning"]);
case "self-linked":
return this._createIcon("rotate-left", "Links to its own wiki 😕", ["warning"]);
case "unlinked":
return this._createIcon("chain-broken", "Misses one or more links 😕", ["error"]);
case "redirected":
return this._createIcon("mail-forward", "Links to a redirect 😕", ["warning"]);
case "wrongly-cased":
return this._createIcon("text-height", "Links with incorrect capitalisation 😕", ["warning"]);
default:
throw new Error(`Invalid page state '${state}'`);
}
})
.map(it => `${it}<span> </span>`);
const label = this._generateLabel(network.pages, srcPage);
const cells = network.pages.map(dstPage => {
const linkState = verdict.pages.find(it => it.page.link.equals(dstPage.link)).verdict;
switch (linkState) {
case "linked":
return this._createIcon("check", "Linked 🙂", ["success"]);
case "self-linked":
return this._createIcon("rotate-left", "Links to its own wiki 😕", ["warning"]);
case "unlinked":
return this._createIcon("times", "Link is missing 😕", ["error"]);
case "self-unlinked":
return `<span></span>`;
case "redirected":
return this._createIcon("mail-forward", "Links to a redirect 😕", ["warning"]);
case "wrongly-cased":
return this._createIcon("text-height", "Links with incorrect capitalisation 😕", ["warning"]);
default:
throw new Error(`Invalid link state '${linkState}'`);
}
});
return "" +
`<tr>` +
/**/`<th>${icons}</th>` +
/**/`<th class="sourceLabel">${label}</th>` +
/**/cells.map(it => `<td>${it}</td>`) +
`</tr>`;
});
return `<tbody>${rows}</tbody>`;
}
/**
* Renders the the table describing the interlanguage network.
*
* @param id {String} the ID to assign to the table element
* @param network {InterlangNetwork} the network of pages to render
* @return {HTMLElement} the generated table
*/
render(id, network) {
const table = stringToHtml(
`<table id="${id}">` +
/**/this._generateTableHead(network) +
/**/this._generateTableBody(network) +
`</table>`,
"table"
);
// Add event handlers
table.querySelectorAll(".copyIcon").forEach(icon => {
icon.addEventListener("click", () => {
// noinspection JSIgnoredPromiseFromCall
navigator.clipboard.writeText(`[[${icon.dataset.clipboarddata}]]`);
icon.classList.replace("fa-clipboard", "fa-check");
setTimeout(() => icon.classList.replace("fa-check", "fa-clipboard"), 1000);
});
});
return table;
}
}