1function SuggestController() { 2 3 this.init = function () { 4 5 jQuery('form[data-suggest]').each(function () { 6 var $form = $(this), 7 $searchBox = $form.find('.tx-solr-suggest'), 8 $formAutoComplete; 9 10 // Use .parents() to match the working code 11 if ($form.parents('.tx-solr-autocomplete').length > 0) { 12 $formAutoComplete = $form.parents('.tx-solr-autocomplete'); 13 } else { 14 $formAutoComplete = $('body'); 15 } 16 17 // $form.find('.tx-solr-suggest-focus').focus(); 18 19 // When no specific container found, use the form as container 20 if ($searchBox.length === 0) { 21 $searchBox = $form; 22 } 23 $searchBox.css('position', 'relative'); 24 25 // Prevent submit of empty search form 26 $form.on('submit', function (e) { 27 if ($form.find('.tx-solr-suggest').val() === '') { 28 e.preventDefault(); 29 $form.find('.tx-solr-suggest').focus(); 30 } 31 }); 32 33 $form.find('.tx-solr-suggest').devbridgeAutocomplete({ 34 serviceUrl: $form.data('suggest'), 35 dataType: 'json', 36 paramName: 'tx_solr[queryString]', 37 groupBy: 'category', 38 maxHeight: 1000, 39 appendTo: $formAutoComplete, 40 autoSelectFirst: false, 41 triggerSelectOnValidInput: false, 42 width: $searchBox.outerWidth() * 0.66, 43 onSelect: function (suggestion) { 44 // go to link when selecting found result 45 if (suggestion.data.link) { 46 // Open youtube in overlay 47 if (suggestion.data.link.indexOf('https://www.youtube.com') === 0) { 48 openVideoOverlay(suggestion.data.link); 49 } else { 50 location.href = suggestion.data.link; 51 } 52 } else { 53 $form.trigger('submit'); 54 } 55 }, 56 transformResult: function (response) { 57 // suggestions is an object: {kalo: 165, ...} 58 var result = { suggestions: [] }; 59 60 // Add word suggestions 61 if (response.suggestions && typeof response.suggestions === 'object') { 62 Object.keys(response.suggestions).forEach(function (suggestion) { 63 result.suggestions.push({ 64 value: suggestion, 65 data: { category: 'suggestion', count: response.suggestions[suggestion] } 66 }); 67 }); 68 } 69 70 // Add document results 71 if (Array.isArray(response.documents)) { 72 response.documents.forEach(function (doc) { 73 // Use doc.title as value, and doc as data 74 var dataObject = $.extend({}, doc); 75 var defaultGroup = $form.data('suggest-header') ? $form.data('suggest-header') : 'Top results'; 76 dataObject.category = defaultGroup; 77 if (dataObject.group) { 78 dataObject.category = $form.data('suggest-header-' + dataObject.group) ? $form.data('suggest-header-' + dataObject.group) : dataObject.group; 79 } 80 result.suggestions.push({ 81 value: doc.title, 82 data: dataObject 83 }); 84 }); 85 } 86 87 return result; 88 }, 89 beforeRender: function (container) { 90 // remove first group header 91 container.find('.autocomplete-group:first').remove(); 92 container.addClass('tx-solr-autosuggest'); 93 $searchBox.parent().addClass('autocomplete-active').fadeIn(); 94 }, 95 formatResult: function (suggestion, currentValue) { 96 if (!currentValue) { 97 return suggestion.value; 98 } 99 var pattern = '(' + $.Autocomplete.utils.escapeRegExChars(currentValue.trim()) + ')'; 100 if (suggestion.data.category === 'suggestion') { 101 return suggestion.value 102 .replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>') 103 .replace(/&/g, '&') 104 .replace(/</g, '<') 105 .replace(/>/g, '>') 106 .replace(/"/g, '"') 107 .replace(/<(\/?strong)>/g, '<$1>'); 108 } else { 109 var title = suggestion.data.title 110 .replace(/<\/?span[^>]*>/g, "") 111 .replace(new RegExp(pattern, 'gi'), '<em>$1<\/em>') 112 .replace(/&/g, '&') 113 .replace(/</g, '<') 114 .replace(/>/g, '>') 115 .replace(/"/g, '"') 116 .replace(/<(\/?em)>/g, '<$1>'); 117 return '<div class="' + suggestion.data.type + '">' + 118 (!!suggestion.data.previewImage ? '<figure ' + (!!suggestion.data.hasVideo ? 'class="hasVideo"' : '') + '>
118<img src="' + suggestion.data.previewImage + '" /></figure>' : '') + 119 '<a href="' + suggestion.data.link + '" class="internal-link">' + title + '</a>' + 120 '</div>'; 121 } 122 } 123 }).on('blur', function () { 124 $searchBox.parent().removeClass('autocomplete-active'); 125 var $box = $(this); 126 setTimeout(function () { 127 $box.devbridgeAutocomplete('hide'); 128 }, 200); 129 }); 130 }); 131 }; 132} 133 134jQuery(document).ready(function () { 135 var solrSuggestController = new SuggestController(); 136 solrSuggestController.init(); 137 138 jQuery("body").on("tx_solr_updated", function () { 139 solrSuggestController.init(); 140 }); 141});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.