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 // when no specific container found, use the form as container 15 if ($searchBox.length === 0) { 16 $searchBox = $form; 17 } 18 $searchBox.css('position', 'relative'); 19 20 $form.find('.tx-solr-suggest-focus').focus(); 21 22 // Prevent submit of empty search form 23 $form.on('submit', function (e) { 24 if ($form.find('.tx-solr-suggest-focus').val() === '') { 25 e.preventDefault(); 26 $form.find('.tx-solr-suggest-focus').focus(); 27 } 28 }); 29 30 $form.find('.tx-solr-suggest').devbridgeAutocomplete({ 31 serviceUrl: $form.data('suggest'), 32 dataType: 'json', 33 paramName: 'tx_solr[queryString]', 34 groupBy: 'category', 35 maxHeight: 1000, 36 appendTo: $formAutoComplete, 37 autoSelectFirst: false, 38 triggerSelectOnValidInput: false, 39 width: $searchBox.outerWidth() * 0.66, 40 onSelect: function (suggestion) { 41 // go to link when selecting found result 42 if (suggestion.data.link) { 43 // Open youtube in overlay 44 if (suggestion.data.link.indexOf('https://www.youtube.com') === 0) { 45 openVideoOverlay(suggestion.data.link); 46 } else { 47 location.href = suggestion.data.link; 48 } 49 // else trigger form submit (do search) 50 } else { 51 $form.trigger('submit'); 52 } 53 }, 54 transformResult: function (response) { 55 if (!response.suggestions) return {suggestions: []}; 56 var firstSuggestion, result = { 57 suggestions: $.map(response.suggestions, function (count, suggestion) { 58 if (!firstSuggestion) firstSuggestion = suggestion; 59 return {value: suggestion, data: {category: 'suggestion', count: count}}; 60 }) 61 }; 62 63 $.each(response.documents, function (key, value) { 64 var dataObject = value; 65 66 var defaultGroup = $form.data('suggest-header') ? $form.data('suggest-header') : 'Top results'; 67 dataObject.category = defaultGroup; 68 69 // if a group is set we try to get a label 70 if(dataObject.group) { 71 dataObject.category = $form.data('suggest-header-' + dataObject.group) ? $form.data('suggest-header-' + dataObject.group) : dataObject.group; 72 } 73 74 result.suggestions.push( 75 { 76 value: firstSuggestion, 77 data: dataObject 78 } 79 ); 80 }); 81 82 return result; 83 }, 84 beforeRender: function (container) { 85 // remove first group header 86 container.find('.autocomplete-group:first').remove(); 87 container.addClass('tx-solr-autosuggest'); 88 89 // add active class to container 90 $searchBox.parent().addClass('autocomplete-active').fadeIn(); 91 }, 92 formatResult: function (suggestion, currentValue) {
93 // Do not replace anything if there current value is empty 94 if (!currentValue) { 95 return suggestion.value; 96 } 97 var pattern = '(' + $.Autocomplete.utils.escapeRegExChars(currentValue.trim()) + ')'; 98 // normal suggestion 99 if (suggestion.data.category === 'suggestion') { 100 return suggestion.value 101 .replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>') 102 .replace(/&/g, '&') 103 .replace(/</g, '<') 104 .replace(/>/g, '>') 105 .replace(/"/g, '"') 106 .replace(/<(\/?strong)>/g, '<$1>'); 107 108 // results 109 } else { 110 var title = suggestion.data.title 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 118 return '<div class="' + suggestion.data.type + '">' + 119 (!!suggestion.data.previewImage ? '<figure ' + (!!suggestion.data.hasVideo ? 'class="hasVideo"' : '') + '>
119<img src="' + suggestion.data.previewImage + '" /></figure>' : '') + 120 '<a href="' + suggestion.data.link + '" class="internal-link">' + title + '</a>' + 121 '</div>'; 122 } 123 124 } 125 }).on('blur', function () { 126 $searchBox.parent().removeClass('autocomplete-active'); 127 var $box = $(this); 128 setTimeout(function () { 129 $box.devbridgeAutocomplete('hide'); 130 }, 200); 131 }); 132 }); 133 }; 134} 135 136jQuery(document).ready(function() { 137 /** solr search autocomplete **/ 138 var solrSuggestController = new SuggestController(); 139 solrSuggestController.init(); 140 141 jQuery("body").on("tx_solr_updated", function() { 142 solrSuggestController.init(); 143 }); 144}); 145
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.