Skip to content

Commit

Permalink
Only add filters to filter history when loosing focus on the input
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfb committed Jan 27, 2025
1 parent 88f8f01 commit 2b25b2f
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions assets/webpack/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ function setUpEventListeners() {
document.getElementById('verbosity').addEventListener('change', updateSearchResults);
document.getElementById('columns').addEventListener('change', updateSearchResults);

// Keep track of the latest filter values in the local storage and rebuild
// the filter history list in the UI each time a new filter is set.
document.getElementById('filter').addEventListener('change', (event) => {
const filterValue = event.target.value;
if (filterValue) {
const filterHistory = config.getFilterHistory();
const index = filterHistory.indexOf(filterValue);
if (index !== 0) {
if (index !== -1) {
filterHistory.splice(index, 1);
}
filterHistory.unshift(filterValue);
if (filterHistory.length > 10) {
filterHistory.pop();
}
config.setFilterHistory(filterHistory);
}
rebuildFilterHistoryList();
}
});

// On change in the aggregator, report the new value to all the charts.
document.getElementById('aggregator').addEventListener('change', (event) => {
document.getElementById('clusters').querySelectorAll('.chart').forEach((chartDiv) => {
Expand Down Expand Up @@ -323,33 +344,19 @@ function updateSearchResults() {
`${numVisibleMetrics} metrics found (${numMetrics-numVisibleMetrics} hidden),` +
` organized in ${numVisibleClusters} clusters (${numClusters-numVisibleClusters}` +
' hidden)';
}

// Keep track of the latest filter values in the local storage and rebuild
// the filter history list in the UI each time a new search is done.
const filterValue = document.getElementById('filter').value;
const filterHistory = config.getFilterHistory();
function rebuildFilterHistoryList() {
const filterHistoryList = document.getElementById('filterHistoryList');
if (filterValue) {
const index = filterHistory.indexOf(filterValue);
if (index != 0) {
if (index != -1) {
filterHistory.splice(index, 1);
}
filterHistory.unshift(filterValue);
if (filterHistory.length > 10) {
filterHistory.pop();
}
config.setFilterHistory(filterHistory);
}
}
filterHistoryList.innerHTML = '';
filterHistory.forEach(item => {
config.getFilterHistory().forEach(item => {
const li = document.createElement('li');
li.classList.add('dropdown-item');
li.textContent = item;
li.addEventListener('click', () => {
const input = document.getElementById('filter');
input.value = item;
input.dispatchEvent(new Event('change'));
updateSearchResults();
});
filterHistoryList.appendChild(li);
Expand All @@ -376,8 +383,9 @@ document.addEventListener('DOMContentLoaded', async () => {
// Set up event listeners for all the widgets.
setUpEventListeners();

// Initialize Bootstrap components.
// Prepare filter history list dropdown.
new Dropdown(document.getElementById('filterHistoryList'));
rebuildFilterHistoryList();

// Load metrics.
reloadMetrics();
Expand Down

0 comments on commit 2b25b2f

Please sign in to comment.