Sort searchbox typeahead in a more intelligent way.

Ensure that every result has one of:
* find what you typed in
* search for what you typed in
* Narrow to a stream related to what you typed
* Narrow to a person related to what you type

(imported from commit 2178f17932f951a48f53d982ef660942562b55dc)
This commit is contained in:
Waseem Daher 2012-11-26 15:39:15 -05:00
parent ab8496b9ba
commit 8c0445bff5
2 changed files with 49 additions and 5 deletions

View File

@ -65,6 +65,49 @@ function narrow_or_search_for_term(item) {
return item; return item;
} }
function get_query(obj) {
return obj.query;
}
function get_person(obj) {
return typeahead_helper.render_pm_object(obj.query);
}
function searchbox_sorter(items) {
var searches = [];
var search_narrows = [];
var streams = [];
var people = [];
var objects = [];
$.each(items, function (idx, elt) {
var obj = mapped[elt];
if (obj.action === 'stream') {
streams.push(obj);
} else if (obj.action === 'private_message') {
people.push(obj);
} else if (obj.action === 'search') {
searches.push(obj);
} else if (obj.action === 'search_narrow') {
search_narrows.push(obj);
}
});
searches = typeahead_helper.sorter(this.query, searches, get_query);
search_narrows = typeahead_helper.sorter(this.query, search_narrows, get_query);
streams = typeahead_helper.sorter(this.query, streams, get_query);
people = typeahead_helper.sorter(this.query, people, get_person);
$.each([searches, search_narrows, streams, people], function (idx, elt) {
var obj = elt.shift();
if (obj) objects.push(obj);
});
return $.map(objects, function (elt, idx) {
return render_object(elt);
});
}
exports.initialize = function () { exports.initialize = function () {
$( "#search_query" ).typeahead({ $( "#search_query" ).typeahead({
source: function (query, process) { source: function (query, process) {
@ -99,7 +142,8 @@ exports.initialize = function () {
// Case-insensitive (from Bootstrap's default matcher). // Case-insensitive (from Bootstrap's default matcher).
return (actual_search_term.toLowerCase().indexOf(this.query.toLowerCase()) !== -1); return (actual_search_term.toLowerCase().indexOf(this.query.toLowerCase()) !== -1);
}, },
updater: narrow_or_search_for_term updater: narrow_or_search_for_term,
sorter: searchbox_sorter
}); });
$("#searchbox_form").keydown(function (e) { $("#searchbox_form").keydown(function (e) {

View File

@ -48,12 +48,12 @@ exports.highlight_with_escaping = function (query, item) {
exports.private_message_typeahead_list = []; exports.private_message_typeahead_list = [];
exports.private_message_mapped = {}; exports.private_message_mapped = {};
function render_pm_object(person) { exports.render_pm_object = function (person) {
return person.full_name + " <" + person.email + ">"; return person.full_name + " <" + person.email + ">";
} };
function add_to_known_recipients(recipient_data, count_towards_autocomplete_preference) { function add_to_known_recipients(recipient_data, count_towards_autocomplete_preference) {
var name_string = render_pm_object(recipient_data); var name_string = exports.render_pm_object(recipient_data);
if (typeahead_helper.private_message_mapped[name_string] === undefined) { if (typeahead_helper.private_message_mapped[name_string] === undefined) {
typeahead_helper.private_message_mapped[name_string] = recipient_data; typeahead_helper.private_message_mapped[name_string] = recipient_data;
typeahead_helper.private_message_mapped[name_string].count = 0; typeahead_helper.private_message_mapped[name_string].count = 0;
@ -65,7 +65,7 @@ function add_to_known_recipients(recipient_data, count_towards_autocomplete_pref
} }
exports.known_to_typeahead = function (recipient_data) { exports.known_to_typeahead = function (recipient_data) {
return typeahead_helper.private_message_mapped[render_pm_object(recipient_data)] !== undefined; return typeahead_helper.private_message_mapped[exports.render_pm_object(recipient_data)] !== undefined;
}; };
exports.update_all_recipients = function (recipients) { exports.update_all_recipients = function (recipients) {