|
| 1 | +(function($) { |
| 2 | + var template = |
| 3 | + '<div class="aa-dataset-a">' + |
| 4 | + '</div>' + |
| 5 | + '<div class="branding-footer">' + |
| 6 | + '<div class="branding"><span>Powered by</span><a target="_blank" href="https://www.algolia.com/?utm_source=scaladocs"><img src="https://www.algolia.com/assets/algolia128x40.png"/></a></div>' + |
| 7 | + '</div>'; |
| 8 | + |
| 9 | + var client = algoliasearch('BH4D9OD16A', '3403ae7bb4cb839fba71e2fae9ab1534'); |
| 10 | + var index = client.initIndex('scaladocs'); |
| 11 | + var ATTRIBUTES = ['content', 'h6', 'h5', 'h4', 'h3', 'h2', 'h1' ]; |
| 12 | + |
| 13 | + var capitalize = function(string) { |
| 14 | + return string.charAt(0).toUpperCase() + string.slice(1); |
| 15 | + }; |
| 16 | + |
| 17 | + var hitsSource = autocomplete.sources.hits(index, { hitsPerPage: 7, tagFilters: [ 'en' ], numericFilters: 'importance>0' }); |
| 18 | + |
| 19 | + autocomplete('#q', { |
| 20 | + hint: false, |
| 21 | + debug: false, |
| 22 | + templates: { |
| 23 | + dropdownMenu: template |
| 24 | + } |
| 25 | + }, [ |
| 26 | + { |
| 27 | + source: function(query, callback) { |
| 28 | + hitsSource(query, function(suggestions) { |
| 29 | + var categories = {}; |
| 30 | + suggestions.forEach(function(suggestion) { |
| 31 | + var enIndex = suggestion._tags.indexOf('en'); |
| 32 | + if(enIndex > -1) { |
| 33 | + suggestion._tags.splice(enIndex, 1); |
| 34 | + } |
| 35 | + categories[suggestion._tags[0]] = categories[suggestion._tags[0]] || [] |
| 36 | + categories[suggestion._tags[0]].push(suggestion) |
| 37 | + }); |
| 38 | + |
| 39 | + categories = $.map(Object.keys(categories).sort(), function(categoryName) { |
| 40 | + var items = categories[categoryName]; |
| 41 | + items[0].isCategoryHeader = true; |
| 42 | + items[0].categoryName = capitalize(categoryName); |
| 43 | + |
| 44 | + return items; |
| 45 | + }); |
| 46 | + callback(categories); |
| 47 | + }); |
| 48 | + }, |
| 49 | + name: 'a', |
| 50 | + displayKey: 'title', |
| 51 | + templates: { |
| 52 | + suggestion: function(suggestion) { |
| 53 | + var html = []; |
| 54 | + if(suggestion.isCategoryHeader) { |
| 55 | + html.push('<div class="suggestion-category">' + suggestion.categoryName + '</div>'); |
| 56 | + } |
| 57 | + |
| 58 | + var highlight = suggestion._highlightResult || {}; |
| 59 | + var snippet = suggestion._snippetResult || {}; |
| 60 | + var title = highlight.title.value; |
| 61 | + var text = ''; |
| 62 | + for(var i = 0 ; i < ATTRIBUTES.length ; i++) { |
| 63 | + if(highlight[ATTRIBUTES[i]] !== undefined) { |
| 64 | + text = highlight[ATTRIBUTES[i]].value; |
| 65 | + text = (snippet[ATTRIBUTES[i]] || {}).value || text; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + html.push(' <div class="suggestion-content">'); |
| 71 | + html.push(' <div class="suggestion-title">' + title + '</div>'); |
| 72 | + html.push(' <div class="suggestion-text">' + text + '</div>'); |
| 73 | + html.push(' </div>'); |
| 74 | + return html.join(' '); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ]).on('autocomplete:selected', function(event, suggestion) { |
| 79 | + window.location.href = suggestion.link; |
| 80 | + }); |
| 81 | +})(jQuery); |
0 commit comments