Skip to content

Commit

Permalink
Allow adlist duplicates (#2958)
Browse files Browse the repository at this point in the history
  • Loading branch information
DL6ER authored Mar 2, 2024
2 parents 034ca9e + 2098a11 commit 64e865e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
7 changes: 6 additions & 1 deletion scripts/pi-hole/js/groups-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function processGroupResult(data, type, done, notDone) {
}

// eslint-disable-next-line no-unused-vars
function delGroupItems(type, ids, table) {
function delGroupItems(type, ids, table, listType = undefined) {
// Check input validity
if (!Array.isArray(ids)) return;

Expand All @@ -60,6 +60,11 @@ function delGroupItems(type, ids, table) {
// Append "s" to type if more than one item is deleted
type += ids.length > 1 ? "s" : "";

// Prepend listType to type if it is not undefined
if (listType !== undefined) {
type = listType + " " + type;
}

utils.disableAll();
utils.showAlert("info", "", "Deleting " + ids.length + " " + type + "...", idstring);

Expand Down
44 changes: 23 additions & 21 deletions scripts/pi-hole/js/groups-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ function initTable() {
$("body > .bootstrap-select.dropdown").remove();
},
rowCallback: function (row, data) {
var dataId = utils.hexEncode(data.address);
var dataId = utils.hexEncode(data.address + "_" + data.type);
$(row).attr("data-id", dataId);
$(row).attr("data-address", utils.hexEncode(data.address));
$(row).attr("data-type", data.type);

var statusCode = 0;
Expand Down Expand Up @@ -289,6 +290,8 @@ function initTable() {
);
}

var applyBtn = "#btn_apply_" + dataId;

// Select assigned groups
selectEl.val(data.groups);
// Initialize bootstrap-select
Expand Down Expand Up @@ -333,8 +336,6 @@ function initTable() {
' class="btn btn-block btn-sm" disabled>Apply</button>'
);

var applyBtn = "#btn_apply_" + dataId;

// Highlight row (if url parameter "listid=" is used)
if ("listid" in GETDict && data.id === parseInt(GETDict.listid, 10)) {
$(row).find("td").addClass("highlight");
Expand Down Expand Up @@ -397,10 +398,10 @@ function initTable() {
var ids = [];
$("tr.selected").each(function () {
// ... add the row identified by "data-id".
ids.push({ item: $(this).attr("data-id") });
ids.push({ item: $(this).attr("data-address"), type: $(this).attr("data-type") });
});
// Delete all selected rows at once
delGroupItems("list", ids, table);
delGroupItems("list", ids, table, "multiple ");
},
},
],
Expand Down Expand Up @@ -484,9 +485,10 @@ function initTable() {
$.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";

function deleteList() {
// Passes the button data-id attribute as ID
const ids = [{ item: $(this).attr("data-id") }];
delGroupItems("list", ids, table);
const tr = $(this).closest("tr");
const listType = tr.attr("data-type");
const ids = [{ item: tr.attr("data-address"), type: listType }];
delGroupItems("list", ids, table, listType);
}

function addList(event) {
Expand Down Expand Up @@ -523,7 +525,7 @@ function addList(event) {
data: JSON.stringify({ address: addresses, comment: comment, type: type }),
success: function (data) {
utils.enableAll();
utils.listsAlert("list", addresses, data);
utils.listsAlert(type + "list", addresses, data);
$("#new_address").val("");
$("#new_comment").val("");
table.ajax.reload(null, false);
Expand All @@ -545,19 +547,20 @@ function editList() {
const elem = $(this).attr("id");
const tr = $(this).closest("tr");
const type = tr.attr("data-type");
const address = tr.attr("data-id");
const enabled = tr.find("#enabled_" + address).is(":checked");
const comment = utils.escapeHtml(tr.find("#comment_" + address).val());
const dataId = tr.attr("data-id");
const address = utils.hexDecode(tr.attr("data-address"));
const enabled = tr.find("#enabled_" + dataId).is(":checked");
const comment = utils.escapeHtml(tr.find("#comment_" + dataId).val());
// Convert list of string integers to list of integers using map(Number)
const groups = tr
.find("#multiselect_" + address)
.find("#multiselect_" + dataId)
.val()
.map(Number);

var done = "edited";
var notDone = "editing";
switch (elem) {
case "enabled_" + address:
case "enabled_" + dataId:
if (!enabled) {
done = "disabled";
notDone = "disabling";
Expand All @@ -567,11 +570,11 @@ function editList() {
}

break;
case "comment_" + address:
case "comment_" + dataId:
done = "edited comment of";
notDone = "editing comment of";
break;
case "multiselect_" + address:
case "multiselect_" + dataId:
done = "edited groups of";
notDone = "editing groups of";
break;
Expand All @@ -581,10 +584,9 @@ function editList() {
}

utils.disableAll();
const addressDecoded = utils.hexDecode(address);
utils.showAlert("info", "", "Editing address...", addressDecoded);
utils.showAlert("info", "", "Editing address...", address);
$.ajax({
url: "/api/lists/" + encodeURIComponent(addressDecoded),
url: "/api/lists/" + encodeURIComponent(address) + "?type=" + type,
method: "put",
dataType: "json",
processData: false,
Expand All @@ -597,7 +599,7 @@ function editList() {
}),
success: function (data) {
utils.enableAll();
processGroupResult(data, "list", done, notDone);
processGroupResult(data, type + "list", done, notDone);
table.ajax.reload(null, false);
},
error: function (data, exception) {
Expand All @@ -606,7 +608,7 @@ function editList() {
utils.showAlert(
"error",
"",
"Error while " + notDone + " list " + addressDecoded,
"Error while " + notDone + type + "list " + address,
data.responseText
);
console.log(exception); // eslint-disable-line no-console
Expand Down
14 changes: 7 additions & 7 deletions scripts/pi-hole/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ function eventsource(partial) {
// Group results in res.gravity by res.gravity[].address
var grouped = {};
for (const list of res.gravity) {
if (grouped[list.address] === undefined) {
grouped[list.address] = [];
if (grouped[list.address + "_" + list.type] === undefined) {
grouped[list.address + "_" + list.type] = [];
}

grouped[list.address].push(list);
grouped[list.address + "_" + list.type].push(list);
}

const numLists = Object.keys(grouped).length;
Expand All @@ -112,14 +112,14 @@ function eventsource(partial) {
"</strong>'" +
(numLists > 0 ? ":" : ".") +
"<br><br>";
for (const address of Object.keys(grouped)) {
const list = grouped[address][0];
for (const listId of Object.keys(grouped)) {
const list = grouped[listId][0];
const color = list.type === "block" ? "red" : "green";
result +=
" - <a href='groups-lists.lp?listid=" +
list.id +
"' target='_blank'>" +
utils.escapeHtml(address) +
utils.escapeHtml(list.address) +
"</a><br> <strong class='text-" +
color +
"'>" +
Expand All @@ -144,7 +144,7 @@ function eventsource(partial) {
? '<br> comment: "' + utils.escapeHtml(list.comment) + '"'
: "<br> no comment") +
"<br> matching entries:<br>";
for (const lists of grouped[address]) {
for (const lists of grouped[listId]) {
result +=
" - <strong class='text-blue'>" + utils.escapeHtml(lists.domain) + "</strong><br>";
}
Expand Down

0 comments on commit 64e865e

Please sign in to comment.