Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid window.open and add more visible eye icon #1996

Merged
merged 14 commits into from
Dec 21, 2021
110 changes: 69 additions & 41 deletions scripts/pi-hole/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,33 +321,64 @@ function updateQueryTypesPie() {
queryTypePieChart.options.animation.duration = 0;
// Generate legend in separate div
$("#query-types-legend").html(queryTypePieChart.generateLegend());
$("#query-types-legend > ul > li").on("mousedown", function (e) {
if (e.which === 2) {
// which == 2 is middle mouse button
$(this).toggleClass("strike");
var index = $(this).index();
var ci = e.view.queryTypePieChart;
var mobj = ci.data.datasets[0]._meta;
var metas = Object.keys(mobj).map(function (e) {
return mobj[e];
});
metas.forEach(function (meta) {
var curr = meta.data[index];
curr.hidden = !curr.hidden;
});

ci.update();
} else if (e.which === 1) {
// which == 1 is left mouse button
window.open("queries.php?querytype=" + querytypeids[$(this).index()], "_self");
$("#query-types-legend > ul > li").prepend(createEyeConElement());
$("#query-types-legend > ul > li").click(function (e) {
if (isEyeCon(e.target)) {
return false;
}

window.location.href = "queries.php?querytype=" + querytypeids[$(this).index()];
});
}).done(function () {
// Reload graph after minute
setTimeout(updateQueryTypesPie, 60000);
});
}

function hidePieSlice(event) {
toggleEyeCon(event.target);

var legendID = $(event.target).closest(".chart-legend").attr("id");
var ci =
legendID === "query-types-legend"
? event.view.queryTypePieChart
: event.view.forwardDestinationPieChart;

var listItemParent = $(event.target).closest("li");
$(listItemParent).toggleClass("strike");

var index = $(listItemParent).index();
var mobj = ci.data.datasets[0]._meta;
var metas = Object.keys(mobj).map(function (e) {
return mobj[e];
});
metas.forEach(function (meta) {
var curr = meta.data[index];
curr.hidden = !curr.hidden;
});

ci.update();
}

function toggleEyeCon(target) {
var parentListItem = $(target).closest("li");
var eyeCon = $(parentListItem).find(".fa-eye, .fa-eye-slash");

if (eyeCon) {
$(eyeCon).toggleClass("fa-eye");
$(eyeCon).toggleClass("fa-eye-slash");
}
}

function isEyeCon(target) {
// See if click happened on eyeConWrapper or child SVG
if ($(target).closest(".eyeConWrapper")[0]) {
return true;
}

return false;
}

function updateClientsOverTime() {
$.getJSON("api.php?overTimeDataClients&getClientNames", function (data) {
if ("FTLnotrunning" in data) {
Expand Down Expand Up @@ -437,6 +468,16 @@ function updateClientsOverTime() {
});
}

function createEyeConElement() {
var eyeConWrapper = $("<span></span>")
.addClass("eyeConWrapper")
.click(function (e) {
hidePieSlice(e);
});
eyeConWrapper.append($("<i class='fa fa-eye'></i>"));
return eyeConWrapper;
}

function updateForwardDestinationsPie() {
$.getJSON("api.php?getForwardDestinations", function (data) {
if ("FTLnotrunning" in data) {
Expand Down Expand Up @@ -480,28 +521,15 @@ function updateForwardDestinationsPie() {
forwardDestinationPieChart.options.animation.duration = 0;
// Generate legend in separate div
$("#forward-destinations-legend").html(forwardDestinationPieChart.generateLegend());
$("#forward-destinations-legend > ul > li").on("mousedown", function (e) {
if (e.which === 2) {
// which == 2 is middle mouse button
$(this).toggleClass("strike");
var index = $(this).index();
var ci = e.view.forwardDestinationPieChart;
var mobj = ci.data.datasets[0]._meta;
var metas = Object.keys(mobj).map(function (e) {
return mobj[e];
});
metas.forEach(function (meta) {
var curr = meta.data[index];
curr.hidden = !curr.hidden;
});

ci.update();
} else if (e.which === 1) {
// which == 1 is left mouse button
var obj = encodeURIComponent(e.target.textContent);
if (obj.length > 0) {
window.open("queries.php?forwarddest=" + obj, "_self");
}
$("#forward-destinations-legend > ul > li").prepend(createEyeConElement());
$("#forward-destinations-legend > ul > li").click(function (e) {
if (isEyeCon(e.target)) {
return false;
}

var obj = encodeURIComponent(e.target.textContent);
if (obj.length > 0) {
window.location.href = "queries.php?forwarddest=" + obj;
}
});
}).done(function () {
Expand Down
14 changes: 13 additions & 1 deletion scripts/pi-hole/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ $(".api-token").confirm({
text: "Make sure that nobody else can scan this code around you. They will have full access to the API without having to know the password. Note that the generation of the QR code will take some time.",
title: "Confirmation required",
confirm: function () {
window.open("scripts/pi-hole/php/api_token.php");
$("#apiTokenModal").modal("show");
},
cancel: function () {
// nothing to do
Expand All @@ -182,6 +182,18 @@ $(".api-token").confirm({
dialogClass: "modal-dialog",
});

$("#apiTokenModal").on("show.bs.modal", function () {
var bodyStyle = {
"font-family": $("body").css("font-family"),
"background-color": "white",
};
$('iframe[name="apiToken_iframe"]').contents().find("body").css(bodyStyle);
var qrCodeStyle = {
margin: "auto",
};
$('iframe[name="apiToken_iframe"]').contents().find("table").css(qrCodeStyle);
});

$("#DHCPchk").click(function () {
$("input.DHCPgroup").prop("disabled", !this.checked);
$("#dhcpnotice").prop("hidden", !this.checked).addClass("lookatme");
Expand Down
16 changes: 16 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,22 @@ function convertseconds($argument)
</div>
</div>
</form>
<div class="modal fade" id="apiTokenModal" role="dialog" data-keyboard="false"
tabindex="-1" data-backdrop="static" aria-labelledby="apiTokenModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="apiTokenModalHeaderLabel">API Token</h4>
</div>
<div class="modal-body">
<pre><iframe id="apiTokenIframe" name="apiToken_iframe" src="scripts/pi-hole/php/api_token.php"></iframe></pre>
Copy link
Contributor Author

@paul-man paul-man Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The QR code doesn't look great on mobile. Because it's built using a <table> the height can only be responsive to a certain extent. I tried using the createImage() function from the QR code lib being used but no dice.

Not sure how important this is, since if you're accessing the API token on mobile you most likely will be copying the raw string rather than using another mobile device to take a photo of the qr code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion it's an edge case. Generating a qr code with a mobile to be scanned with another mobile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This is how it looks on my screen:
Screenshot_20211209-205750.png

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Could you add support for the theme's font color as we did in #1934. (Otherwise it's black font on black background in the dark themes)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be safer to always use white background and black text for the <pre> element that displays the QRcode (even in dark themes).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use a white background no matter the theme. My QR scanner worked with each theme. Thanks for pointing this out and brainstorming on this!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small nit: could could you remove (or color white) the small leftover margin. Best seen in LCARS theme

Bildschirmfoto zu 2021-12-14 07-11-32

Copy link
Member

@rdwebdesign rdwebdesign Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the <pre> background-color.

You just need to add this into pi-hole.css:

#apiTokenModal pre {
    background: #FFF;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also in the other themes!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited.

</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<form role="form" method="post">
Expand Down
10 changes: 10 additions & 0 deletions style/pi-hole.css
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,13 @@ td.details-control {
.navbar-nav {
height: 50px;
}

.eyeConWrapper {
font-size: 17px;
}

#apiTokenIframe {
width: 100%;
border: 0;
height: 500px;
}