120 lines
3.0 KiB
JavaScript
120 lines
3.0 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;
|
|
}
|
|
|
|
function openResourceModal(id) {
|
|
const modal = document.getElementById(id);
|
|
if (!modal) {
|
|
return false;
|
|
}
|
|
|
|
if (typeof modal.showModal === "function") {
|
|
modal.showModal();
|
|
} else {
|
|
modal.setAttribute("open", "");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function closeResourceModal(id) {
|
|
const modal = document.getElementById(id);
|
|
if (!modal) {
|
|
return false;
|
|
}
|
|
|
|
if (typeof modal.close === "function") {
|
|
modal.close();
|
|
} else {
|
|
modal.removeAttribute("open");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
document.addEventListener("click", (event) => {
|
|
const modal = event.target;
|
|
if (typeof HTMLDialogElement !== "undefined" && modal instanceof HTMLDialogElement && modal.classList.contains("resource-modal")) {
|
|
modal.close();
|
|
}
|
|
});
|