Browse Source

logs: Added prioritization of filter names starting with the search query, for example bringing 'Li' higher up when the search query is 'li'.

1.2 1.2.3
Gisle Aune 6 years ago
parent
commit
4657ae2aec
  1. 31
      marko/page/logs/components/filter-menu/component.js

31
marko/page/logs/components/filter-menu/component.js

@ -49,9 +49,9 @@ module.exports = class {
const search = this.state.search.toLocaleLowerCase() const search = this.state.search.toLocaleLowerCase()
this.state.characters = input.characters.filter(c => !this.state.activeMap.characters[c.id]).filter(c => this.matches(c.name, search)).sort((a,b) => a.name.localeCompare(b.name))
this.state.channels = input.channels.filter(c => !this.state.activeMap.channels[c.name]).filter(c => this.matches(c.name, search)).sort((a,b) => a.name.localeCompare(b.name))
this.state.eventNames = input.eventNames.filter(e => !this.state.activeMap.events[e]).filter(e => this.matches(e, search)).sort((a,b) => a.localeCompare(b))
this.state.characters = input.characters.filter(c => !this.state.activeMap.characters[c.id]).filter(c => this.matches(c.name, search)).sort((a,b) => compareFilters(a.name, b.name, search))
this.state.channels = input.channels.filter(c => !this.state.activeMap.channels[c.name]).filter(c => this.matches(c.name, search)).sort((a,b) => compareFilters(a.name, b.name, search))
this.state.eventNames = input.eventNames.filter(e => !this.state.activeMap.events[e]).filter(e => this.matches(e, search)).sort((a,b) => compareFilters(a, b, search))
} }
updateActive({filter, characters, channels, eventNames}) { updateActive({filter, characters, channels, eventNames}) {
@ -126,4 +126,29 @@ module.exports = class {
return str.toLowerCase().indexOf(search) !== -1 return str.toLowerCase().indexOf(search) !== -1
} }
}
/**
*
* @param {string} a
* @param {string} b
* @param {string} search
*/
function compareFilters(a, b, search) {
if (search != "") {
if (search.charAt(0) != "#" && a.charAt(0) == "#" && b.charAt(0) == "#") {
search = '#' + search
}
const aStarts = a.toLocaleLowerCase().startsWith(search)
const bStarts = b.toLocaleLowerCase().startsWith(search)
if (aStarts && !bStarts) {
return -1
} else if (bStarts && !aStarts) {
return 1
}
}
return a.localeCompare(b)
} }
Loading…
Cancel
Save