Skip to content

Commit 8dc2662

Browse files
committed
extract helper function to requery in search script
1 parent 5b28488 commit 8dc2662

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/js/vendor/docsearch.bundle.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@
116116
}
117117

118118
function toggleFilter (e) {
119-
this.$input.focus()
119+
if ('restoring' in this.dropdown) return
120120
window.localStorage.setItem(SEARCH_FILTER_ACTIVE_KEY, e.target.checked)
121-
if (isClosed(this)) return
122-
var dropdown = this.dropdown
123-
dropdown.update(this.getVal())
121+
isClosed(this) ? this.$input.focus() : requery.call(this)
124122
}
125123

126124
function confineEvent (e) {
@@ -146,10 +144,7 @@
146144

147145
function handleShortcuts (e) {
148146
var target = e.target || {}
149-
if (e.ctrlKey && e.keyCode === LT_KEY_CODE && target === this.$input[0]) {
150-
restoreSearch.call(this)
151-
return
152-
}
147+
if (e.ctrlKey && e.keyCode === LT_KEY_CODE && target === this.$input[0]) return restoreSearch.call(this)
153148
if (e.altKey || e.shiftKey || target.isContentEditable || 'disabled' in target) return
154149
if (e.ctrlKey ? e.keyCode === SOLIDUS_KEY_CODE : e.keyCode === S_KEY_CODE) {
155150
this.$input.focus()
@@ -208,6 +203,13 @@
208203
delete this.ctrlKeyDown
209204
}
210205

206+
function requery (query) {
207+
this.$input.focus()
208+
query === undefined ? (query = this.input.getInputValue()) : this.input.setInputValue(query, true)
209+
this.input.setQuery(query)
210+
this.dropdown.update(query)
211+
}
212+
211213
// preserves the original order of results by qualifying unique occurrences of the same lvl0 and lvl1 values
212214
function protectHitOrder (hits) {
213215
var prevLvl0
@@ -244,12 +246,9 @@
244246
function restoreSearch () {
245247
var searchState = readSavedSearchState()
246248
if (!searchState) return
247-
this.setVal()
248-
this.$facetFilterInput.prop('checked', searchState.filter)
249-
var dropdown = this.dropdown
250-
dropdown.restoring = searchState
251-
this.$input.focus()
252-
this.setVal(searchState.query) // cursor is restored by onResultsUpdated =>
249+
this.dropdown.restoring = searchState
250+
this.$facetFilterInput.prop('checked', searchState.filter) // change event will be ignored
251+
requery.call(this, searchState.query) // cursor is restored by onResultsUpdated =>
253252
}
254253

255254
function saveSearchState () {

0 commit comments

Comments
 (0)