search: Remove redundant re-declaration of variables for search box.

This is just a minor refactor.
This commit is contained in:
Shubham Dhama
2018-06-22 15:29:47 +05:30
committed by showell
parent 053b5a0da6
commit 35be5a88ac

View File

@@ -42,6 +42,8 @@ exports.update_button_visibility = function () {
}; };
exports.initialize = function () { exports.initialize = function () {
var search_query_box = $('#search_query');
var searchbox_form = $('#searchbox_form');
// Data storage for the typeahead. // Data storage for the typeahead.
// This maps a search string to an object with a "description" field. // This maps a search string to an object with a "description" field.
@@ -50,7 +52,7 @@ exports.initialize = function () {
// just represents the key of the hash, so it's redundant.) // just represents the key of the hash, so it's redundant.)
var search_object = {}; var search_object = {};
$("#search_query").typeahead({ search_query_box.typeahead({
source: function (query) { source: function (query) {
var suggestions = search_suggestion.get_suggestions(query); var suggestions = search_suggestion.get_suggestions(query);
// Update our global search_object hash // Update our global search_object hash
@@ -74,7 +76,7 @@ exports.initialize = function () {
}, },
}); });
$('#searchbox_form').on('compositionend', function () { searchbox_form.on('compositionend', function () {
// Set `is_using_input_method` to true if enter is pressed to exit // Set `is_using_input_method` to true if enter is pressed to exit
// the input tool popover and get the text in the search bar. Then // the input tool popover and get the text in the search bar. Then
// we suppress searching triggered by this enter key by checking // we suppress searching triggered by this enter key by checking
@@ -83,10 +85,9 @@ exports.initialize = function () {
is_using_input_method = true; is_using_input_method = true;
}); });
$("#searchbox_form").keydown(function (e) { searchbox_form.keydown(function (e) {
exports.update_button_visibility(); exports.update_button_visibility();
var code = e.which; var code = e.which;
var search_query_box = $("#search_query");
if (code === 13 && search_query_box.is(":focus")) { if (code === 13 && search_query_box.is(":focus")) {
// Don't submit the form so that the typeahead can instead // Don't submit the form so that the typeahead can instead
// handle our Enter keypress. Any searching that needs // handle our Enter keypress. Any searching that needs
@@ -100,7 +101,6 @@ exports.initialize = function () {
return; return;
} }
var code = e.which; var code = e.which;
var search_query_box = $("#search_query");
if (code === 13 && search_query_box.is(":focus")) { if (code === 13 && search_query_box.is(":focus")) {
// We just pressed enter and the box had focus, which // We just pressed enter and the box had focus, which
// means we didn't use the typeahead at all. In that // means we didn't use the typeahead at all. In that
@@ -119,9 +119,8 @@ exports.initialize = function () {
// more work to re-order everything and make them private. // more work to re-order everything and make them private.
$('#search_exit').on('click', exports.clear_search); $('#search_exit').on('click', exports.clear_search);
var query = $('#search_query'); search_query_box.on('focus', exports.focus_search);
query.on('focus', exports.focus_search); search_query_box.on('blur' , function () {
query.on('blur' , function () {
// The search query box is a visual cue as to // The search query box is a visual cue as to
// whether search or narrowing is active. If // whether search or narrowing is active. If
// the user blurs the search box, then we should // the user blurs the search box, then we should
@@ -139,7 +138,7 @@ exports.initialize = function () {
setTimeout(function () { setTimeout(function () {
var search_string = narrow_state.search_string(); var search_string = narrow_state.search_string();
query.val(search_string); search_query_box.val(search_string);
exports.update_button_visibility(); exports.update_button_visibility();
}, 100); }, 100);
}); });