Skip to content

Commit 91844e9

Browse files
committed
Merge pull request scala#472 from algolia/master
Autocomplete search: Improvements
2 parents d076f98 + 7b026d2 commit 91844e9

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

‎resources/javascript/searchbar.js‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,40 @@
3636
categories[suggestion._tags[0]].push(suggestion)
3737
});
3838

39-
categories=$.map(Object.keys(categories).sort(),function(categoryName){
40-
varitems=categories[categoryName];
41-
items[0].isCategoryHeader=true;
42-
items[0].categoryName=capitalize(categoryName);
39+
varcategoriesWithSubCategories={}
40+
$.each(categories,function(categoryName,suggestions){
41+
varsubCategories={}
42+
suggestions.forEach(function(suggestion){
43+
varhighlight=suggestion._highlightResult||{};
44+
vartitle=(highlight.title||{}).value
45+
title=title||suggestion.title
46+
title=title||categoryName
47+
subCategories[title]=subCategories[title]||[]
48+
subCategories[title].push(suggestion)
49+
});
50+
categoriesWithSubCategories[categoryName]=subCategories;
51+
});
52+
53+
varsuggestionsSorted=[]
54+
Object.keys(categoriesWithSubCategories).sort().forEach(function(categoryName){
55+
varsubCategories=categoriesWithSubCategories[categoryName];
56+
varfirstInCategory=true;
57+
Object.keys(subCategories).sort().forEach(function(subCategoryName){
58+
varitems=subCategories[subCategoryName];
59+
items[0].isSubCategoryHeader=true;
60+
items[0].subCategoryName=capitalize(subCategoryName);
61+
62+
if(firstInCategory){
63+
items[0].isCategoryHeader=true;
64+
items[0].categoryName=capitalize(categoryName);
65+
firstInCategory=false;
66+
}
4367

44-
returnitems;
68+
suggestionsSorted=suggestionsSorted.concat(items);
69+
});
4570
});
46-
callback(categories);
71+
72+
callback(suggestionsSorted);
4773
});
4874
},
4975
name: 'a',
@@ -54,21 +80,33 @@
5480
if(suggestion.isCategoryHeader){
5581
html.push('<div class="suggestion-category">'+suggestion.categoryName+'</div>');
5682
}
83+
if(suggestion.isSubCategoryHeader){
84+
html.push('<div class="suggestion-subcategory">'+suggestion.subCategoryName+'</div>');
85+
}else{
86+
html.push('<div class="suggestion-empty-subcategory">&nbsp;</div>');
87+
}
5788

5889
varhighlight=suggestion._highlightResult||{};
5990
varsnippet=suggestion._snippetResult||{};
60-
vartitle=highlight.title.value;
6191
vartext='';
6292
for(vari=0;i<ATTRIBUTES.length;i++){
6393
if(highlight[ATTRIBUTES[i]]!==undefined){
6494
text=highlight[ATTRIBUTES[i]].value;
65-
text=(snippet[ATTRIBUTES[i]]||{}).value||text;
95+
varsnippet=(snippet[ATTRIBUTES[i]]||{}).value
96+
if(snippet){
97+
if(snippet.charAt(0)===snippet.charAt(0).toLowerCase()){
98+
snippet="…&nbsp;"+snippet;
99+
}
100+
if(['.','!','?'].indexOf(snippet.charAt(snippet.length-1))===-1){
101+
snippet=snippet+"&nbsp;…";
102+
}
103+
}
104+
text=snippet||text;
66105
break;
67106
}
68107
}
69108

70109
html.push(' <div class="suggestion-content">');
71-
html.push(' <div class="suggestion-title">'+title+'</div>');
72110
html.push(' <div class="suggestion-text">'+text+'</div>');
73111
html.push(' </div>');
74112
returnhtml.join(' ');

‎resources/stylesheets/base.css‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ div#pop-up-arrow{
232232
}
233233

234234
.algolia-autocomplete .aa-dropdown-menu{
235-
width:500px;
235+
width:550px;
236236
background-color:#fff;
237237
border:1px solid #404040;
238238
margin-top:2px;
239-
margin-left:-319px;
239+
margin-left:-369px;
240240
padding:1px;
241241
}
242242

@@ -246,8 +246,9 @@ div#pop-up-arrow{
246246
}
247247

248248
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor{
249-
background-color:#B2D7FF;
249+
background-color:#EEEEEE;
250250
}
251+
251252
.algolia-autocomplete .aa-dropdown-menu .aa-suggestionem{
252253
font-weight: bold;
253254
font-style: normal;
@@ -263,14 +264,30 @@ div#pop-up-arrow{
263264
cursor: default;
264265
}
265266

266-
.aa-suggestion .suggestion-contentdiv{
267+
.aa-suggestion>div:not(.suggestion-category){
267268
display: table-cell;
268-
padding:5px7px;
269+
padding:5px;
269270
}
270271

271-
.aa-suggestion .suggestion-contentdiv:first-child{
272+
.aa-suggestion.aa-cursor .suggestion-empty-subcategory, .aa-suggestion.aa-cursor .suggestion-subcategory{
273+
background-color: white;
274+
}
275+
276+
.aa-suggestion .suggestion-empty-subcategory, .aa-suggestion .suggestion-subcategory{
272277
border-right:2px solid #DE3423;
273-
width:180px;
278+
width:150px;
279+
font-weight:500;
280+
z-index:1000;
281+
text-align: right;
282+
}
283+
284+
.aa-suggestion .suggestion-content{
285+
width:376px;
286+
border-top:1px#999 solid;
287+
}
288+
289+
.aa-suggestion .suggestion-category~ .suggestion-content{
290+
border-top: none;
274291
}
275292

276293
.aa-dropdown-menu .branding-footer{

0 commit comments

Comments
(0)