Skip to content

Commit 13d96f9

Browse files
committed
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 <[email protected]>
1 parent 387ad34 commit 13d96f9

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

scripts/js/settings-dhcp.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ $(() => {
6464
],
6565
drawCallback() {
6666
$('button[id^="deleteLease_"]').on("click", deleteLease);
67+
$('button[id^="copyLease_"]').on("click", copyLease);
6768

6869
// Hide buttons if all messages were deleted
6970
const hasRows = this.api().rows({ filter: "applied" }).data().length > 0;
@@ -74,15 +75,23 @@ $(() => {
7475
},
7576
rowCallback(row, data) {
7677
$(row).attr("data-id", data.ip);
77-
const button =
78+
const copyButton =
79+
'<button type="button" class="btn btn-default btn-xs" id="copyLease_"' +
80+
'data-hwaddr="' +
81+
data.hwaddr +
82+
'" data-ip="' +
83+
data.ip +
84+
'" data-name="' +
85+
data.name +
86+
'" title="Copy as static DHCP lease">' +
87+
'<span class="far fa-copy"></span></button>';
88+
const deleteButton =
7889
'<button type="button" class="btn btn-danger btn-xs" id="deleteLease_' +
7990
data.ip +
8091
'" data-del-ip="' +
8192
data.ip +
82-
'">' +
83-
'<span class="far fa-trash-alt"></span>' +
84-
"</button>";
85-
$("td:eq(6)", row).html(button);
93+
'"><span class="far fa-trash-alt"></span></button>';
94+
$("td:eq(6)", row).html(copyButton + "&nbsp;" + deleteButton);
8695
},
8796
select: {
8897
style: "multi",
@@ -159,6 +168,38 @@ $(() => {
159168
});
160169
});
161170

171+
function copyLease() {
172+
const button = $(this);
173+
const hwaddr = button.data("hwaddr");
174+
const ip = button.data("ip");
175+
const name = button.data("name");
176+
177+
// Handle cases where name is not available
178+
const hostname = name === "*" || name === null ? "" : name;
179+
180+
const textToCopy = `${hwaddr},${ip},${hostname}`;
181+
182+
navigator.clipboard
183+
.writeText(textToCopy)
184+
.then(() => {
185+
utils.showAlert(
186+
"success",
187+
"far fa-copy",
188+
"Copied to clipboard!",
189+
textToCopy
190+
);
191+
})
192+
.catch(err => {
193+
console.error("Could not copy text: ", err); // eslint-disable-line no-console
194+
utils.showAlert(
195+
"error",
196+
"",
197+
"Failed to copy to clipboard",
198+
"See browser console for details"
199+
);
200+
});
201+
}
202+
162203
function deleteLease() {
163204
// Passes the button data-del-id attribute as IP
164205
delLease($(this).attr("data-del-ip"));

0 commit comments

Comments
 (0)