navbar: Only set searchbox text when displaying the searchbox.

Previously, the navbar failed at managing the searchbox text state in
cases where, eg, the user performs navigation by browser history.

This commit resolves the issue by ensuring that the searchbox text is
only (and always) set when the searchbox is made visible, and as such
there is no "state" to manage and we will always display the correct
text.

It also adds a test in `search_legacy.js` to make sure that the search
text is placed as intended.

Fixes: #14771.
This commit is contained in:
YashRE42
2020-06-10 22:22:22 +00:00
committed by Tim Abbott
parent 939f040674
commit 6506447bf1
3 changed files with 12 additions and 7 deletions

View File

@@ -251,6 +251,7 @@ run_test('initiate_search', () => {
// this implicitly expects the code to used the chained
// function calls, which is something to keep in mind if
// this test ever fails unexpectedly.
narrow_state.filter = () => ({is_search: return_true});
let typeahead_forced_open = false;
let is_searchbox_text_selected = false;
$('#search_query').select = noop;
@@ -267,4 +268,10 @@ run_test('initiate_search', () => {
search.initiate_search();
assert(typeahead_forced_open);
assert(is_searchbox_text_selected);
assert.equal($('#search_query').val(), "ver");
// test that we append space for user convenience
narrow_state.filter = () => ({is_search: return_false});
search.initiate_search();
assert.equal($('#search_query').val(), "ver ");
});

View File

@@ -345,12 +345,6 @@ exports.activate = function (raw_operators, opts) {
compose.update_closed_compose_buttons_for_stream();
}
// Put the narrow operators in the search bar.
// we append a space to make searching more convenient in some cases.
if (filter && !filter.is_search()) {
$('#search_query').val(Filter.unparse(operators) + " ");
}
search.update_button_visibility();
compose_actions.on_narrow(opts);

View File

@@ -123,8 +123,12 @@ function build_tab_bar(filter) {
// we rely entirely on this function to ensure
// the searchbar has the right text.
exports.reset_searchbox_text = function () {
const search_string = narrow_state.search_string();
let search_string = narrow_state.search_string();
if (search_string !== "") {
if (!page_params.search_pills_enabled && !narrow_state.filter().is_search()) {
// saves the user a keystroke for quick searches
search_string = search_string + " ";
}
$("#search_query").val(search_string);
}
};