diff options
author | Felix Hammerl <fhammerl@thoughtworks.com> | 2016-03-03 12:25:25 +0100 |
---|---|---|
committer | Felix Hammerl <fhammerl@thoughtworks.com> | 2016-03-09 15:36:10 +0100 |
commit | 23b175742f20d96e5b5d3d9cfcc0ed7067197f92 (patch) | |
tree | 224e5d536b9a6c4859671b2cff57f1f60236ef9f /web-ui/app/js/search/results_highlighter.js | |
parent | d2d6fb98b909021efa435411ec95f554769fa9c9 (diff) |
Issue #617: Highlight search terms by altering mail content
Diffstat (limited to 'web-ui/app/js/search/results_highlighter.js')
-rw-r--r-- | web-ui/app/js/search/results_highlighter.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/web-ui/app/js/search/results_highlighter.js b/web-ui/app/js/search/results_highlighter.js index 9e3ba167..831be0cd 100644 --- a/web-ui/app/js/search/results_highlighter.js +++ b/web-ui/app/js/search/results_highlighter.js @@ -40,6 +40,7 @@ define( var domIdent = data.where; if(this.attr.keywords) { _.each(this.attr.keywords, function (keyword) { + keyword = escapeRegExp(keyword); $(domIdent).highlightRegex(new RegExp(keyword, 'i'), { tagType: 'em', className: 'search-highlight' @@ -57,12 +58,40 @@ define( }); }; + this.highlightString = function (string) { + _.each(this.attr.keywords, function (keyword) { + keyword = escapeRegExp(keyword); + var regex = new RegExp('(' + keyword + ')', 'ig'); + string = string.replace(regex, '<em class="search-highlight">$1</em>'); + }); + return string; + }; + + /* + * Alter data.mail.textPlainBody to highlight each of this.attr.keywords + * and pass it back to the mail_view when done + */ + this.highlightMailContent = function(ev, data){ + var mail = data.mail; + mail.textPlainBody = this.highlightString(mail.textPlainBody); + this.trigger(document, events.mail.display, data); + }; + + /* + * Escapes the special charaters used regular expressions that + * would cause problems with strings in the RegExp constructor + */ + function escapeRegExp(string){ + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + } + this.after('initialize', function () { this.on(document, events.search.perform, this.getKeywordsSearch); this.on(document, events.ui.tag.select, this.clearHighlights); this.on(document, events.search.resetHighlight, this.clearHighlights); this.on(document, events.search.highlightResults, this.highlightResults); + this.on(document, events.mail.highlightMailContent, this.highlightMailContent); }); } }); |