/* Pi-hole: A black hole for Internet advertisements * (c) 2017 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 utils:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false */ /* exported initTable */ var table; var GETDict = {}; $(function () { GETDict = utils.parseQueryString(); $("#btnAddAllow").on("click", { type: "allow" }, addList); $("#btnAddBlock").on("click", { type: "block" }, addList); utils.setBsSelectDefaults(); getGroups(); }); function format(data) { // Generate human-friendly status string var statusText = "Unknown"; var numbers = true; if (data.status !== null) { switch (parseInt(data.status, 10)) { case 0: statusText = data.enabled === 0 ? "List is disabled and not checked" : "List was not downloaded so far"; numbers = false; break; case 1: statusText = 'List download was successful (OK)'; break; case 2: statusText = 'List unchanged upstream, Pi-hole used a local copy (OK)'; break; case 3: statusText = 'List unavailable, Pi-hole used a local copy (check list)'; break; case 4: statusText = 'List unavailable, there is no local copy of this list available on your Pi-hole (replace list)'; numbers = false; break; default: statusText = 'Unknown (' + parseInt(data.status, 10) + ")"; break; } } // Compile extra info for displaying return ( "
| Type of this list: | ' + data.type + 'list |
| Health status of this list: | ' + statusText + ' |
| This list was added to Pi-hole | ' + utils.datetimeRelative(data.date_added) + " (" + utils.datetime(data.date_added, false) + ') |
| Database entry was last modified | ' + utils.datetimeRelative(data.date_modified) + " (" + utils.datetime(data.date_modified, false) + ') |
| The list contents were last updated | ' + (data.date_updated > 0 ? utils.datetimeRelative(data.date_updated) + " (" + utils.datetime(data.date_updated, false) + ")" : "N/A") + ' |
| Number of entries on this list: | ' + (data.number !== null && numbers === true ? parseInt(data.number, 10).toLocaleString() : "N/A") + (data.abp_entries !== null && parseInt(data.abp_entries, 10) > 0 && numbers === true ? " (out of which " + parseInt(data.abp_entries, 10).toLocaleString() + " are in ABP-style)" : "") + ' |
| Number of non-domains on this list: | " + "" + (data.invalid_domains !== null && numbers === true ? parseInt(data.invalid_domains, 10).toLocaleString() : "N/A") + ' |
| Database ID of this list: | ' + data.id + " |
' + data.address + ""
);
} else {
$("td:eq(2)", row).html(
'' +
data.address +
""
);
}
$("td:eq(3)", row).html(
'"
);
var statusEl = $("#enabled_" + dataId, row);
statusEl.bootstrapToggle({
on: "Enabled",
off: "Disabled",
size: "small",
onstyle: "success",
width: "80px",
});
statusEl.on("change", editList);
$("td:eq(4)", row).html('');
var commentEl = $("#comment_" + dataId, row);
commentEl.val(utils.unescapeHtml(data.comment));
commentEl.on("change", editList);
$("td:eq(5)", row).empty();
$("td:eq(5)", row).append(
''
);
var selectEl = $("#multiselect_" + dataId, row);
// Add all known groups
for (var i = 0; i < groups.length; i++) {
var dataSub = "";
if (!groups[i].enabled) {
dataSub = 'data-subtext="(disabled)"';
}
selectEl.append(
$("")
.val(groups[i].id)
.text(groups[i].name)
);
}
// Select assigned groups
selectEl.val(data.groups);
// Initialize bootstrap-select
selectEl
// fix dropdown if it would stick out right of the viewport
.on("show.bs.select", function () {
var winWidth = $(window).width();
var dropdownEl = $("body > .bootstrap-select.dropdown");
if (dropdownEl.length > 0) {
dropdownEl.removeClass("align-right");
var width = dropdownEl.width();
var left = dropdownEl.offset().left;
if (left + width > winWidth) {
dropdownEl.addClass("align-right");
}
}
})
.on("changed.bs.select", function () {
// enable Apply button
if ($(applyBtn).prop("disabled")) {
$(applyBtn)
.addClass("btn-success")
.prop("disabled", false)
.on("click", function () {
editList.call(selectEl);
});
}
})
.on("hide.bs.select", function () {
// Restore values if drop-down menu is closed without clicking the Apply button
if (!$(applyBtn).prop("disabled")) {
$(this).val(data.groups).selectpicker("refresh");
$(applyBtn).removeClass("btn-success").prop("disabled", true).off("click");
}
})
.selectpicker()
.siblings(".dropdown-menu")
.find(".bs-actionsbox")
.prepend(
''
);
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");
}
var button =
'";
$("td:eq(6)", row).html(button);
},
dom:
"<'row'<'col-sm-6'l><'col-sm-6'f>>" +
"<'row'<'col-sm-3'B><'col-sm-9'p>>" +
"<'row'<'col-sm-12'<'table-responsive'tr>>>" +
"<'row'<'col-sm-3'B><'col-sm-9'p>>" +
"<'row'<'col-sm-12'i>>",
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "All"],
],
select: {
style: "multi",
selector: "td:not(:last-child)",
info: false,
},
buttons: [
{
text: '',
titleAttr: "Select All",
className: "btn-sm datatable-bt selectAll",
action: function () {
table.rows({ page: "current" }).select();
},
},
{
text: '',
titleAttr: "Select All",
className: "btn-sm datatable-bt selectMore",
action: function () {
table.rows({ page: "current" }).select();
},
},
{
extend: "selectNone",
text: '',
titleAttr: "Deselect All",
className: "btn-sm datatable-bt removeAll",
},
{
text: '',
titleAttr: "Delete Selected",
className: "btn-sm datatable-bt deleteSelected",
action: function () {
// For each ".selected" row ...
var ids = [];
$("tr.selected").each(function () {
// ... add the row identified by "data-id".
ids.push($(this).attr("data-id"));
});
// Delete all selected rows at once
delItems(ids);
},
},
],
stateSave: true,
stateDuration: 0,
stateSaveCallback: function (settings, data) {
utils.stateSaveCallback("groups-lists-table", data);
},
stateLoadCallback: function () {
var data = utils.stateLoadCallback("groups-lists-table");
// Return if not available
if (data === null) {
return null;
}
// Reset visibility of ID column
data.columns[0].visible = false;
// Apply loaded state to table
return data;
},
initComplete: function () {
if ("listid" in GETDict) {
var pos = table
.column(0, { order: "current" })
.data()
.indexOf(parseInt(GETDict.listid, 10));
if (pos >= 0) {
var page = Math.floor(pos / table.page.info().length);
table.page(page).draw(false);
}
}
},
});
table.on("init select deselect", function () {
utils.changeBulkDeleteStates(table);
});
table.on("order.dt", function () {
var order = table.order();
if (order[0][0] !== 0 || order[0][1] !== "asc") {
$("#resetButton").removeClass("hidden");
} else {
$("#resetButton").addClass("hidden");
}
});
$("#resetButton").on("click", function () {
table.order([[0, "asc"]]).draw();
$("#resetButton").addClass("hidden");
});
// Add event listener for opening and closing details
$("#listsTable tbody").on("click", "td.details-control", function () {
var tr = $(this).closest("tr");
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass("shown");
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass("shown");
}
});
// Disable autocorrect in the search box
var input = document.querySelector("input[type=search]");
if (input !== null) {
input.setAttribute("autocomplete", "off");
input.setAttribute("autocorrect", "off");
input.setAttribute("autocapitalize", "off");
input.setAttribute("spellcheck", false);
}
}
// Remove 'bnt-group' class from container, to avoid grouping
$.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";
function deleteList() {
// Passes the button data-id attribute as ID
const ids = [$(this).attr("data-id")];
delItems(ids);
}
function delItems(ids) {
// Check input validity
if (!Array.isArray(ids)) return;
// Get first element from array
const addressRaw = ids[0];
const address = utils.hexDecode(addressRaw);
// Remove first element from array
ids.shift();
utils.disableAll();
const idstring = ids.join(", ");
utils.showAlert("info", "", "Deleting list(s) ...", "