Add basic up/down search functionality, including across narrows.

(imported from commit c82acad95e97733b87d65287c685caf7533a774e)
This commit is contained in:
Jessica McKellar 2012-10-26 10:59:38 -04:00 committed by Waseem Daher
parent 74a75eea0c
commit 92593b7cc8
6 changed files with 133 additions and 0 deletions

View File

@ -25,6 +25,7 @@
<link href="{{ static_hidden }}styles/zephyr.css?dummy_time={% now "U" %}" rel="stylesheet">
<link href="{{ static_hidden }}styles/pygments.css" rel="stylesheet">
<script type="text/javascript" src="{{ static_third }}jquery/jquery.form.js"></script>
<script type="text/javascript" src="{{ static_third }}jquery/jquery.highlight.js"></script>
<script type="text/javascript" src="{{ static_third }}xdate/xdate.js"></script>
<script type="text/javascript" src="{{ static_third }}handlebars/handlebars-1.0.rc.1.js"></script>
<script type="text/javascript" src="{{ static_third }}spin/spin.min.js"></script>
@ -38,6 +39,7 @@
<script type="text/javascript" src="{{ static_hidden }}js/compose.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/subs.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/ui.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/search.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/hotkey.js"></script>
<script type="text/javascript" src="{{ static_hidden }}js/zephyr.js"></script>

View File

@ -23,6 +23,9 @@ var globals =
// narrow.js
+ ' narrow'
// search.js
+ ' search_button_handler something_is_highlighted update_highlight_on_narrow'
// setup.js
+ ' loading_spinner templates'

View File

@ -0,0 +1 @@
../../../static/js/search.js

View File

@ -128,6 +128,11 @@ process_key_in_input = function (code) {
// If the user hit the escape key, cancel the current compose
compose.cancel();
}
if ((code === 13) && $("#search").is(":focus")) {
$("#search_up").focus();
search_button_handler(true);
return;
}
// Otherwise, let the browser handle the key normally
return false;
};

View File

@ -37,6 +37,9 @@ function do_narrow(icon, description, filter_function) {
// Your pointer isn't changed when narrowed.
persistent_message_id = selected_message_id;
// Before we clear the table, check if anything was highlighted.
var highlighted = something_is_highlighted();
// Empty the filtered table right before we fill it again
clear_table('zfilt');
add_to_table(message_array, 'zfilt', filter_function, 'bottom');
@ -47,6 +50,7 @@ function do_narrow(icon, description, filter_function) {
$("#show_all_messages").removeAttr("disabled");
$(".narrowed_to_bar").show();
$("#loading_control").hide();
$("#top_narrowed_whitespace").show();
$("#main_div").addClass("narrowed_view");
$("#currently_narrowed_to").html(icon + " " + description).attr("title", description.replace(/&nbsp;/g, ""));
$("#zhome").removeClass("focused_table");
@ -58,6 +62,11 @@ function do_narrow(icon, description, filter_function) {
selected_message_class = "narrowed_selected_message";
select_message_by_id(target_id,
{then_scroll: true, update_server: false});
// If anything was highlighted before, try to rehighlight it.
if (highlighted) {
update_highlight_on_narrow();
}
}
// This is the message we're about to select, within the narrowed view.
@ -140,6 +149,7 @@ exports.show_all_messages = function () {
$("#zhome").addClass('focused_table');
$(".narrowed_to_bar").hide();
$("#loading_control").show();
$("#top_narrowed_whitespace").hide();
$("#main_div").removeClass('narrowed_view');
$("#show_all_messages").attr("disabled", "disabled");
$("#currently_narrowed_to").html("");
@ -149,6 +159,8 @@ exports.show_all_messages = function () {
select_message_by_id(persistent_message_id, {then_scroll: true});
scroll_to_selected();
update_highlight_on_narrow();
};
return exports;

110
zephyr/static/js/search.js Normal file
View File

@ -0,0 +1,110 @@
var cached_term = "";
var cached_matches = [];
var cached_index;
var cached_table = $('table.focused_table');
function get_zid_as_int(object) {
return parseInt(object.attr("zid"), 10);
}
function match_on_visible_text(row, search_term) {
// You can't select on :visible, since that includes hidden elements that
// take up space.
if (row.find(".message_content, .sender_name, .message_header").text().toLowerCase().indexOf(search_term) !== -1) {
return true;
}
return false;
}
function search(term, highlighted_message, reverse) {
// term: case-insensitive text to search for
// highlighted_message: the current location of the pointer. Ignored
// on cached queries
// reverse: boolean as to whether the search is forward or backwards
//
// returns a message object containing the search term.
var previous_header_matched = false;
var focused_table = $('table.focused_table');
if ((term !== cached_term) || (cached_table[0] !== focused_table[0])) {
cached_term = term;
cached_matches = [];
cached_index = null;
cached_table = focused_table;
var selected_zid = get_zid_as_int(highlighted_message);
$.each(focused_table.find('.message_row, .recipient_row'), function (index, row) {
row = $(row);
if (previous_header_matched || (match_on_visible_text(row, term))) {
previous_header_matched = false;
if (row.hasClass("recipient_row")) {
previous_header_matched = true;
} else {
cached_matches.push(row);
var zid = get_zid_as_int(row);
if ((reverse && (zid <= selected_zid)) ||
(!reverse && (zid >= selected_zid) && !cached_index)) {
// Keep track of the closest match going up or down.
cached_index = cached_matches.length - 1;
}
}
}
});
return cached_matches[cached_index];
}
if (reverse) {
if (cached_index > 0) {
cached_index--;
}
} else {
if (cached_index < cached_matches.length - 1) {
cached_index++;
}
}
return cached_matches[cached_index];
}
function highlight_match(row, search_term) {
$('table tr').removeHighlight();
row.highlight(search_term);
row = row.prev('.recipient_row');
if ((row.length !== 0) && (match_on_visible_text(row, search_term))) {
row.highlight(search_term);
}
}
function search_button_handler(reverse) {
var query = $('#search').val().toLowerCase();
var res = search(query, selected_message, reverse);
if (!res) {
return;
}
select_message_by_id(res.attr("zid"));
highlight_match(res, query);
scroll_to_selected();
}
function clear_search_cache() {
cached_term = "";
}
function clear_search() {
$('table tr').removeHighlight();
$('#search').val('').focus();
clear_search_cache();
}
function something_is_highlighted() {
return $(".highlight").length > 0;
}
function update_highlight_on_narrow() {
highlight_match(selected_message, cached_term);
clear_search_cache();
}