2012-10-26 16:59:38 +02:00
|
|
|
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.
|
2012-10-29 17:25:18 +01:00
|
|
|
return row.find(".message_content, .sender_name, .message_header, .message_time")
|
2012-10-27 04:14:17 +02:00
|
|
|
.text().toLowerCase().indexOf(search_term) !== -1;
|
2012-10-26 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
2012-11-01 23:03:10 +01:00
|
|
|
function disable_search_arrows_if(condition, affected_arrows) {
|
|
|
|
var i, button;
|
|
|
|
|
|
|
|
for (i = 0; i < affected_arrows.length; i++) {
|
|
|
|
button = $("#search_" + affected_arrows[i]);
|
|
|
|
if (condition) {
|
|
|
|
button.attr("disabled", "disabled");
|
|
|
|
} else {
|
|
|
|
button.removeAttr("disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
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);
|
|
|
|
|
2012-10-27 02:16:46 +02:00
|
|
|
focused_table.find('.message_row, .recipient_row').each(function (index, row) {
|
2012-10-26 16:59:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-11-01 23:03:10 +01:00
|
|
|
disable_search_arrows_if(cached_matches.length === 0, ["up", "down"]);
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
return cached_matches[cached_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse) {
|
|
|
|
if (cached_index > 0) {
|
|
|
|
cached_index--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cached_index < cached_matches.length - 1) {
|
|
|
|
cached_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 23:03:10 +01:00
|
|
|
disable_search_arrows_if(cached_matches.length === 0, ["up", "down"]);
|
|
|
|
disable_search_arrows_if(cached_index === 0, ["up"]);
|
|
|
|
disable_search_arrows_if(cached_index === cached_matches.length - 1, ["down"]);
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
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 = "";
|
|
|
|
}
|
|
|
|
|
2012-11-01 19:20:51 +01:00
|
|
|
function focus_search() {
|
|
|
|
$("#search").width("504px");
|
|
|
|
$("#search_arrows").addClass("input-append");
|
|
|
|
$('.search_button').show();
|
2012-11-01 23:03:10 +01:00
|
|
|
disable_search_arrows_if(false, ["up", "down"]);
|
2012-11-01 19:20:51 +01:00
|
|
|
}
|
|
|
|
|
2012-11-01 17:14:33 +01:00
|
|
|
function initiate_search() {
|
|
|
|
$('#search').val('').focus();
|
|
|
|
}
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
function clear_search() {
|
|
|
|
$('table tr').removeHighlight();
|
2012-11-01 19:20:51 +01:00
|
|
|
// Reset the width to that in the stylesheet. If you change it there, change
|
|
|
|
// it here.
|
|
|
|
$('#search').val('').width("610px");
|
|
|
|
$("#search_arrows").removeClass("input-append");
|
2012-11-01 23:03:10 +01:00
|
|
|
$("#search_up, #search_down").removeAttr("disabled");
|
2012-11-02 17:00:31 +01:00
|
|
|
$('.search_button').blur().hide();
|
2012-10-26 16:59:38 +02:00
|
|
|
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();
|
|
|
|
}
|