diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js index b0f4450c13..20539aef12 100644 --- a/static/js/search_suggestion.js +++ b/static/js/search_suggestion.js @@ -2,12 +2,15 @@ function stream_matches_query(stream_name, q) { return common.phrase_match(q, stream_name); } -function highlight_person(query, person) { - const hilite = typeahead_helper.highlight_query_in_phrase; - if (settings_org.show_email()) { - return hilite(query, person.full_name) + " <" + hilite(query, person.email) + ">"; - } - return hilite(query, person.full_name); +function make_person_highlighter(query) { + const hilite = typeahead_helper.make_query_highlighter(query); + + return function (person) { + if (settings_org.show_email()) { + return hilite(person.full_name) + " <" + hilite(person.email) + ">"; + } + return hilite(person.full_name); + }; } function match_criteria(operators, criteria) { @@ -148,13 +151,15 @@ function get_group_suggestions(all_persons, last, operators) { const prefix = Filter.operator_to_prefix('pm-with', negated); + const highlight_person = make_person_highlighter(last_part); + const suggestions = _.map(persons, function (person) { const term = { operator: 'pm-with', operand: all_but_last_part + ',' + person.email, negated: negated, }; - const name = highlight_person(last_part, person); + const name = highlight_person(person); const description = prefix + ' ' + Handlebars.Utils.escapeExpression(all_but_last_part) + ',' + name; let terms = [term]; if (negated) { @@ -202,8 +207,10 @@ function get_person_suggestions(all_persons, last, operators, autocomplete_opera const prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated); + const highlight_person = make_person_highlighter(query); + const objs = _.map(persons, function (person) { - const name = highlight_person(query, person); + const name = highlight_person(person); const description = prefix + ' ' + name; const terms = [{ operator: autocomplete_operator, diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.js index e41d7e29d0..ca2c67e483 100644 --- a/static/js/typeahead_helper.js +++ b/static/js/typeahead_helper.js @@ -51,21 +51,23 @@ exports.highlight_with_escaping_and_regex = function (regex, item) { return result; }; -exports.highlight_query_in_phrase = function (query, phrase) { +exports.make_query_highlighter = function (query) { let i; query = query.toLowerCase(); query = query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); const regex = new RegExp('(^' + query + ')', 'ig'); - let result = ""; - const parts = phrase.split(' '); - for (i = 0; i < parts.length; i += 1) { - if (i > 0) { - result += " "; + return function (phrase) { + let result = ""; + const parts = phrase.split(' '); + for (i = 0; i < parts.length; i += 1) { + if (i > 0) { + result += " "; + } + result += exports.highlight_with_escaping_and_regex(regex, parts[i]); } - result += exports.highlight_with_escaping_and_regex(regex, parts[i]); - } - return result; + return result; + }; }; exports.render_typeahead_item = function (args) {