mirror of
https://github.com/pi-hole/web.git
synced 2026-08-23 06:39:09 +01:00
150 lines
3.9 KiB
JavaScript
150 lines
3.9 KiB
JavaScript
/* Pi-hole: A black hole for Internet advertisements
|
|
* (c) 2023 Pi-hole, LLC (https://pi-hole.net)
|
|
* Network-wide ad blocking via your own hardware.
|
|
*
|
|
* This file is copyright under the latest version of the EUPL.
|
|
* Please see LICENSE file for your rights under this license. */
|
|
|
|
/* global apiFailure:false, utils:false, initTable:false, updateFtlInfo:false */
|
|
|
|
"use strict";
|
|
|
|
let groups = [];
|
|
|
|
function populateGroupSelect(selectEl) {
|
|
if (selectEl.length === 0) {
|
|
// No select element found, return
|
|
return;
|
|
}
|
|
|
|
// Add all known groups
|
|
for (const group of groups) {
|
|
const label = group.enabled ? group.name : group.name + " (disabled)";
|
|
|
|
selectEl.append($("<option/>").val(group.id).text(label));
|
|
}
|
|
|
|
// Default Group is preselected
|
|
selectEl.val(["0"]);
|
|
|
|
utils.createGroupSelect(selectEl);
|
|
}
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
function getGroups() {
|
|
$.ajax({
|
|
url: document.body.dataset.apiurl + "/groups",
|
|
type: "GET",
|
|
dataType: "json",
|
|
success(data) {
|
|
groups = data.groups;
|
|
|
|
// Get all <select> elements with the class "group-select"
|
|
const groupSelector = $(".group-select");
|
|
// Populate the groupSelector with the groups
|
|
for (const element of groupSelector) {
|
|
populateGroupSelect($(element));
|
|
}
|
|
|
|
// Actually load table contents
|
|
initTable();
|
|
},
|
|
error(data) {
|
|
apiFailure(data);
|
|
},
|
|
});
|
|
}
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
function processGroupResult(data, type, done, notDone) {
|
|
// Helper function to get the label of an item
|
|
const itemLabel = entry => {
|
|
if (typeof entry === "string") {
|
|
return entry;
|
|
}
|
|
|
|
if (entry && typeof entry === "object" && typeof entry.item === "string") {
|
|
return entry.item;
|
|
}
|
|
|
|
return String(entry);
|
|
};
|
|
|
|
// Loop over data.processed.success and show toasts
|
|
for (const item of data.processed.success) {
|
|
utils.showAlert(
|
|
"success",
|
|
"fas fa-pencil-alt",
|
|
`Successfully ${done} ${type}`,
|
|
itemLabel(item)
|
|
);
|
|
}
|
|
|
|
// Loop over errors and display them
|
|
for (const error of data.processed.errors) {
|
|
console.log(error); // eslint-disable-line no-console
|
|
utils.showAlert(
|
|
"error",
|
|
"",
|
|
`Error while ${notDone} ${type} ${itemLabel(error)}`,
|
|
typeof error.error === "string" ? error.error : String(error.error)
|
|
);
|
|
}
|
|
}
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
function delGroupItems(type, ids, table, listType = undefined) {
|
|
// Check input validity
|
|
if (!Array.isArray(ids)) {
|
|
return;
|
|
}
|
|
|
|
const url = document.body.dataset.apiurl + "/" + type + "s:batchDelete";
|
|
|
|
// use utils.hexDecode() to decode all clients
|
|
let idstring = "";
|
|
for (const id of ids) {
|
|
id.item = utils.hexDecode(id.item);
|
|
idstring += id.item + ", ";
|
|
}
|
|
|
|
// Remove last comma and space from idstring
|
|
idstring = idstring.substring(0, idstring.length - 2);
|
|
|
|
// 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);
|
|
|
|
$.ajax({
|
|
url,
|
|
data: JSON.stringify(ids),
|
|
contentType: "application/json",
|
|
method: "POST",
|
|
})
|
|
.done(() => {
|
|
utils.enableAll();
|
|
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted " + type, idstring);
|
|
table.ajax.reload(null, false);
|
|
|
|
// Clear selection after deletion
|
|
table.rows().deselect();
|
|
utils.changeTableButtonStates(table);
|
|
|
|
// Update number of <type> items in the sidebar
|
|
updateFtlInfo();
|
|
})
|
|
.fail((data, exception) => {
|
|
apiFailure(data);
|
|
utils.enableAll();
|
|
utils.showAlert("error", "", "Error while deleting " + type, data.responseText);
|
|
console.log(exception); // eslint-disable-line no-console
|
|
});
|
|
}
|