mirror of
https://github.com/Mibew/simple-icons.git
synced 2025-05-02 09:06:43 +03:00
Merge pull request #659 from ericcornelissen/fuzzy-search
Add fuzzy search
This commit is contained in:
commit
04d9c945cc
51
index.html
51
index.html
@ -597,16 +597,51 @@
|
|||||||
|
|
||||||
function search(value) {
|
function search(value) {
|
||||||
var hiddenCounter = 0,
|
var hiddenCounter = 0,
|
||||||
value = normalizeSearchTerm(value);
|
query = normalizeSearchTerm(value);
|
||||||
|
|
||||||
icons.forEach(function(e, i) {
|
icons.map(function(icon, iconIndex) {
|
||||||
if (e.indexOf(value) > -1) {
|
var letters = query.split(''),
|
||||||
$icons[i].classList.remove('hidden');
|
indexes = [],
|
||||||
} else {
|
index = 0;
|
||||||
hiddenCounter++;
|
|
||||||
$icons[i].classList.add('hidden');
|
if (icon === query) {
|
||||||
|
return {element: $icons[iconIndex], score: 1};
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
for (var i = 0; i < letters.length; i++) {
|
||||||
|
var letter = letters[i];
|
||||||
|
index = icon.indexOf(letter, index);
|
||||||
|
|
||||||
|
if (index === -1) {
|
||||||
|
$icons[iconIndex].classList.add('hidden');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
indexes.push(index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
element: $icons[iconIndex],
|
||||||
|
score: indexes.reduce(function(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}, 2)
|
||||||
|
};
|
||||||
|
}).filter(function(item) {
|
||||||
|
return item !== null;
|
||||||
|
}).sort(function(a, b) {
|
||||||
|
return a.score - b.score;
|
||||||
|
}).forEach(function(item, index) {
|
||||||
|
item.element.classList.remove('hidden');
|
||||||
|
|
||||||
|
if (query !== '') {
|
||||||
|
// Order according to relevance (i.e. score) if there is a query
|
||||||
|
item.element.style.order = index;
|
||||||
|
} else {
|
||||||
|
// Use color-based order if there is no query
|
||||||
|
item.element.style.removeProperty('order');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$grid.classList.toggle('search__empty', hiddenCounter == icons.length);
|
$grid.classList.toggle('search__empty', hiddenCounter == icons.length);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user