Skip to content

Instantly share code, notes, and snippets.

@alexdunae
Created June 13, 2014 18:38
Show Gist options
  • Save alexdunae/e9ce6ad3582c0ab2c20c to your computer and use it in GitHub Desktop.
Save alexdunae/e9ce6ad3582c0ab2c20c to your computer and use it in GitHub Desktop.
// heavily inspired https://github.com/alphagov/static/blob/master/app/assets/javascripts/search.js.erb
$.extend($.ui.autocomplete.prototype, {
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category && item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'><span>" + item.category + "</span></li>" );
currentCategory = item.category;
}
that._renderItemData( ul, item );
});
$(ul).find('li.post:last').addClass('last');
},
_renderItem: function (ul, item) {
var list = "<li class='" + (item.type === undefined ? '' : item.type) + "'></li>",
html;
if (item.type == 'post') {
html = '<strong>' + item.date + ': ' + item.label + '</strong>';
} else {
html = '<strong>' + item.label + '</strong>';
}
if (item.desc) {
// truncate really long descriptions
if (item.desc.length > 140) {
item.desc = item.desc.substring(0, 120) + '&hellip;';
}
html += '<br><span class="excerpt">' + item.desc + '</span>';
}
return $(list)
.data("item.autocomplete", item)
.append($("<a href='" + item.url + "' class='" + item.type + "'></a>").html(html))
.appendTo(ul);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment