Pail/repo-search-sorter.js

37 lines
1.0 KiB
JavaScript

const compareResults = currentUser => (a, b) => {
return ['/projects/', currentUser]
.some(href => a.children[0].href.includes(href)) ? -1 : 1
}
const sortChildren = ol => {
const children = Array.from(ol.children)
children.forEach(child => ol.removeChild(child))
children
.sort(compareResults(getCurrentUser()))
.forEach(child => ol.appendChild(child))
}
/// Re-order the search results to list projects and the current user first
let claim = 0
const sortSearchResult = () => {
const searchBox = Array.from(document.getElementsByTagName('input'))
.filter(input => input.name === 'repository-search')[0]
if (!searchBox) {
return
}
searchBox.onkeyup = e => {
claim++
const myClaim = claim
setTimeout(() => {
if (claim !== myClaim) {
return
}
const ol = getClassNameElementsArray('search-results')[0]?.firstChild
sortChildren(ol)
claim = 0
}, 1000)
}
}
addFix(sortSearchResult)