Modularize search.js.

(imported from commit 72f00f832846124d833071fdd46b026c99189512)
This commit is contained in:
Waseem Daher 2012-11-14 14:52:53 -05:00
parent 50b8195bf3
commit a1c4aa6865
5 changed files with 29 additions and 22 deletions

View File

@ -5,10 +5,10 @@
<div id="searchbox" class="always_in_view"> <div id="searchbox" class="always_in_view">
<form class="form-search form-inline"> <form class="form-search form-inline">
<div id="search_arrows"> <div id="search_arrows">
<input class="search-query" id="search" type="text" placeholder="Search…" onfocus="focus_search();" autocomplete="off"> <input class="search-query" id="search" type="text" placeholder="Search…" onfocus="search.focus_search();" autocomplete="off">
<button class="btn search_button search_button_middle" type="button" id="search_up" onclick="search_button_handler(true);"><i class="icon-chevron-up"></i></button> <button class="btn search_button search_button_middle" type="button" id="search_up" onclick="search.search_button_handler(true);"><i class="icon-chevron-up"></i></button>
<button class="btn search_button search_button_middle" type="button" id="search_down" onclick="search_button_handler(false);"><i class="icon-chevron-down"></i></button> <button class="btn search_button search_button_middle" type="button" id="search_down" onclick="search.search_button_handler(false);"><i class="icon-chevron-down"></i></button>
<button class="btn search_button" type="button" id="search_exit" onclick="clear_search();"><i class="icon-remove"></i></button> <button class="btn search_button" type="button" id="search_exit" onclick="search.clear_search();"><i class="icon-remove"></i></button>
</div> </div>
</form> </form>
</div> </div>

View File

@ -27,8 +27,7 @@ var globals =
+ ' reload' + ' reload'
// search.js // search.js
+ ' search_button_handler something_is_highlighted update_highlight_on_narrow' + ' search'
+ ' initiate_search'
// setup.js // setup.js
+ ' loading_spinner templates csrf_token' + ' loading_spinner templates csrf_token'

View File

@ -100,7 +100,7 @@ function process_hotkey(e) {
respond_to_message("personal"); respond_to_message("personal");
return process_hotkey; return process_hotkey;
case 47: // '/': initiate search case 47: // '/': initiate search
initiate_search(); search.initiate_search();
return process_hotkey; return process_hotkey;
case 63: // '?': Show keyboard shortcuts page case 63: // '?': Show keyboard shortcuts page
$('#keyboard-shortcuts').modal('show'); $('#keyboard-shortcuts').modal('show');

View File

@ -42,7 +42,7 @@ function do_narrow(bar, filter_function) {
} }
// Before we clear the table, check if anything was highlighted. // Before we clear the table, check if anything was highlighted.
var highlighted = something_is_highlighted(); var highlighted = search.something_is_highlighted();
// Empty the filtered table right before we fill it again // Empty the filtered table right before we fill it again
clear_table('zfilt'); clear_table('zfilt');
@ -71,7 +71,7 @@ function do_narrow(bar, filter_function) {
// If anything was highlighted before, try to rehighlight it. // If anything was highlighted before, try to rehighlight it.
if (highlighted) { if (highlighted) {
update_highlight_on_narrow(); search.update_highlight_on_narrow();
} }
} }
@ -171,7 +171,7 @@ exports.show_all_messages = function () {
scroll_to_selected(); scroll_to_selected();
update_highlight_on_narrow(); search.update_highlight_on_narrow();
}; };
exports.restore_home_state = function() { exports.restore_home_state = function() {

View File

@ -1,3 +1,7 @@
var search = (function () {
var exports = {};
var cached_term = ""; var cached_term = "";
var cached_matches = []; var cached_matches = [];
var cached_index; var cached_index;
@ -91,7 +95,7 @@ function highlight_match(row, search_term) {
} }
} }
function search_button_handler(reverse) { exports.search_button_handler = function (reverse) {
var query = $('#search').val().toLowerCase(); var query = $('#search').val().toLowerCase();
var res = search(query, selected_message, reverse); var res = search(query, selected_message, reverse);
if (!res) { if (!res) {
@ -101,24 +105,24 @@ function search_button_handler(reverse) {
select_message(res); select_message(res);
highlight_match(res, query); highlight_match(res, query);
scroll_to_selected(); scroll_to_selected();
} };
function clear_search_cache() { function clear_search_cache() {
cached_term = ""; cached_term = "";
} }
function focus_search() { exports.focus_search = function () {
$("#search").width("504px"); $("#search").width("504px");
$("#search_arrows").addClass("input-append"); $("#search_arrows").addClass("input-append");
$('.search_button').show(); $('.search_button').show();
disable_search_arrows_if(false, ["up", "down"]); disable_search_arrows_if(false, ["up", "down"]);
} };
function initiate_search() { exports.initiate_search = function () {
$('#search').val('').focus(); $('#search').val('').focus();
} };
function clear_search() { exports.clear_search = function () {
$('table tr').removeHighlight(); $('table tr').removeHighlight();
// Reset the width to that in the stylesheet. If you change it there, change // Reset the width to that in the stylesheet. If you change it there, change
// it here. // it here.
@ -127,13 +131,17 @@ function clear_search() {
$("#search_up, #search_down").removeAttr("disabled"); $("#search_up, #search_down").removeAttr("disabled");
$('.search_button').blur().hide(); $('.search_button').blur().hide();
clear_search_cache(); clear_search_cache();
} };
function something_is_highlighted() { exports.something_is_highlighted = function () {
return $(".highlight").length > 0; return $(".highlight").length > 0;
} };
function update_highlight_on_narrow() { exports.update_highlight_on_narrow = function () {
highlight_match(selected_message, cached_term); highlight_match(selected_message, cached_term);
clear_search_cache(); clear_search_cache();
} };
return exports;
}());