From 43a861a6a90f6dd9ee1dad7f6a2f59e9b48ce5f1 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 22 Jan 2020 19:10:21 +0100 Subject: [PATCH 1/2] Disable form elements while database operation is pending. Signed-off-by: DL6ER --- groups-adlists.php | 1 + groups-clients.php | 1 + groups-domains.php | 1 + groups.php | 1 + scripts/pi-hole/js/groups-adlists.js | 78 ++++-------------------- scripts/pi-hole/js/groups-clients.js | 74 +++++------------------ scripts/pi-hole/js/groups-common.js | 90 ++++++++++++++++++++++++++++ scripts/pi-hole/js/groups-domains.js | 79 +++++------------------- scripts/pi-hole/js/groups.js | 78 ++++-------------------- 9 files changed, 148 insertions(+), 255 deletions(-) create mode 100644 scripts/pi-hole/js/groups-common.js diff --git a/groups-adlists.php b/groups-adlists.php index 9b806967..49ddb58b 100644 --- a/groups-adlists.php +++ b/groups-adlists.php @@ -72,6 +72,7 @@ + + + + " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - title: title, - message: message - }; - info = $.notify(opts); - break; - case "success": - opts = { - type: "success", - icon: icon, - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "warning": - opts = { - type: "warning", - icon: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function get_groups() { $.post( @@ -83,10 +26,6 @@ function get_groups() { ); } -function datetime(date) { - return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); -} - $(document).ready(function() { $("#btnAdd").on("click", addAdlist); @@ -230,6 +169,7 @@ function addAdlist() { var address = $("#new_address").val(); var comment = $("#new_comment").val(); + disableAll(); showAlert("info", "", "Adding adlist...", address); if (address.length === 0) { @@ -248,6 +188,7 @@ function addAdlist() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added adlist", address); $("#new_address").val(""); @@ -258,6 +199,7 @@ function addAdlist() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); console.log(exception); } @@ -289,6 +231,7 @@ function editAdlist() { not_done = "editing groups of"; } + disableAll(); showAlert("info", "", "Editing adlist...", address); $.ajax({ @@ -304,6 +247,7 @@ function editAdlist() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -321,6 +265,7 @@ function editAdlist() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -337,6 +282,7 @@ function deleteAdlist() { var tr = $(this).closest("tr"); var address = tr.find("#address").text(); + disableAll(); showAlert("info", "", "Deleting adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -344,6 +290,7 @@ function deleteAdlist() { dataType: "json", data: { action: "delete_adlist", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted adlist ", address); table @@ -353,6 +300,7 @@ function deleteAdlist() { } else showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index 68fbed9e..0edb1a50 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -5,69 +5,13 @@ * 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 disableAll:false */ +/* global enableAll:false */ + var table; var groups = []; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - title: title, - message: message - }; - info = $.notify(opts); - break; - case "success": - opts = { - type: "success", - icon: icon, - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "warning": - opts = { - type: "warning", - icon: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function reload_client_suggestions() { $.post( @@ -238,9 +182,11 @@ function addClient() { ip = $("#ip-custom").val(); } + disableAll(); showAlert("info", "", "Adding client...", ip); if (ip.length === 0) { + enableAll(); showAlert("warning", "", "Warning", "Please specify a client IP address"); return; } @@ -251,6 +197,7 @@ function addClient() { dataType: "json", data: { action: "add_client", ip: ip, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); reload_client_suggestions(); @@ -260,6 +207,7 @@ function addClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new client", jqXHR.responseText); console.log(exception); } @@ -286,6 +234,7 @@ function editClient() { ip_name += " (" + name + ")"; } + disableAll(); showAlert("info", "", "Editing client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -293,6 +242,7 @@ function editClient() { dataType: "json", data: { action: "edit_client", id: id, groups: groups, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -305,6 +255,7 @@ function editClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -327,6 +278,7 @@ function deleteClient() { ip_name += " (" + name + ")"; } + disableAll(); showAlert("info", "", "Deleting client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -334,6 +286,7 @@ function deleteClient() { dataType: "json", data: { action: "delete_client", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted client ", ip_name); table @@ -346,6 +299,7 @@ function deleteClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js new file mode 100644 index 00000000..dce071f8 --- /dev/null +++ b/scripts/pi-hole/js/groups-common.js @@ -0,0 +1,90 @@ +/* Pi-hole: A black hole for Internet advertisements + * (c) 2020 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 moment:false */ +/* global showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ + +var info = null; +function showAlert(type, icon, title, message) { + var opts = {}; + title = " " + title + "
"; + switch (type) { + case "info": + opts = { + type: "info", + icon: "glyphicon glyphicon-time", + title: title, + message: message + }; + info = $.notify(opts); + break; + case "success": + opts = { + type: "success", + icon: icon, + title: title, + message: message + }; + if (info) { + info.update(opts); + } else { + $.notify(opts); + } + + break; + case "warning": + opts = { + type: "warning", + icon: "glyphicon glyphicon-warning-sign", + title: title, + message: message + }; + if (info) { + info.update(opts); + } else { + $.notify(opts); + } + + break; + case "error": + opts = { + type: "danger", + icon: "glyphicon glyphicon-remove", + title: " Error, something went wrong!
", + message: message + }; + if (info) { + info.update(opts); + } else { + $.notify(opts); + } + + break; + default: + } +} + +function datetime(date) { + return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); +} + +function disableAll() { + $("input").attr("disabled", true); + $("select").attr("disabled", true); + $("button").attr("disabled", true); + $("textarea").attr("disabled", true); +} + +function enableAll() { + $("input").attr("disabled", false); + $("select").attr("disabled", false); + $("button").attr("disabled", false); + $("textarea").attr("disabled", false); +} diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js index b83c5296..ecdf696b 100644 --- a/scripts/pi-hole/js/groups-domains.js +++ b/scripts/pi-hole/js/groups-domains.js @@ -5,71 +5,14 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global moment:false */ +/* global showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ var table; var groups = []; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - title: title, - message: message - }; - info = $.notify(opts); - break; - case "success": - opts = { - type: "success", - icon: icon, - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "warning": - opts = { - type: "warning", - icon: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function get_groups() { $.post( @@ -83,10 +26,6 @@ function get_groups() { ); } -function datetime(date) { - return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); -} - $(document).ready(function() { $("#btnAdd").on("click", addDomain); @@ -248,9 +187,11 @@ function addDomain() { var type = $("#new_type").val(); var comment = $("#new_comment").val(); + disableAll(); showAlert("info", "", "Adding domain...", domain); if (domain.length === 0) { + enableAll(); showAlert("warning", "", "Warning", "Please specify a domain"); return; } @@ -267,6 +208,7 @@ function addDomain() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); $("#new_domain").val(""); @@ -275,6 +217,7 @@ function addDomain() { } else showAlert("error", "", "Error while adding new domain", response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new domain", jqXHR.responseText); console.log(exception); } @@ -313,6 +256,7 @@ function editDomain() { not_done = "editing groups of"; } + disableAll(); showAlert("info", "", "Editing domain...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -328,6 +272,7 @@ function editDomain() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -344,6 +289,7 @@ function editDomain() { ); }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -360,6 +306,7 @@ function deleteDomain() { var tr = $(this).closest("tr"); var domain = tr.find("#domain").text(); + disableAll(); showAlert("info", "", "Deleting domain...", domain); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -367,6 +314,7 @@ function deleteDomain() { dataType: "json", data: { action: "delete_domain", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted domain", domain); table @@ -376,6 +324,7 @@ function deleteDomain() { } else showAlert("error", "", "Error while deleting domain with ID " + id, response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups.js b/scripts/pi-hole/js/groups.js index b7ec372c..48798451 100644 --- a/scripts/pi-hole/js/groups.js +++ b/scripts/pi-hole/js/groups.js @@ -5,74 +5,13 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global moment:false */ +/* global showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ var table; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - title: title, - message: message - }; - info = $.notify(opts); - break; - case "success": - opts = { - type: "success", - icon: icon, - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "warning": - opts = { - type: "warning", - icon: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} - -function datetime(date) { - return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); -} $(document).ready(function() { $("#btnAdd").on("click", addGroup); @@ -191,6 +130,7 @@ function addGroup() { var name = $("#new_name").val(); var desc = $("#new_desc").val(); + disableAll(); showAlert("info", "", "Adding group...", name); if (name.length === 0) { @@ -204,6 +144,7 @@ function addGroup() { dataType: "json", data: { action: "add_group", name: name, desc: desc, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name); $("#new_name").val(""); @@ -214,6 +155,7 @@ function addGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new group", jqXHR.responseText); console.log(exception); } @@ -244,6 +186,7 @@ function editGroup() { not_done = "editing description of"; } + disableAll(); showAlert("info", "", "Editing group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -258,6 +201,7 @@ function editGroup() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-pencil", "Successfully " + done + " group", name); } else { @@ -270,6 +214,7 @@ function editGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -286,6 +231,7 @@ function deleteGroup() { var tr = $(this).closest("tr"); var name = tr.find("#name").val(); + disableAll(); showAlert("info", "", "Deleting group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -293,6 +239,7 @@ function deleteGroup() { dataType: "json", data: { action: "delete_group", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted group ", name); table @@ -304,6 +251,7 @@ function deleteGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText); console.log(exception); } From 516c9e946a2f9c9c62065d31c09317e678ec9c29 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 27 Jan 2020 11:01:48 +0000 Subject: [PATCH 2/2] Add utils object for common code. Signed-off-by: DL6ER --- scripts/pi-hole/js/groups-adlists.js | 65 ++++++++++++++++------------ scripts/pi-hole/js/groups-clients.js | 59 ++++++++++++++----------- scripts/pi-hole/js/groups-common.js | 13 ++++-- scripts/pi-hole/js/groups-domains.js | 62 +++++++++++++------------- scripts/pi-hole/js/groups.js | 63 +++++++++++++++------------ 5 files changed, 147 insertions(+), 115 deletions(-) diff --git a/scripts/pi-hole/js/groups-adlists.js b/scripts/pi-hole/js/groups-adlists.js index 5c571724..f16e1bc5 100644 --- a/scripts/pi-hole/js/groups-adlists.js +++ b/scripts/pi-hole/js/groups-adlists.js @@ -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 groups = []; @@ -59,9 +56,9 @@ function initTable() { 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( @@ -173,11 +170,11 @@ function addAdlist() { var address = $("#new_address").val(); var comment = $("#new_comment").val(); - disableAll(); - showAlert("info", "", "Adding adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Adding adlist...", address); if (address.length === 0) { - showAlert("warning", "", "Warning", "Please specify an adlist address"); + utils.showAlert("warning", "", "Warning", "Please specify an adlist address"); return; } @@ -192,19 +189,24 @@ function addAdlist() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added adlist", address); + utils.showAlert( + "success", + "glyphicon glyphicon-plus", + "Successfully added adlist", + address + ); $("#new_address").val(""); $("#new_comment").val(""); table.ajax.reload(); } else { - showAlert("error", "", "Error while adding new adlist: ", response.message); + utils.showAlert("error", "", "Error while adding new adlist: ", response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); console.log(exception); } }); @@ -235,8 +237,8 @@ function editAdlist() { not_done = "editing groups of"; } - disableAll(); - showAlert("info", "", "Editing adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Editing adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -251,16 +253,16 @@ function editAdlist() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-pencil", "Successfully " + done + " adlist ", address ); } else { - showAlert( + utils.showAlert( "error", "", "Error while " + not_done + " adlist with ID " + id, @@ -269,8 +271,8 @@ function editAdlist() { } }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " adlist with ID " + id, @@ -286,26 +288,33 @@ function deleteAdlist() { var tr = $(this).closest("tr"); var address = tr.find("#address").text(); - disableAll(); - showAlert("info", "", "Deleting adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Deleting adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_adlist", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted adlist ", address); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted adlist ", + address + ); table .row(tr) .remove() .draw(false); - } else showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); + } else { + utils.showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); + } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index 3a4cae4e..71e0e585 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -5,9 +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 disableAll:false */ -/* global enableAll:false */ +/* global utils:false */ var table; var groups = []; @@ -186,12 +184,12 @@ function addClient() { ip = $("#ip-custom").val(); } - disableAll(); - showAlert("info", "", "Adding client...", ip); + utils.disableAll(); + utils.showAlert("info", "", "Adding client...", ip); if (ip.length === 0) { - enableAll(); - showAlert("warning", "", "Warning", "Please specify a client IP address"); + utils.enableAll(); + utils.showAlert("warning", "", "Warning", "Please specify a client IP address"); return; } @@ -201,18 +199,18 @@ function addClient() { dataType: "json", data: { action: "add_client", ip: ip, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); + utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); reload_client_suggestions(); table.ajax.reload(); } else { - showAlert("error", "", "Error while adding new client", response.message); + utils.showAlert("error", "", "Error while adding new client", response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new client", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new client", jqXHR.responseText); console.log(exception); } }); @@ -238,29 +236,33 @@ function editClient() { ip_name += " (" + name + ")"; } - disableAll(); - showAlert("info", "", "Editing client...", ip_name); + utils.disableAll(); + utils.showAlert("info", "", "Editing client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "edit_client", id: id, groups: groups, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-plus", "Successfully " + done + " client", ip_name ); } else { - showAlert("error", "Error while " + not_done + " client with ID " + id, response.message); + utils.showAlert( + "error", + "Error while " + not_done + " client with ID " + id, + response.message + ); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " client with ID " + id, @@ -282,29 +284,34 @@ function deleteClient() { ip_name += " (" + name + ")"; } - disableAll(); - showAlert("info", "", "Deleting client...", ip_name); + utils.disableAll(); + utils.showAlert("info", "", "Deleting client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_client", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted client ", ip_name); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted client ", + ip_name + ); table .row(tr) .remove() .draw(false); reload_client_suggestions(); } else { - showAlert("error", "", "Error while deleting client with ID " + id, response.message); + utils.showAlert("error", "", "Error while deleting client with ID " + id, response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js index dce071f8..8cff4727 100644 --- a/scripts/pi-hole/js/groups-common.js +++ b/scripts/pi-hole/js/groups-common.js @@ -6,10 +6,6 @@ * Please see LICENSE file for your rights under this license. */ /* global moment:false */ -/* global showAlert:false */ -/* global datetime:false */ -/* global disableAll:false */ -/* global enableAll:false */ var info = null; function showAlert(type, icon, title, message) { @@ -88,3 +84,12 @@ function enableAll() { $("button").attr("disabled", false); $("textarea").attr("disabled", false); } + +window.utils = (function() { + return { + showAlert: showAlert, + datetime: datetime, + disableAll: disableAll, + enableAll: enableAll + }; +})(); diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js index 36748b40..85ae0647 100644 --- a/scripts/pi-hole/js/groups-domains.js +++ b/scripts/pi-hole/js/groups-domains.js @@ -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 groups = []; @@ -60,9 +57,9 @@ function initTable() { 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( @@ -191,12 +188,12 @@ function addDomain() { var type = $("#new_type").val(); var comment = $("#new_comment").val(); - disableAll(); - showAlert("info", "", "Adding domain...", domain); + utils.disableAll(); + utils.showAlert("info", "", "Adding domain...", domain); if (domain.length === 0) { - enableAll(); - showAlert("warning", "", "Warning", "Please specify a domain"); + utils.enableAll(); + utils.showAlert("warning", "", "Warning", "Please specify a domain"); return; } @@ -212,17 +209,17 @@ function addDomain() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); + utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); $("#new_domain").val(""); $("#new_comment").val(""); table.ajax.reload(); - } else showAlert("error", "", "Error while adding new domain", response.message); + } else utils.showAlert("error", "", "Error while adding new domain", response.message); }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new domain", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new domain", jqXHR.responseText); console.log(exception); } }); @@ -260,8 +257,8 @@ function editDomain() { not_done = "editing groups of"; } - disableAll(); - showAlert("info", "", "Editing domain...", name); + utils.disableAll(); + utils.showAlert("info", "", "Editing domain...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", @@ -276,16 +273,16 @@ function editDomain() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-pencil", "Successfully " + done + " domain", domain ); } else - showAlert( + utils.showAlert( "error", "", "Error while " + not_done + " domain with ID " + id, @@ -293,8 +290,8 @@ function editDomain() { ); }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " domain with ID " + id, @@ -310,26 +307,33 @@ function deleteDomain() { var tr = $(this).closest("tr"); var domain = tr.find("#domain").text(); - disableAll(); - showAlert("info", "", "Deleting domain...", domain); + utils.disableAll(); + utils.showAlert("info", "", "Deleting domain...", domain); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_domain", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted domain", domain); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted domain", + domain + ); table .row(tr) .remove() .draw(false); - } else showAlert("error", "", "Error while deleting domain with ID " + id, response.message); + } else { + utils.showAlert("error", "", "Error while deleting domain with ID " + id, response.message); + } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups.js b/scripts/pi-hole/js/groups.js index d4a6bbc7..0ffa3dcc 100644 --- a/scripts/pi-hole/js/groups.js +++ b/scripts/pi-hole/js/groups.js @@ -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); } });