Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable form elements while database operation is pending #1140

Merged
merged 4 commits into from Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions groups-adlists.php
Expand Up @@ -72,6 +72,7 @@
</div>
</div>

<script src="scripts/pi-hole/js/groups-common.js"></script>
<script src="scripts/pi-hole/js/groups-adlists.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions groups-clients.php
Expand Up @@ -72,6 +72,7 @@
</div>
</div>

<script src="scripts/pi-hole/js/groups-common.js"></script>
<script src="scripts/pi-hole/js/groups-clients.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions groups-domains.php
Expand Up @@ -82,6 +82,7 @@
</div>
</div>

<script src="scripts/pi-hole/js/groups-common.js"></script>
<script src="scripts/pi-hole/js/groups-domains.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions groups.php
Expand Up @@ -71,6 +71,7 @@
</div>
</div>

<script src="scripts/pi-hole/js/groups-common.js"></script>
<script src="scripts/pi-hole/js/groups.js"></script>

<?php
Expand Down
117 changes: 37 additions & 80 deletions scripts/pi-hole/js/groups-adlists.js
Expand Up @@ -5,71 +5,11 @@
* 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 utils:false */

var table;
var groups = [];
var token = $("#token").html();
var info = null;

function showAlert(type, icon, title, message) {
var opts = {};
title = "&nbsp;<strong>" + title + "</strong><br>";
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: "&nbsp;<strong>Error, something went wrong!</strong><br>",
message: message
};
if (info) {
info.update(opts);
} else {
$.notify(opts);
}

break;
default:
}
}

function get_groups() {
$.post(
Expand All @@ -83,10 +23,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);

Expand Down Expand Up @@ -120,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(
Expand Down Expand Up @@ -234,10 +170,11 @@ function addAdlist() {
var address = $("#new_address").val();
var comment = $("#new_comment").val();

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;
}

Expand All @@ -252,17 +189,24 @@ function addAdlist() {
token: token
},
success: function(response) {
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) {
showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText);
utils.enableAll();
utils.showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText);
console.log(exception);
}
});
Expand Down Expand Up @@ -293,7 +237,8 @@ function editAdlist() {
not_done = "editing groups of";
}

showAlert("info", "", "Editing adlist...", address);
utils.disableAll();
utils.showAlert("info", "", "Editing adlist...", address);

$.ajax({
url: "scripts/pi-hole/php/groups.php",
Expand All @@ -308,15 +253,16 @@ function editAdlist() {
token: token
},
success: function(response) {
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,
Expand All @@ -325,7 +271,8 @@ function editAdlist() {
}
},
error: function(jqXHR, exception) {
showAlert(
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while " + not_done + " adlist with ID " + id,
Expand All @@ -341,23 +288,33 @@ function deleteAdlist() {
var tr = $(this).closest("tr");
var address = tr.find("#address").text();

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) {
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) {
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);
}
});
Expand Down