|
280 | 280 | return {
|
281 | 281 | name: name,
|
282 | 282 | local: backend.toTypeaheadArray(),
|
283 |
| - header: "<h3>" + backend.label + "</h3>", |
| 283 | + header: '<h3 class="result-heading"><span class="collapsible"></span>' + backend.label + '</h3>', |
284 | 284 | limit: options.limit,
|
285 | 285 | valueKey: "name",
|
286 | 286 | engine: Hogan,
|
|
293 | 293 | var results = {};
|
294 | 294 |
|
295 | 295 | // 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) { |
299 | 314 | window.location = "/manual/" + options.language + "/" + item.id;
|
300 |
| - }).on("keyup", (function () { |
| 315 | + }); |
| 316 | + |
| 317 | + searchTypeahead.on("keyup", (function () { |
301 | 318 | /* typeahead.js doesn't give us a reliable event for the
|
302 | 319 | * dropdown entries having been updated, so we'll hook into the
|
303 | 320 | * input element's keyup instead. The aim here is to put in
|
|
313 | 330 | * when the user has typed something into the search box after
|
314 | 331 | * typeahead.js has done its thing. */
|
315 | 332 | return function () {
|
316 |
| - // Grab what the user entered. |
317 |
| - var pattern = $(element).val(); |
318 |
| - |
319 | 333 | // Add result totals to each section heading.
|
320 | 334 | $.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 | + |
323 | 347 |
|
324 |
| - $("h3 .result-count", container).remove(); |
325 |
| - $("h3", container).append(results); |
326 | 348 | });
|
327 | 349 |
|
| 350 | + // Grab what the user entered. |
| 351 | + var pattern = $(element).val(); |
| 352 | + |
328 | 353 | /* Add a global search option. Note that, as above, the
|
329 | 354 | * link is only displayed if more than 2 characters have
|
330 | 355 | * been entered: this is due to our search functionality
|
|
0 commit comments