Lesmianator
[wolnelektury.git] / wolnelektury / static / js / jquery.autocomplete.js
index 4e425e8..6f46e1b 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 ;(function($) {
  */
 
 ;(function($) {
-       
+
 $.fn.extend({
        autocomplete: function(urlOrData, options) {
                var isUrl = typeof urlOrData == "string";
 $.fn.extend({
        autocomplete: function(urlOrData, options) {
                var isUrl = typeof urlOrData == "string";
@@ -26,13 +26,13 @@ $.fn.extend({
                        delay: isUrl ? $.Autocompleter.defaults.delay : 10,
                        max: options && !options.scroll ? 10 : 150
                }, options);
                        delay: isUrl ? $.Autocompleter.defaults.delay : 10,
                        max: options && !options.scroll ? 10 : 150
                }, options);
-               
+
                // if highlight is set to false, replace it with a do-nothing function
                options.highlight = options.highlight || function(value) { return value; };
                // if highlight is set to false, replace it with a do-nothing function
                options.highlight = options.highlight || function(value) { return value; };
-               
+
                // if the formatMatch option is not specified, then use formatItem for backwards compatibility
                options.formatMatch = options.formatMatch || options.formatItem;
                // if the formatMatch option is not specified, then use formatItem for backwards compatibility
                options.formatMatch = options.formatMatch || options.formatItem;
-               
+
                return this.each(function() {
                        new $.Autocompleter(this, options);
                });
                return this.each(function() {
                        new $.Autocompleter(this, options);
                });
@@ -81,9 +81,9 @@ $.Autocompleter = function(input, options) {
                mouseDownOnSelect: false
        };
        var select = $.Autocompleter.Select(options, input, selectCurrent, config);
                mouseDownOnSelect: false
        };
        var select = $.Autocompleter.Select(options, input, selectCurrent, config);
-       
+
        var blockSubmit;
        var blockSubmit;
-       
+
        // prevent form submit in opera when selecting with return key
        $.browser.opera && $(input.form).bind("submit.autocomplete", function() {
                if (blockSubmit) {
        // prevent form submit in opera when selecting with return key
        $.browser.opera && $(input.form).bind("submit.autocomplete", function() {
                if (blockSubmit) {
@@ -91,7 +91,7 @@ $.Autocompleter = function(input, options) {
                        return false;
                }
        });
                        return false;
                }
        });
-       
+
        // only opera doesn't trigger keydown multiple times while pressed, others don't work with keypress at all
        $input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
                // a keypress means the input has focus
        // only opera doesn't trigger keydown multiple times while pressed, others don't work with keypress at all
        $input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
                // a keypress means the input has focus
@@ -100,7 +100,7 @@ $.Autocompleter = function(input, options) {
                // track last key pressed
                lastKeyPressCode = event.keyCode;
                switch(event.keyCode) {
                // track last key pressed
                lastKeyPressCode = event.keyCode;
                switch(event.keyCode) {
-               
+
                        case KEY.UP:
                                event.preventDefault();
                                if ( select.visible() ) {
                        case KEY.UP:
                                event.preventDefault();
                                if ( select.visible() ) {
@@ -109,7 +109,7 @@ $.Autocompleter = function(input, options) {
                                        onChange(0, true);
                                }
                                break;
                                        onChange(0, true);
                                }
                                break;
-                               
+
                        case KEY.DOWN:
                                event.preventDefault();
                                if ( select.visible() ) {
                        case KEY.DOWN:
                                event.preventDefault();
                                if ( select.visible() ) {
@@ -118,7 +118,7 @@ $.Autocompleter = function(input, options) {
                                        onChange(0, true);
                                }
                                break;
                                        onChange(0, true);
                                }
                                break;
-                               
+
                        case KEY.PAGEUP:
                                event.preventDefault();
                                if ( select.visible() ) {
                        case KEY.PAGEUP:
                                event.preventDefault();
                                if ( select.visible() ) {
@@ -127,7 +127,7 @@ $.Autocompleter = function(input, options) {
                                        onChange(0, true);
                                }
                                break;
                                        onChange(0, true);
                                }
                                break;
-                               
+
                        case KEY.PAGEDOWN:
                                event.preventDefault();
                                if ( select.visible() ) {
                        case KEY.PAGEDOWN:
                                event.preventDefault();
                                if ( select.visible() ) {
@@ -136,7 +136,7 @@ $.Autocompleter = function(input, options) {
                                        onChange(0, true);
                                }
                                break;
                                        onChange(0, true);
                                }
                                break;
-                       
+
                        // matches also semicolon
                        case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
                        case KEY.TAB:
                        // matches also semicolon
                        case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
                        case KEY.TAB:
@@ -148,11 +148,11 @@ $.Autocompleter = function(input, options) {
                                        return false;
                                }
                                break;
                                        return false;
                                }
                                break;
-                               
+
                        case KEY.ESC:
                                select.hide();
                                break;
                        case KEY.ESC:
                                select.hide();
                                break;
-                               
+
                        default:
                                clearTimeout(timeout);
                                timeout = setTimeout(onChange, options.delay);
                        default:
                                clearTimeout(timeout);
                                timeout = setTimeout(onChange, options.delay);
@@ -203,16 +203,16 @@ $.Autocompleter = function(input, options) {
                $input.unbind();
                $(input.form).unbind(".autocomplete");
        });
                $input.unbind();
                $(input.form).unbind(".autocomplete");
        });
-       
-       
+
+
        function selectCurrent() {
                var selected = select.selected();
                if( !selected )
                        return false;
        function selectCurrent() {
                var selected = select.selected();
                if( !selected )
                        return false;
-               
+
                var v = selected.result;
                previousValue = v;
                var v = selected.result;
                previousValue = v;
-               
+
                if ( options.multiple ) {
                        var words = trimWords($input.val());
                        if ( words.length > 1 ) {
                if ( options.multiple ) {
                        var words = trimWords($input.val());
                        if ( words.length > 1 ) {
@@ -234,26 +234,26 @@ $.Autocompleter = function(input, options) {
                        }
                        v += options.multipleSeparator;
                }
                        }
                        v += options.multipleSeparator;
                }
-               
+
                $input.val(v);
                hideResultsNow();
                $input.trigger("result", [selected.data, selected.value]);
                return true;
        }
                $input.val(v);
                hideResultsNow();
                $input.trigger("result", [selected.data, selected.value]);
                return true;
        }
-       
+
        function onChange(crap, skipPrevCheck) {
                if( lastKeyPressCode == KEY.DEL ) {
                        select.hide();
                        return;
                }
        function onChange(crap, skipPrevCheck) {
                if( lastKeyPressCode == KEY.DEL ) {
                        select.hide();
                        return;
                }
-               
+
                var currentValue = $input.val();
                var currentValue = $input.val();
-               
+
                if ( !skipPrevCheck && currentValue == previousValue )
                        return;
                if ( !skipPrevCheck && currentValue == previousValue )
                        return;
-               
+
                previousValue = currentValue;
                previousValue = currentValue;
-               
+
                currentValue = lastWord(currentValue);
                if ( currentValue.length >= options.minChars) {
                        $input.addClass(options.loadingClass);
                currentValue = lastWord(currentValue);
                if ( currentValue.length >= options.minChars) {
                        $input.addClass(options.loadingClass);
@@ -265,7 +265,7 @@ $.Autocompleter = function(input, options) {
                        select.hide();
                }
        };
                        select.hide();
                }
        };
-       
+
        function trimWords(value) {
                if (!value)
                        return [""];
        function trimWords(value) {
                if (!value)
                        return [""];
@@ -275,12 +275,12 @@ $.Autocompleter = function(input, options) {
                        return $.trim(value).length ? $.trim(word) : null;
                });
        }
                        return $.trim(value).length ? $.trim(word) : null;
                });
        }
-       
+
        function lastWord(value) {
                if ( !options.multiple )
                        return value;
                var words = trimWords(value);
        function lastWord(value) {
                if ( !options.multiple )
                        return value;
                var words = trimWords(value);
-               if (words.length == 1) 
+               if (words.length == 1)
                        return words[0];
                var cursorAt = $(input).selection().start;
                if (cursorAt == value.length) {
                        return words[0];
                var cursorAt = $(input).selection().start;
                if (cursorAt == value.length) {
@@ -290,7 +290,7 @@ $.Autocompleter = function(input, options) {
                }
                return words[words.length - 1];
        }
                }
                return words[words.length - 1];
        }
-       
+
        // fills in the input box w/the first match (assumed to be the best match)
        // q: the term entered
        // sValue: the first matching result
        // fills in the input box w/the first match (assumed to be the best match)
        // q: the term entered
        // sValue: the first matching result
@@ -355,14 +355,14 @@ $.Autocompleter = function(input, options) {
                        success(term, data);
                // if an AJAX url has been supplied, try loading the data now
                } else if( (typeof options.url == "string") && (options.url.length > 0) ){
                        success(term, data);
                // if an AJAX url has been supplied, try loading the data now
                } else if( (typeof options.url == "string") && (options.url.length > 0) ){
-                       
+
                        var extraParams = {
                                timestamp: +new Date()
                        };
                        $.each(options.extraParams, function(key, param) {
                                extraParams[key] = typeof param == "function" ? param() : param;
                        });
                        var extraParams = {
                                timestamp: +new Date()
                        };
                        $.each(options.extraParams, function(key, param) {
                                extraParams[key] = typeof param == "function" ? param() : param;
                        });
-                       
+
                        $.ajax({
                                // try to leverage ajaxQueue plugin to abort previous requests
                                mode: "abort",
                        $.ajax({
                                // try to leverage ajaxQueue plugin to abort previous requests
                                mode: "abort",
@@ -386,7 +386,7 @@ $.Autocompleter = function(input, options) {
                        failure(term);
                }
        };
                        failure(term);
                }
        };
-       
+
        function parse(data) {
                var parsed = [];
                var rows = data.split("\n");
        function parse(data) {
                var parsed = [];
                var rows = data.split("\n");
@@ -455,9 +455,9 @@ $.Autocompleter.Cache = function(options) {
 
        var data = {};
        var length = 0;
 
        var data = {};
        var length = 0;
-       
+
        function matchSubset(s, sub) {
        function matchSubset(s, sub) {
-               if (!options.matchCase) 
+               if (!options.matchCase)
                        s = s.toLowerCase();
                var i = s.indexOf(sub);
                if (options.matchContains == "word"){
                        s = s.toLowerCase();
                var i = s.indexOf(sub);
                if (options.matchContains == "word"){
@@ -467,17 +467,17 @@ $.Autocompleter.Cache = function(options) {
                if (i == -1) return false;
                return i == 0 || options.matchContains;
        };
                if (i == -1) return false;
                return i == 0 || options.matchContains;
        };
-       
+
        function add(q, value) {
                if (length > options.cacheLength){
                        flush();
                }
        function add(q, value) {
                if (length > options.cacheLength){
                        flush();
                }
-               if (!data[q]){ 
+               if (!data[q]){
                        length++;
                }
                data[q] = value;
        }
                        length++;
                }
                data[q] = value;
        }
-       
+
        function populate(){
                if( !options.data ) return false;
                // track the matches
        function populate(){
                if( !options.data ) return false;
                // track the matches
@@ -486,23 +486,23 @@ $.Autocompleter.Cache = function(options) {
 
                // no url was specified, we need to adjust the cache length to make sure it fits the local data store
                if( !options.url ) options.cacheLength = 1;
 
                // no url was specified, we need to adjust the cache length to make sure it fits the local data store
                if( !options.url ) options.cacheLength = 1;
-               
+
                // track all options for minChars = 0
                stMatchSets[""] = [];
                // track all options for minChars = 0
                stMatchSets[""] = [];
-               
+
                // loop through the array and create a lookup structure
                for ( var i = 0, ol = options.data.length; i < ol; i++ ) {
                        var rawValue = options.data[i];
                        // if rawValue is a string, make an array otherwise just reference the array
                        rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue;
                // loop through the array and create a lookup structure
                for ( var i = 0, ol = options.data.length; i < ol; i++ ) {
                        var rawValue = options.data[i];
                        // if rawValue is a string, make an array otherwise just reference the array
                        rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue;
-                       
+
                        var value = options.formatMatch(rawValue, i+1, options.data.length);
                        if ( value === false )
                                continue;
                        var value = options.formatMatch(rawValue, i+1, options.data.length);
                        if ( value === false )
                                continue;
-                               
+
                        var firstChar = value.charAt(0).toLowerCase();
                        // if no lookup array for this character exists, look it up now
                        var firstChar = value.charAt(0).toLowerCase();
                        // if no lookup array for this character exists, look it up now
-                       if( !stMatchSets[firstChar] ) 
+                       if( !stMatchSets[firstChar] )
                                stMatchSets[firstChar] = [];
 
                        // if the match is a string
                                stMatchSets[firstChar] = [];
 
                        // if the match is a string
@@ -511,7 +511,7 @@ $.Autocompleter.Cache = function(options) {
                                data: rawValue,
                                result: options.formatResult && options.formatResult(rawValue) || value
                        };
                                data: rawValue,
                                result: options.formatResult && options.formatResult(rawValue) || value
                        };
-                       
+
                        // push the current match into the set list
                        stMatchSets[firstChar].push(row);
 
                        // push the current match into the set list
                        stMatchSets[firstChar].push(row);
 
@@ -529,15 +529,15 @@ $.Autocompleter.Cache = function(options) {
                        add(i, value);
                });
        }
                        add(i, value);
                });
        }
-       
+
        // populate any existing data
        setTimeout(populate, 25);
        // populate any existing data
        setTimeout(populate, 25);
-       
+
        function flush(){
                data = {};
                length = 0;
        }
        function flush(){
                data = {};
                length = 0;
        }
-       
+
        return {
                flush: flush,
                add: add,
        return {
                flush: flush,
                add: add,
@@ -545,7 +545,7 @@ $.Autocompleter.Cache = function(options) {
                load: function(q) {
                        if (!options.cacheLength || !length)
                                return null;
                load: function(q) {
                        if (!options.cacheLength || !length)
                                return null;
-                       /* 
+                       /*
                         * if dealing w/local data and matchContains than we must make sure
                         * to loop through all the data collections looking for matches
                         */
                         * if dealing w/local data and matchContains than we must make sure
                         * to loop through all the data collections looking for matches
                         */
@@ -565,9 +565,9 @@ $.Autocompleter.Cache = function(options) {
                                                        }
                                                });
                                        }
                                                        }
                                                });
                                        }
-                               }                               
+                               }
                                return csub;
                                return csub;
-                       } else 
+                       } else
                        // if the exact item exists, use it
                        if (data[q]){
                                return data[q];
                        // if the exact item exists, use it
                        if (data[q]){
                                return data[q];
@@ -595,7 +595,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
        var CLASSES = {
                ACTIVE: "ac_over"
        };
        var CLASSES = {
                ACTIVE: "ac_over"
        };
-       
+
        var listItems,
                active = -1,
                data,
        var listItems,
                active = -1,
                data,
@@ -603,7 +603,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
                needsInit = true,
                element,
                list;
                needsInit = true,
                element,
                list;
-       
+
        // Create results
        function init() {
                if (!needsInit)
        // Create results
        function init() {
                if (!needsInit)
@@ -613,11 +613,11 @@ $.Autocompleter.Select = function (options, input, select, config) {
                .addClass(options.resultsClass)
                .css("position", "absolute")
                .appendTo(document.body);
                .addClass(options.resultsClass)
                .css("position", "absolute")
                .appendTo(document.body);
-       
+
                list = $("<ul/>").appendTo(element).mouseover( function(event) {
                        if(target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') {
                    active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event));
                list = $("<ul/>").appendTo(element).mouseover( function(event) {
                        if(target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') {
                    active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event));
-                           $(target(event)).addClass(CLASSES.ACTIVE);            
+                           $(target(event)).addClass(CLASSES.ACTIVE);
                }
                }).click(function(event) {
                        $(target(event)).addClass(CLASSES.ACTIVE);
                }
                }).click(function(event) {
                        $(target(event)).addClass(CLASSES.ACTIVE);
@@ -630,13 +630,13 @@ $.Autocompleter.Select = function (options, input, select, config) {
                }).mouseup(function() {
                        config.mouseDownOnSelect = false;
                });
                }).mouseup(function() {
                        config.mouseDownOnSelect = false;
                });
-               
+
                if( options.width > 0 )
                        element.css("width", options.width);
                if( options.width > 0 )
                        element.css("width", options.width);
-                       
+
                needsInit = false;
                needsInit = false;
-       } 
-       
+       }
+
        function target(event) {
                var element = event.target;
                while(element && element.tagName != "LI")
        function target(event) {
                var element = event.target;
                while(element && element.tagName != "LI")
@@ -663,7 +663,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
             }
         }
        };
             }
         }
        };
-       
+
        function movePosition(step) {
                active += step;
                if (active < 0) {
        function movePosition(step) {
                active += step;
                if (active < 0) {
@@ -672,13 +672,13 @@ $.Autocompleter.Select = function (options, input, select, config) {
                        active = 0;
                }
        }
                        active = 0;
                }
        }
-       
+
        function limitNumberOfItems(available) {
                return options.max && options.max < available
                        ? options.max
                        : available;
        }
        function limitNumberOfItems(available) {
                return options.max && options.max < available
                        ? options.max
                        : available;
        }
-       
+
        function fillList() {
                list.empty();
                var max = limitNumberOfItems(data.length);
        function fillList() {
                list.empty();
                var max = limitNumberOfItems(data.length);
@@ -700,7 +700,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
                if ( $.fn.bgiframe )
                        list.bgiframe();
        }
                if ( $.fn.bgiframe )
                        list.bgiframe();
        }
-       
+
        return {
                display: function(d, q) {
                        init();
        return {
                display: function(d, q) {
                        init();
@@ -752,7 +752,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
                                        maxHeight: options.scrollHeight,
                                        overflow: 'auto'
                                });
                                        maxHeight: options.scrollHeight,
                                        overflow: 'auto'
                                });
-                               
+
                 if($.browser.msie && typeof document.body.style.maxHeight === "undefined") {
                                        var listHeight = 0;
                                        listItems.each(function() {
                 if($.browser.msie && typeof document.body.style.maxHeight === "undefined") {
                                        var listHeight = 0;
                                        listItems.each(function() {
@@ -765,7 +765,7 @@ $.Autocompleter.Select = function (options, input, select, config) {
                                                listItems.width( list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right")) );
                                        }
                 }
                                                listItems.width( list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right")) );
                                        }
                 }
-                
+
             }
                },
                selected: function() {
             }
                },
                selected: function() {