Skip to content

Commit 4c637ee

Browse files
authored
resolves #107 restore previous search on return to page after navigating to search result (PR #108)
1 parent 6dfcddb commit 4c637ee

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/js/vendor/docsearch.bundle.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
;(function () {
22
'use strict'
33

4+
var FORWARD_BACK_TYPE = 2
45
var CTRL_KEY_CODE = 17
56
var LT_KEY_CODE = 188
67
var S_KEY_CODE = 83
@@ -41,7 +42,7 @@
4142
typeahead.setVal() // clear value on page reload
4243
input.on('autocomplete:closed', clearSearch.bind(typeahead))
4344
input.on('autocomplete:cursorchanged autocomplete:cursorremoved', saveSearchState.bind(typeahead))
44-
input.on('autocomplete:selected', onSuggestionSelected)
45+
input.on('autocomplete:selected', onSuggestionSelected.bind(typeahead))
4546
input.on('autocomplete:updated', onResultsUpdated.bind(typeahead))
4647
dropdown._ensureVisible = ensureVisible
4748
menu.off('mousedown.aa')
@@ -59,6 +60,19 @@
5960
document.documentElement.addEventListener('click', clearSearch.bind(typeahead))
6061
document.addEventListener('keydown', handleShortcuts.bind(typeahead))
6162
if (input.attr('autofocus') != null) input.focus()
63+
window.addEventListener('pageshow', reactivateSearch.bind(typeahead))
64+
}
65+
66+
function reactivateSearch (e) {
67+
var navigationType = (window.performance.navigation || {}).type
68+
if (navigationType && navigationType !== FORWARD_BACK_TYPE) return
69+
if (e.persisted && !isClosed(this)) {
70+
this.$input.focus()
71+
this.$input.val(this.getVal())
72+
} else if (window.sessionStorage.getItem('docs:restore-search-on-back') === 'true') {
73+
restoreSearch.call(this)
74+
}
75+
window.sessionStorage.removeItem('docs:restore-search-on-back')
6276
}
6377

6478
function appendStylesheet (href) {
@@ -135,6 +149,7 @@
135149

136150
function onCtrlKeyDown (e) {
137151
if (e.keyCode !== CTRL_KEY_CODE) return
152+
this.ctrlKeyDown = true
138153
var dropdown = this.dropdown
139154
var container = getScrollableResultsContainer(dropdown)
140155
var prevScrollTop = container.scrollTop()
@@ -144,6 +159,7 @@
144159

145160
function onCtrlKeyUp (e) {
146161
if (e.keyCode !== CTRL_KEY_CODE) return
162+
delete this.ctrlKeyDown
147163
this.$input.focus()
148164
}
149165

@@ -155,14 +171,19 @@
155171
dropdown._setCursor(suggestion, false)
156172
}
157173

158-
function onSuggestionSelected (e) {
174+
function onSuggestionSelected (e, suggestion, datasetNum, context) {
175+
if (!this.ctrlKeyDown) {
176+
if (context.selectionMethod === 'click') saveSearchState.call(this)
177+
window.sessionStorage.setItem('docs:restore-search-on-back', 'true')
178+
}
159179
e.isDefaultPrevented = function () {
160180
return true
161181
}
162182
}
163183

164184
function clearSearch () {
165185
this.setVal()
186+
delete this.ctrlKeyDown
166187
}
167188

168189
// preserves the original order of results by qualifying unique occurrences of the same lvl0 and lvl1 values
@@ -206,6 +227,7 @@
206227
var dropdown = this.dropdown
207228
dropdown.datasets[0].clearCachedSuggestions()
208229
dropdown.restoring = searchState
230+
this.$input.focus()
209231
this.setVal(searchState.query) // cursor is restored by onResultsUpdated =>
210232
}
211233

0 commit comments

Comments
 (0)