Skip to content

Commit

Permalink
docs: change search function from list.js to fuse.js
Browse files Browse the repository at this point in the history
use debounced fuse.js extended search in search function
  • Loading branch information
korki43 committed Mar 26, 2023
1 parent 3b27adc commit f389dcf
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2,066 deletions.
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module:
target: assets/js/vendor/bootstrap.bundle.min.js
- source: node_modules/clipboard/dist/clipboard.min.js
target: assets/js/vendor/clipboard.min.js
- source: node_modules/fuse.js/dist/fuse.min.js
target: assets/js/vendor/fuse.min.js

params:
description: "Official open source SVG icon library for Bootstrap"
Expand Down
38 changes: 0 additions & 38 deletions docs/assets/js/list.js

This file was deleted.

61 changes: 61 additions & 0 deletions docs/assets/js/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* global Fuse:false */

(function () {
'use strict'

const iconsBody = document.querySelector('#icons-body')

if (!iconsBody) return

const searchInput = iconsBody.querySelector('#search')
const iconListContainer = iconsBody.querySelector('#icons-list')
const iconElementList = Array.from(iconListContainer.children)

const iconDataList = iconElementList.map(element => ({
name: element.dataset.name,
categories: element.dataset.categories.split(' '),
tags: element.dataset.tags.split(' ')
}))

const fuse = new Fuse(iconDataList, {
ignoreLocation: true,
useExtendedSearch: true,
shouldSort: false,
keys: ['name', 'categories', 'tags'],
threshold: 0
})

function search(searchTerm) {
const searchResult = fuse.search(searchTerm)

iconListContainer.innerHTML = ''
if (searchTerm.length > 0) {
const resultElements = searchResult.map(result => iconElementList[result.refIndex])
iconListContainer.append(...resultElements)
} else {
iconListContainer.append(...iconElementList)
}

const newUrl = new URL(window.location)
if (searchTerm.length > 0) {
newUrl.searchParams.set('q', searchTerm)
} else {
newUrl.searchParams.delete('q')
}

window.history.replaceState(null, null, newUrl)
}

let timeout
searchInput.addEventListener('input', () => {
clearTimeout(timeout)
timeout = setTimeout(() => search(searchInput.value), 250)
})

const query = new URLSearchParams(window.location.search).get('q')
if (query) {
search(query)
searchInput.value = query
document.querySelector('#content').scrollIntoView()
}
})()
Loading

0 comments on commit f389dcf

Please sign in to comment.