From a2cc42234b93ba08bf19173b07420f7a42d74d89 Mon Sep 17 00:00:00 2001 From: Florrie Date: Fri, 28 Dec 2018 19:59:40 -0400 Subject: Prioritize searching past current index Well, not the current index; rather the index which was selected when the jump element was opened. --- ui.js | 35 ++++++++++++++++++++++++++++++----- 1 file 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 { -- cgit 1.3.0-6-gf8a5