Skip to content

Commit e2b48d8

Browse files
committed
Adding collapsible functionality to the search results as the menu goes way below the screen. Adding some CSS classes to the markup and thus improving CSS/JS selector readability and performance.
1 parent 0bbfd67 commit e2b48d8

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

images/search-sprites.png

1.62 KB
Loading

js/search.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
return {
281281
name: name,
282282
local: backend.toTypeaheadArray(),
283-
header: "<h3>" + backend.label + "</h3>",
283+
header: '<h3 class="result-heading"><span class="collapsible"></span>' + backend.label + '</h3>',
284284
limit: options.limit,
285285
valueKey: "name",
286286
engine: Hogan,
@@ -293,11 +293,28 @@
293293
var results = {};
294294

295295
// Set up the typeahead and the various listeners we need.
296-
$(element).typeahead(typeaheadOptions).on("typeahead:selected", function (_, item) {
297-
/* If the user has selected an autocomplete item and hits
298-
* enter, we should take them straight to the page. */
296+
var searchTypeahead = $(element).typeahead(typeaheadOptions);
297+
298+
// Delegate click events to result-heading collapsible icons, and trigger the accordion action
299+
$('.tt-dropdown-menu').delegate('.result-heading .collapsible', 'click', function() {
300+
var el = $(this), suggestions = el.parent().parent().find('.tt-suggestions');
301+
suggestions.stop();
302+
if(!el.hasClass('closed')) {
303+
suggestions.slideUp();
304+
el.addClass('closed');
305+
} else {
306+
suggestions.slideDown();
307+
el.removeClass('closed');
308+
}
309+
310+
});
311+
312+
// If the user has selected an autocomplete item and hits enter, we should take them straight to the page.
313+
searchTypeahead.on("typeahead:selected", function (_, item) {
299314
window.location = "/manual/" + options.language + "/" + item.id;
300-
}).on("keyup", (function () {
315+
});
316+
317+
searchTypeahead.on("keyup", (function () {
301318
/* typeahead.js doesn't give us a reliable event for the
302319
* dropdown entries having been updated, so we'll hook into the
303320
* input element's keyup instead. The aim here is to put in
@@ -313,18 +330,26 @@
313330
* when the user has typed something into the search box after
314331
* typeahead.js has done its thing. */
315332
return function () {
316-
// Grab what the user entered.
317-
var pattern = $(element).val();
318-
319333
// Add result totals to each section heading.
320334
$.each(results, function (name, numResults) {
321-
var container = $(".tt-dataset-" + name, $(element).parent());
322-
var results = $("<span class='result-count' />").text(numResults);
335+
var container = $(".tt-dataset-" + name, $(element).parent()),
336+
resultHeading = container.find('.result-heading'),
337+
resultCount = container.find('.result-count');
338+
339+
// Does a result count already exist in this resultHeading?
340+
if(resultCount.length == 0) {
341+
var results = $("<span class='result-count' />").text(numResults);
342+
resultHeading.append(results);
343+
} else {
344+
resultCount.text(numResults);
345+
}
346+
323347

324-
$("h3 .result-count", container).remove();
325-
$("h3", container).append(results);
326348
});
327349

350+
// Grab what the user entered.
351+
var pattern = $(element).val();
352+
328353
/* Add a global search option. Note that, as above, the
329354
* link is only displayed if more than 2 characters have
330355
* been entered: this is due to our search functionality

styles/theme-base.css

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,20 +1011,40 @@ fieldset {
10111011
z-index: 1!important;
10121012
}
10131013

1014-
.tt-dropdown-menu h3 {
1014+
.tt-dropdown-menu .result-heading {
10151015
font-size:1.1rem;
1016-
background: rgb(136, 146, 191);
10171016
border-bottom: 2px solid #4F5B93;
10181017
color: #E2E4EF;
10191018
text-shadow:0 -1px 0 rgba(0,0,0,.25);
10201019
word-spacing:6px;
10211020
margin: 0;
10221021
padding: 0.1rem 0.3rem;
10231022
line-height: 2.5rem;
1023+
background-color: rgb(136, 146, 191);
1024+
}
1025+
1026+
.tt-dropdown-menu .result-heading .collapsible {
1027+
background: url(../images/search-sprites.png) no-repeat left center;
1028+
background-position: 0 -15px;
1029+
width: 30px;
1030+
height: 13px;
1031+
display: inline-block;
1032+
}
1033+
1034+
.tt-dropdown-menu .result-heading .collapsible:hover {
1035+
cursor: pointer;
1036+
}
1037+
1038+
.tt-dropdown-menu .result-heading .collapsible.closed {
1039+
background-position: 0 -2px;
1040+
}
1041+
1042+
.tt-dropdown-menu .result-heading::after {
1043+
border-bottom: none;
10241044
}
10251045

10261046
.tt-dropdown-menu .result-count {
1027-
display: block;
1047+
display: inline-block;
10281048
float: right;
10291049
opacity: 0.6;
10301050
text-align: right;

0 commit comments

Comments
 (0)