Skip to content

Commit aad0a2f

Browse files
committed
Add categorized search results.
1 parent 29fe5f3 commit aad0a2f

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

html/css/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ td p {
199199
color: #000000;
200200
}
201201

202+
.ui-autocomplete-category {
203+
font-weight: bold;
204+
font-style: italic;
205+
background-color: #DAFE8C;
206+
padding: 2px 5px;
207+
line-height: 1.5;
208+
}
209+
202210
.menu {
203211
border: none;
204212
text-align: center;

htmlify.p6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ sub write-search-file () {
587587
my $items = $*DR.get-kinds.map(-> $kind {
588588
$*DR.lookup($kind, :by<kind>).categorize({escape .name})\
589589
.pairs.sort({.key}).map: -> (:key($name), :value(@docs)) {
590-
qq[[\{ label: "{
590+
qq[[\{ category: "{
591591
( @docs > 1 ?? $kind !! @docs.[0].subkinds[0] ).wordcase
592-
}: $name", value: "$name", url: "{@docs.[0].url}" \}]] #"
592+
}", value: "$name", url: "{@docs.[0].url}" \}]] #"
593593
}
594594
}).flat.join(",\n");
595595
spurt("html/js/search.js", $template.subst("ITEMS", $items));

template/search_template.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
$(function(){
2-
$("#query").autocomplete({
2+
$.widget( "custom.catcomplete", $.ui.autocomplete, {
3+
_create: function() {
4+
this._super();
5+
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
6+
},
7+
_renderMenu: function( ul, items ) {
8+
var that = this,
9+
currentCategory = "";
10+
function sortBy(a, b) {
11+
if (a.category.toLowerCase() < b.category.toLowerCase()) {
12+
return -1;
13+
} else if (a.category.toLowerCase() > b.category.toLowerCase()) {
14+
return 1;
15+
} else if (a.value.toLowerCase() < b.value.toLowerCase()) {
16+
return -1;
17+
} else if (a.value.toLowerCase() > b.value.toLowerCase()) {
18+
return 1;
19+
} else {
20+
return 0;
21+
}
22+
}
23+
$.each( items.sort(sortBy), function( index, item ) {
24+
var li;
25+
if ( item.category != currentCategory ) {
26+
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
27+
currentCategory = item.category;
28+
}
29+
li = that._renderItemData( ul, item );
30+
if ( item.category ) {
31+
li.attr( "aria-label", item.category + " : " + item.label );
32+
}
33+
});
34+
}
35+
});
36+
$("#query").catcomplete({
337
response: function( e, ui ) {
438
if ( ! ui.content.length ) { $('#search').addClass( 'not-found') }
539
else { $('#search').removeClass('not-found') }

0 commit comments

Comments
 (0)