~ubuntu-branches/ubuntu/quantal/xen-api/quantal

« back to all changes in this revision

Viewing changes to javascript/jquery/jquery.autocomplete.js

  • Committer: Package Import Robot
  • Author(s): Jon Ludlam
  • Date: 2011-07-07 21:50:18 UTC
  • Revision ID: package-import@ubuntu.com-20110707215018-3t9ekbh7qy5y2b1p
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Autocomplete - jQuery plugin 1.0
 
3
 *
 
4
 * Copyright (c) 2007 Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, Jƶrn Zaefferer
 
5
 *
 
6
 * Dual licensed under the MIT and GPL licenses:
 
7
 *   http://www.opensource.org/licenses/mit-license.php
 
8
 *   http://www.gnu.org/licenses/gpl.html
 
9
 *
 
10
 * Revision: $Id: jquery.autocomplete.js 5329 2008-04-27 13:07:34Z joern.zaefferer $
 
11
 *
 
12
 */
 
13
 
 
14
;(function($) {
 
15
        
 
16
$.fn.extend({
 
17
        autocomplete: function(urlOrData, options) {
 
18
                var isUrl = typeof urlOrData == "string";
 
19
                options = $.extend({}, $.Autocompleter.defaults, {
 
20
                        url: isUrl ? urlOrData : null,
 
21
                        data: isUrl ? null : urlOrData,
 
22
                        delay: isUrl ? $.Autocompleter.defaults.delay : 10,
 
23
                        max: options && !options.scroll ? 10 : 150
 
24
                }, options);
 
25
                
 
26
                // if highlight is set to false, replace it with a do-nothing function
 
27
                options.highlight = options.highlight || function(value) { return value; };
 
28
                
 
29
                // if the formatMatch option is not specified, then use formatItem for backwards compatibility
 
30
                options.formatMatch = options.formatMatch || options.formatItem;
 
31
                
 
32
                return this.each(function() {
 
33
                        new $.Autocompleter(this, options);
 
34
                });
 
35
        },
 
36
        result: function(handler) {
 
37
                return this.bind("result", handler);
 
38
        },
 
39
        search: function(handler) {
 
40
                return this.trigger("search", [handler]);
 
41
        },
 
42
        flushCache: function() {
 
43
                return this.trigger("flushCache");
 
44
        },
 
45
        setOptions: function(options){
 
46
                return this.trigger("setOptions", [options]);
 
47
        },
 
48
        unautocomplete: function() {
 
49
                return this.trigger("unautocomplete");
 
50
        }
 
51
});
 
52
 
 
53
$.Autocompleter = function(input, options) {
 
54
 
 
55
        var KEY = {
 
56
                UP: 38,
 
57
                DOWN: 40,
 
58
                DEL: 46,
 
59
                TAB: 9,
 
60
                RETURN: 13,
 
61
                ESC: 27,
 
62
                COMMA: 188,
 
63
                PAGEUP: 33,
 
64
                PAGEDOWN: 34,
 
65
                BACKSPACE: 8
 
66
        };
 
67
 
 
68
        // Create $ object for input element
 
69
        var $input = $(input).attr("autocomplete", "off").addClass(options.inputClass);
 
70
 
 
71
        var timeout;
 
72
        var previousValue = "";
 
73
        var cache = $.Autocompleter.Cache(options);
 
74
        var hasFocus = 0;
 
75
        var lastKeyPressCode;
 
76
        var config = {
 
77
                mouseDownOnSelect: false
 
78
        };
 
79
        var select = $.Autocompleter.Select(options, input, selectCurrent, config);
 
80
        
 
81
        $input.keydown(function(event) {
 
82
                // track last key pressed
 
83
                lastKeyPressCode = event.keyCode;
 
84
                switch(event.keyCode) {
 
85
                
 
86
                        case KEY.UP:
 
87
                                event.preventDefault();
 
88
                                if ( select.visible() ) {
 
89
                                        select.prev();
 
90
                                } else {
 
91
                                        onChange(0, true);
 
92
                                }
 
93
                                break;
 
94
                                
 
95
                        case KEY.DOWN:
 
96
                                event.preventDefault();
 
97
                                if ( select.visible() ) {
 
98
                                        select.next();
 
99
                                } else {
 
100
                                        onChange(0, true);
 
101
                                }
 
102
                                break;
 
103
                                
 
104
                        case KEY.PAGEUP:
 
105
                                event.preventDefault();
 
106
                                if ( select.visible() ) {
 
107
                                        select.pageUp();
 
108
                                } else {
 
109
                                        onChange(0, true);
 
110
                                }
 
111
                                break;
 
112
                                
 
113
                        case KEY.PAGEDOWN:
 
114
                                event.preventDefault();
 
115
                                if ( select.visible() ) {
 
116
                                        select.pageDown();
 
117
                                } else {
 
118
                                        onChange(0, true);
 
119
                                }
 
120
                                break;
 
121
                        
 
122
                        // matches also semicolon
 
123
                        case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
 
124
                        case KEY.TAB:
 
125
                        case KEY.RETURN:
 
126
                                if( selectCurrent() ){
 
127
                                        // make sure to blur off the current field
 
128
                                        if( !options.multiple )
 
129
                                                $input.blur();
 
130
                                        event.preventDefault();
 
131
                                }
 
132
                                break;
 
133
                                
 
134
                        case KEY.ESC:
 
135
                                select.hide();
 
136
                                break;
 
137
                                
 
138
                        default:
 
139
                                clearTimeout(timeout);
 
140
                                timeout = setTimeout(onChange, options.delay);
 
141
                                break;
 
142
                }
 
143
        }).keypress(function() {
 
144
                // having fun with opera - remove this binding and Opera submits the form when we select an entry via return
 
145
        }).focus(function(){
 
146
                // track whether the field has focus, we shouldn't process any
 
147
                // results if the field no longer has focus
 
148
                hasFocus++;
 
149
        }).blur(function() {
 
150
                hasFocus = 0;
 
151
                if (!config.mouseDownOnSelect) {
 
152
                        hideResults();
 
153
                }
 
154
        }).click(function() {
 
155
                // show select when clicking in a focused field
 
156
                if ( hasFocus++ > 1 && !select.visible() ) {
 
157
                        onChange(0, true);
 
158
                }
 
159
        }).bind("search", function() {
 
160
                // TODO why not just specifying both arguments?
 
161
                var fn = (arguments.length > 1) ? arguments[1] : null;
 
162
                function findValueCallback(q, data) {
 
163
                        var result;
 
164
                        if( data && data.length ) {
 
165
                                for (var i=0; i < data.length; i++) {
 
166
                                        if( data[i].result.toLowerCase() == q.toLowerCase() ) {
 
167
                                                result = data[i];
 
168
                                                break;
 
169
                                        }
 
170
                                }
 
171
                        }
 
172
                        if( typeof fn == "function" ) fn(result);
 
173
                        else $input.trigger("result", result && [result.data, result.value]);
 
174
                }
 
175
                $.each(trimWords($input.val()), function(i, value) {
 
176
                        request(value, findValueCallback, findValueCallback);
 
177
                });
 
178
        }).bind("flushCache", function() {
 
179
                cache.flush();
 
180
        }).bind("setOptions", function() {
 
181
                $.extend(options, arguments[1]);
 
182
                // if we've updated the data, repopulate
 
183
                if ( "data" in arguments[1] )
 
184
                        cache.populate();
 
185
        }).bind("unautocomplete", function() {
 
186
                select.unbind();
 
187
                $input.unbind();
 
188
        });
 
189
        
 
190
        
 
191
        function selectCurrent() {
 
192
                var selected = select.selected();
 
193
                if( !selected )
 
194
                        return false;
 
195
                
 
196
                var v = selected.result;
 
197
                previousValue = v;
 
198
                
 
199
                if ( options.multiple ) {
 
200
                        var words = trimWords($input.val());
 
201
                        if ( words.length > 1 ) {
 
202
                                v = words.slice(0, words.length - 1).join( options.multipleSeparator ) + options.multipleSeparator + v;
 
203
                        }
 
204
                        v += options.multipleSeparator;
 
205
                }
 
206
                
 
207
                $input.val(v);
 
208
                hideResultsNow();
 
209
                $input.trigger("result", [selected.data, selected.value]);
 
210
                return true;
 
211
        }
 
212
        
 
213
        function onChange(crap, skipPrevCheck) {
 
214
                if( lastKeyPressCode == KEY.DEL ) {
 
215
                        select.hide();
 
216
                        return;
 
217
                }
 
218
                
 
219
                var currentValue = $input.val();
 
220
                
 
221
                if ( !skipPrevCheck && currentValue == previousValue )
 
222
                        return;
 
223
                
 
224
                previousValue = currentValue;
 
225
                
 
226
                currentValue = lastWord(currentValue);
 
227
                if ( currentValue.length >= options.minChars) {
 
228
                        $input.addClass(options.loadingClass);
 
229
                        if (!options.matchCase)
 
230
                                currentValue = currentValue.toLowerCase();
 
231
                        request(currentValue, receiveData, hideResultsNow);
 
232
                } else {
 
233
                        stopLoading();
 
234
                        select.hide();
 
235
                }
 
236
        };
 
237
        
 
238
        function trimWords(value) {
 
239
                if ( !value ) {
 
240
                        return [""];
 
241
                }
 
242
                var words = value.split( options.multipleSeparator );
 
243
                var result = [];
 
244
                $.each(words, function(i, value) {
 
245
                        if ( $.trim(value) )
 
246
                                result[i] = $.trim(value);
 
247
                });
 
248
                return result;
 
249
        }
 
250
        
 
251
        function lastWord(value) {
 
252
                if ( !options.multiple )
 
253
                        return value;
 
254
                var words = trimWords(value);
 
255
                return words[words.length - 1];
 
256
        }
 
257
        
 
258
        // fills in the input box w/the first match (assumed to be the best match)
 
259
        // q: the term entered
 
260
        // sValue: the first matching result
 
261
        function autoFill(q, sValue){
 
262
                // autofill in the complete box w/the first match as long as the user hasn't entered in more data
 
263
                // if the last user key pressed was backspace, don't autofill
 
264
                if( options.autoFill && (lastWord($input.val()).toLowerCase() == q.toLowerCase()) && lastKeyPressCode != KEY.BACKSPACE ) {
 
265
                        // fill in the value (keep the case the user has typed)
 
266
                        $input.val($input.val() + sValue.substring(lastWord(previousValue).length));
 
267
                        // select the portion of the value not typed by the user (so the next character will erase)
 
268
                        $.Autocompleter.Selection(input, previousValue.length, previousValue.length + sValue.length);
 
269
                }
 
270
        };
 
271
 
 
272
        function hideResults() {
 
273
                clearTimeout(timeout);
 
274
                timeout = setTimeout(hideResultsNow, 200);
 
275
        };
 
276
 
 
277
        function hideResultsNow() {
 
278
                select.hide();
 
279
                clearTimeout(timeout);
 
280
                stopLoading();
 
281
                if (options.mustMatch) {
 
282
                        // call search and run callback
 
283
                        $input.search(
 
284
                                function (result){
 
285
                                        // if no value found, clear the input box
 
286
                                        if( !result ) $input.val("");
 
287
                                }
 
288
                        );
 
289
                }
 
290
        };
 
291
 
 
292
        function receiveData(q, data) {
 
293
                if ( data && data.length && hasFocus ) {
 
294
                        stopLoading();
 
295
                        select.display(data, q);
 
296
                        autoFill(q, data[0].value);
 
297
                        select.show();
 
298
                } else {
 
299
                        hideResultsNow();
 
300
                }
 
301
        };
 
302
 
 
303
        function request(term, success, failure) {
 
304
                if (!options.matchCase)
 
305
                        term = term.toLowerCase();
 
306
                var data = cache.load(term);
 
307
                // recieve the cached data
 
308
                if (data && data.length) {
 
309
                        success(term, data);
 
310
                // if an AJAX url has been supplied, try loading the data now
 
311
                } else if( (typeof options.url == "string") && (options.url.length > 0) ){
 
312
                        
 
313
                        var extraParams = {
 
314
                                timestamp: +new Date()
 
315
                        };
 
316
                        $.each(options.extraParams, function(key, param) {
 
317
                                extraParams[key] = typeof param == "function" ? param() : param;
 
318
                        });
 
319
                        
 
320
                        $.ajax({
 
321
                                // try to leverage ajaxQueue plugin to abort previous requests
 
322
                                mode: "abort",
 
323
                                // limit abortion to this input
 
324
                                port: "autocomplete" + input.name,
 
325
                                dataType: options.dataType,
 
326
                                url: options.url,
 
327
                                data: $.extend({
 
328
                                        q: lastWord(term),
 
329
                                        limit: options.max
 
330
                                }, extraParams),
 
331
                                success: function(data) {
 
332
                                        var parsed = options.parse && options.parse(data) || parse(data);
 
333
                                        cache.add(term, parsed);
 
334
                                        success(term, parsed);
 
335
                                }
 
336
                        });
 
337
                } else {
 
338
                        // if we have a failure, we need to empty the list -- this prevents the the [TAB] key from selecting the last successful match
 
339
                        select.emptyList();
 
340
                        failure(term);
 
341
                }
 
342
        };
 
343
        
 
344
        function parse(data) {
 
345
                var parsed = [];
 
346
                var rows = data.split("\n");
 
347
                for (var i=0; i < rows.length; i++) {
 
348
                        var row = $.trim(rows[i]);
 
349
                        if (row) {
 
350
                                row = row.split("|");
 
351
                                parsed[parsed.length] = {
 
352
                                        data: row,
 
353
                                        value: row[0],
 
354
                                        result: options.formatResult && options.formatResult(row, row[0]) || row[0]
 
355
                                };
 
356
                        }
 
357
                }
 
358
                return parsed;
 
359
        };
 
360
 
 
361
        function stopLoading() {
 
362
                $input.removeClass(options.loadingClass);
 
363
        };
 
364
 
 
365
};
 
366
 
 
367
$.Autocompleter.defaults = {
 
368
        inputClass: "ac_input",
 
369
        resultsClass: "ac_results",
 
370
        loadingClass: "ac_loading",
 
371
        minChars: 1,
 
372
        delay: 400,
 
373
        matchCase: false,
 
374
        matchSubset: true,
 
375
        matchContains: false,
 
376
        cacheLength: 10,
 
377
        max: 100,
 
378
        mustMatch: false,
 
379
        extraParams: {},
 
380
        selectFirst: true,
 
381
        formatItem: function(row) { return row[0]; },
 
382
        formatMatch: null,
 
383
        autoFill: false,
 
384
        width: 0,
 
385
        multiple: false,
 
386
        multipleSeparator: ", ",
 
387
        highlight: function(value, term) {
 
388
                return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
 
389
        },
 
390
    scroll: true,
 
391
    scrollHeight: 180
 
392
};
 
393
 
 
394
$.Autocompleter.Cache = function(options) {
 
395
 
 
396
        var data = {};
 
397
        var length = 0;
 
398
        
 
399
        function matchSubset(s, sub) {
 
400
                if (!options.matchCase) 
 
401
                        s = s.toLowerCase();
 
402
                var i = s.indexOf(sub);
 
403
                if (i == -1) return false;
 
404
                return i == 0 || options.matchContains;
 
405
        };
 
406
        
 
407
        function add(q, value) {
 
408
                if (length > options.cacheLength){
 
409
                        flush();
 
410
                }
 
411
                if (!data[q]){ 
 
412
                        length++;
 
413
                }
 
414
                data[q] = value;
 
415
        }
 
416
        
 
417
        function populate(){
 
418
                if( !options.data ) return false;
 
419
                // track the matches
 
420
                var stMatchSets = {},
 
421
                        nullData = 0;
 
422
 
 
423
                // no url was specified, we need to adjust the cache length to make sure it fits the local data store
 
424
                if( !options.url ) options.cacheLength = 1;
 
425
                
 
426
                // track all options for minChars = 0
 
427
                stMatchSets[""] = [];
 
428
                
 
429
                // loop through the array and create a lookup structure
 
430
                for ( var i = 0, ol = options.data.length; i < ol; i++ ) {
 
431
                        var rawValue = options.data[i];
 
432
                        // if rawValue is a string, make an array otherwise just reference the array
 
433
                        rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue;
 
434
                        
 
435
                        var value = options.formatMatch(rawValue, i+1, options.data.length);
 
436
                        if ( value === false )
 
437
                                continue;
 
438
                                
 
439
                        var firstChar = value.charAt(0).toLowerCase();
 
440
                        // if no lookup array for this character exists, look it up now
 
441
                        if( !stMatchSets[firstChar] ) 
 
442
                                stMatchSets[firstChar] = [];
 
443
 
 
444
                        // if the match is a string
 
445
                        var row = {
 
446
                                value: value,
 
447
                                data: rawValue,
 
448
                                result: options.formatResult && options.formatResult(rawValue) || value
 
449
                        };
 
450
                        
 
451
                        // push the current match into the set list
 
452
                        stMatchSets[firstChar].push(row);
 
453
 
 
454
                        // keep track of minChars zero items
 
455
                        if ( nullData++ < options.max ) {
 
456
                                stMatchSets[""].push(row);
 
457
                        }
 
458
                };
 
459
 
 
460
                // add the data items to the cache
 
461
                $.each(stMatchSets, function(i, value) {
 
462
                        // increase the cache size
 
463
                        options.cacheLength++;
 
464
                        // add to the cache
 
465
                        add(i, value);
 
466
                });
 
467
        }
 
468
        
 
469
        // populate any existing data
 
470
        setTimeout(populate, 25);
 
471
        
 
472
        function flush(){
 
473
                data = {};
 
474
                length = 0;
 
475
        }
 
476
        
 
477
        return {
 
478
                flush: flush,
 
479
                add: add,
 
480
                populate: populate,
 
481
                load: function(q) {
 
482
                        if (!options.cacheLength || !length)
 
483
                                return null;
 
484
                        /* 
 
485
                         * if dealing w/local data and matchContains than we must make sure
 
486
                         * to loop through all the data collections looking for matches
 
487
                         */
 
488
                        if( !options.url && options.matchContains ){
 
489
                                // track all matches
 
490
                                var csub = [];
 
491
                                // loop through all the data grids for matches
 
492
                                for( var k in data ){
 
493
                                        // don't search through the stMatchSets[""] (minChars: 0) cache
 
494
                                        // this prevents duplicates
 
495
                                        if( k.length > 0 ){
 
496
                                                var c = data[k];
 
497
                                                $.each(c, function(i, x) {
 
498
                                                        // if we've got a match, add it to the array
 
499
                                                        if (matchSubset(x.value, q)) {
 
500
                                                                csub.push(x);
 
501
                                                        }
 
502
                                                });
 
503
                                        }
 
504
                                }                               
 
505
                                return csub;
 
506
                        } else 
 
507
                        // if the exact item exists, use it
 
508
                        if (data[q]){
 
509
                                return data[q];
 
510
                        } else
 
511
                        if (options.matchSubset) {
 
512
                                for (var i = q.length - 1; i >= options.minChars; i--) {
 
513
                                        var c = data[q.substr(0, i)];
 
514
                                        if (c) {
 
515
                                                var csub = [];
 
516
                                                $.each(c, function(i, x) {
 
517
                                                        if (matchSubset(x.value, q)) {
 
518
                                                                csub[csub.length] = x;
 
519
                                                        }
 
520
                                                });
 
521
                                                return csub;
 
522
                                        }
 
523
                                }
 
524
                        }
 
525
                        return null;
 
526
                }
 
527
        };
 
528
};
 
529
 
 
530
$.Autocompleter.Select = function (options, input, select, config) {
 
531
        var CLASSES = {
 
532
                ACTIVE: "ac_over"
 
533
        };
 
534
        
 
535
        var listItems,
 
536
                active = -1,
 
537
                data,
 
538
                term = "",
 
539
                needsInit = true,
 
540
                element,
 
541
                list;
 
542
        
 
543
        // Create results
 
544
        function init() {
 
545
                if (!needsInit)
 
546
                        return;
 
547
            element = $($.create("div",{ns:xhtmlns},[]))
 
548
                .hide()
 
549
                .addClass(options.resultsClass)
 
550
                .css("position", "absolute")
 
551
                .appendTo(document.body);
 
552
        
 
553
                list = $($.create("ul",{ns:xhtmlns},[])).appendTo(element).mouseover( function(event) {
 
554
                        if(target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') {
 
555
                    active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event));
 
556
                            $(target(event)).addClass(CLASSES.ACTIVE);            
 
557
                }
 
558
                }).click(function(event) {
 
559
                        $(target(event)).addClass(CLASSES.ACTIVE);
 
560
                        select();
 
561
                        input.focus();
 
562
                        return false;
 
563
                }).mousedown(function() {
 
564
                        config.mouseDownOnSelect = true;
 
565
                }).mouseup(function() {
 
566
                        config.mouseDownOnSelect = false;
 
567
                });
 
568
                
 
569
                if( options.width > 0 )
 
570
                        element.css("width", options.width);
 
571
                        
 
572
                needsInit = false;
 
573
        } 
 
574
        
 
575
        function target(event) {
 
576
                var element = event.target;
 
577
                while(element && element.tagName != "LI")
 
578
                        element = element.parentNode;
 
579
                // more fun with IE, sometimes event.target is empty, just ignore it then
 
580
                if(!element)
 
581
                        return [];
 
582
                return element;
 
583
        }
 
584
 
 
585
        function moveSelect(step) {
 
586
                listItems.slice(active, active + 1).removeClass(CLASSES.ACTIVE);
 
587
                movePosition(step);
 
588
        var activeItem = listItems.slice(active, active + 1).addClass(CLASSES.ACTIVE);
 
589
        if(options.scroll) {
 
590
            var offset = 0;
 
591
            listItems.slice(0, active).each(function() {
 
592
                                offset += this.offsetHeight;
 
593
                        });
 
594
            if((offset + activeItem[0].offsetHeight - list.scrollTop()) > list[0].clientHeight) {
 
595
                list.scrollTop(offset + activeItem[0].offsetHeight - list.innerHeight());
 
596
            } else if(offset < list.scrollTop()) {
 
597
                list.scrollTop(offset);
 
598
            }
 
599
        }
 
600
        };
 
601
        
 
602
        function movePosition(step) {
 
603
                active += step;
 
604
                if (active < 0) {
 
605
                        active = listItems.size() - 1;
 
606
                } else if (active >= listItems.size()) {
 
607
                        active = 0;
 
608
                }
 
609
        }
 
610
        
 
611
        function limitNumberOfItems(available) {
 
612
                return options.max && options.max < available
 
613
                        ? options.max
 
614
                        : available;
 
615
        }
 
616
        
 
617
        function fillList() {
 
618
                list.empty();
 
619
                var max = limitNumberOfItems(data.length);
 
620
                for (var i=0; i < max; i++) {
 
621
                        if (!data[i])
 
622
                                continue;
 
623
                        var formatted = options.formatItem(data[i].data, i+1, max, data[i].value, term);
 
624
                        if ( formatted === false )
 
625
                                continue;
 
626
                    var li = $($.create("li",{ns:xhtmlns},[])).html( options.highlight(formatted, term) ).addClass(i%2 == 0 ? "ac_event" : "ac_odd").appendTo(list)[0];
 
627
                        $.data(li, "ac_data", data[i]);
 
628
                }
 
629
                listItems = list.find("li");
 
630
                if ( options.selectFirst ) {
 
631
                        listItems.slice(0, 1).addClass(CLASSES.ACTIVE);
 
632
                        active = 0;
 
633
                }
 
634
                //list.bgiframe();
 
635
        }
 
636
        
 
637
        return {
 
638
                display: function(d, q) {
 
639
                        init();
 
640
                        data = d;
 
641
                        term = q;
 
642
                        fillList();
 
643
                },
 
644
                next: function() {
 
645
                        moveSelect(1);
 
646
                },
 
647
                prev: function() {
 
648
                        moveSelect(-1);
 
649
                },
 
650
                pageUp: function() {
 
651
                        if (active != 0 && active - 8 < 0) {
 
652
                                moveSelect( -active );
 
653
                        } else {
 
654
                                moveSelect(-8);
 
655
                        }
 
656
                },
 
657
                pageDown: function() {
 
658
                        if (active != listItems.size() - 1 && active + 8 > listItems.size()) {
 
659
                                moveSelect( listItems.size() - 1 - active );
 
660
                        } else {
 
661
                                moveSelect(8);
 
662
                        }
 
663
                },
 
664
                hide: function() {
 
665
                        element && element.hide();
 
666
                        active = -1;
 
667
                },
 
668
                visible : function() {
 
669
                        return element && element.is(":visible");
 
670
                },
 
671
                current: function() {
 
672
                        return this.visible() && (listItems.filter("." + CLASSES.ACTIVE)[0] || options.selectFirst && listItems[0]);
 
673
                },
 
674
                show: function() {
 
675
                        var offset = $(input).offset();
 
676
                        element.css({
 
677
                                width: typeof options.width == "string" || options.width > 0 ? options.width : $(input).width(),
 
678
                                top: offset.top + input.offsetHeight,
 
679
                                left: offset.left
 
680
                        }).show();
 
681
            if(options.scroll) {
 
682
                list.scrollTop(0);
 
683
                list.css({
 
684
                                        maxHeight: options.scrollHeight,
 
685
                                        overflow: 'auto'
 
686
                                });
 
687
                                
 
688
                if($.browser.msie && typeof document.body.style.maxHeight === "undefined") {
 
689
                                        var listHeight = 0;
 
690
                                        listItems.each(function() {
 
691
                                                listHeight += this.offsetHeight;
 
692
                                        });
 
693
                                        var scrollbarsVisible = listHeight > options.scrollHeight;
 
694
                    list.css('height', scrollbarsVisible ? options.scrollHeight : listHeight );
 
695
                                        if (!scrollbarsVisible) {
 
696
                                                // IE doesn't recalculate width when scrollbar disappears
 
697
                                                listItems.width( list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right")) );
 
698
                                        }
 
699
                }
 
700
                
 
701
            }
 
702
                },
 
703
                selected: function() {
 
704
                        var selected = listItems && listItems.filter("." + CLASSES.ACTIVE).removeClass(CLASSES.ACTIVE);
 
705
                        return selected && selected.length && $.data(selected[0], "ac_data");
 
706
                },
 
707
                emptyList: function (){
 
708
                        list && list.empty();
 
709
                },
 
710
                unbind: function() {
 
711
                        element && element.remove();
 
712
                }
 
713
        };
 
714
};
 
715
 
 
716
$.Autocompleter.Selection = function(field, start, end) {
 
717
        if( field.createTextRange ){
 
718
                var selRange = field.createTextRange();
 
719
                selRange.collapse(true);
 
720
                selRange.moveStart("character", start);
 
721
                selRange.moveEnd("character", end);
 
722
                selRange.select();
 
723
        } else if( field.setSelectionRange ){
 
724
                field.setSelectionRange(start, end);
 
725
        } else {
 
726
                if( field.selectionStart ){
 
727
                        field.selectionStart = start;
 
728
                        field.selectionEnd = end;
 
729
                }
 
730
        }
 
731
        field.focus();
 
732
};
 
733
 
 
734
})(jQuery);
 
 
b'\\ No newline at end of file'