From 32cd50b71887740c4a9d0997158ddbb3b89c4899 Mon Sep 17 00:00:00 2001 From: Rob Gill Date: Sat, 11 Aug 2018 08:29:03 +1000 Subject: [PATCH 1/2] Changes to formatting only (no functional changes) Issues identified by codacy *Multiple spaces removed *piholeChanged defined before use *curly brackets after if conditions *lonely if converted to else if Signed-off-by: Rob Gill --- scripts/pi-hole/js/db_lists.js | 6 ++--- scripts/pi-hole/js/footer.js | 36 ++++++++++++------------- scripts/pi-hole/js/index.js | 49 ++++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/scripts/pi-hole/js/db_lists.js b/scripts/pi-hole/js/db_lists.js index 43e569f7..e867b6bc 100644 --- a/scripts/pi-hole/js/db_lists.js +++ b/scripts/pi-hole/js/db_lists.js @@ -59,7 +59,7 @@ function updateTopClientsChart() { // Clear tables before filling them with data $("#client-frequency td").parent().remove(); - var clienttable = $("#client-frequency").find("tbody:last"); + var clienttable = $("#client-frequency").find("tbody:last"); var client, percentage, clientname, clientip; var sum = 0; for (client in data.top_sources) { @@ -113,7 +113,7 @@ function updateTopDomainsChart() { // Clear tables before filling them with data $("#domain-frequency td").parent().remove(); - var domaintable = $("#domain-frequency").find("tbody:last"); + var domaintable = $("#domain-frequency").find("tbody:last"); var domain, percentage; var sum = 0; for (domain in data.top_domains) { @@ -155,7 +155,7 @@ function updateTopAdsChart() { // Clear tables before filling them with data $("#ad-frequency td").parent().remove(); - var adtable = $("#ad-frequency").find("tbody:last"); + var adtable = $("#ad-frequency").find("tbody:last"); var ad, percentage; var sum = 0; for (ad in data.top_ads) { diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index e185d5e4..155efa29 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -15,24 +15,6 @@ function secondsTimeSpanToHMS(s) { return h+":"+(m < 10 ? "0"+m : m)+":"+(s < 10 ? "0"+s : s); //zero padding on minutes and seconds } -function countDown(){ - var ena = $("#enableLabel"); - var enaT = $("#enableTimer"); - var target = new Date(parseInt(enaT.html())); - var seconds = Math.round((target.getTime() - new Date().getTime()) / 1000); - - if(seconds > 0){ - setTimeout(countDown,1000); - ena.text("Enable (" + secondsTimeSpanToHMS(seconds) + ")"); - } - else - { - ena.text("Enable"); - piholeChanged("enabled"); - localStorage.removeItem("countDownTarget"); - } -} - function piholeChanged(action) { var status = $("#status"); @@ -56,6 +38,24 @@ function piholeChanged(action) } +function countDown(){ + var ena = $("#enableLabel"); + var enaT = $("#enableTimer"); + var target = new Date(parseInt(enaT.html())); + var seconds = Math.round((target.getTime() - new Date().getTime()) / 1000); + + if(seconds > 0){ + setTimeout(countDown,1000); + ena.text("Enable (" + secondsTimeSpanToHMS(seconds) + ")"); + } + else + { + ena.text("Enable"); + piholeChanged("enabled"); + localStorage.removeItem("countDownTarget"); + } +} + function piholeChange(action, duration) { var token = encodeURIComponent($("#token").html()); diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 8c28f0bf..7d475193 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -79,7 +79,7 @@ var customTooltips = function(tooltip) { var style = "background:" + colors.backgroundColor; style += "; border-color:" + colors.borderColor; style += "; border-width: 2px"; - var span = ""; + var span = ""; var num = body[0].split(": "); if(num[1] > 0) { @@ -189,7 +189,7 @@ function updateQueryTypesOverTime() { // remove last data point since it not representative data.over_time[0].splice(-1,1); var timestamps = data.over_time[0]; - var plotdata = data.over_time[1]; + var plotdata = data.over_time[1]; // Remove possibly already existing data queryTypeChart.data.labels = []; queryTypeChart.data.datasets[0].data = []; @@ -307,7 +307,9 @@ function updateForwardedOverTime() { var key, i, j; for (key in data.forward_destinations) { - if (!{}.hasOwnProperty.call(data.forward_destinations, key)) continue; + if (!{}.hasOwnProperty.call(data.forward_destinations, key)) { + continue; + } if(key.indexOf("|") > -1) { var idx = key.indexOf("|"); @@ -344,10 +346,14 @@ function updateForwardedOverTime() { // Add data for each dataset that is available for (j in timestamps) { - if (!{}.hasOwnProperty.call(timestamps, j)) continue; + if (!{}.hasOwnProperty.call(timestamps, j)) { + continue; + } for (key in plotdata[j]) { - if (!{}.hasOwnProperty.call(plotdata[j], key)) continue; + if (!{}.hasOwnProperty.call(plotdata[j], key)) { + continue; + } forwardDestinationChart.data.datasets[key].data.push(1e-2*plotdata[j][key]); } @@ -396,7 +402,9 @@ function updateClientsOverTime() { var key, i, j; for (key in data.clients) { - if (!{}.hasOwnProperty.call(data.clients, key)) continue; + if (!{}.hasOwnProperty.call(data.clients, key)) { + continue; + } var clientname; if(data.clients[key].name.length > 0) { @@ -436,10 +444,14 @@ function updateClientsOverTime() { // Add data for each dataset that is available for (j in timestamps) { - if (!{}.hasOwnProperty.call(timestamps, j)) continue; + if (!{}.hasOwnProperty.call(timestamps, j)) { + continue; + } for (key in plotdata[j]) { - if (!{}.hasOwnProperty.call(plotdata[j], key)) continue; + if (!{}.hasOwnProperty.call(plotdata[j], key)) { + continue; + } clientsChart.data.datasets[key].data.push(plotdata[j][key]); } @@ -730,19 +742,16 @@ function updateSummaryData(runOnce) { FTLoffline = true; } - else + else if(FTLoffline) { - if(FTLoffline) - { - // FTL was previously offline - FTLoffline = false; - $("#temperature").text(" "); - updateQueriesOverTime(); - updateForwardedOverTime(); - updateQueryTypesOverTime(); - updateTopClientsChart(); - updateTopLists(); - } + // FTL was previously offline + FTLoffline = false; + $("#temperature").text(" "); + updateQueriesOverTime(); + updateForwardedOverTime(); + updateQueryTypesOverTime(); + updateTopClientsChart(); + updateTopLists(); } ["ads_blocked_today", "dns_queries_today", "ads_percentage_today", "unique_clients"].forEach(function(today) { From 5dfe0eedef35fe0ddd274c8cd1a8b44c9d1993c1 Mon Sep 17 00:00:00 2001 From: Rob Gill Date: Sat, 11 Aug 2018 09:26:58 +1000 Subject: [PATCH 2/2] Standalone open curly brackets for if and for statements For code consistency all opening curly brackets for if and for statements now stand alone on their own lines Signed-off-by: Rob Gill --- scripts/pi-hole/js/index.js | 62 ++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 7d475193..6c59917a 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -15,14 +15,15 @@ function padNumber(num) { // Helper function needed for converting the Objects to Arrays -function objectToArray(p){ +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++) { + for (var i = 0; i < keys.length; i++) + { arr.push(p[keys[i]]); idx.push(keys[i]); } @@ -34,14 +35,16 @@ var lastTooltipTime = 0; var customTooltips = function(tooltip) { // Tooltip Element var tooltipEl = document.getElementById("chartjs-tooltip"); - if (!tooltipEl) { + if (!tooltipEl) + { tooltipEl = document.createElement("div"); tooltipEl.id = "chartjs-tooltip"; document.body.appendChild(tooltipEl); $(tooltipEl).html("
"); } // Hide if no tooltip - if (tooltip.opacity === 0) { + if (tooltip.opacity === 0) + { tooltipEl.style.opacity = 0; return; } @@ -49,14 +52,16 @@ var customTooltips = function(tooltip) { // Limit rendering to once every 50ms. This gives the DOM time to react, // and avoids "lag" caused by not giving the DOM time to reapply CSS. var now = Date.now(); - if(now - lastTooltipTime < 50) { + if(now - lastTooltipTime < 50) + { return; } lastTooltipTime = now; // Set caret Position tooltipEl.classList.remove("above", "below", "no-transform"); - if (tooltip.yAlign) { + if (tooltip.yAlign) + { tooltipEl.classList.add(tooltip.yAlign); } else { tooltipEl.classList.add("above"); @@ -65,7 +70,8 @@ var customTooltips = function(tooltip) { return bodyItem.lines; } // Set Text - if (tooltip.body) { + if (tooltip.body) + { var titleLines = tooltip.title || []; var bodyLines = tooltip.body.map(getBody); var innerHtml = ""; @@ -139,8 +145,10 @@ function updateQueriesOverTime() { timeLineChart.data.datasets[1].data = []; // Add data for each hour that is available - for (var hour in data.ads_over_time[0]) { - if ({}.hasOwnProperty.call(data.ads_over_time[0], hour)) { + for (var hour in data.ads_over_time[0]) + { + if ({}.hasOwnProperty.call(data.ads_over_time[0], hour)) + { var d,h; h = parseInt(data.domains_over_time[0][hour]); if(parseInt(data.ads_over_time[0][0]) < 1200) @@ -201,8 +209,10 @@ function updateQueryTypesOverTime() { queryTypeChart.data.datasets[1].backgroundColor = colors[1]; // Add data for each hour that is available - for (var j in timestamps) { - if ({}.hasOwnProperty.call(timestamps, j)) { + for (var j in timestamps) + { + if ({}.hasOwnProperty.call(timestamps, j)) + { var d,h; h = parseInt(timestamps[j]); // New style: Get Unix timestamps @@ -307,7 +317,8 @@ function updateForwardedOverTime() { var key, i, j; for (key in data.forward_destinations) { - if (!{}.hasOwnProperty.call(data.forward_destinations, key)) { + if (!{}.hasOwnProperty.call(data.forward_destinations, key)) + { continue; } if(key.indexOf("|") > -1) @@ -346,12 +357,14 @@ function updateForwardedOverTime() { // Add data for each dataset that is available for (j in timestamps) { - if (!{}.hasOwnProperty.call(timestamps, j)) { + if (!{}.hasOwnProperty.call(timestamps, j)) + { continue; } for (key in plotdata[j]) { - if (!{}.hasOwnProperty.call(plotdata[j], key)) { + if (!{}.hasOwnProperty.call(plotdata[j], key)) + { continue; } forwardDestinationChart.data.datasets[key].data.push(1e-2*plotdata[j][key]); @@ -402,7 +415,8 @@ function updateClientsOverTime() { var key, i, j; for (key in data.clients) { - if (!{}.hasOwnProperty.call(data.clients, key)) { + if (!{}.hasOwnProperty.call(data.clients, key)) + { continue; } var clientname; @@ -444,12 +458,14 @@ function updateClientsOverTime() { // Add data for each dataset that is available for (j in timestamps) { - if (!{}.hasOwnProperty.call(timestamps, j)) { + if (!{}.hasOwnProperty.call(timestamps, j)) + { continue; } for (key in plotdata[j]) { - if (!{}.hasOwnProperty.call(plotdata[j], key)) { + if (!{}.hasOwnProperty.call(plotdata[j], key)) + { continue; } clientsChart.data.datasets[key].data.push(plotdata[j][key]); @@ -597,7 +613,8 @@ function updateTopClientsChart() { // Clear tables before filling them with data $("#client-frequency-blocked td").parent().remove(); var clientblockedtable = $("#client-frequency-blocked").find("tbody:last"); - for (client in data.top_sources_blocked) { + for (client in data.top_sources_blocked) + { if ({}.hasOwnProperty.call(data.top_sources_blocked, client)){ // Sanitize client @@ -660,7 +677,8 @@ function updateTopLists() { var domaintable = $("#domain-frequency").find("tbody:last"); var adtable = $("#ad-frequency").find("tbody:last"); var url, domain, percentage; - for (domain in data.top_queries) { + for (domain in data.top_queries) + { if ({}.hasOwnProperty.call(data.top_queries,domain)){ // Sanitize domain if(escapeHtml(domain) !== domain) @@ -683,7 +701,8 @@ function updateTopLists() { $("#domain-frequency").parent().remove(); } - for (domain in data.top_ads) { + for (domain in data.top_ads) + { if ({}.hasOwnProperty.call(data.top_ads,domain)){ // Sanitize domain if(escapeHtml(domain) !== domain) @@ -716,7 +735,8 @@ function updateTopLists() { var FTLoffline = false; function updateSummaryData(runOnce) { var setTimer = function(timeInSeconds) { - if (!runOnce) { + if (!runOnce) + { setTimeout(updateSummaryData, timeInSeconds * 1000); } };