Match names w/prefix and turn off highlighting.

(imported from commit 4ebc4aba5d05173ad6b8176d47fdf062e23c1441)
This commit is contained in:
Steve Howell 2013-05-06 14:43:55 -04:00 committed by Tim Abbott
parent 0f75cbd30f
commit 64ba2864bc
1 changed files with 17 additions and 10 deletions

View File

@ -20,18 +20,28 @@ function get_person(obj) {
return typeahead_helper.render_person(obj.query);
}
function stream_matches_query(stream_name, q) {
function phrase_match(phrase, q) {
// match "tes" to "test" and "stream test" but not "hostess"
var i;
var parts = stream_name.split(' ');
q = q.toLowerCase();
var parts = phrase.split(' ');
for (i = 0; i < parts.length; i++) {
if (parts[i].toLowerCase().indexOf(q.toLowerCase()) === 0) {
if (parts[i].toLowerCase().indexOf(q) === 0) {
return true;
}
}
return false;
}
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);
}
function render_object_in_parts(obj) {
// N.B. action is *not* escaped by the caller
switch (obj.action) {
@ -202,11 +212,8 @@ exports.initialize = function () {
highlighter: function (item) {
var query = this.query;
var parts = render_object_in_parts(mapped[item]);
// We provide action, not the user, so this should
// be fine from a not-needing-escaping perspective.
return parts.prefix + " " +
typeahead_helper.highlight_with_escaping(query, parts.query)
+ " " + parts.suffix;
return Handlebars.Utils.escapeExpression(
parts.prefix + " " + parts.query + " " + parts.suffix);
},
matcher: function (item) {
var obj = mapped[item];
@ -215,10 +222,10 @@ exports.initialize = function () {
if (obj.action === 'stream') {
return stream_matches_query(obj.query, this.query);
}
var actual_search_term = obj.query;
if (obj.action === 'private_message' || obj.action === "sender") {
actual_search_term = obj.query.full_name + ' <' + obj.query.email + '>';
return person_matches_query(obj.query, this.query);
}
var actual_search_term = obj.query;
// Case-insensitive (from Bootstrap's default matcher).
return (actual_search_term.toLowerCase().indexOf(this.query.toLowerCase()) !== -1);
},