From 4bf4f3a09a9f9c2a248eb68dea4809d8a14da2fb Mon Sep 17 00:00:00 2001 From: DroidFreak32 Date: Wed, 2 Jul 2025 02:14:34 +0530 Subject: [PATCH] settings-dhcp: Implement copyButton to copy active DHCP leases in dnsmasq format This will allow users to easily add a current lease into the static leases configuration, closer to v5 behavior. Signed-off-by: DroidFreak32 --- scripts/js/settings-dhcp.js | 46 +++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/scripts/js/settings-dhcp.js b/scripts/js/settings-dhcp.js index 370396cdd4..323e64da36 100644 --- a/scripts/js/settings-dhcp.js +++ b/scripts/js/settings-dhcp.js @@ -64,6 +64,7 @@ $(() => { ], drawCallback() { $('button[id^="deleteLease_"]').on("click", deleteLease); + $('button[id^="copyLease_"]').on("click", copyLease); // Hide buttons if all messages were deleted const hasRows = this.api().rows({ filter: "applied" }).data().length > 0; @@ -74,15 +75,23 @@ $(() => { }, rowCallback(row, data) { $(row).attr("data-id", data.ip); - const button = + const copyButton = + ''; + const deleteButton = '"; - $("td:eq(6)", row).html(button); + '">'; + $("td:eq(6)", row).html(copyButton + " " + deleteButton); }, select: { style: "multi", @@ -159,6 +168,33 @@ $(() => { }); }); +function copyLease() { + const button = $(this); + const hwaddr = button.data("hwaddr"); + const ip = button.data("ip"); + const name = button.data("name"); + + // Handle cases where name is not available + const hostname = name === "*" || name === null ? "" : name; + + const textToCopy = `${hwaddr},${ip},${hostname}`; + + navigator.clipboard + .writeText(textToCopy) + .then(() => { + utils.showAlert("success", "far fa-copy", "Copied to clipboard!", textToCopy); + }) + .catch(error => { + console.error("Could not copy text:", error); // eslint-disable-line no-console + utils.showAlert( + "error", + "", + "Failed to copy to clipboard", + "See browser console for details" + ); + }); +} + function deleteLease() { // Passes the button data-del-id attribute as IP delLease($(this).attr("data-del-ip"));