217 lines
5.9 KiB
JavaScript
217 lines
5.9 KiB
JavaScript
/* 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, i18n */
|
|
|
|
"use strict";
|
|
|
|
let table;
|
|
const toasts = {};
|
|
|
|
i18n.waitForTranslations(() => {
|
|
const ignoreNonfatal = localStorage
|
|
? localStorage.getItem("hideNonfatalDnsmasqWarnings_chkbox") === "true"
|
|
: false;
|
|
const url =
|
|
document.body.dataset.apiurl +
|
|
"/info/messages" +
|
|
(ignoreNonfatal ? "?filter_dnsmasq_warnings=true" : "");
|
|
table = $("#messagesTable").DataTable({
|
|
ajax: {
|
|
url,
|
|
type: "GET",
|
|
dataSrc: "messages",
|
|
},
|
|
order: [[0, "asc"]],
|
|
columns: [
|
|
{ data: "id", visible: false },
|
|
{ data: null, visible: true, width: "15px" },
|
|
{ data: "timestamp", width: "8%", render: utils.renderTimestamp },
|
|
{ data: "type", width: "8%" },
|
|
{ data: "html", orderable: false, render: utils.htmlPass },
|
|
{ data: null, width: "22px", orderable: false },
|
|
],
|
|
columnDefs: [
|
|
{
|
|
targets: 1,
|
|
orderable: false,
|
|
className: "select-checkbox",
|
|
render() {
|
|
return "";
|
|
},
|
|
},
|
|
{
|
|
targets: "_all",
|
|
render: $.fn.dataTable.render.text(),
|
|
},
|
|
],
|
|
drawCallback() {
|
|
$('button[id^="deleteMessage_"]').on("click", deleteMessage);
|
|
|
|
// Hide buttons if all messages were deleted
|
|
const hasRows = this.api().rows({ filter: "applied" }).data().length > 0;
|
|
$(".datatable-bt").css("visibility", hasRows ? "visible" : "hidden");
|
|
|
|
// Remove visible dropdown to prevent orphaning
|
|
$("body > .bootstrap-select.dropdown").remove();
|
|
},
|
|
rowCallback(row, data) {
|
|
$(row).attr("data-id", data.id);
|
|
const button =
|
|
'<button type="button" class="btn btn-danger btn-xs" id="deleteMessage_' +
|
|
data.id +
|
|
'" data-del-id="' +
|
|
data.id +
|
|
'">' +
|
|
'<span class="far fa-trash-alt"></span>' +
|
|
"</button>";
|
|
$("td:eq(4)", row).html(button);
|
|
},
|
|
select: {
|
|
style: "multi",
|
|
selector: "td:not(:last-child)",
|
|
info: false,
|
|
},
|
|
buttons: [
|
|
{
|
|
text: '<span class="far fa-square"></span>',
|
|
titleAttr: i18n.t("shared.select_all"),
|
|
className: "btn-sm datatable-bt selectAll",
|
|
action() {
|
|
table.rows({ page: "current" }).select();
|
|
},
|
|
},
|
|
{
|
|
text: '<span class="far fa-plus-square"></span>',
|
|
titleAttr: i18n.t("shared.select_all"),
|
|
className: "btn-sm datatable-bt selectMore",
|
|
action() {
|
|
table.rows({ page: "current" }).select();
|
|
},
|
|
},
|
|
{
|
|
extend: "selectNone",
|
|
text: '<span class="far fa-check-square"></span>',
|
|
titleAttr: i18n.t("shared.deselect_all"),
|
|
className: "btn-sm datatable-bt removeAll",
|
|
},
|
|
{
|
|
text: '<span class="far fa-trash-alt"></span>',
|
|
titleAttr: i18n.t("shared.delete_selected"),
|
|
className: "btn-sm datatable-bt deleteSelected",
|
|
action() {
|
|
// For each ".selected" row ...
|
|
$("tr.selected").each(function () {
|
|
// ... delete the row identified by "data-id".
|
|
delMsg($(this).attr("data-id"));
|
|
});
|
|
},
|
|
},
|
|
],
|
|
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, i18n.t("shared.all")],
|
|
],
|
|
language: {
|
|
emptyTable: i18n.t("messages.no_issues_found"),
|
|
},
|
|
stateSave: true,
|
|
stateDuration: 0,
|
|
stateSaveCallback(settings, data) {
|
|
utils.stateSaveCallback("messages-table", data);
|
|
},
|
|
stateLoadCallback() {
|
|
const data = utils.stateLoadCallback("messages-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;
|
|
},
|
|
});
|
|
table.on("init select deselect", () => {
|
|
utils.changeTableButtonStates(table);
|
|
});
|
|
});
|
|
|
|
// Remove 'bnt-group' class from container, to avoid grouping
|
|
$.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";
|
|
|
|
function deleteMessage() {
|
|
// Passes the button data-del-id attribute as ID
|
|
delMsg($(this).attr("data-del-id"));
|
|
}
|
|
|
|
function delMsg(id) {
|
|
id = Number.parseInt(id, 10);
|
|
utils.disableAll();
|
|
toasts[id] = utils.showAlert(
|
|
"info",
|
|
"",
|
|
i18n.t("messages.deleting_message"),
|
|
i18n.tf("messages.id", id),
|
|
null
|
|
);
|
|
|
|
$.ajax({
|
|
url: document.body.dataset.apiurl + "/info/messages/" + id,
|
|
method: "DELETE",
|
|
})
|
|
.done(response => {
|
|
utils.enableAll();
|
|
if (response === undefined) {
|
|
utils.showAlert(
|
|
"success",
|
|
"far fa-trash-alt",
|
|
i18n.t("messages.deleting_successfull"),
|
|
i18n.tf("messages.id", id),
|
|
toasts[id]
|
|
);
|
|
table.row(id).remove();
|
|
|
|
table.draw(false).ajax.reload(null, false);
|
|
} else {
|
|
utils.showAlert(
|
|
"error",
|
|
"",
|
|
i18n.tf("messages.error_deleting", id),
|
|
response.message,
|
|
toasts[id]
|
|
);
|
|
}
|
|
|
|
// Clear selection after deletion
|
|
table.rows().deselect();
|
|
utils.changeTableButtonStates(table);
|
|
})
|
|
.done(
|
|
utils.checkMessages() // Update icon warnings count
|
|
)
|
|
.fail((jqXHR, exception) => {
|
|
utils.enableAll();
|
|
utils.showAlert(
|
|
"error",
|
|
"",
|
|
i18n.tf("messages.error_deleting", id),
|
|
jqXHR.responseText,
|
|
toasts[id]
|
|
);
|
|
console.log(exception); // eslint-disable-line no-console
|
|
});
|
|
}
|