WebUI: Setup copy events only once

It is enough to set them up just once during initial load. Number of elements with copyToClipboard class is fixed - they are context menu items.

PR #21558.
This commit is contained in:
skomerko 2024-10-12 08:29:54 +02:00 committed by GitHub
commit cbcb46bcfb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -672,36 +672,6 @@ window.addEventListener("DOMContentLoaded", () => {
tracker.classList.toggle("selectedFilter", (Number(tracker.id) === selectedTracker));
};
const setupCopyEventHandler = (function() {
let clipboardEvent;
return () => {
if (clipboardEvent)
clipboardEvent.destroy();
clipboardEvent = new ClipboardJS(".copyToClipboard", {
text: function(trigger) {
switch (trigger.id) {
case "copyName":
return copyNameFN();
case "copyInfohash1":
return copyInfohashFN(1);
case "copyInfohash2":
return copyInfohashFN(2);
case "copyMagnetLink":
return copyMagnetLinkFN();
case "copyID":
return copyIdFN();
case "copyComment":
return copyCommentFN();
default:
return "";
}
}
});
};
})();
let syncMainDataTimeoutID = -1;
let syncRequestInProgress = false;
const syncMainData = function() {
@ -824,7 +794,6 @@ window.addEventListener("DOMContentLoaded", () => {
updateTrackers = true;
}
if (response["torrents"]) {
let updateTorrentList = false;
for (const key in response["torrents"]) {
if (!Object.hasOwn(response["torrents"], key))
continue;
@ -838,12 +807,7 @@ window.addEventListener("DOMContentLoaded", () => {
update_categories = true;
if (addTorrentToTagList(response["torrents"][key]))
updateTags = true;
if (response["torrents"][key]["name"])
updateTorrentList = true;
}
if (updateTorrentList)
setupCopyEventHandler();
}
if (response["torrents_removed"]) {
response["torrents_removed"].each((hash) => {
@ -1663,6 +1627,27 @@ window.addEventListener("DOMContentLoaded", () => {
}
}
}).activate();
new ClipboardJS(".copyToClipboard", {
text: function(trigger) {
switch (trigger.id) {
case "copyName":
return copyNameFN();
case "copyInfohash1":
return copyInfohashFN(1);
case "copyInfohash2":
return copyInfohashFN(2);
case "copyMagnetLink":
return copyMagnetLinkFN();
case "copyID":
return copyIdFN();
case "copyComment":
return copyCommentFN();
default:
return "";
}
}
});
});
window.addEventListener("load", () => {