1function SuggestController() { 2 3 this.init = function () { 4 5 jQuery('form[data-suggest]').each(function () { 6 var $form = $(this), $searchBox = $form.find('.tx-solr-suggest'), $formAutoComplete; 7 8 if ($form.find('.tx-solr-autocomplete').length > 0){ 9 $formAutoComplete = $form.find('.tx-solr-autocomplete'); 10 } else { 11 $formAutoComplete = $('body'); 12 } 13 14 $form.find('.tx-solr-suggest-focus').focus(); 15 16 // when no specific container found, use the form as container 17 if ($searchBox.length === 0) { 18 $searchBox = $form; 19 } 20 $searchBox.css('position', 'relative'); 21 22 // Prevent submit of empty search form 23 $form.on('submit', function (e) { 24 if ($form.find('.tx-solr-suggest').val() === '') { 25 e.preventDefault(); 26 $form.find('.tx-solr-suggest').focus(); 27 } 28 }); 29 30 $form.find('.tx-solr-suggest').devbridgeAutocomplete({ 31 serviceUrl: $form.data('suggest'), 32 dataType: 'jsonp', 33 ajaxSettings: { 34 jsonp: "tx_solr[callback]" 35 }, 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 trigger form submit (do search) 53 } else { 54 $form.trigger('submit'); 55 } 56 }, 57 transformResult: function (response) { 58 if (!response.suggestions) return {suggestions: []}; 59 var firstSuggestion, result = { 60 suggestions: $.map(response.suggestions, function (count, suggestion) { 61 if (!firstSuggestion) firstSuggestion = suggestion; 62 return {value: suggestion, data: {category: 'suggestion', count: count}}; 63 }) 64 }; 65 66 $.each(response.documents, function (key, value) { 67 var dataObject = value; 68 69 var defaultGroup = $form.data('suggest-header') ? $form.data('suggest-header') : 'Top results'; 70 dataObject.category = defaultGroup; 71 72 // if a group is set we try to get a label 73 if(dataObject.group) { 74 dataObject.category = $form.data('suggest-header-' + dataObject.group) ? $form.data('suggest-header-' + dataObject.group) : dataObject.group; 75 } 76 77 result.suggestions.push( 78 { 79 value: firstSuggestion, 80 data: dataObject 81 } 82 ); 83 }); 84 85 return result; 86 }, 87 beforeRender: function (container) { 88 // remove first group header 89 container.find('.autocomplete-group:first').remove(); 90 container.addClass('tx-solr-autosuggest'); 91 92 // add active class to container 93 $searchBox.parent().addClass('autocomplete-active').fadeIn(); 94 }, 95 formatResult: function (suggestion, currentValue) {
96 // Do not replace anything if there current value is empty 97 if (!currentValue) { 98 return suggestion.value; 99 } 100 var pattern = '(' + $.Autocomplete.utils.escapeRegExChars(currentValue.trim()) + ')'; 101 // normal suggestion 102 if (suggestion.data.category === 'suggestion') { 103 return suggestion.value 104 .replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>') 105 .replace(/&/g, '&') 106 .replace(/</g, '<') 107 .replace(/>/g, '>') 108 .replace(/"/g, '"') 109 .replace(/<(\/?strong)>/g, '<$1>'); 110 111 // results 112 } else { 113 var title = suggestion.data.title 114 .replace(new RegExp(pattern, 'gi'), '<em>$1<\/em>') 115 .replace(/&/g, '&') 116 .replace(/</g, '<') 117 .replace(/>/g, '>') 118 .replace(/"/g, '"') 119 .replace(/<(\/?em)>/g, '<$1>'); 120 121 return '<div class="' + suggestion.data.type + '">' + 122 (!!suggestion.data.previewImage ? '<figure ' + (!!suggestion.data.hasVideo ? 'class="hasVideo"' : '') + '>
122<img src="' + suggestion.data.previewImage + '" /></figure>' : '') + 123 '<a href="' + suggestion.data.link + '" class="internal-link">' + title + '</a>' + 124 '</div>'; 125 } 126 127 } 128 }).on('blur', function () { 129 $searchBox.parent().removeClass('autocomplete-active'); 130 var $box = $(this); 131 setTimeout(function () { 132 $box.devbridgeAutocomplete('hide'); 133 }, 200); 134 }); 135 }); 136 }; 137} 138 139jQuery(document).ready(function() { 140 /** solr search autocomplete **/ 141 var solrSuggestController = new SuggestController(); 142 solrSuggestController.init(); 143 144 jQuery("body").on("tx_solr_updated", function() { 145 solrSuggestController.init(); 146 }); 147}); 148
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.