Files
web/scripts/js/groups-common.js
T
Adam Warner 72d682c284 Fix xo lint errors surfaced after rebasing onto development
This branch's code predates development's xo 2 -> 3 upgrade and hadn't been checked against its stricter ruleset. Fix the new violations: drop unnecessary globalThis prefixes on location/matchMedia, prefer location.assign() over href assignment, use early returns instead of wrapping onDropdownClose bodies in an if, fix a parameter shadow in getGroups, and use {capture: true} instead of a bare boolean for addEventListener.

Signed-off-by: Adam Warner <github@promofaux.dev>
2026-07-17 22:48:48 +01:00

127 lines
3.5 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) {
// 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}`, 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} ${error.item}`, 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
});
}