diff options
-rw-r--r-- | ui.js | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/ui.js b/ui.js index cf39bf5..c20d319 100644 --- a/ui.js +++ b/ui.js @@ -1048,14 +1048,39 @@ class GrouplikeListingElement extends Form { handleJumpValue(value, isConfirm) { // Don't perform the search if the user didn't enter anything. if (value.length) { + // We prioritize searching past the index that the user opened the jump + // element from (oldFocusedIndex). This is so that it's more practical + // to do a "repeated" search, wherein the user searches for the same + // value over and over, each time jumping to the next match, until they + // have found the one they're looking for. + const lower = value.toLowerCase() const getName = inp => (inp.item && inp.item.name) ? inp.item.name.toLowerCase().trim() : '' - // TODO: Search past the current index, for repeated searches? - const startsIndex = this.form.inputs.findIndex(inp => getName(inp).startsWith(lower)) - const includesIndex = this.form.inputs.findIndex(inp => getName(inp).includes(lower)) - const matchedIndex = startsIndex >= 0 ? startsIndex : includesIndex - if (matchedIndex >= 0) { + const testStartsWith = inp => getName(inp).startsWith(lower) + const testIncludes = inp => getName(inp).includes(lower) + + const searchPastCurrentIndex = test => { + const start = this.oldFocusedIndex + 1 + const match = this.form.inputs.slice(start).findIndex(test) + if (match === -1) { + return -1 + } else { + return start + match + } + } + + const start = this.oldFocusedIndex + const allIndexes = [ + searchPastCurrentIndex(testStartsWith), + searchPastCurrentIndex(testIncludes), + this.form.inputs.findIndex(testStartsWith), + this.form.inputs.findIndex(testIncludes) + ] + + const matchedIndex = allIndexes.find(value => value >= 0) + + if (typeof matchedIndex !== 'undefined') { this.form.curIndex = matchedIndex this.form.scrollSelectedElementIntoView() } else { |