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">
<form class="form-search form-inline">
<div id="search_arrows">
<input class="search-query" id="search" type="text" placeholder="Search…" onfocus="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_down" onclick="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>
<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.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.search_button_handler(false);"><i class="icon-chevron-down"></i></button>
<button class="btn search_button" type="button" id="search_exit" onclick="search.clear_search();"><i class="icon-remove"></i></button>
</div>
</form>
</div>

View File

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

View File

@ -100,7 +100,7 @@ function process_hotkey(e) {
respond_to_message("personal");
return process_hotkey;
case 47: // '/': initiate search
initiate_search();
search.initiate_search();
return process_hotkey;
case 63: // '?': Show keyboard shortcuts page
$('#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.
var highlighted = something_is_highlighted();
var highlighted = search.something_is_highlighted();
// Empty the filtered table right before we fill it again
clear_table('zfilt');
@ -71,7 +71,7 @@ function do_narrow(bar, filter_function) {
// If anything was highlighted before, try to rehighlight it.
if (highlighted) {
update_highlight_on_narrow();
search.update_highlight_on_narrow();
}
}
@ -171,7 +171,7 @@ exports.show_all_messages = function () {
scroll_to_selected();
update_highlight_on_narrow();
search.update_highlight_on_narrow();
};
exports.restore_home_state = function() {

View File

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