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

Add suggested domains in group domains page #2209

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 53 additions & 0 deletions scripts/pi-hole/js/groups-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,67 @@ $(function () {
$("#new_domain").val("");
$("#wildcard_checkbox").prop("checked", false);
}

clearTimeout(suggestTimeout);
$("#suggest_domains").hide();
});

$("#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