2012-11-14 20:52:53 +01:00
|
|
|
var search = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
var cached_term = "";
|
|
|
|
var cached_matches = [];
|
|
|
|
var cached_index;
|
|
|
|
var cached_table = $('table.focused_table');
|
|
|
|
|
2012-11-15 03:44:50 +01:00
|
|
|
// Data storage for the typeahead -- to go from object to string representation and vice versa.
|
|
|
|
var labels = [];
|
|
|
|
var mapped = {};
|
|
|
|
|
2012-11-27 17:25:03 +01:00
|
|
|
function render_object_in_parts(obj) {
|
2012-12-17 19:53:14 +01:00
|
|
|
// N.B. action is *not* escaped by the caller
|
|
|
|
switch (obj.action) {
|
|
|
|
case 'search':
|
|
|
|
return {action: 'Find', search: obj.query};
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
return {action: 'Narrow to stream', search: obj.query};
|
|
|
|
|
|
|
|
case 'private_message':
|
|
|
|
return {action: 'Narrow to person',
|
|
|
|
search: typeahead_helper.render_pm_object(obj.query)};
|
|
|
|
|
2012-12-18 00:28:31 +01:00
|
|
|
case 'operators':
|
|
|
|
// HACK: This label needs to be distinct from the above, because of the
|
|
|
|
// way we identify action objects by their labels. Using two spaces
|
|
|
|
// after 'Narrow to' ensures this, and is invisible with standard HTML
|
|
|
|
// whitespace handling.
|
|
|
|
return {action: 'Narrow to ', search: narrow.describe(obj.operators)};
|
2012-11-19 01:31:23 +01:00
|
|
|
}
|
2012-12-17 19:53:14 +01:00
|
|
|
return {action: 'Error', search: 'Error'};
|
2012-11-27 17:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function render_object(obj) {
|
|
|
|
var parts = render_object_in_parts(obj);
|
2012-12-17 19:53:14 +01:00
|
|
|
return parts.action + " " + parts.search;
|
2012-11-19 01:31:23 +01:00
|
|
|
}
|
|
|
|
|
2012-12-18 00:28:31 +01:00
|
|
|
exports.update_typeahead = function () {
|
2012-11-19 01:31:23 +01:00
|
|
|
var streams = $.map(stream_list, function(elt,idx) {
|
|
|
|
return {action: 'stream', query: elt};
|
|
|
|
});
|
|
|
|
var people = $.map(people_list, function(elt,idx) {
|
|
|
|
return {action: 'private_message', query: elt};
|
|
|
|
});
|
|
|
|
var options = streams.concat(people);
|
2012-11-18 22:53:50 +01:00
|
|
|
// The first two slots are reserved for our query.
|
2012-12-18 00:28:31 +01:00
|
|
|
options.unshift({action: 'operators', query: '', operators: []});
|
2012-11-19 01:31:23 +01:00
|
|
|
options.unshift({action: 'search', query: ''});
|
|
|
|
|
|
|
|
mapped = {};
|
|
|
|
labels = [];
|
|
|
|
$.each(options, function (i, obj) {
|
|
|
|
var label = render_object(obj);
|
|
|
|
mapped[label] = obj;
|
|
|
|
labels.push(label);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-11-14 22:12:21 +01:00
|
|
|
function narrow_or_search_for_term(item) {
|
2012-12-12 19:36:18 +01:00
|
|
|
var search_query_box = $("#search_query");
|
2012-11-15 03:44:50 +01:00
|
|
|
var obj = mapped[item];
|
2012-12-17 19:47:09 +01:00
|
|
|
switch (obj.action) {
|
|
|
|
case 'search':
|
2012-11-30 19:19:07 +01:00
|
|
|
$("#search_up").focus();
|
|
|
|
exports.search_button_handler(true);
|
2012-11-15 03:44:50 +01:00
|
|
|
return obj.query;
|
2012-12-17 19:47:09 +01:00
|
|
|
|
|
|
|
case 'stream':
|
2012-12-12 20:34:47 +01:00
|
|
|
narrow.activate([['stream', obj.query]]);
|
2012-11-15 03:44:50 +01:00
|
|
|
// It's sort of annoying that this is not in a position to
|
|
|
|
// blur the search box, because it means that Esc won't
|
|
|
|
// unnarrow, it'll leave the searchbox.
|
2012-12-12 19:36:18 +01:00
|
|
|
|
|
|
|
// Narrowing will have already put some operators in the search box,
|
|
|
|
// so leave the current text in.
|
|
|
|
return search_query_box.val();
|
2012-12-17 19:47:09 +01:00
|
|
|
|
|
|
|
case 'private_message':
|
2012-12-12 20:34:47 +01:00
|
|
|
narrow.activate([['pm-with', obj.query.email]], {show_floating_recipient: false});
|
2012-12-12 19:36:18 +01:00
|
|
|
return search_query_box.val();
|
2012-12-17 19:47:09 +01:00
|
|
|
|
2012-12-18 00:28:31 +01:00
|
|
|
case 'operators':
|
|
|
|
narrow.activate(obj.operators);
|
2012-12-12 19:36:18 +01:00
|
|
|
return search_query_box.val();
|
2012-11-15 03:44:50 +01:00
|
|
|
}
|
2012-11-14 22:12:21 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-11-26 21:39:15 +01:00
|
|
|
function get_query(obj) {
|
|
|
|
return obj.query;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_person(obj) {
|
|
|
|
return typeahead_helper.render_pm_object(obj.query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchbox_sorter(items) {
|
2012-12-17 22:06:58 +01:00
|
|
|
var objects_by_action = {};
|
|
|
|
var result = [];
|
2012-11-26 21:39:15 +01:00
|
|
|
|
|
|
|
$.each(items, function (idx, elt) {
|
|
|
|
var obj = mapped[elt];
|
2012-12-17 22:06:58 +01:00
|
|
|
if (objects_by_action[obj.action] === undefined)
|
|
|
|
objects_by_action[obj.action] = [];
|
|
|
|
objects_by_action[obj.action].push(obj);
|
2012-11-26 21:39:15 +01:00
|
|
|
});
|
|
|
|
|
2012-12-17 22:06:58 +01:00
|
|
|
var query = this.query;
|
2012-12-18 00:28:31 +01:00
|
|
|
$.each(['search', 'operators', 'stream', 'private_message'], function (idx, action) {
|
2012-12-17 22:06:58 +01:00
|
|
|
var objs = objects_by_action[action];
|
|
|
|
if (!objs)
|
|
|
|
return;
|
|
|
|
// Get the first object in sorted order.
|
|
|
|
var obj = typeahead_helper.sorter(query, objs,
|
|
|
|
(action === 'private_message') ? get_person : get_query)
|
|
|
|
.shift();
|
|
|
|
if (obj)
|
|
|
|
result.push(render_object(obj));
|
2012-11-26 21:39:15 +01:00
|
|
|
});
|
|
|
|
|
2012-12-17 22:06:58 +01:00
|
|
|
return result;
|
2012-11-26 21:39:15 +01:00
|
|
|
}
|
|
|
|
|
2012-11-14 22:12:21 +01:00
|
|
|
exports.initialize = function () {
|
|
|
|
$( "#search_query" ).typeahead({
|
|
|
|
source: function (query, process) {
|
2012-12-18 00:28:31 +01:00
|
|
|
// Delete our old search queries (one for find-in-page, one for operators)
|
|
|
|
var i;
|
|
|
|
for (i=0; i<2; i++) {
|
|
|
|
delete mapped[labels.shift()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an entry for narrow by operators.
|
|
|
|
var operators = narrow.parse(query);
|
|
|
|
var obj = {action: 'operators', query: query, operators: operators};
|
2012-11-19 01:31:23 +01:00
|
|
|
var label = render_object(obj);
|
|
|
|
mapped[label] = obj;
|
|
|
|
labels.unshift(label);
|
2012-12-18 00:28:31 +01:00
|
|
|
|
|
|
|
// Add an entry for find-in-page.
|
2012-11-18 22:53:50 +01:00
|
|
|
obj = {action: 'search', query: query};
|
2012-12-18 00:28:31 +01:00
|
|
|
if (operators.length > 0 && operators[0][0] !== 'search') {
|
|
|
|
// We have operators other than a search term.
|
|
|
|
// Disable find-in-page.
|
|
|
|
obj.disabled = true;
|
|
|
|
}
|
2012-11-18 22:53:50 +01:00
|
|
|
label = render_object(obj);
|
|
|
|
mapped[label] = obj;
|
|
|
|
labels.unshift(label);
|
2012-12-18 00:28:31 +01:00
|
|
|
|
2012-11-15 03:44:50 +01:00
|
|
|
return labels;
|
2012-11-14 22:12:21 +01:00
|
|
|
},
|
2012-11-18 22:53:50 +01:00
|
|
|
items: 4,
|
2012-11-18 19:19:52 +01:00
|
|
|
highlighter: function (item) {
|
|
|
|
var query = this.query;
|
2012-11-27 17:25:03 +01:00
|
|
|
var parts = render_object_in_parts(mapped[item]);
|
2012-12-17 19:53:14 +01:00
|
|
|
// We provide action, not the user, so this should
|
2012-11-27 17:25:03 +01:00
|
|
|
// be fine from a not-needing-escaping perspective.
|
2012-12-17 19:53:14 +01:00
|
|
|
return parts.action + " " +
|
|
|
|
typeahead_helper.highlight_with_escaping(query, parts.search);
|
2012-11-18 19:19:52 +01:00
|
|
|
},
|
2012-11-15 03:44:50 +01:00
|
|
|
matcher: function (item) {
|
|
|
|
var obj = mapped[item];
|
2012-12-18 00:28:31 +01:00
|
|
|
if (obj.disabled)
|
|
|
|
return false;
|
2012-11-15 03:44:50 +01:00
|
|
|
var actual_search_term = obj.query;
|
|
|
|
if (obj.action === 'private_message') {
|
|
|
|
actual_search_term = obj.query.full_name + ' <' + obj.query.email + '>';
|
|
|
|
}
|
|
|
|
// Case-insensitive (from Bootstrap's default matcher).
|
|
|
|
return (actual_search_term.toLowerCase().indexOf(this.query.toLowerCase()) !== -1);
|
|
|
|
},
|
2012-11-26 21:39:15 +01:00
|
|
|
updater: narrow_or_search_for_term,
|
|
|
|
sorter: searchbox_sorter
|
2012-11-14 22:12:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#searchbox_form").keydown(function (e) {
|
|
|
|
var code = e.which;
|
2012-11-15 20:22:20 +01:00
|
|
|
var search_query_box = $("#search_query");
|
|
|
|
if (code === 13 && search_query_box.is(":focus")) {
|
|
|
|
// Don't submit the form so that the typeahead can instead
|
|
|
|
// handle our Enter keypress. Any searching that needs
|
|
|
|
// to be done will be handled in the keyup.
|
2012-11-14 22:12:21 +01:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-18 22:58:20 +01:00
|
|
|
}).keyup(function (e) {
|
2012-11-15 20:22:20 +01:00
|
|
|
var code = e.which;
|
|
|
|
var search_query_box = $("#search_query");
|
|
|
|
if (code === 13 && search_query_box.is(":focus")) {
|
|
|
|
// We just pressed enter and the box had focus, so one of
|
|
|
|
// two things is true:
|
|
|
|
// 1) There's a value in the search box and we should
|
|
|
|
// search for it
|
|
|
|
// 2) There's no value in the searchbox, so we just
|
|
|
|
// narrowed, so we should blur the box.
|
2012-11-30 19:19:07 +01:00
|
|
|
//
|
|
|
|
// Note that if the typeahead completion box was up, it
|
|
|
|
// has already handled the keypress and defocused the
|
|
|
|
// box. So we only get here when enter was pressed while
|
|
|
|
// the completion box was not displayed.
|
2012-11-15 20:22:20 +01:00
|
|
|
if (search_query_box.val()) {
|
|
|
|
$("#search_up").focus();
|
|
|
|
exports.search_button_handler(true);
|
|
|
|
} else {
|
|
|
|
exports.clear_search();
|
|
|
|
search_query_box.blur();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-11-14 22:12:21 +01:00
|
|
|
};
|
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
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;
|
2012-11-01 19:20:41 +01:00
|
|
|
var selected_zid = rows.id(highlighted_message);
|
2012-10-26 16:59:38 +02:00
|
|
|
|
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);
|
2012-11-01 19:20:41 +01:00
|
|
|
var zid = rows.id(row);
|
2012-10-26 16:59:38 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.search_button_handler = function (reverse) {
|
2012-11-14 21:08:49 +01:00
|
|
|
var query = $('#search_query').val().toLowerCase();
|
2012-10-26 16:59:38 +02:00
|
|
|
var res = search(query, selected_message, reverse);
|
|
|
|
if (!res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-01 19:23:15 +01:00
|
|
|
select_message(res);
|
2012-10-26 16:59:38 +02:00
|
|
|
highlight_match(res, query);
|
|
|
|
scroll_to_selected();
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-10-26 16:59:38 +02:00
|
|
|
|
|
|
|
function clear_search_cache() {
|
|
|
|
cached_term = "";
|
|
|
|
}
|
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.focus_search = function () {
|
2012-11-16 04:13:52 +01:00
|
|
|
if (!$('.search_button').is(':visible')) {
|
|
|
|
// Shrink the searchbox to make room for the buttons.
|
|
|
|
var search_query = $('#search_query');
|
|
|
|
var new_width = search_query.width() -
|
|
|
|
$('.search_button').outerWidth(true)*3;
|
|
|
|
search_query.width(new_width);
|
|
|
|
$("#search_arrows").addClass("input-append");
|
|
|
|
$('.search_button').show();
|
|
|
|
disable_search_arrows_if(false, ["up", "down"]);
|
|
|
|
}
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-11-01 19:20:51 +01:00
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.initiate_search = function () {
|
2012-11-14 21:08:49 +01:00
|
|
|
$('#search_query').val('').focus();
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-11-01 17:14:33 +01:00
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.clear_search = function () {
|
2012-12-18 21:45:48 +01:00
|
|
|
narrow.deactivate();
|
2012-10-26 16:59:38 +02:00
|
|
|
$('table tr').removeHighlight();
|
2012-11-15 21:40:05 +01:00
|
|
|
// Clear & reset searchbox to its normal size
|
|
|
|
$('#search_query').val('').width('');
|
2012-11-01 19:20:51 +01:00
|
|
|
$("#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();
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-10-26 16:59:38 +02:00
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.something_is_highlighted = function () {
|
2012-10-26 16:59:38 +02:00
|
|
|
return $(".highlight").length > 0;
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-10-26 16:59:38 +02:00
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.update_highlight_on_narrow = function () {
|
2012-10-26 16:59:38 +02:00
|
|
|
highlight_match(selected_message, cached_term);
|
|
|
|
clear_search_cache();
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
|
|
|
|
2012-12-07 20:18:01 +01:00
|
|
|
exports.keyboard_currently_finding = function () {
|
|
|
|
// This is somewhat subtle because it doesn't actually mean
|
|
|
|
// "is a find in progress" -- it means "is the keyboard
|
|
|
|
// currently driving a find"
|
|
|
|
// (If you have a Find going and you just click the button,
|
|
|
|
// the focus goes away and this starts being False,
|
|
|
|
// even though a find is still active.)
|
|
|
|
return $('.search_button').is(':focus');
|
|
|
|
};
|
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|