Skip to content

Commit

Permalink
Move some duplicated code to utils.js
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
  • Loading branch information
PromoFaux authored and XhmikosR committed May 23, 2020
1 parent eb05394 commit 3957e9e
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 143 deletions.
2 changes: 1 addition & 1 deletion groups-domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<li role="presentation">
<a href="#tab_regex" aria-controls="tab_regex" aria-expanded="false" role="tab" data-toggle="tab">RegEx filter</a>
</li>
</ul>
</ul>
<div class="tab-content">
<!-- Domain tab -->
<div id="tab_domain" class="tab-pane active fade in">
Expand Down
1 change: 1 addition & 0 deletions network.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@
require "scripts/pi-hole/php/footer.php";
?>

<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/ip-address-sorting.js"></script>
<script src="scripts/pi-hole/js/network.js"></script>
1 change: 1 addition & 0 deletions queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
</div>
<!-- /.row -->

<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/queries.js"></script>

<?php
Expand Down
16 changes: 2 additions & 14 deletions scripts/pi-hole/js/groups-adlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,10 @@ function initTable() {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("groups-adlists-table", JSON.stringify(data));
utils.stateSaveCallback("groups-adlists-table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("groups-adlists-table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
var data = utils.stateLoadCallback("groups-adlists-table");
// Reset visibility of ID column
data.columns[0].visible = false;
// Apply loaded state to table
Expand Down
16 changes: 2 additions & 14 deletions scripts/pi-hole/js/groups-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,10 @@ function initTable() {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("groups-clients-table", JSON.stringify(data));
utils.stateSaveCallback("groups-clients-table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("groups-clients-table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
var data = utils.stateLoadCallback("groups-clients-table");
// Reset visibility of ID column
data.columns[0].visible = false;
// Apply loaded state to table
Expand Down
16 changes: 2 additions & 14 deletions scripts/pi-hole/js/groups-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,10 @@ function initTable() {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("groups-domains-table", JSON.stringify(data));
utils.stateSaveCallback("groups-domains-table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("groups-domains-table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
var data = utils.stateLoadCallback("groups-domains-table");
// Reset visibility of ID column
data.columns[0].visible = false;
// Show group assignment column only on full page
Expand Down
16 changes: 2 additions & 14 deletions scripts/pi-hole/js/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,10 @@ $(document).ready(function () {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("groups-table", JSON.stringify(data));
utils.stateSaveCallback("groups-table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("groups-table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
var data = utils.stateLoadCallback("groups-table");
// Reset visibility of ID column
data.columns[0].visible = false;
// Apply loaded state to table
Expand Down
17 changes: 2 additions & 15 deletions scripts/pi-hole/js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,10 @@ $(document).ready(function () {
},
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("messages-table", JSON.stringify(data));
utils.stateSaveCallback("messages-table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("messages-table");
// Return if not available
if (data === null) {
return null;
}

// Parse loaded data
data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
var data = utils.stateLoadCallback("messages-table");
// Reset visibility of ID and blob columns
var hiddenCols = [0, 4, 5, 6, 7, 8];
for (var key in hiddenCols) {
Expand Down
20 changes: 3 additions & 17 deletions scripts/pi-hole/js/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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 moment:false, utils:false */

var tableApi;

Expand Down Expand Up @@ -186,24 +186,10 @@ $(document).ready(function () {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("network_table", JSON.stringify(data));
utils.stateSaveCallback("network_table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("network_table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page
data.start = 0;
// Always start with empty search field
data.search.search = "";
// Apply loaded state to table
return data;
return utils.stateLoadCallback("network_table");
},
columnDefs: [
{
Expand Down
20 changes: 3 additions & 17 deletions scripts/pi-hole/js/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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 moment:false, utils:false */

var tableApi;

Expand Down Expand Up @@ -399,24 +399,10 @@ $(document).ready(function () {
],
stateSave: true,
stateSaveCallback: function (settings, data) {
// Store current state in client's local storage area
localStorage.setItem("query_log_table", JSON.stringify(data));
utils.stateSaveCallback("query_log_table", data);
},
stateLoadCallback: function () {
// Receive previous state from client's local storage area
var data = localStorage.getItem("query_log_table");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
// Apply loaded state to table
return data;
return utils.stateLoadCallback("query_log_table");
},
columnDefs: [
{
Expand Down
46 changes: 10 additions & 36 deletions scripts/pi-hole/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/* global utils:false */

$(function () {
$("[data-static]").on("click", function () {
var row = $(this).closest("tr");
Expand Down Expand Up @@ -176,25 +178,11 @@ $(document).ready(function () {
scrollX: true,
order: [[2, "asc"]],
stateSave: true,
stateSaveCallback: function(settings, data) {
// Store current state in client's local storage area
localStorage.setItem("activeDhcpLeaseTable", JSON.stringify(data));
stateSaveCallback: function (settings, data) {
utils.stateSaveCallback("activeDhcpLeaseTable", data);
},
stateLoadCallback: function() {
// Receive previous state from client's local storage area
var data = localStorage.getItem("activeDhcpLeaseTable");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
// Apply loaded state to table
return data;
stateLoadCallback: function () {
return utils.stateLoadCallback("activeDhcpLeaseTable");
}
});
}
Expand All @@ -209,25 +197,11 @@ $(document).ready(function () {
scrollX: true,
order: [[2, "asc"]],
stateSave: true,
stateSaveCallback: function(settings, data) {
// Store current state in client's local storage area
localStorage.setItem("staticDhcpLeaseTable", JSON.stringify(data));
stateSaveCallback: function (settings, data) {
utils.stateSaveCallback("staticDhcpLeaseTable", data);
},
stateLoadCallback: function() {
// Receive previous state from client's local storage area
var data = localStorage.getItem("staticDhcpLeaseTable");
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
// Apply loaded state to table
return data;
stateLoadCallback: function () {
return utils.stateLoadCallback("staticDhcpLeaseTable");
}
});
}
Expand Down
25 changes: 24 additions & 1 deletion scripts/pi-hole/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ function setBsSelectDefaults() {
};
}

function stateSaveCallback(itemName, data) {
localStorage.setItem(itemName, JSON.stringify(data));
}

function stateLoadCallback(itemName) {
// Receive previous state from client's local storage area
var data = localStorage.getItem(itemName);
// Return if not available
if (data === null) {
return null;
}

data = JSON.parse(data);
// Always start on the first page to show most recent queries
data.start = 0;
// Always start with empty search field
data.search.search = "";
// Apply loaded state to table
return data;
}

window.utils = (function () {
return {
showAlert: showAlert,
Expand All @@ -157,6 +178,8 @@ window.utils = (function () {
enableAll: enableAll,
validateIPv4CIDR: validateIPv4CIDR,
validateIPv6CIDR: validateIPv6CIDR,
setBsSelectDefaults: setBsSelectDefaults
setBsSelectDefaults: setBsSelectDefaults,
stateSaveCallback: stateSaveCallback,
stateLoadCallback: stateLoadCallback
};
})();
1 change: 1 addition & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ function convertseconds($argument)
</div>

<script src="scripts/vendor/jquery.confirm.min.js"></script>
<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/settings.js"></script>

<?php
Expand Down

0 comments on commit 3957e9e

Please sign in to comment.