Skip to content

Commit

Permalink
Show suggested domains in group domains page
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony de Jong <11158888+anthony-de-jong@users.noreply.github.com>
  • Loading branch information
anthony-de-jong committed May 31, 2022
1 parent 56d725d commit f4d91de
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion groups-domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
<div class="col-md-6">
<div class="form-group">
<label for="new_domain">Domain:</label>
<input id="new_domain" type="url" class="form-control active" placeholder="Domain to be added" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
<input id="new_domain" type="url" class="form-control active" placeholder="Domain to be added" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
<div id="suggest_domains" class="table-responsive no-border"></div>
</div>
</div>
<div class="col-md-6 form-group">
Expand Down
50 changes: 50 additions & 0 deletions scripts/pi-hole/js/groups-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,60 @@ $(function () {

$("#add2black, #add2white").on("click", addDomain);

var suggestTimeout;
$("#new_domain").on("input", function (e) {
hideSuggestDomains();
clearTimeout(suggestTimeout);
suggestTimeout = setTimeout(showSuggestDomains, 1000, e.target.value);
});

utils.setBsSelectDefaults();
getGroups();
});

function showSuggestDomains(value) {
function createButton(hostname) {
// Purposefully omit 'btn' class to save space on padding
return $('<button type="button" class="btn-link btn-block text-right">')
.append($("<i>").text(hostname))
.click(function () {
hideSuggestDomains();
newDomainEl.val(hostname);
});
}

var newDomainEl = $("#new_domain");
var suggestDomainEl = $("#suggest_domains");

try {
// URL is not supported in all browsers, but we are in a try-block so we can ignore it
// eslint-disable-next-line compat/compat
var parts = new URL(value).hostname.split(".");
var table = $("<table>");

for (var i = 0; i < parts.length - 1; ++i) {
var hostname = parts.slice(i).join(".");

table.append(
$("<tr>")
.append($('<td class="text-nowrap text-right">').text(i === 0 ? "Did you mean" : "or"))
.append($("<td>").append(createButton(hostname)))
);
}

suggestDomainEl.slideUp("fast", function () {
suggestDomainEl.html(table);
suggestDomainEl.slideDown("fast");
});
} catch {
hideSuggestDomains();
}
}

function hideSuggestDomains() {
$("#suggest_domains").slideUp("fast");
}

function initTable() {
table = $("#domainsTable").DataTable({
ajax: {
Expand Down

0 comments on commit f4d91de

Please sign in to comment.