Skip to content

Commit

Permalink
Use automatic percent formatting (#2972)
Browse files Browse the repository at this point in the history
  • Loading branch information
PromoFaux committed Mar 4, 2024
2 parents 8f23b3a + 2a45388 commit f79a404
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
9 changes: 4 additions & 5 deletions scripts/pi-hole/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,8 @@ function updateSummaryData(runOnce = false) {
intl.format(parseInt(data.clients.total, 10)) + " total clients"
);
glowIfChanged($("span#blocked_queries"), intl.format(parseFloat(data.queries.blocked)));
glowIfChanged(
$("span#percent_blocked"),
parseFloat(data.queries.percent_blocked).toFixed(1) + "%"
);
var formattedPercentage = utils.toPercent(data.queries.percent_blocked, 1);
glowIfChanged($("span#percent_blocked"), formattedPercentage);
glowIfChanged(
$("span#gravity_size"),
intl.format(parseInt(data.gravity.domains_being_blocked, 10))
Expand Down Expand Up @@ -536,7 +534,8 @@ $(function () {
percentage = (100 * blocked) / (permitted + blocked);
}

label += ": " + tooltipLabel.parsed.y + " (" + percentage.toFixed(1) + "%)";
var formattedPercentage = utils.toPercent(percentage, 1);
label += `: ${tooltipLabel.parsed.y} (${formattedPercentage})`;
} else {
label += ": " + tooltipLabel.parsed.y;
}
Expand Down
17 changes: 14 additions & 3 deletions scripts/pi-hole/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,20 @@ function addTD(content) {
return "<td>" + content + "</td> ";
}

function toPercent(number, fractionDigits = 0) {
const userLocale = navigator.language || "en-US";
return new Intl.NumberFormat(userLocale, {
style: "percent",
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
}).format(number / 100);
}

function colorBar(percentage, total, cssClass) {
var title = percentage.toFixed(1) + "% of " + total;
var bar = '<div class="progress-bar ' + cssClass + '" style="width: ' + percentage + '%"></div>';
return '<div class="progress progress-sm" title="' + title + '"> ' + bar + " </div>";
const formattedPercentage = toPercent(percentage, 1);
const title = `${formattedPercentage} of ${total}`;
const bar = `<div class="progress-bar ${cssClass}" style="width: ${percentage}%"></div>`;
return `<div class="progress progress-sm" title="${title}"> ${bar} </div>`;
}

function checkMessages() {
Expand Down Expand Up @@ -676,6 +686,7 @@ window.utils = (function () {
validateHostname: validateHostname,
addFromQueryLog: addFromQueryLog,
addTD: addTD,
toPercent: toPercent,
colorBar: colorBar,
checkMessages: checkMessages,
changeBulkDeleteStates: changeBulkDeleteStates,
Expand Down

0 comments on commit f79a404

Please sign in to comment.