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

feat(searchbar): keyboard navigation #399

Merged
merged 1 commit into from
Jul 13, 2024
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
104 changes: 98 additions & 6 deletions public/components/searchbar/searchbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const kHelpersTitleName = {
license: "Available licenses",
flag: "Available flags"
};
const kFocusHelperBgColor = "#1976d2";
const kHelpersTemplateName = {
flag: "search_helpers_flags",
license(linker) {
Expand Down Expand Up @@ -92,11 +93,82 @@ export class SearchBar {
this.forceNewItem = false;
let confirmBackspace = false;
let currentActiveQueryName = null;
let helperSelectionIndex = null;

this.container.addEventListener("click", () => this.open());

this.input.addEventListener("keyup", (event) => {
const shownPackages = document.querySelectorAll(".search-result-pannel > .package:not(.hide)");
const shownHelpers = document.querySelectorAll(".helpers > .line:not(.hide)");
const shownItems = [...shownHelpers, ...shownPackages];

if (event.key === "ArrowDown") {
if (helperSelectionIndex === null) {
helperSelectionIndex = 0;
}
else {
helperSelectionIndex++;
if (helperSelectionIndex >= shownItems.length) {
helperSelectionIndex = 0;
}
}

shownItems.forEach((line, index) => {
line.style.backgroundColor = "initial";

if (index === helperSelectionIndex) {
line.style.backgroundColor = kFocusHelperBgColor;
}
});

return;
}
else if (event.key === "ArrowUp") {
if (helperSelectionIndex === null) {
helperSelectionIndex = shownItems.length - 1;
}
else {
helperSelectionIndex--;
if (helperSelectionIndex < 0) {
helperSelectionIndex = shownItems.length - 1;
}
}

shownItems.forEach((line, index) => {
line.style.backgroundColor = "initial";

if (index === helperSelectionIndex) {
line.style.backgroundColor = kFocusHelperBgColor;
}
});

return;
}
else if (event.key !== "Enter") {
helperSelectionIndex = null;
}
if ((this.input.value === null || this.input.value === "")) {
if (event.key === "Enter" && helperSelectionIndex !== null) {
const line = shownItems[helperSelectionIndex];
if (line.classList.contains("package")) {
helperSelectionIndex = null;
this.close();
this.focusNodeById(line.getAttribute("data-value"));

return;
}
this.input.value = line.getAttribute("data-value");

const [keyword] = this.input.value.split(":");
if (kFiltersName.has(keyword)) {
currentActiveQueryName = keyword;
this.showPannelHelper(currentActiveQueryName);
helperSelectionIndex = null;
}

return;
}

// we always want to show the default helpers when the input is empty!
this.showPannelHelper();

Expand Down Expand Up @@ -168,6 +240,29 @@ export class SearchBar {
return;
}

if (event.key === "Enter" && helperSelectionIndex !== null) {
const line = shownItems[helperSelectionIndex];
helperSelectionIndex = null;
if (line.classList.contains("package")) {
line.style.backgroundColor = "initial";
this.close();
this.focusNodeById(line.getAttribute("data-value"));
}
else {
if (line.getAttribute("data-value").startsWith(this.input.value)) {
this.input.value = line.getAttribute("data-value");
const [keyword] = this.input.value.split(":");
currentActiveQueryName = keyword;
this.showPannelHelper(currentActiveQueryName);

return;
}

this.input.value += line.getAttribute("data-value");

this.helper.classList.add("hide");
}
}
// fetch the search text
const text = isPackageSearch ?
this.input.value.trim() :
Expand All @@ -192,10 +287,7 @@ export class SearchBar {
this.showPannelHelper();
currentActiveQueryName = null;
this.input.value = "";

if (!isPackageSearch && !this.forceNewItem) {
return;
}
helperSelectionIndex = null;
}

this.showResultsByIds(matchingIds);
Expand All @@ -205,6 +297,7 @@ export class SearchBar {
if (!this.container.contains(event.target)
&& this.background.classList.contains("show")
&& !this.inputUpdateValue && !this.forceNewItem) {
helperSelectionIndex = null;
this.close();
}
});
Expand All @@ -213,15 +306,14 @@ export class SearchBar {
const self = this;
for (const domElement of this.allSearchPackages) {
domElement.addEventListener("click", function clikEvent() {
helperSelectionIndex = null;
self.focusNodeById(this.getAttribute("data-value"));
});
}
}

addNewSearchText(filterName, searchedValue) {
const matchingIds = this.computeText(filterName, searchedValue);
// this.input.focus();
// this.input.click();

this.showResultsByIds(matchingIds);

Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ <h2><i class="icon-keyboard"></i>[[=z.token('settings.shortcuts.title')]]</h2>
<div class="line" data-value="flag:"><b>flag:</b>
<p>name</p>
</div>
<div class="line" data-value="flag:"><b>size:</b>
<div class="line" data-value="size:"><b>size:</b>
<p>size</p>
</div>
</template>
Expand Down
Loading