|
1 | 1 | ;(function () { |
2 | 2 | 'use strict' |
3 | 3 |
|
| 4 | + var FORWARD_BACK_TYPE = 2 |
4 | 5 | var CTRL_KEY_CODE = 17 |
5 | 6 | var LT_KEY_CODE = 188 |
6 | 7 | var S_KEY_CODE = 83 |
|
41 | 42 | typeahead.setVal() // clear value on page reload |
42 | 43 | input.on('autocomplete:closed', clearSearch.bind(typeahead)) |
43 | 44 | input.on('autocomplete:cursorchanged autocomplete:cursorremoved', saveSearchState.bind(typeahead)) |
44 | | - input.on('autocomplete:selected', onSuggestionSelected) |
| 45 | + input.on('autocomplete:selected', onSuggestionSelected.bind(typeahead)) |
45 | 46 | input.on('autocomplete:updated', onResultsUpdated.bind(typeahead)) |
46 | 47 | dropdown._ensureVisible = ensureVisible |
47 | 48 | menu.off('mousedown.aa') |
|
59 | 60 | document.documentElement.addEventListener('click', clearSearch.bind(typeahead)) |
60 | 61 | document.addEventListener('keydown', handleShortcuts.bind(typeahead)) |
61 | 62 | 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') |
62 | 76 | } |
63 | 77 |
|
64 | 78 | function appendStylesheet (href) { |
|
135 | 149 |
|
136 | 150 | function onCtrlKeyDown (e) { |
137 | 151 | if (e.keyCode !== CTRL_KEY_CODE) return |
| 152 | + this.ctrlKeyDown = true |
138 | 153 | var dropdown = this.dropdown |
139 | 154 | var container = getScrollableResultsContainer(dropdown) |
140 | 155 | var prevScrollTop = container.scrollTop() |
|
144 | 159 |
|
145 | 160 | function onCtrlKeyUp (e) { |
146 | 161 | if (e.keyCode !== CTRL_KEY_CODE) return |
| 162 | + delete this.ctrlKeyDown |
147 | 163 | this.$input.focus() |
148 | 164 | } |
149 | 165 |
|
|
155 | 171 | dropdown._setCursor(suggestion, false) |
156 | 172 | } |
157 | 173 |
|
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 | + } |
159 | 179 | e.isDefaultPrevented = function () { |
160 | 180 | return true |
161 | 181 | } |
162 | 182 | } |
163 | 183 |
|
164 | 184 | function clearSearch () { |
165 | 185 | this.setVal() |
| 186 | + delete this.ctrlKeyDown |
166 | 187 | } |
167 | 188 |
|
168 | 189 | // preserves the original order of results by qualifying unique occurrences of the same lvl0 and lvl1 values |
|
206 | 227 | var dropdown = this.dropdown |
207 | 228 | dropdown.datasets[0].clearCachedSuggestions() |
208 | 229 | dropdown.restoring = searchState |
| 230 | + this.$input.focus() |
209 | 231 | this.setVal(searchState.query) // cursor is restored by onResultsUpdated => |
210 | 232 | } |
211 | 233 |
|
|
0 commit comments