1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-07 22:45:23 +02:00

common search function added

This commit is contained in:
Umang G. Patel 2022-10-12 20:29:26 +05:30
parent 294fce5dd1
commit 5b668f7fb0

View file

@ -285,6 +285,17 @@ export default class SidebarFilter {
}
}
/**
* Check if content contains search text.
*
* @param {string} content - content to be searched.
* @param {string} searchValue - Search value.
* @returns {boolean} - true if content contains search value.
*/
isValueMatched(content, searchValue) {
return content.toLowerCase().indexOf(searchValue.toLowerCase()) !== -1;
}
/**
* filter sidebar items.
*
@ -297,8 +308,8 @@ export default class SidebarFilter {
const sectionList = section.querySelector('.' + SidebarFilter.CSS.sectionList);
// check if section title matches.
const isTitleMatch = sectionTitle.innerText.trim().toLowerCase()
.indexOf(searchValue.toLowerCase()) !== -1;
const isTitleMatch = this.isValueMatched(sectionTitle.textContent, searchValue);
const matchResults = [];
// match with section items.
let isSingleItemMatch = false;
@ -307,8 +318,7 @@ export default class SidebarFilter {
const sectionListItems = sectionList.querySelectorAll('.' + SidebarFilter.CSS.sectionListItem);
sectionListItems.forEach(item => {
if (item.innerText.trim().toLowerCase()
.indexOf(searchValue.toLowerCase()) !== -1) {
if (this.isValueMatched(item.textContent, searchValue)) {
// remove hiden class from item.
item.parentElement.classList.remove(SidebarFilter.CSS.sectionListItemWrapperHidden);
// add item to search results.