From: Alexander Ebert Date: Sun, 2 May 2021 10:12:12 +0000 (+0200) Subject: Implement `Dom/Util.innerSuccess()` X-Git-Tag: 5.4.0_Alpha_1~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=4543ea4a32c4b5d8f0d8cac6c5b27eedf1de4f79;p=GitHub%2FWoltLab%2FWCF.git Implement `Dom/Util.innerSuccess()` Closes #4170 --- diff --git a/ts/WoltLabSuite/Core/Dom/Util.ts b/ts/WoltLabSuite/Core/Dom/Util.ts index a630f6cb0c..838c52b4ca 100644 --- a/ts/WoltLabSuite/Core/Dom/Util.ts +++ b/ts/WoltLabSuite/Core/Dom/Util.ts @@ -449,7 +449,7 @@ const DomUtil = { } } - let innerError = element.nextElementSibling; + let innerError = element.nextElementSibling as HTMLElement | null; if (innerError === null || innerError.nodeName !== "SMALL" || !innerError.classList.contains("innerError")) { if (errorMessage === "") { innerError = null; @@ -466,10 +466,64 @@ const DomUtil = { innerError = null; } } else { - innerError![isHtml ? "innerHTML" : "textContent"] = errorMessage; + if (isHtml) { + innerError!.innerHTML = errorMessage; + } else { + innerError!.textContent = errorMessage; + } + } + + return innerError; + }, + + /** + * Displays or removes an error message below the provided element. + */ + innerSuccess(element: HTMLElement, message?: string | false | null, isHtml?: boolean): HTMLElement | null { + const parent = element.parentNode; + if (parent === null) { + throw new Error("Only elements that have a parent element or document are valid."); + } + + if (typeof message !== "string") { + if (!message) { + message = ""; + } else { + throw new TypeError( + "The message must be a string; `false`, `null` or `undefined` can be used as a substitute for an empty string.", + ); + } + } + + let innerSuccess = element.nextElementSibling as HTMLElement | null; + if ( + innerSuccess === null || + innerSuccess.nodeName !== "SMALL" || + !innerSuccess.classList.contains("innerSuccess") + ) { + if (message === "") { + innerSuccess = null; + } else { + innerSuccess = document.createElement("small"); + innerSuccess.className = "innerSuccess"; + parent.insertBefore(innerSuccess, element.nextSibling); + } + } + + if (message === "") { + if (innerSuccess !== null) { + innerSuccess.remove(); + innerSuccess = null; + } + } else { + if (isHtml) { + innerSuccess!.innerHTML = message; + } else { + innerSuccess!.textContent = message; + } } - return innerError as HTMLElement | null; + return innerSuccess; }, /** diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js index 6f07819176..49f7a0ed7f 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js @@ -398,10 +398,60 @@ define(["require", "exports", "tslib", "../StringUtil"], function (require, expo } } else { - innerError[isHtml ? "innerHTML" : "textContent"] = errorMessage; + if (isHtml) { + innerError.innerHTML = errorMessage; + } + else { + innerError.textContent = errorMessage; + } } return innerError; }, + /** + * Displays or removes an error message below the provided element. + */ + innerSuccess(element, message, isHtml) { + const parent = element.parentNode; + if (parent === null) { + throw new Error("Only elements that have a parent element or document are valid."); + } + if (typeof message !== "string") { + if (!message) { + message = ""; + } + else { + throw new TypeError("The message must be a string; `false`, `null` or `undefined` can be used as a substitute for an empty string."); + } + } + let innerSuccess = element.nextElementSibling; + if (innerSuccess === null || + innerSuccess.nodeName !== "SMALL" || + !innerSuccess.classList.contains("innerSuccess")) { + if (message === "") { + innerSuccess = null; + } + else { + innerSuccess = document.createElement("small"); + innerSuccess.className = "innerSuccess"; + parent.insertBefore(innerSuccess, element.nextSibling); + } + } + if (message === "") { + if (innerSuccess !== null) { + innerSuccess.remove(); + innerSuccess = null; + } + } + else { + if (isHtml) { + innerSuccess.innerHTML = message; + } + else { + innerSuccess.textContent = message; + } + } + return innerSuccess; + }, /** * Finds the closest element that matches the provided selector. This is a helper * function because `closest()` does exist on elements only, for example, it is