highlight search results

This commit is contained in:
Athou
2013-07-31 11:02:39 +02:00
parent 54e5621267
commit c17cc5bd1c
3 changed files with 31 additions and 4 deletions

View File

@@ -424,4 +424,26 @@ module.directive('droppable', [ 'CategoryService', 'FeedService',
});
}
};
} ]);
} ]);
module.filter('highlight', function() {
return function(html, keywords) {
if (keywords) {
var tokens = keywords.split(' ');
for (var i = 0; i < tokens.length; i++) {
var expr = new RegExp(tokens[i], 'gi');
var container = $('<span>').html(html);
var elements = container.find('*').addBack();
var textNodes = elements.not('iframe').contents().not(elements);
textNodes.each(function() {
var replaced = this.nodeValue.replace(expr, '<span class="highlight-search">$&</span>');
$('<span>').html(replaced).insertBefore(this);
$(this).remove();
});
return container.html();
}
}
return html;
};
});