Changeset View
Changeset View
Standalone View
Standalone View
web/src/utils.js
- This file was added.
| // SPDX-FileCopyrightText: 2026 g10 code GmbH | |||||
| // SPDX-Contributor: Thomas Friedrichsmeier <thomas.friedrichsmeier@gnupg.com> | |||||
| // SPDX-License-Identifier: GPL-2.0-or-later | |||||
| // More of less generic/low-level helper functions | |||||
| export function getElement(id) { | |||||
| return document.getElementById(id); | |||||
| } | |||||
| export function changeElement(id, newElement) { | |||||
| let oldElement = getElement(id); | |||||
| oldElement.replaceWith(newElement); | |||||
| newElement.id = id; | |||||
| } | |||||
| export function getIcon(id) { | |||||
| return (getElement("icon-" + id).cloneNode(true)); | |||||
| } | |||||
| export function spanFromHTML(html) { | |||||
| let span = document.createElement("span"); | |||||
| span.innerHTML = html; | |||||
| return span; | |||||
| } | |||||
| export function divFromHTML(html) { | |||||
| let div = document.createElement("div"); | |||||
| div.innerHTML = html; | |||||
| return div; | |||||
| } | |||||
| export function showElement(el, show) { | |||||
| if (show) { | |||||
| el.classList.remove("d-none"); | |||||
| } else { | |||||
| el.classList.add("d-none"); | |||||
| } | |||||
| } | |||||
| export function makeButton(icon, text, callback, classes=["w-100", "btn", "rounded-md", "mt-3"]) { | |||||
| let button = document.createElement("button"); | |||||
| button.setIconAndText = function(icon, text) { | |||||
| this.replaceChildren(getIcon(icon), spanFromHTML(text)); | |||||
| }.bind(button); | |||||
| button.setIconAndText(icon, text); | |||||
| button.replaceChildren(getIcon(icon), spanFromHTML(text)); | |||||
| button.classList.add.apply(button.classList, classes); | |||||
| button.addEventListener("click", (event) => { callback() }); | |||||
| return button; | |||||
| } | |||||
| export function setMessage(divid, msg, icon) { | |||||
| let box = getElement(divid); | |||||
| if (msg.length == 0) { | |||||
| console.log("hide", divid); | |||||
| showElement(box, false); | |||||
| box.replaceChildren(); | |||||
| } else { | |||||
| showElement(box, true); | |||||
| box.replaceChildren(getIcon(icon), spanFromHTML(msg)); | |||||
| } | |||||
| } | |||||
| export function setErrorMessage(msg) { | |||||
| setMessage("errorbox", msg, "error"); | |||||
| } | |||||
| export function haveError() { | |||||
| return getElement("errorbox").classList.contains("d-none"); | |||||
| } | |||||