Skip to content
Merged
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
34 changes: 34 additions & 0 deletions static/js/commands-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,51 @@ function filter() {
return;
}

const visibleElements = [];
const nameFilterValue = FILTERS.name.element.value.toLowerCase();

for (const element of commandElements) {
let shouldHide = false;

for (const [key, filter] of Object.entries(FILTERS)) {
if (!filter.element.value) continue;

const elementValue = key == 'alpha' ? element.dataset['name'] : element.dataset[key];
if (!match(filter, elementValue)) {
element.style.display = 'none';
hiddenCards.push(element);
shouldHide = true;
break;
}
}

if (!shouldHide) {
visibleElements.push(element);
}
}

// Sort visible elements: commands starting with search term first, then others
if (nameFilterValue) {
const grid = document.querySelector('#commands-grid');

visibleElements.sort((a, b) => {
const aName = a.dataset.name.toLowerCase();
const bName = b.dataset.name.toLowerCase();
const aStarts = aName.startsWith(nameFilterValue);
const bStarts = bName.startsWith(nameFilterValue);

// If one starts with the search term and the other doesn't, prioritize the one that starts
if (aStarts && !bStarts) return -1;
if (!aStarts && bStarts) return 1;

// Otherwise maintain alphabetical order
return aName.localeCompare(bName);
});

// Re-append elements in sorted order
visibleElements.forEach(element => {
grid.appendChild(element);
});
}
}

Expand Down