${component.title}
`).join('\n');
} else {
rendered = '
';
}
// set active class on active menu item
document.querySelector('.filter-button-group a.active')?.classList?.remove?.('active');
document.querySelector(`.filter-button-group a[href="${hash}"]`)?.classList?.add?.('active');
if (hash === "") {
document.querySelector('.filter-button-group a[href*="#featured"]').classList.add('active');
}
// remove previous elements and css classes, add the new stuff and then trigger the fade-in css animation
componentContainerEl.innerHTML = '';
componentContainerEl.classList.remove('show-items');
componentContainerEl.classList.remove('remove-items');
componentContainerEl.innerHTML = rendered;
componentContainerEl.classList.add('show-items');
}
function buildImageEl(component) {
const urlBase = [
'https://brands.home-assistant.io',
component.ha_brand ? 'brands' : '_',
component.domain,
'icon'
].join('/');
return `
`;
}
// update view by filter selection
for (let filterLink of document.querySelectorAll('.filter-button-group a')) {
filterLink.addEventListener('click', () => {
history.pushState('', '', filterLink.getAttribute('href'));
applyFilter();
return false;
});
}
// update view on select change
const versionsEl = document.getElementById('versions');
versionsEl.addEventListener('change', () => {
history.pushState('', '', versionsEl.value);
applyFilter();
});
const categoriesEl = document.getElementById('categories');
categoriesEl.addEventListener('change', () => {
history.pushState('', '', categoriesEl.value);
applyFilter();
});
/**
* Simple debounce implementation
*/
function debounce(func, wait) {
let timeout;
return () => {
clearTimeout(timeout);
timeout = setTimeout(func, wait);
};
};
// update view by search text
let lastSearchText = '';
searchInputEl.addEventListener('keyup', debounce(() => {
const text = searchInputEl.value
// sanitize input
.replace(/[(\?|\&\{\}\(\))]/gi, '')
.trim();
// Only apply filter if value has changed
if (lastSearchText !== text) {
lastSearchText = text;
const newHash = typeof text === "string" && text.length >= 1
? SEARCH_PREFIX + text
: '#all';
history.pushState('', '', newHash);
applyFilter();
}
}, 500));
window.addEventListener('hashchange', applyFilter);
applyFilter();
})();