Files
appfactory-portal/app/static/portal.js
T
2026-05-28 13:33:22 +02:00

85 lines
2.2 KiB
JavaScript

function writeClipboard(value) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(value);
}
const textarea = document.createElement("textarea");
textarea.value = value;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.top = "-1000px";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
return Promise.resolve();
}
function nearestCopyValue(button) {
if (button.dataset.copyValue) {
return button.dataset.copyValue;
}
const scopes = [
button.closest(".copy-row"),
button.closest(".cmd-row"),
button.closest("td"),
button.parentElement,
];
for (const scope of scopes) {
if (!scope) {
continue;
}
const target = scope.querySelector("input, textarea, pre, code");
if (target) {
return "value" in target ? target.value : target.textContent;
}
}
return "";
}
function showCopied(button, ok) {
const label = button.textContent;
button.textContent = ok ? "Zkopírováno" : "Nelze kopírovat";
window.setTimeout(() => {
button.textContent = label;
}, 1200);
}
function copyValue(idOrButton) {
const target = typeof idOrButton === "string" ? document.getElementById(idOrButton) : idOrButton;
if (!target) {
return false;
}
const value = "value" in target ? target.value : nearestCopyValue(target);
if ("select" in target) {
target.select();
target.setSelectionRange(0, target.value.length);
}
writeClipboard(value)
.then(() => {
if (!("value" in target)) {
showCopied(target, true);
}
})
.catch(() => {
if (!("value" in target)) {
showCopied(target, false);
}
});
return false;
}
function copyText(button) {
const value = nearestCopyValue(button);
writeClipboard(value)
.then(() => showCopied(button, true))
.catch(() => showCopied(button, false));
return false;
}