diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index 1f6e1bf7..1eb94137 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -16,17 +16,17 @@ function piholeChanged(action) switch(action) { case "enabled": - status.html(" Active"); - ena.hide(); - dis.show(); - dis.removeClass("active"); - break; + status.html(" Active"); + ena.hide(); + dis.show(); + dis.removeClass("active"); + break; case "disabled": - status.html(" Offline"); - ena.show(); - dis.hide(); - break; + status.html(" Offline"); + ena.show(); + dis.hide(); + break; } } @@ -38,26 +38,26 @@ function piholeChange(action, duration) switch(action) { case "enable": - btnStatus = $("#flip-status-enable"); - btnStatus.html(" "); - $.getJSON("api.php?enable&token=" + token, function(data) { - if(data.status === "enabled") { - btnStatus.html(""); - piholeChanged("enabled"); - } - }); - break; + btnStatus = $("#flip-status-enable"); + btnStatus.html(" "); + $.getJSON("api.php?enable&token=" + token, function(data) { + if(data.status === "enabled") { + btnStatus.html(""); + piholeChanged("enabled"); + } + }); + break; case "disable": - btnStatus = $("#flip-status-disable"); - btnStatus.html(" "); - $.getJSON("api.php?disable=" + duration + "&token=" + token, function(data) { - if(data.status === "disabled") { - btnStatus.html(""); - piholeChanged("disabled"); - } - }); - break; + btnStatus = $("#flip-status-disable"); + btnStatus.html(" "); + $.getJSON("api.php?disable=" + duration + "&token=" + token, function(data) { + if(data.status === "disabled") { + btnStatus.html(""); + piholeChanged("disabled"); + } + }); + break; } } @@ -72,27 +72,71 @@ $("#pihole-disable-permanently").on("click", function(e){ }); $("#pihole-disable-10s").on("click", function(e){ e.preventDefault(); - piholeChange("disable","10"); - setTimeout(function(){piholeChanged("enabled");},10000); + setCountdownTarget(10); + setTimeout(countDown,1000); }); $("#pihole-disable-30s").on("click", function(e){ e.preventDefault(); piholeChange("disable","30"); - setTimeout(function(){piholeChanged("enabled");},30000); + setCountdownTarget(30); + setTimeout(countDown,1000); }); $("#pihole-disable-5m").on("click", function(e){ e.preventDefault(); piholeChange("disable","300"); - setTimeout(function(){piholeChanged("enabled");},300000); + setCountdownTarget(300); + setTimeout(countDown,1000); }); $("#pihole-disable-custom").on("click", function(e){ e.preventDefault(); - var custVal = $("#customTimeout").val(); - custVal = $("#btnMins").hasClass("active") ? custVal * 60 : custVal; + var custVal = $('#customTimeout').val(); + custVal = $('#btnMins').hasClass("active") ? custVal * 60 : custVal; piholeChange("disable",custVal); - setTimeout(function(){piholeChanged("enabled");},custVal * 1000); + setCountdownTarget(custVal); + setTimeout(countDown,1000); }); +//The following three functions allow us to display time until pi-hole is enabled after disabling. +//Works between all pages +$( document ).ready(function() { + var countDownTarget = localStorage.getItem("countDownTarget"); + if (countDownTarget != null) + { + setTimeout(countDown,1000); + } +}); + +function setCountdownTarget(seconds){ + var target = new Date(); + target = new Date(target.getTime() + seconds * 1000); + localStorage.setItem('countDownTarget', target); +} + +function countDown(){ + var ena = $("#enableLabel"); + var target = new Date(localStorage.getItem("countDownTarget")); + 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 secondsTimeSpanToHMS(s) { + var h = Math.floor(s/3600); //Get whole hours + s -= h*3600; + var m = Math.floor(s/60); //Get remaining minutes + s -= m*60; + return h+":"+(m < 10 ? '0'+m : m)+":"+(s < 10 ? '0'+s : s); //zero padding on minutes and seconds +} + var piholeVersion = $("#piholeVersion").html(); var webVersion = $("#webVersion").html(); @@ -201,15 +245,15 @@ else // Hide "exact match" button on queryads.php page if version is 2.9.5 or lower if(versionCompare(piholeVersion, "v2.9.5") < 1) { - $("#btnSearchExact").hide(); + $("#btnSearchExact").hide(); } // Handle Strg + Enter button on Login page $(document).keypress(function(e) { - if((e.keyCode === 10 || e.keyCode === 13) && e.ctrlKey && $("#loginpw").is(":focus")) { - $("#loginform").attr("action", "settings.php"); - $("#loginform").submit(); - } + if((e.keyCode === 10 || e.keyCode === 13) && e.ctrlKey && $("#loginpw").is(":focus")) { + $("#loginform").attr("action", "settings.php"); + $("#loginform").submit(); + } }); function testCookies()