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