Move objectToArray() to utils.

Signed-off-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
XhmikosR
2020-05-25 12:36:55 +03:00
parent eab6485edb
commit 7f71f1d5fe
4 changed files with 28 additions and 44 deletions
+1
View File
@@ -77,6 +77,7 @@ $token = $_SESSION['token'];
</div>
<script src="scripts/vendor/daterangepicker.js"></script>
<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/db_graph.js"></script>
<?php
+3 -21
View File
@@ -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 Chart:false, moment:false */
/* global utils:false, Chart:false, moment:false */
var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf() / 1000;
@@ -54,24 +54,6 @@ function padNumber(num) {
return ("00" + num).substr(-2, 2);
}
// Helper function needed for converting the Objects to Arrays
function objectToArray(p) {
var keys = Object.keys(p);
keys.sort(function (a, b) {
return a - b;
});
var arr = [],
idx = [];
for (var i = 0; i < keys.length; i++) {
arr.push(p[keys[i]]);
idx.push(keys[i]);
}
return [idx, arr];
}
var timeLineChart;
function compareNumbers(a, b) {
@@ -110,8 +92,8 @@ function updateQueriesOverTime() {
"api_db.php?getGraphData&from=" + from + "&until=" + until + "&interval=" + interval,
function (data) {
// convert received objects to arrays
data.domains_over_time = objectToArray(data.domains_over_time);
data.ads_over_time = objectToArray(data.ads_over_time);
data.domains_over_time = utils.objectToArray(data.domains_over_time);
data.ads_over_time = utils.objectToArray(data.ads_over_time);
// Remove possibly already existing data
timeLineChart.data.labels = [];
timeLineChart.data.datasets[0].data = [];
+3 -21
View File
@@ -34,24 +34,6 @@ function padNumber(num) {
return ("00" + num).substr(-2, 2);
}
// Helper function needed for converting the Objects to Arrays
function objectToArray(p) {
var keys = Object.keys(p);
keys.sort(function (a, b) {
return a - b;
});
var arr = [],
idx = [];
for (var i = 0; i < keys.length; i++) {
arr.push(p[keys[i]]);
idx.push(keys[i]);
}
return [idx, arr];
}
var customTooltips = function (tooltip) {
var tooltipEl = document.getElementById(this._chart.canvas.id + "-customTooltip");
if (!tooltipEl) {
@@ -245,8 +227,8 @@ function updateQueriesOverTime() {
}
// convert received objects to arrays
data.domains_over_time = objectToArray(data.domains_over_time);
data.ads_over_time = objectToArray(data.ads_over_time);
data.domains_over_time = utils.objectToArray(data.domains_over_time);
data.ads_over_time = utils.objectToArray(data.ads_over_time);
// remove last data point since it not representative
data.ads_over_time[0].splice(-1, 1);
// Remove possibly already existing data
@@ -369,7 +351,7 @@ function updateClientsOverTime() {
}
// convert received objects to arrays
data.over_time = objectToArray(data.over_time);
data.over_time = utils.objectToArray(data.over_time);
// remove last data point since it not representative
data.over_time[0].splice(-1, 1);
+21 -2
View File
@@ -7,7 +7,7 @@
/* global moment:false */
// Credit: https://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
// Credit: https://stackoverflow.com/a/4835406
function escapeHtml(text) {
var map = {
"&": "&amp;",
@@ -22,7 +22,25 @@ function escapeHtml(text) {
});
}
var info = null;
// Helper function for converting Objects to Arrays after sorting the keys
function objectToArray(obj) {
var arr = [];
var idx = [];
var keys = Object.keys(obj);
keys.sort(function (a, b) {
return a - b;
});
for (var i = 0; i < keys.length; i++) {
arr.push(obj[keys[i]]);
idx.push(keys[i]);
}
return [idx, arr];
}
var info = null; // TODO clear this up; there shouldn't be a global var here
function showAlert(type, icon, title, message) {
var opts = {};
title = "&nbsp;<strong>" + title + "</strong><br>";
@@ -188,6 +206,7 @@ function stateLoadCallback(itemName) {
window.utils = (function () {
return {
escapeHtml: escapeHtml,
objectToArray: objectToArray,
showAlert: showAlert,
datetime: datetime,
disableAll: disableAll,