Add utils object for common code.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-01-27 11:01:48 +00:00
parent cfec7412da
commit 516c9e946a
5 changed files with 147 additions and 115 deletions

View File

@@ -5,10 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
/* global showAlert:false */
/* global datetime:false */
/* global disableAll:false */
/* global enableAll:false */
/* global utils:false */
var table;
var token = $("#token").html();
@@ -36,9 +33,9 @@ $(document).ready(function() {
rowCallback: function(row, data) {
var tooltip =
"Added: " +
datetime(data.date_added) +
utils.datetime(data.date_added) +
"\nLast modified: " +
datetime(data.date_modified) +
utils.datetime(data.date_modified) +
"\nDatabase ID: " +
data.id;
$("td:eq(0)", row).html(
@@ -134,11 +131,11 @@ function addGroup() {
var name = $("#new_name").val();
var desc = $("#new_desc").val();
disableAll();
showAlert("info", "", "Adding group...", name);
utils.disableAll();
utils.showAlert("info", "", "Adding group...", name);
if (name.length === 0) {
showAlert("warning", "", "Warning", "Please specify a group name");
utils.showAlert("warning", "", "Warning", "Please specify a group name");
return;
}
@@ -148,19 +145,19 @@ function addGroup() {
dataType: "json",
data: { action: "add_group", name: name, desc: desc, token: token },
success: function(response) {
enableAll();
utils.enableAll();
if (response.success) {
showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name);
utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name);
$("#new_name").val("");
$("#new_desc").val("");
table.ajax.reload();
} else {
showAlert("error", "", "Error while adding new group", response.message);
utils.showAlert("error", "", "Error while adding new group", response.message);
}
},
error: function(jqXHR, exception) {
enableAll();
showAlert("error", "", "Error while adding new group", jqXHR.responseText);
utils.enableAll();
utils.showAlert("error", "", "Error while adding new group", jqXHR.responseText);
console.log(exception);
}
});
@@ -190,8 +187,8 @@ function editGroup() {
not_done = "editing description of";
}
disableAll();
showAlert("info", "", "Editing group...", name);
utils.disableAll();
utils.showAlert("info", "", "Editing group...", name);
$.ajax({
url: "scripts/pi-hole/php/groups.php",
method: "post",
@@ -205,11 +202,16 @@ function editGroup() {
token: token
},
success: function(response) {
enableAll();
utils.enableAll();
if (response.success) {
showAlert("success", "glyphicon glyphicon-pencil", "Successfully " + done + " group", name);
utils.showAlert(
"success",
"glyphicon glyphicon-pencil",
"Successfully " + done + " group",
name
);
} else {
showAlert(
utils.showAlert(
"error",
"",
"Error while " + not_done + " group with ID " + id,
@@ -218,8 +220,8 @@ function editGroup() {
}
},
error: function(jqXHR, exception) {
enableAll();
showAlert(
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while " + not_done + " group with ID " + id,
@@ -235,28 +237,33 @@ function deleteGroup() {
var tr = $(this).closest("tr");
var name = tr.find("#name").val();
disableAll();
showAlert("info", "", "Deleting group...", name);
utils.disableAll();
utils.showAlert("info", "", "Deleting group...", name);
$.ajax({
url: "scripts/pi-hole/php/groups.php",
method: "post",
dataType: "json",
data: { action: "delete_group", id: id, token: token },
success: function(response) {
enableAll();
utils.enableAll();
if (response.success) {
showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted group ", name);
utils.showAlert(
"success",
"glyphicon glyphicon-trash",
"Successfully deleted group ",
name
);
table
.row(tr)
.remove()
.draw(false);
} else {
showAlert("error", "", "Error while deleting group with ID " + id, response.message);
utils.showAlert("error", "", "Error while deleting group with ID " + id, response.message);
}
},
error: function(jqXHR, exception) {
enableAll();
showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText);
utils.enableAll();
utils.showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText);
console.log(exception);
}
});