1(function($) { 2 3 var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g'); 4 5 function fnFormatResult(value, data, currentValue) { 6 var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')'; 7 return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>'); 8 } 9 10 function Autocomplete(el, options) { 11 this.el = $(el); 12 this.el.attr('autocomplete', 'off'); 13 this.suggestions = []; 14 this.data = []; 15 this.badQueries = []; 16 this.selectedIndex = -1; 17 this.currentValue = this.el.val(); 18 this.intervalId = 0; 19 this.cachedResponse = []; 20 this.onChangeInterval = null; 21 this.ignoreValueChange = false; 22 this.serviceUrl = options.serviceUrl; 23 this.isLocal = false; 24 this.options = { 25 autoSubmit: false, 26 minChars: 1, 27 maxHeight: 300, 28 deferRequestBy: 0, 29 width: 0, 30 highlight: true, 31 params: {}, 32 fnFormatResult: fnFormatResult, 33 delimiter: null, 34 zIndex: 9999 35 }; 36 this.initialize(); 37 this.setOptions(options); 38 } 39 40 $.fn.autocomplete = function(options) { 41 return new Autocomplete(this.get(0)||$('<input />'), options); 42 }; 43 44 45 Autocomplete.prototype = { 46 47 killerFn: null, 48 49 initialize: function() { 50 51 var me, uid, autocompleteElId; 52 me = this; 53 uid = Math.floor(Math.random()*0x100000).toString(16); 54 autocompleteElId = 'Autocomplete_' + uid; 55 56 this.killerFn = function(e) { 57 if ($(e.target).parents('.autocomplete').size() === 0) { 58 me.killSuggestions(); 59 me.disableKillerFn(); 60 } 61 }; 62 63 if (!this.options.width) { this.options.width = this.el.width(); } 64 this.mainContainerId = 'AutocompleteContainter_' + uid; 65 66 $('<div id="' + this.mainContainerId + '" style="position:absolute;z-index:9999;"><div class="autocomplete-w1"><div class="autocomplete" id="' + autocompleteElId + '" style="display:none; width:300px;"></div></div></div>').appendTo('body'); 67 68 this.container = $('#' + autocompleteElId); 69 this.fixPosition(); 70 if (window.opera) { 71 this.el.keypress(function(e) { me.onKeyPress(e); }); 72 } else { 73 this.el.keydown(function(e) { me.onKeyPress(e); }); 74 } 75 this.el.keyup(function(e) { me.onKeyUp(e); }); 76 this.el.blur(function() { me.enableKillerFn(); }); 77 this.el.focus(function() { me.fixPosition(); }); 78 }, 79 80 setOptions: function(options){ 81 var o = this.options; 82 $.extend(o, options); 83 if(o.lookup){ 84 this.isLocal = true; 85 if($.isArray(o.lookup)){ o.lookup = { suggestions:o.lookup, data:[] }; } 86 } 87 $('#'+this.mainContainerId).css({ zIndex:o.zIndex }); 88 this.container.css({ maxHeight: o.maxHeight + 'px', width:o.width }); 89 }, 90 91 clearCache: function(){ 92 this.cachedResponse = []; 93 this.badQueries = []; 94 }, 95 96 disable: function(){ 97 this.disabled = true; 98 }, 99 100 enable: function(){ 101 this.disabled = false; 102 }, 103 104 fixPosition: function() { 105 var offset = this.el.offset(); 106 $('#' + this.mainContainerId).css({ top: (offset.top + this.el.innerHeight()) + 'px', left: offset.left + 'px' }); 107 }, 108 109 enableKillerFn: function() { 110 var me = this; 111 $(document).bind('click', me.killerFn); 112 }, 113 114 disableKillerFn: function() { 115 var me = this; 116 $(document).unbind('click', me.killerFn); 117 }, 118 119 killSuggestions: function() { 120 var me = this; 121 this.stopKillSuggestions(); 122 this.intervalId = window.setInterval(function() { me.hide(); me.stopKillSuggestions(); }, 300); 123 }, 124 125 stopKillSuggestions: function() { 126 window.clearInterval(this.intervalId); 127 }, 128 129 onKeyPress: function(e) { 130 if (this.disabled || !this.enabled) { return; } 131 // return will exit the function 132 // and event will not be prevented 133 switch (e.keyCode) { 134 case 27: //KEY_ESC: 135 this.el.val(this.currentValue); 136 this.hide(); 137 break; 138 case 9: //KEY_TAB: 139 case 13: //KEY_RETURN: 140 if (this.selectedIndex === -1) { 141 this.hide(); 142 return; 143 } 144 this.select(this.selectedIndex); 145 if(e.keyCode === 9){ return; } 146 break; 147 case 38: //KEY_UP: 148 this.moveUp(); 149 break; 150 case 40: //KEY_DOWN: 151 this.moveDown(); 152 break; 153 default: 154 return; 155 } 156 e.stopImmediatePropagation(); 157 e.preventDefault(); 158 }, 159 160 onKeyUp: function(e) { 161 if(this.disabled){ return; } 162 switch (e.keyCode) { 163 case 38: //KEY_UP: 164 case 40: //KEY_DOWN: 165 return; 166 } 167 clearInterval(this.onChangeInterval); 168 if (this.currentValue !== this.el.val()) { 169 if (this.options.deferRequestBy > 0) { 170 // Defer lookup in case when value changes very quickly: 171 var me = this; 172 this.onChangeInterval = setInterval(function() { me.onValueChange(); }, this.options.deferRequestBy); 173 } else { 174 this.onValueChange(); 175 } 176 } 177 }, 178 179 onValueChange: function() { 180 clearInterval(this.onChangeInterval); 181 this.currentValue = this.el.val(); 182 var q = this.getQuery(this.currentValue); 183 this.selectedIndex = -1; 184 if (this.ignoreValueChange) { 185 this.ignoreValueChange = false; 186 return; 187 } 188 if (q === '' || q.length < this.options.minChars) { 189 this.hide(); 190 } else {
191 this.getSuggestions(q); 192 } 193 }, 194 195 getQuery: function(val) { 196 var d, arr; 197 d = this.options.delimiter; 198 if (!d) { return $.trim(val); } 199 arr = val.split(d); 200 return $.trim(arr[arr.length - 1]); 201 }, 202 203 getSuggestionsLocal: function(q) { 204 var ret, arr, len, val, i; 205 arr = this.options.lookup; 206 len = arr.suggestions.length; 207 ret = { suggestions:[], data:[] }; 208 q = q.toLowerCase(); 209 for(i=0; i< len; i++){ 210 val = arr.suggestions[i]; 211 if(val.toLowerCase().indexOf(q) === 0){ 212 ret.suggestions.push(val); 213 ret.data.push(arr.data[i]); 214 } 215 } 216 return ret; 217 }, 218 219 getSuggestions: function(q) { 220 var cr, me; 221 cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; 222 if (cr && $.isArray(cr.suggestions)) { 223 this.suggestions = cr.suggestions; 224 this.data = cr.data; 225 this.suggest(); 226 } else if (!this.isBadQuery(q)) { 227 me = this; 228 me.options.params.query = q; 229 $.get(this.serviceUrl, me.options.params, function(txt) { me.processResponse(txt); }, 'text'); 230 } 231 }, 232 233 isBadQuery: function(q) { 234 var i = this.badQueries.length; 235 while (i--) { 236 if (q.indexOf(this.badQueries[i]) === 0) { return true; } 237 } 238 return false; 239 }, 240 241 hide: function() { 242 this.enabled = false; 243 this.selectedIndex = -1; 244 this.container.hide(); 245 }, 246 247 suggest: function() { 248 if (this.suggestions.length === 0) { 249 this.hide(); 250 return; 251 } 252 253 var me, len, div, f, v, i, s, mOver, mClick; 254 me = this; 255 len = this.suggestions.length; 256 f = this.options.fnFormatResult; 257 v = this.getQuery(this.currentValue); 258 mOver = function(xi) { return function() { me.activate(xi); }; }; 259 mClick = function(xi) { return function() { me.select(xi); }; }; 260 this.container.hide().empty(); 261 for (i = 0; i < len; i++) { 262 s = this.suggestions[i]; 263 div = $((me.selectedIndex === i ? '<div class="selected"' : '<div') + ' title="' + s + '">' + f(s, this.data[i], v) + '</div>'); 264 div.mouseover(mOver(i)); 265 div.click(mClick(i)); 266 this.container.append(div); 267 } 268 this.enabled = true; 269 this.container.show(); 270 }, 271 272 processResponse: function(text) { 273 var response; 274 try { 275 response = eval('(' + text + ')'); 276 } catch (err) { return; } 277 if (!$.isArray(response.data)) { response.data = []; } 278 if(!this.options.noCache){ 279 this.cachedResponse[response.query] = response; 280 if (response.suggestions.length === 0) { this.badQueries.push(response.query); } 281 } 282 if (response.query === this.getQuery(this.currentValue)) { 283 this.suggestions = response.suggestions; 284 this.data = response.data; 285 this.suggest(); 286 } 287 }, 288 289 activate: function(index) { 290 var divs, activeItem; 291 divs = this.container.children(); 292 // Clear previous selection: 293 if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) { 294 $(divs.get(this.selectedIndex)).removeClass(); 295 } 296 this.selectedIndex = index; 297 if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) { 298 activeItem = divs.get(this.selectedIndex); 299 $(activeItem).addClass('selected'); 300 } 301 return activeItem; 302 }, 303 304 deactivate: function(div, index) { 305 div.className = ''; 306 if (this.selectedIndex === index) { this.selectedIndex = -1; } 307 }, 308 309 select: function(i) { 310 var selectedValue, f; 311 selectedValue = this.suggestions[i]; 312 if (selectedValue) { 313 this.el.val(selectedValue); 314 if (this.options.autoSubmit) { 315 f = this.el.parents('form'); 316 if (f.length > 0) { f.get(0).submit(); } 317 } 318 this.ignoreValueChange = true; 319 this.hide(); 320 this.onSelect(i); 321 } 322 }, 323 324 moveUp: function() { 325 if (this.selectedIndex === -1) { return; } 326 if (this.selectedIndex === 0) { 327 this.container.children().get(0).className = ''; 328 this.selectedIndex = -1; 329 this.el.val(this.currentValue); 330 return; 331 } 332 this.adjustScroll(this.selectedIndex - 1); 333 }, 334 335 moveDown: function() { 336 if (this.selectedIndex === (this.suggestions.length - 1)) { return; } 337 this.adjustScroll(this.selectedIndex + 1); 338 }, 339 340 adjustScroll: function(i) { 341 var activeItem, offsetTop, upperBound, lowerBound; 342 activeItem = this.activate(i); 343 offsetTop = activeItem.offsetTop; 344 upperBound = this.container.scrollTop(); 345 lowerBound = upperBound + this.options.maxHeight - 25; 346 if (offsetTop < upperBound) { 347 this.container.scrollTop(offsetTop); 348 } else if (offsetTop > lowerBound) { 349 this.container.scrollTop(offsetTop - this.options.maxHeight + 25); 350 } 351 this.el.val(this.getValue(this.suggestions[i])); 352 }, 353 354 onSelect: function(i) { 355 var me, fn, s, d; 356 me = this; 357 fn = me.options.onSelect; 358 s = me.suggestions[i]; 359 d = me.data[i]; 360 me.el.val(me.getValue(s)); 361 if ($.isFunction(fn)) { fn(s, d, me.el); } 362 }, 363 364 getValue: function(value){ 365 var del, currVal, arr, me; 366 me = this; 367 del = me.options.delimiter; 368 if (!del) { return value; } 369 currVal = me.currentValue; 370 arr = currVal.split(del); 371 if (arr.length === 1) { return value; } 372 return currVal.substr(0, currVal.length - arr[arr.length - 1].length) + value; 373 } 374 375 }; 376 377}(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.