summaryrefslogtreecommitdiff
path: root/web-ui/app/js/search/results_highlighter.js
diff options
context:
space:
mode:
Diffstat (limited to 'web-ui/app/js/search/results_highlighter.js')
-rw-r--r--web-ui/app/js/search/results_highlighter.js29
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);
});
}
});