btcpayserver/BTCPayServer/wwwroot/js/copy-to-clipboard.js
Dennis Reimann 0054fe0886 Refactor copy to clipboard
Separated out of #2701, prerequisite for the LNbank plugin integration.
2021-07-28 08:44:32 +02:00

29 lines
1.1 KiB
JavaScript

window.copyToClipboard = function (e, text) {
if (navigator.clipboard) {
e.preventDefault();
var item = e.currentTarget;
var data = text || item.getAttribute('data-clipboard');
var confirm = item.querySelector('[data-clipboard-confirm]') || item;
var message = confirm.getAttribute('data-clipboard-confirm') || 'Copied ✔';
if (!confirm.dataset.clipboardInitialText) {
confirm.dataset.clipboardInitialText = confirm.innerText;
confirm.style.minWidth = confirm.getBoundingClientRect().width + 'px';
}
navigator.clipboard.writeText(data).then(function () {
confirm.innerText = message;
setTimeout(function(){ confirm.innerText = confirm.dataset.clipboardInitialText; }, 2500);
});
item.blur();
}
}
window.copyUrlToClipboard = function (e) {
window.copyToClipboard(e, window.location)
}
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll("[data-clipboard]").forEach(item => {
item.addEventListener("click", window.copyToClipboard)
})
})