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 $('#' + this.mainContainerId).css({ top: (offset.top + this.el.innerHeight()) + 'px', left: (offset.left - this.options.width + this.el.innerWidth()) + 'px' }); 121 }, 122 123 enableKillerFn: function() { 124 var me = this; 125 $(document).bind('click', me.killerFn); 126 }, 127 128 disableKillerFn: function() { 129 var me = this; 130 $(document).unbind('click', me.killerFn); 131 }, 132
133 killSuggestions: function() { 134 var me = this; 135 this.stopKillSuggestions(); 136 this.intervalId = window.setInterval(function() { me.hide(); me.stopKillSuggestions(); }, 300); 137 }, 138 139 stopKillSuggestions: function() { 140 window.clearInterval(this.intervalId); 141 }, 142 143 onKeyPress: function(e) { 144 if (this.disabled || !this.enabled) { return; } 145 // return will exit the function 146 // and event will not be prevented 147 switch (e.keyCode) { 148 case 27: //KEY_ESC: 149 this.el.val(this.currentValue); 150 this.hide(); 151 break; 152 case 9: //KEY_TAB: 153 case 13: //KEY_RETURN: 154 if (this.selectedIndex === -1) { 155 this.hide(); 156 return; 157 } 158 this.select(this.selectedIndex); 159 if(e.keyCode === 9){ return; } 160 break; 161 case 38: //KEY_UP: 162 this.moveUp(); 163 break; 164 case 40: //KEY_DOWN: 165 this.moveDown(); 166 break; 167 default: 168 return; 169 } 170 e.stopImmediatePropagation(); 171 e.preventDefault(); 172 }, 173 174 onKeyUp: function(e) { 175 if(this.disabled){ return; } 176 switch (e.keyCode) { 177 case 38: //KEY_UP: 178 case 40: //KEY_DOWN: 179 return; 180 } 181 clearInterval(this.onChangeInterval); 182 if (this.currentValue !== this.el.val()) { 183 if (this.options.deferRequestBy > 0) { 184 // Defer lookup in case when value changes very quickly: 185 var me = this; 186 this.onChangeInterval = setInterval(function() { me.onValueChange(); }, this.options.deferRequestBy); 187 } else { 188 this.onValueChange(); 189 } 190 } 191 }, 192 193 onValueChange: function() { 194 clearInterval(this.onChangeInterval); 195 this.currentValue = this.el.val(); 196 var q = this.getQuery(this.currentValue); 197 this.selectedIndex = -1; 198 if (this.ignoreValueChange) { 199 this.ignoreValueChange = false; 200 return; 201 } 202 if (q === '' || q.length < this.options.minChars) { 203 this.hide(); 204 } else {
205 this.getSuggestions(q); 206 } 207 }, 208 209 getQuery: function(val) { 210 var d, arr; 211 d = this.options.delimiter; 212 if (!d) { return $.trim(val); } 213 arr = val.split(d); 214 return $.trim(arr[arr.length - 1]); 215 }, 216 217 getSuggestionsLocal: function(q) { 218 var ret, arr, len, val, i; 219 arr = this.options.lookup; 220 len = arr.suggestions.length; 221 ret = { suggestions:[], data:[] }; 222 q = q.toLowerCase(); 223 for(i=0; i< len; i++){ 224 val = arr.suggestions[i]; 225 if(val.toLowerCase().indexOf(q) === 0){ 226 ret.suggestions.push(val); 227 ret.data.push(arr.data[i]); 228 } 229 } 230 return ret; 231 }, 232 233 getSuggestions: function(q) { 234 var cr, me; 235 cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; 236 if (cr && $.isArray(cr.suggestions)) { 237 this.suggestions = cr.suggestions; 238 this.data = cr.data; 239 this.suggest(); 240 } else if (!this.isBadQuery(q)) { 241 me = this; 242 me.options.params.query = q; 243 $.get(this.serviceUrl, me.options.params, function(txt) { me.processResponse(txt); }, 'text'); 244 } 245 }, 246 247 isBadQuery: function(q) { 248 var i = this.badQueries.length; 249 while (i--) { 250 if (q.indexOf(this.badQueries[i]) === 0) { return true; } 251 } 252 return false; 253 }, 254 255 hide: function() { 256 this.enabled = false; 257 this.selectedIndex = -1; 258 this.container.hide(); 259 }, 260 261 suggest: function() { 262 if (this.suggestions.length === 0) { 263 this.hide(); 264 return; 265 } 266 267 var me, len, div, f, v, i, s, mOver, mClick; 268 me = this; 269 len = this.suggestions.length; 270 f = this.options.fnFormatResult; 271 v = this.getQuery(this.currentValue); 272 mOver = function(xi) { return function() { me.activate(xi); }; }; 273 mClick = function(xi) { return function() { me.select(xi); }; }; 274 this.container.hide().empty(); 275 for (i = 0; i < len; i++) { 276 s = this.suggestions[i]; 277 div = $((me.selectedIndex === i ? '<div class="selected"' : '<div') + ' title="' + s + '">' + f(s, this.data[i], v) + '</div>'); 278 div.mouseover(mOver(i)); 279 div.click(mClick(i)); 280 this.container.append(div); 281 } 282 this.enabled = true; 283 this.container.show(); 284 }, 285 286 processResponse: function(text) { 287 var response; 288 try { 289 response = eval('(' + text + ')'); 290 } catch (err) { return; } 291 if (!$.isArray(response.data)) { response.data = []; } 292 if(!this.options.noCache){ 293 this.cachedResponse[response.query] = response; 294 if (response.suggestions.length === 0) { this.badQueries.push(response.query); } 295 } 296 if (response.query === this.getQuery(this.currentValue)) { 297 this.suggestions = response.suggestions; 298 this.data = response.data; 299 this.suggest(); 300 } 301 }, 302 303 activate: function(index) { 304 var divs, activeItem; 305 divs = this.container.children(); 306 // Clear previous selection: 307 if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) { 308 $(divs.get(this.selectedIndex)).removeClass(); 309 } 310 this.selectedIndex = index; 311 if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) { 312 activeItem = divs.get(this.selectedIndex); 313 $(activeItem).addClass('selected'); 314 } 315 return activeItem; 316 }, 317 318 deactivate: function(div, index) { 319 div.className = ''; 320 if (this.selectedIndex === index) { this.selectedIndex = -1; } 321 }, 322 323 select: function(i) { 324 var selectedValue, f; 325 selectedValue = this.suggestions[i]; 326 if (selectedValue) { 327 this.el.val(selectedValue); 328 if (this.options.autoSubmit) { 329 f = this.el.parents('form'); 330 if (f.length > 0) { f.get(0).submit(); } 331 } 332 this.ignoreValueChange = true; 333 this.hide(); 334 this.onSelect(i); 335 } 336 }, 337 338 moveUp: function() { 339 if (this.selectedIndex === -1) { return; } 340 if (this.selectedIndex === 0) { 341 this.container.children().get(0).className = ''; 342 this.selectedIndex = -1; 343 this.el.val(this.currentValue); 344 return; 345 } 346 this.adjustScroll(this.selectedIndex - 1); 347 }, 348 349 moveDown: function() { 350 if (this.selectedIndex === (this.suggestions.length - 1)) { return; } 351 this.adjustScroll(this.selectedIndex + 1); 352 }, 353 354 adjustScroll: function(i) { 355 var activeItem, offsetTop, upperBound, lowerBound; 356 activeItem = this.activate(i); 357 offsetTop = activeItem.offsetTop; 358 upperBound = this.container.scrollTop(); 359 lowerBound = upperBound + this.options.maxHeight - 25; 360 if (offsetTop < upperBound) { 361 this.container.scrollTop(offsetTop); 362 } else if (offsetTop > lowerBound) { 363 this.container.scrollTop(offsetTop - this.options.maxHeight + 25); 364 } 365 this.el.val(this.getValue(this.suggestions[i])); 366 }, 367 368 onSelect: function(i) { 369 var me, fn, s, d; 370 me = this; 371 fn = me.options.onSelect; 372 s = me.suggestions[i]; 373 d = me.data[i]; 374 me.el.val(me.getValue(s)); 375 if ($.isFunction(fn)) { fn(s, d, me.el); } 376 }, 377 378 getValue: function(value){ 379 var del, currVal, arr, me; 380 me = this; 381 del = me.options.delimiter; 382 if (!del) { return value; } 383 currVal = me.currentValue; 384 arr = currVal.split(del); 385 if (arr.length === 1) { return value; } 386 return currVal.substr(0, currVal.length - arr[arr.length - 1].length) + value; 387 } 388 389 }; 390 391}(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.