Simplify narrow/search interactions.

Before this change, if you hit ESC, then hotkey
code would call search.clear_search, which would
call narrow.deactivate(), which would then use
`$('#search_query')` to clear a value, but then
let search.clear_search blur the input and
disable the exit button.  It was all confusing.

Things are a bit more organized now.

Now the code works like this:

    hotkey.process_escape_key
        Just call narrow.deactivate.

    $('#search_exit').on('click', ...):
        Just call narrow.deactivate.

    narrow.deactivate:
        Just call search.clear_search_form

    search.clear_search_form:
        Just do simple jquery stuff.  Don't
        change the entire user's narrow, not
        even indirectly!

There's still a two-way interaction between
the narrow.js module and the search.js module,
but in each direction it's a one-liner.

The guiding principle here is that we only
want one top-level API, which is narrow.deactivate,
and that does the whole "kitchen sink" of
clearing searches, closing popovers, switching
in views, etc.  And then all the functions it
calls out to tend to have much smaller jobs to
do.

This commit can mostly be considered a refactoring, but the
order of operations changes slightly.  Basically, as
soon as you hit ESC or click on the search "X", we
clear the search widget.  Most users won't notice
any difference, because we don't have to hit the
server to populate the home view.  And it's arguably
an improvement to give more immediate feedback.
This commit is contained in:
Steve Howell
2018-09-10 17:36:58 +00:00
committed by Tim Abbott
parent 08f0690333
commit 06b1aece31
5 changed files with 27 additions and 36 deletions

View File

@@ -160,7 +160,7 @@ exports.initialize = function () {
// Some of these functions don't actually need to be exported,
// but the code was moved here from elsewhere, and it would be
// more work to re-order everything and make them private.
$('#search_exit').on('click', exports.clear_search);
$('#search_exit').on('click', narrow.deactivate);
search_query_box.on('focus', exports.focus_search);
search_query_box.on('blur' , function () {
@@ -212,11 +212,10 @@ exports.initiate_search = function () {
}
};
exports.clear_search = function () {
narrow.deactivate();
exports.clear_search_form = function () {
$('#search_query').val('');
$('#search_query').blur();
exports.update_button_visibility();
$('.search_button').prop('disabled', true);
};
return exports;