Skip to content

Commit

Permalink
Fix word highlighting in filtering-select widget, broken by f15fd21
Browse files Browse the repository at this point in the history
Refs #2362, Closes #2369
  • Loading branch information
mshibuya committed Jul 30, 2015
1 parent 452481b commit 7aee78b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/assets/javascripts/rails_admin/ra.filtering-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
input.data("ui-autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append( $( "<a></a>" ).text( item.label || item.id ) )
.append( $( "<a></a>" ).html( item.html || item.id ) )
.appendTo(ul);
};

Expand All @@ -118,17 +118,21 @@

_getResultSet: function(request, data, xhr) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
var highlighter = function(label, word){
if(word.length > 0){
return $.map(label.split(word), function(el, i){
return $('<span></span>').text(el).html();
}).join($('<strong></strong>').text(word)[0].outerHTML);
}else{
return $('<span></span>').text(label).html();
}
};

return $.map(data, function(el, i) {
// match regexp only for local requests, remote ones are already filtered, and label may not contain filtered term.
if ((el.id || el.value) && (xhr || matcher.test(el.label))) {
return {
label: el.label ? el.label.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>") : el.id,
html: highlighter(el.label || el.id, request.term),
value: el.label || el.id,
id: el.id || el.value
};
Expand Down

1 comment on commit 7aee78b

@fmh
Copy link
Contributor

@fmh fmh commented on 7aee78b Jul 30, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.