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');
|
2013-03-04 23:24:36 +01:00
|
|
|
var current_search_term;
|
2012-10-26 16:59:38 +02:00
|
|
|
|
2012-11-15 03:44:50 +01:00
|
|
|
// Data storage for the typeahead -- to go from object to string representation and vice versa.
|
2013-07-16 02:18:36 +02:00
|
|
|
var search_object = {};
|
2012-11-15 03:44:50 +01:00
|
|
|
|
2013-05-06 20:43:55 +02:00
|
|
|
function phrase_match(phrase, q) {
|
2013-05-06 19:29:02 +02:00
|
|
|
// match "tes" to "test" and "stream test" but not "hostess"
|
|
|
|
var i;
|
2013-05-06 20:43:55 +02:00
|
|
|
q = q.toLowerCase();
|
|
|
|
|
|
|
|
var parts = phrase.split(' ');
|
2013-05-06 19:29:02 +02:00
|
|
|
for (i = 0; i < parts.length; i++) {
|
2013-05-06 20:43:55 +02:00
|
|
|
if (parts[i].toLowerCase().indexOf(q) === 0) {
|
2013-05-06 19:29:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-05-06 18:35:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-06 20:43:55 +02:00
|
|
|
function person_matches_query(person, q) {
|
|
|
|
return phrase_match(person.full_name, q) || phrase_match(person.email, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stream_matches_query(stream_name, q) {
|
|
|
|
return phrase_match(stream_name, q);
|
|
|
|
}
|
|
|
|
|
2013-07-15 19:25:47 +02:00
|
|
|
|
|
|
|
// Convert a list of operators to a human-readable description.
|
|
|
|
function describe(operators) {
|
2013-07-19 21:38:26 +02:00
|
|
|
if (operators.length === 0) {
|
|
|
|
return 'Go to Home view';
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:03:23 +02:00
|
|
|
var parts = [];
|
|
|
|
|
|
|
|
if (operators.length >= 2) {
|
|
|
|
if (operators[0][0] === 'stream' && operators[1][0] === 'topic') {
|
|
|
|
var stream = operators[0][1];
|
|
|
|
var topic = operators[1][1];
|
|
|
|
var part = 'Narrow to ' + stream + ' > ' + topic;
|
|
|
|
parts = [part];
|
|
|
|
operators = operators.slice(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var more_parts = $.map(operators, function (elem) {
|
2013-07-15 19:25:47 +02:00
|
|
|
var operand = elem[1];
|
2013-07-16 22:52:42 +02:00
|
|
|
switch (narrow.canonicalize_operator(elem[0])) {
|
2013-07-15 19:25:47 +02:00
|
|
|
case 'is':
|
|
|
|
if (operand === 'private') {
|
|
|
|
return 'Narrow to all private messages';
|
|
|
|
} else if (operand === 'starred') {
|
|
|
|
return 'Narrow to starred messages';
|
|
|
|
} else if (operand === 'mentioned') {
|
|
|
|
return 'Narrow to mentioned messages';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
return 'Narrow to stream ' + operand;
|
|
|
|
|
2013-07-16 22:52:02 +02:00
|
|
|
case 'topic':
|
|
|
|
return 'Narrow to topic ' + operand;
|
2013-07-15 19:25:47 +02:00
|
|
|
|
|
|
|
case 'sender':
|
|
|
|
return 'Narrow to sender ' + operand;
|
|
|
|
|
|
|
|
case 'pm-with':
|
|
|
|
return 'Narrow to private messages with ' + operand;
|
|
|
|
|
|
|
|
case 'search':
|
|
|
|
return 'Search for ' + operand;
|
|
|
|
|
|
|
|
case 'in':
|
|
|
|
return 'Narrow to messages in ' + operand;
|
|
|
|
}
|
|
|
|
return 'Narrow to (unknown operator)';
|
2013-07-19 19:03:23 +02:00
|
|
|
});
|
|
|
|
return parts.concat(more_parts).join(', ');
|
2013-07-15 19:25:47 +02:00
|
|
|
}
|
|
|
|
|
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");
|
2013-07-16 02:18:36 +02:00
|
|
|
var obj = search_object[item];
|
2013-01-22 00:30:27 +01:00
|
|
|
ui.change_tab_to('#home');
|
2013-07-16 04:55:46 +02:00
|
|
|
var operators = narrow.parse(obj.search_string);
|
|
|
|
narrow.activate(operators, {trigger: 'search'});
|
2012-12-17 19:47:09 +01:00
|
|
|
|
2013-07-16 04:55:46 +02: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.
|
2013-02-28 22:10:22 +01:00
|
|
|
|
2013-07-16 04:55:46 +02:00
|
|
|
// Narrowing will have already put some operators in the search box,
|
|
|
|
// so leave the current text in.
|
|
|
|
search_query_box.blur();
|
|
|
|
return search_query_box.val();
|
2012-11-14 22:12:21 +01:00
|
|
|
}
|
|
|
|
|
2013-01-02 19:58:55 +01:00
|
|
|
function update_buttons_with_focus(focused) {
|
2012-12-18 23:38:55 +01:00
|
|
|
var search_query = $('#search_query');
|
|
|
|
|
|
|
|
// Show buttons iff the search input is focused, or has non-empty contents,
|
|
|
|
// or we are narrowed.
|
|
|
|
if (focused
|
|
|
|
|| search_query.val()
|
|
|
|
|| narrow.active()) {
|
2013-03-06 20:46:33 +01:00
|
|
|
$('.search_button').removeAttr('disabled');
|
2012-12-18 23:38:55 +01:00
|
|
|
} else {
|
2013-03-06 20:46:33 +01:00
|
|
|
$('.search_button').attr('disabled', 'disabled');
|
2012-12-18 23:38:55 +01:00
|
|
|
}
|
2013-01-02 19:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_button_visibility = function () {
|
|
|
|
update_buttons_with_focus($('#search_query').is(':focus'));
|
2012-12-18 23:38:55 +01:00
|
|
|
};
|
|
|
|
|
2013-05-07 15:50:46 +02:00
|
|
|
function highlight_person(query, person) {
|
|
|
|
var hilite = typeahead_helper.highlight_query_in_phrase;
|
2013-05-07 21:32:57 +02:00
|
|
|
return hilite(query, person.full_name) + " <" + hilite(query, person.email) + ">";
|
2013-05-07 15:50:46 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 20:53:30 +02:00
|
|
|
function get_stream_suggestions(operators) {
|
|
|
|
var query;
|
|
|
|
|
|
|
|
switch (operators.length) {
|
|
|
|
case 0:
|
|
|
|
query = '';
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
var operand = operators[0][0];
|
|
|
|
query = operators[0][1];
|
|
|
|
if (!(operand === 'stream' || operand === 'search')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:40:30 +02:00
|
|
|
var streams = subs.subscribed_streams();
|
|
|
|
|
|
|
|
streams = $.grep(streams, function (stream) {
|
|
|
|
return stream_matches_query(stream, query);
|
2013-07-15 23:20:23 +02:00
|
|
|
});
|
2013-07-16 03:40:30 +02:00
|
|
|
|
2013-07-16 23:46:45 +02:00
|
|
|
streams = typeahead_helper.sorter(query, streams);
|
2013-07-15 23:20:23 +02:00
|
|
|
|
2013-07-16 23:46:45 +02:00
|
|
|
var objs = $.map(streams, function (stream) {
|
2013-07-16 01:03:13 +02:00
|
|
|
var prefix = 'Narrow to stream';
|
2013-07-16 23:46:45 +02:00
|
|
|
var highlighted_stream = typeahead_helper.highlight_query_in_phrase(query, stream);
|
|
|
|
var description = prefix + ' ' + highlighted_stream;
|
2013-07-18 23:14:24 +02:00
|
|
|
var search_string = narrow.unparse([['stream', stream]]);
|
2013-07-16 23:46:45 +02:00
|
|
|
return {description: description, search_string: search_string};
|
2013-07-16 01:03:13 +02:00
|
|
|
});
|
|
|
|
|
2013-07-16 03:40:30 +02:00
|
|
|
return objs;
|
2013-07-15 23:20:23 +02:00
|
|
|
}
|
|
|
|
|
2013-07-20 00:17:06 +02:00
|
|
|
function get_private_suggestions(all_people, operators) {
|
|
|
|
if (operators.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok = false;
|
|
|
|
if ((operators[0][0] === 'is') && (operators[0][1] === 'private')) {
|
|
|
|
operators = operators.slice(1);
|
|
|
|
ok = true;
|
|
|
|
} else if (operators[0][0] === 'pm-with') {
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var query;
|
|
|
|
|
|
|
|
if (operators.length === 0) {
|
|
|
|
query = '';
|
|
|
|
} else if (operators.length === 1) {
|
|
|
|
var operator = operators[0][0];
|
|
|
|
if (operator === 'search' || operator === 'pm-with') {
|
|
|
|
query = operators[0][1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var people = $.grep(all_people, function (person) {
|
|
|
|
return (query === '') || person_matches_query(person, query);
|
|
|
|
});
|
|
|
|
|
|
|
|
people.sort(typeahead_helper.compare_by_pms);
|
|
|
|
|
2013-07-22 22:31:06 +02:00
|
|
|
// Take top 15 people, since they're ordered by pm_recipient_count.
|
|
|
|
people = people.slice(0, 15);
|
|
|
|
|
2013-07-20 00:17:06 +02:00
|
|
|
var suggestions = $.map(people, function (person) {
|
|
|
|
var name = highlight_person(query, person);
|
|
|
|
var description = 'Narrow to private messages with ' + name;
|
|
|
|
var search_string = narrow.unparse([['pm-with', person.email]]);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
|
2013-07-22 23:49:12 +02:00
|
|
|
suggestions.push({
|
2013-07-20 00:17:06 +02:00
|
|
|
search_string: 'is:private',
|
|
|
|
description: 'Private messages'
|
|
|
|
});
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2013-07-19 21:44:40 +02:00
|
|
|
function get_person_suggestions(all_people, query, prefix, operator) {
|
2013-07-19 21:38:26 +02:00
|
|
|
if (query === '') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2013-07-16 02:58:17 +02:00
|
|
|
var people = $.grep(all_people, function (person) {
|
|
|
|
return person_matches_query(person, query);
|
2013-07-15 23:34:19 +02:00
|
|
|
});
|
2013-07-16 02:58:17 +02:00
|
|
|
|
2013-07-17 00:28:26 +02:00
|
|
|
people.sort(typeahead_helper.compare_by_pms);
|
2013-07-16 22:47:03 +02:00
|
|
|
|
2013-07-17 00:28:26 +02:00
|
|
|
var objs = $.map(people, function (person) {
|
|
|
|
var name = highlight_person(query, person);
|
|
|
|
var description = prefix + ' ' + name;
|
|
|
|
var search_string = operator + ':' + person.email;
|
|
|
|
return {description: description, search_string: search_string};
|
2013-07-16 01:03:13 +02:00
|
|
|
});
|
|
|
|
|
2013-07-16 02:18:36 +02:00
|
|
|
return objs;
|
2013-07-15 23:34:19 +02:00
|
|
|
}
|
|
|
|
|
2013-07-17 00:41:54 +02:00
|
|
|
function get_suggestion_based_on_query(search_string, operators) {
|
|
|
|
// We expect caller to call narrow.parse to get operators from search_string.
|
|
|
|
var description = describe(operators);
|
|
|
|
description = Handlebars.Utils.escapeExpression(description);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
}
|
|
|
|
|
2013-07-25 18:17:14 +02:00
|
|
|
function get_topic_suggestions(query_operators) {
|
2013-07-18 22:14:58 +02:00
|
|
|
if (query_operators.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var last_term = query_operators.slice(-1)[0];
|
|
|
|
var operator = narrow.canonicalize_operator(last_term[0]);
|
|
|
|
var operand = last_term[1];
|
|
|
|
var stream;
|
|
|
|
var guess;
|
2013-07-19 19:43:22 +02:00
|
|
|
var filter;
|
2013-07-18 22:14:58 +02:00
|
|
|
|
|
|
|
// stream:Rome -> show all Rome topics
|
|
|
|
// stream:Rome topic: -> show all Rome topics
|
|
|
|
// stream:Rome f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
|
|
|
|
// When narrowed to a stream:
|
|
|
|
// topic: -> show all topics in current stream
|
|
|
|
// foo -> show all topics in current stream with words starting with foo
|
|
|
|
|
|
|
|
// If somebody explicitly types search:, then we might
|
|
|
|
// not want to suggest topics, but I feel this is a very
|
|
|
|
// minor issue, and narrow.parse() is currently lossy
|
|
|
|
// in terms of telling us whether they provided the operator,
|
|
|
|
// i.e. "foo" and "search:foo" both become [['search', 'foo']].
|
|
|
|
switch (operator) {
|
|
|
|
case 'stream':
|
2013-07-19 19:43:22 +02:00
|
|
|
filter = new narrow.Filter(query_operators);
|
|
|
|
if (filter.has_operator('topic')) {
|
|
|
|
return [];
|
|
|
|
}
|
2013-07-18 22:14:58 +02:00
|
|
|
guess = '';
|
|
|
|
stream = operand;
|
|
|
|
break;
|
|
|
|
case 'topic':
|
|
|
|
case 'search':
|
|
|
|
guess = operand;
|
|
|
|
query_operators = query_operators.slice(0, -1);
|
2013-07-19 19:43:22 +02:00
|
|
|
filter = new narrow.Filter(query_operators);
|
2013-07-18 22:14:58 +02:00
|
|
|
if (filter.has_operator('topic')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (filter.has_operator('stream')) {
|
|
|
|
stream = filter.operands('stream')[0];
|
|
|
|
} else {
|
|
|
|
stream = narrow.stream();
|
|
|
|
query_operators.push(['stream', stream]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = subs.canonicalized_name(stream);
|
|
|
|
|
|
|
|
var topics = recent_subjects[stream];
|
|
|
|
|
|
|
|
if (!topics) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be defensive here in case recent_subjects gets super huge, but
|
|
|
|
// still slice off enough topics to find matches.
|
|
|
|
topics = topics.slice(0, 100);
|
|
|
|
|
|
|
|
// topics = $.map(topics, (t) -> t.subject)
|
|
|
|
topics = $.map(topics, function (topic) {
|
|
|
|
return topic.subject; // "subject" is just the name of the topic
|
|
|
|
});
|
|
|
|
|
2013-07-23 00:01:27 +02:00
|
|
|
topics = topics.slice(0, 10);
|
|
|
|
|
2013-07-18 22:14:58 +02:00
|
|
|
if (guess !== '') {
|
|
|
|
topics = $.grep(topics, function (topic) {
|
|
|
|
return phrase_match(topic, guess);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just use alphabetical order. While recency and read/unreadness of
|
|
|
|
// subjects do matter in some contexts, you can get that from the left sidebar,
|
|
|
|
// and I'm leaning toward high scannability for autocompletion. I also don't
|
|
|
|
// care about case.
|
|
|
|
topics.sort();
|
|
|
|
|
|
|
|
return $.map(topics, function (topic) {
|
|
|
|
var topic_operator = ['topic', topic];
|
|
|
|
var operators = query_operators.concat([topic_operator]);
|
|
|
|
var search_string = narrow.unparse(operators);
|
|
|
|
var description = describe(operators);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-23 05:22:51 +02:00
|
|
|
function get_operator_subset_suggestions(query, operators) {
|
2013-07-24 19:23:42 +02:00
|
|
|
// For stream:a topic:b search:c, suggest:
|
|
|
|
// stream:a topic:b
|
|
|
|
// stream:a
|
|
|
|
// <Home>
|
2013-07-23 05:22:51 +02:00
|
|
|
if (operators.length < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
|
|
|
var suggestions = [];
|
|
|
|
|
2013-07-24 19:23:42 +02:00
|
|
|
for (i = operators.length - 1; i >= 0; --i) {
|
2013-07-23 05:22:51 +02:00
|
|
|
var subset = operators.slice(0, i);
|
|
|
|
var search_string = narrow.unparse(subset);
|
|
|
|
var description = describe(subset);
|
|
|
|
var suggestion = {description: description, search_string: search_string};
|
|
|
|
suggestions.push(suggestion);
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 22:32:05 +02:00
|
|
|
function get_special_filter_suggestions(query, operators) {
|
|
|
|
if (operators.length >= 2) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var suggestions = [
|
|
|
|
{
|
|
|
|
search_string: '',
|
|
|
|
description: 'Home'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'in:all',
|
|
|
|
description: 'All messages'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'is:private',
|
|
|
|
description: 'Private messages'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'is:starred',
|
|
|
|
description: 'Starred messages'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'is:mentioned',
|
|
|
|
description: '@-mentions'
|
2013-07-19 23:07:47 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'sender:' + page_params.email,
|
|
|
|
description: 'Sent by me'
|
2013-07-19 22:32:05 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
query = query.toLowerCase();
|
|
|
|
|
|
|
|
suggestions = $.grep(suggestions, function (s) {
|
|
|
|
if (s.search_string.toLowerCase() === query) {
|
|
|
|
return false; // redundant
|
|
|
|
}
|
|
|
|
if (query === '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return (s.search_string.toLowerCase().indexOf(query) === 0) ||
|
|
|
|
(s.description.toLowerCase().indexOf(query) === 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2013-07-28 22:27:02 +02:00
|
|
|
// We make this a public method to facilitate testing, but it's only
|
|
|
|
// used internally. This becomes the "source" callback for typeahead.
|
|
|
|
exports.get_suggestions = function (query) {
|
|
|
|
var result = [];
|
|
|
|
var suggestion;
|
|
|
|
var suggestions;
|
|
|
|
|
|
|
|
// Add an entry for narrow by operators.
|
|
|
|
var operators = narrow.parse(query);
|
|
|
|
suggestion = get_suggestion_based_on_query(query, operators);
|
|
|
|
result = [suggestion];
|
|
|
|
|
|
|
|
suggestions = get_special_filter_suggestions(query, operators);
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
suggestions = get_stream_suggestions(operators);
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
var people = page_params.people_list;
|
|
|
|
|
|
|
|
suggestions = get_person_suggestions(people, query, 'Narrow to private messages with', 'pm-with');
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
suggestions = get_person_suggestions(people, query, 'Narrow to messages sent by', 'sender');
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
suggestions = get_private_suggestions(people, operators);
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
suggestions = get_topic_suggestions(operators);
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
suggestions = get_operator_subset_suggestions(query, operators);
|
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
|
|
|
// We can't send typeahead objects, only strings, so we have to create a map
|
|
|
|
// back to our objects, and we also filter duplicates here.
|
|
|
|
search_object = {};
|
|
|
|
var final_result = [];
|
|
|
|
$.each(result, function (idx, obj) {
|
|
|
|
if (!search_object[obj.search_string]) {
|
|
|
|
search_object[obj.search_string] = obj;
|
|
|
|
final_result.push(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return $.map(final_result, function (obj) {
|
|
|
|
return obj.search_string;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-11-14 22:12:21 +01:00
|
|
|
exports.initialize = function () {
|
|
|
|
$( "#search_query" ).typeahead({
|
2013-07-28 22:27:02 +02:00
|
|
|
source: exports.get_suggestions,
|
2013-07-22 22:17:37 +02:00
|
|
|
items: 30,
|
2013-07-19 21:38:26 +02:00
|
|
|
helpOnEmptyStrings: true,
|
2013-07-23 03:05:06 +02:00
|
|
|
naturalSearch: true,
|
2012-11-18 19:19:52 +01:00
|
|
|
highlighter: function (item) {
|
2013-07-16 02:18:36 +02:00
|
|
|
var obj = search_object[item];
|
2013-07-16 01:03:13 +02:00
|
|
|
return obj.description;
|
2012-11-18 19:19:52 +01:00
|
|
|
},
|
2012-11-15 03:44:50 +01:00
|
|
|
matcher: function (item) {
|
2013-07-15 22:43:25 +02:00
|
|
|
return true;
|
2012-11-15 03:44:50 +01:00
|
|
|
},
|
2012-11-26 21:39:15 +01:00
|
|
|
updater: narrow_or_search_for_term,
|
2013-07-15 22:57:52 +02:00
|
|
|
sorter: function (items) {
|
|
|
|
return items;
|
2013-07-29 19:41:46 +02:00
|
|
|
}
|
2012-11-14 22:12:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#searchbox_form").keydown(function (e) {
|
2012-12-18 23:38:55 +01:00
|
|
|
exports.update_button_visibility();
|
2012-11-14 22:12:21 +01:00
|
|
|
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")) {
|
2013-01-31 23:50:39 +01:00
|
|
|
// We just pressed enter and the box had focus, which
|
|
|
|
// means we didn't use the typeahead at all. In that
|
|
|
|
// case, we should act as though we're searching by
|
|
|
|
// operators. (The reason the other actions don't call
|
|
|
|
// this codepath is that they first all blur the box to
|
|
|
|
// indicate that they've done what they need to do)
|
2013-07-19 21:03:46 +02:00
|
|
|
narrow.activate(narrow.parse(search_query_box.val()));
|
2013-01-31 23:50:39 +01:00
|
|
|
search_query_box.blur();
|
|
|
|
update_buttons_with_focus(false);
|
2012-11-15 20:22:20 +01:00
|
|
|
}
|
|
|
|
});
|
2013-02-27 20:29:25 +01:00
|
|
|
|
|
|
|
// Some of these functions don't actually need to be exported,
|
|
|
|
// but the code was moved here from elsewhere, and it would be
|
|
|
|
// more work to re-order everything and make them private.
|
|
|
|
$('#search_exit' ).on('click', exports.clear_search);
|
2013-02-27 21:00:06 +01:00
|
|
|
|
|
|
|
var query = $('#search_query');
|
|
|
|
query.on('focus', exports.focus_search)
|
|
|
|
.on('blur' , function () {
|
|
|
|
|
|
|
|
// The search query box is a visual cue as to
|
|
|
|
// whether search or narrowing is active. If
|
2013-07-23 03:41:21 +02:00
|
|
|
// the user blurs the search box, then we should
|
|
|
|
// update the search string to reflect the currect
|
|
|
|
// narrow (or lack of narrow).
|
2013-02-27 21:00:06 +01:00
|
|
|
//
|
|
|
|
// But we can't do this right away, because
|
|
|
|
// selecting something in the typeahead menu causes
|
2013-07-23 03:41:21 +02:00
|
|
|
// the box to lose focus a moment before.
|
2013-02-27 21:00:06 +01:00
|
|
|
//
|
|
|
|
// The workaround is to check 100ms later -- long
|
|
|
|
// enough for the search to have gone through, but
|
|
|
|
// short enough that the user won't notice (though
|
|
|
|
// really it would be OK if they did).
|
|
|
|
|
|
|
|
setTimeout(function () {
|
2013-07-23 03:41:21 +02:00
|
|
|
var search_string = narrow.search_string();
|
|
|
|
query.val(search_string);
|
2013-02-27 21:00:06 +01:00
|
|
|
exports.update_button_visibility();
|
|
|
|
}, 100);
|
|
|
|
});
|
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.
|
2013-03-04 23:24:36 +01:00
|
|
|
return row.find(".message_content, .message_header")
|
2012-10-27 04:14:17 +02:00
|
|
|
.text().toLowerCase().indexOf(search_term) !== -1;
|
2012-10-26 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:42:13 +01:00
|
|
|
exports.focus_search = function () {
|
2013-01-02 19:58:55 +01:00
|
|
|
// The search bar is not focused yet, but will be.
|
|
|
|
update_buttons_with_focus(true);
|
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 () {
|
2013-01-29 20:54:11 +01:00
|
|
|
$('#search_query').select();
|
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();
|
2013-02-27 21:00:06 +01:00
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
$('table tr').removeHighlight();
|
2013-02-27 21:00:06 +01:00
|
|
|
$('#search_query').blur();
|
2012-12-18 23:38:55 +01:00
|
|
|
exports.update_button_visibility();
|
2012-12-07 20:18:01 +01:00
|
|
|
};
|
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|