Skip to content

Commit

Permalink
Move common functions to common.js
Browse files Browse the repository at this point in the history
Signed-off-by: XhmikosR <[email protected]>
  • Loading branch information
XhmikosR committed Jan 4, 2020
1 parent 8731be8 commit 25cea0f
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 402 deletions.
17 changes: 2 additions & 15 deletions scripts/pi-hole/js/auditlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/* global escapeHtml:false */

// Define global variables
var auditList = [],
auditTimeout;

// Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
function escapeHtml(text) {
var map = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#039;"
};

return text.replace(/[&<>"']/g, function(m) {
return map[m];
});
}

function updateTopLists() {
$.getJSON("api.php?topItems=audit", function(data) {
if ("FTLnotrunning" in data) {
Expand Down
144 changes: 144 additions & 0 deletions scripts/pi-hole/js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 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 ActiveXObject:false */

var info = null;

function quietfilter(ta, data) {
var lines = data.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i].indexOf("results") !== -1 && lines[i].indexOf("0 results") === -1) {
var shortstring = lines[i].replace("::: /etc/pihole/", "");
// Remove "(x results)"
shortstring = shortstring.replace(/\(.*/, "");
ta.append(shortstring + "\n");
}
}
}

// Credit: https://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
window.escapeHtml = function(text) {
var map = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#039;"
};

return text.replace(/[&<>"']/g, function(m) {
return map[m];
});
};

// Credit: https://stackoverflow.com/a/10642418/2087442
window.httpGet = function(ta, theUrl, quiet) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
ta.show();
ta.empty();
if (!quiet) {
ta.append(xmlhttp.responseText);
} else {
quietfilter(ta, xmlhttp.responseText);
}
}
};

xmlhttp.open("GET", theUrl, false);
xmlhttp.send();
};

window.padNumber = function(num) {
return ("00" + num).substr(-2, 2);
};

// Helper function needed for converting the Objects to Arrays
window.objectToArray = function(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];
};

window.showAlert = function(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:
}
};
24 changes: 1 addition & 23 deletions scripts/pi-hole/js/db_graph.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 Chart:false, moment:false */
/* global Chart:false, moment:false, objectToArray:false, padNumber:false */

var start__ = moment().subtract(6, "days");
var from =
Expand Down Expand Up @@ -72,28 +72,6 @@ $(function() {
);
});

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) {
Expand Down
17 changes: 1 addition & 16 deletions scripts/pi-hole/js/db_lists.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, escapeHtml:false */

var start__ = moment().subtract(6, "days");
var from =
Expand Down Expand Up @@ -72,21 +72,6 @@ $(function() {
);
});

// Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
function escapeHtml(text) {
var map = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#039;"
};

return text.replace(/[&<>"']/g, function(m) {
return map[m];
});
}

function updateTopClientsChart() {
$("#client-frequency .overlay").show();
$.getJSON("api_db.php?topClients&from=" + from + "&until=" + until, function(data) {
Expand Down
25 changes: 1 addition & 24 deletions scripts/pi-hole/js/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/* global ActiveXObject: false */

// Credit: http://stackoverflow.com/a/10642418/2087442
function httpGet(ta, theUrl) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
ta.show();
ta.empty();
ta.append(xmlhttp.responseText);
}
};

xmlhttp.open("GET", theUrl, false);
xmlhttp.send();
}
/* global httpGet:false */

function eventsource() {
var ta = $("#output");
Expand Down
62 changes: 1 addition & 61 deletions scripts/pi-hole/js/groups-adlists.js
Original file line number Diff line number Diff line change
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 moment:false, showAlert: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 Down
Loading

0 comments on commit 25cea0f

Please sign in to comment.