1;(function($) { 2 $.fn.sayt = function(options) { 3 this.options = {}; 4 5 //default settings 6 $.fn.sayt.defaultOptions = { 7 placeholderText: "Start typing to get results...", 8 ajaxUrl: "test", 9 simple: true, 10 showIcons: false, 11 prevVal: "", 12 defaults: [], 13 openCallback: undefined, 14 selectCallback: undefined, 15 focusCallback : undefined 16 }; 17 18 this.init = function(options) { 19 this.options = $.extend({}, $.fn.sayt.defaultOptions, options); 20 // privatefn(this.options); 21 //$.fn.sayt.greet("x"); 22 //console.log("SAYT init", options); 23 }; 24 25 this.init(options); 26 27 this.each(function() { 28 var element = $(this); 29 var saytOptions = options; 30 31 //sth 32 //reset search field and focus on load 33 $(element).autocomplete({ 34 autoFocus: false, 35 minLength: 2, 36 source: function( request, response ) { 37 $.ajax({ 38 method: "GET", 39 url: options.ajaxUrl, 40 dataType: "json", 41 data: { 42 searchterm: request.term 43 }, 44 success: function( data ) { 45 if (data.state == "SUCCESS") { 46 var schools = data.schools; 47 var item = []; 48 var len = schools.length; 49 if (len === 1) { 50 $(element).autocomplete("option", "autoFocus", true); 51 } else { 52 $(element).autocomplete("option", "autoFocus", false); 53 } 54 for (var i = 0; i < len; i++) { 55 var obj = { 56 id: schools[i].id, 57 url: schools[i].schoolUrl, 58 imagesource: schools[i].imageUrl, 59 pupilcount: schools[i].pupilcount, 60 label: schools[i].name, 61 schooltype: schools[i].schoolType, 62 city: schools[i].city 63 }; 64 item.push(obj); 65 } 66 response(item); 67 } 68 } 69 }); 70 }, 71 72 focus: function(event, ui) { 73 //console.log("focus"); 74 //event.preventDefault(); // without this: keyboard movements reset the input to '' 75 var $element = $("input#F_schoolname"); 76 77 if ($element.hasClass("event-saytstart")) { 78 $element.removeClass("event-saytstart"); 79 80 var options = $.extend($("body").data(), $element.data()); 81 if (typeof sfTracking !== "undefined") { 82 sfTracking.trackEvent(options.eventCategory, "School SAYT starts", options.eventLabel, options.eventValue, true); 83 } 84 }; 85 if (typeof saytOptions.focusCallback === "function") { 86 saytOptions.focusCallback(ui.item); 87 } 88 }, 89 90 change: function(event, ui) { 91 //console.log("change"); 92 }, 93 94 open: function(event, ui) { 95 //console.log("open"); 96 $($(this).data("clearable")).val(""); 97 98 if (typeof saytOptions.openCallback === "function") { 99 saytOptions.openCallback(ui.item); 100 } 101 }, 102 103 create: function( event, ui ) { 104 //console.log("create"); 105 }, 106 107 select: function(event, ui) { 108 //console.log("select"); 109 event.preventDefault(); 110 111 // save the selected schoolid into form 112 $("input[name='schoolId']").val(ui.item.id); 113 114 //need to force the value manually in the text field as preventDefault() blocks this, but we need it to avoid the page jumping on selection 115 if (ui.item.city != "") { 116 var labelAndInfo = ui.item.label + ", " + ui.item.city; 117 $(element).val(labelAndInfo); 118 } else { 119 $(element).val(ui.item.value); 120 } 121 122 //$(element).blur(); 123 124 // EVENT Tracking 125 var $element = $("input#F_schoolname"); 126 127 if ($element.hasClass("event-saytselect")) { 128 $element.removeClass("event-saytselect"); 129 130 var options = $.extend($("body").data(), $element.data()); 131 if (typeof sfTracking !== "undefined") { 132 sfTracking.trackEvent(options.eventCategory, "User selects school from SAYT proposal list", options.eventLabel, options.eventValue, true); 133 } 134 }; 135 136 if (typeof saytOptions.selectCallback === "function") { 137 saytOptions.selectCallback(ui.item); 138 } 139 }, 140 141 appendTo: $('.search-snippet-container').length > 0 ? '.search-snippet-container' : '' 142 143 }); 144 145 $(window).on('orientationchange', function() { 146 $(element).autocomplete('close'); 147 }); 148 149 // render autocomplete element 150 $(element).data("ui-autocomplete")._renderMenu = function(ul, items) { 151 var self = this; 152 var total = options.defaults.concat(items); 153 154 $.each(total, function(index, item) { 155 // display item 156 self._renderItemData(ul, item); 157 }); 158 }; 159 160 // render each item 161 $(element).data("ui-autocomplete")._renderItemData = function(ul, item) { 162 var searchTerms = $.trim($(this).get(0).term).split(" "); 163 var regExStr = searchTerms.join("|"); 164 165 var schoolName = item.label || ""; 166 var schoolType = item.schooltype || ""; 167 var schoolCity = item.city || ""; 168 var schooltime = item.schooltime || ""; 169 170 var regEx = new RegExp("(" + regExStr + ")", "ig"); 171 schoolName = schoolName.replace(regEx, "<em>$1</em>"); 172 schoolType = schoolType.replace(regEx, "<em>$1</em>"); 173 schoolCity = schoolCity.replace(regEx, "<em>$1</em>"); 174 175 var itemHtmlRow = $("<li></li>"); 176 itemHtmlRow.data("ui-autocomplete-item", item); 177 178 if (item.pre) { 179 itemHtmlRow.addClass("saytResultPre"); 180 } 181 182 var tracking = ''; 183 if ($(element).hasClass('result-event-click')) { 184 var trackingCategory = $(element).data('resultEventCategory'); 185 var trackingAction = $(element).data('resultEventAction'); 186 var trackingLabel = $(element).data('resultEventLabel'); 187 tracking = 'class="event-click" data-event-category="' + trackingCategory + '" data-event-action="' + trackingAction + '" data-event-label="' + trackingLabel + '"'; 188 } 189 190 if (item.label !== '' && item.city !== '') { 191 // display item 192 if (!options.simple && item.imagesource) { 193 itemHtmlRow.append("<a href='" + item.url + "' " + tracking + "><div class='saytResult'><img src='" + item.imagesource + "' /><div class='container'><div class='name'>" + schoolName + "</div><div class='info'>" + schoolType + " - " + schoolCity + "</div></div></div></a>"); 194 } else { 195 itemHtmlRow.append("<a href='#' " + tracking + "><div class='saytResult'><div class='container'><div class='name'>" + schoolName + "</div><div class='info'>" + schoolType + (schoolType != "" ? " - " : "") + schooltime + (schooltime != "" ? " - " : "") + schoolCity + "</div></div></div></a>"); 196 } 197 } else { 198 //display no result row 199 itemHtmlRow.append("<a href='" + item.url + "' " + tracking + "><div class='saytResult'><div class='container'><div class='noresult'>" + schoolName + "</div></div></div></a>"); 200 } 201 itemHtmlRow.appendTo(ul); 202 203 return itemHtmlRow; 204 }; 205 }); 206 207 return this; 208 }; 209}(jQuery));
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.