Commits

Anonymous committed 5ea8612

more fixes

Comments (0)

Files changed (1)

TestWebsite/Scripts/hAutoComplete/hAutoComplete.js

 
 (function($) {
     /*
+    
+    Options:
+    
+    options.selectedValueVerifiedCallback = function(data)   -> For registering a callback function that retrieves additional data every time a filtering call to the server completes.
+    
+    options.extraParameters =  -> For sending additional paramters to the server each time a search operation is performed.  Things like work order number, loging name, etc...
+    {
+    additionalProperty1: 'This can be an additional property. It can be named anything as long as the server can de-serialize it.',
+    additionalProperty2: 'This can be an additional property. It can be named anything as long as the server can de-serialize it.',
+    .
+    .
+    .
+    }
+
+    options.source = 'http...' -> For specifying the URL to where the filtering AJAX call will be called out to.  An object will be serialized into JSON and posted to this URL.  The object
+    that is posted looks like this:
+    {
+    term: 'search term',   //Mandatory, will always exist
+    controlId: 'ID of the control where the search is coming from', //Mandatory, will always exist
+        
+    additionalProperty1: 'This comes from options.extraParameters',
+    additionalProperty2: 'This comes from options.extraParameters',
+    .
+    .
+    .
+    */
+    $.fn.hAutoComplete = function(options, setValue) {
+        if (options === 'set') {
+            $.fn.hAutoComplete.prototype.val.call(this, { term: setValue });
+            return;
+        }
+
+        if (typeof (options) !== 'object') {
+            var msg = "Invalid parameters passed to hAutoComplete";
+            alert(msg); throw msg;
+        }
+
+        var padding, minimumSearchLength, toolTipContainer, showallButton, defaults, that, repositionPopup, getFilterResultsFromServer, hideToolTip, showToolTip, showAllButtonId, msg, otherElementFocus;
+
+
+        that = this; //self reference variable to be used inside functions
+        $.fn.hAutoComplete.prototype.lastfocusedInputbox = null;
+        padding = 10; //How much padding to leave between text and border
+        minimumSearchLength = 1;
+        toolTipContainer = $('<div id="hAutoCompleteContainerBox" class="ui-widget ui-widget-content ui-corner-all">');
+        toolTipContainer.css({
+            position: 'absolute',
+            padding: (padding + 'px')
+        });
+        showallButton = $('<button>Get All Items</button>');
+
+        this.attr('name', ('hAutoCompleteBox_' + this.attr('id'))); //We will set the ajax name so that we will know what ajaxID to wait for if we need to
+        showAllButtonId = 'hAutoCompleteBoxClick_' + this.attr('id');
+        showallButton.attr('id', showAllButtonId);
+
+
+        toolTipContainer.append($('<span style="display:inline">Start typing the description or the part number to filter.<br /> Or click </span>'));
+        toolTipContainer.append(showallButton);
+        toolTipContainer.append($('<span style="display:inline"> if you want to see the whole list.</span>'));
+
+        //Make sure that a URL was specified
+        if ((options.source === null) || (options.source === undefined)) {
+            throw "Missing search URL for hAutoComplete.  Please specify options.source URL.";
+        }
+        //Make sure that the element has an ID associated
+        if (this.attr('id').length <= 0) {
+            msg = "An ID must be defined for hAutoComplete text boxes, this will be passed in to the controller as part of the filter";
+            alert(msg);
+            throw msg;
+        }
+
+        otherElementFocus = function(event) {
+            hideToolTip(event);
+            //event.stopPropagation(); return false;
+        };
+
+        $('body').undelegate('*', 'focus', otherElementFocus);
+        $('body').delegate('*', 'focus', otherElementFocus);
+
+        $(document).undelegate('*', 'click', otherElementFocus);
+        $(document).delegate('*', 'click', otherElementFocus);
+
+        getFilterResultsFromServer = function(searchObject, response, surpressErrors, callType) {
+            //Spawn an ajax request here to the filter controller
+            //Make sure some kind of loading screen appears.
+            if ((surpressErrors === null) || (surpressErrors === undefined)) {
+                surpressErrors = true;
+            }
+            if ((callType === null) || (callType === undefined)) {
+                callType = 'filtering';
+            }
+
+
+            $.AjaxManager.POSTJson({
+                name: that.attr('name'),
+                message: 'Filtering',
+                url: options.source,
+                growl: true,
+                single: false,
+                cache: true,
+                surpressIfError: surpressErrors,
+                dataFunc: function() {
+                    var dataPackage = {
+                        term: searchObject.term,
+                        controlId: that.attr('id'),
+                        TypeOfCall: callType
+                    };
+                    $.extend(dataPackage, options.extraParameters);
+                    return dataPackage;
+                },
+                success: function(json) {
+                    response(json.resultList, json);
+                }
+            });
+        };
+
+
+
+
+        /*
+        a public function and lets user to programatically pre-fill
+        our autocomplete text input box.
+        */
+        $.fn.hAutoComplete.prototype.val = function(inputOptions) {
+            var msg, thisElement;
+            thisElement = $(this);
+
+
+            if ((inputOptions === null)
+                || (inputOptions === undefined)
+                || (inputOptions.term === null)
+                || (inputOptions.term === undefined)
+                || (inputOptions.term.length <= 0)) {
+                msg = "term needs to be defined for options in hAutocomplete.val() function";
+                alert(msg); throw msg;
+            }
+
+            getFilterResultsFromServer(inputOptions, function(resultArray, resultObject) {
+                var selectedId, oldSelectedId;
+                if (resultArray.length === 1) {
+                    selectedId = resultArray[0].id;
+                    oldSelectedId = thisElement.data("oldSelectedValue");
+                    //A check to see if this item was selected before.   if its the same one then don't bother triggering any callbacks since this can cause unnecesary postbacks
+                    if (oldSelectedId !== selectedId) {
+                        if (typeof (options.selectedValueVerifiedCallback) == 'function') {
+                            options.selectedValueVerifiedCallback(resultObject);
+                        }
+
+                        thisElement.data("uiItem", selectedId);
+                        thisElement.val(selectedId);
+                        thisElement.data("oldSelectedValue", selectedId);
+                    }
+                } else {
+                    thisElement.val(thisElement.data("uiItem"));
+                }
+            }, false, 'verifying');
+        };
+
+        hideToolTip = function(event) {
+            //If there is no target event then just hide the tooltip
+            if ((event === null) || (event === undefined)) {
+                toolTipContainer.remove();
+                return;
+            }
+
+            //Check to see what was click or focused on
+            var targetElement = $(event.target);
+            if (targetElement.hasClass('ui-autocomplete-input')) {
+                return;
+            }
+            if (targetElement.attr('id').toLowerCase() === showAllButtonId.toLowerCase()) {
+                that.data('showAllClicked', true);
+                return;
+            }
+            toolTipContainer.remove();
+        };
+
+
+        showToolTip = function(textInputElement) {
+
+            $.fn.hAutoComplete.prototype.lastfocusedInputbox = textInputElement;
+
+            //Make sure that the tool tip is removed from anywhere else if it is still showing.
+            $('#hAutoCompleteContainerBox').each(function() {
+                $(this).remove();
+            });
+
+
+
+            textInputElement.after(toolTipContainer);
+            toolTipContainer.show();
+
+            showallButton.button({ icons: { primary: 'ui-icon-folder-open' }, text: false });
+            showallButton.unbind('click.showallbutton');
+            showallButton.bind('click.showallbutton', function() {
+                var txtInput = toolTipContainer.data('associated');
+                txtInput.autocomplete("option", { minLength: 0 });
+                txtInput.autocomplete("search", '');
+                txtInput.autocomplete("option", { minLength: minimumSearchLength });
+            });
+            showallButton.each(function() {
+                this.tabIndex = -1;
+            });
+
+            repositionPopup(textInputElement);
+            toolTipContainer.data('associated', textInputElement);
+        };
+
+
+
+
+
+        this.unbind('keydown.hAutoComplete');
+        this.bind('keydown.hAutoComplete', function(event) {
+            var keyCode = $.ui.keyCode, that = $(this);
+            switch (event.keyCode) {
+                case keyCode.SHIFT:
+                case keyCode.RIGHT:
+                case keyCode.LEFT:
+                    event.stopPropagation();
+                    break;
+                case keyCode.ENTER:
+                    setTimeout(function() { that.blur(); hideToolTip(); }, 20);
+                    break;
+                default:
+                    break;
+            }
+        })
+
+        this.click(function(event) {
+            event.stopPropagation();
+            return false;
+        });
+
+        repositionPopup = function(element) {
+            toolTipContainer.css({
+                width: (element.width() - padding)
+            });
+
+            toolTipContainer.css({
+                left: element.position().left
+            });
+
+            toolTipContainer.maxZIndex({ inc: 5 });
+            $('.ui-autocomplete').each(function() {
+                if ($(this).is(':visible')) {
+                    $(this).maxZIndex({ inc: 5 });
+                }
+            });
+
+        };
+
+
+        this.unbind('focus.focusToShowToolTip');
+        this.bind('focus.focusToShowToolTip', function() {
+            showToolTip($(this));
+        });
+        this.unbind('click.focusToShowToolTip');
+        this.bind('click.focusToShowToolTip', function() {
+            showToolTip($(this));
+        })
+
+
+
+        //The jQuery-UI autocomplete plugin default settings
+        defaults = {
+            source: getFilterResultsFromServer,
+            select: function(event, ui) {
+                var element = $(this);
+                element.data("uiItem", ui.item.id);
+                element.val(ui.item.id);
+                element.val(element.data("uiItem"));
+                hideToolTip();
+                element.blur(); //to trigger the change event after selecting an item from the dropdown list
+                return false;
+            },
+            close: function(event, ui) {
+                //var element = $(this);
+                //setTimeout(function() { element.val(element.data("uiItem")); }, 10);
+            },
+            open: function(event, ui) {
+                var element = $(this);
+                setTimeout(function() { element.focus(); }, 100);
+                $('.ui-autocomplete').each(function() {
+                    $(this).maxZIndex({ inc: 5 });
+                });
+            },
+            minLength: minimumSearchLength
+        };
+
+        if (this.data('initalized')) {
+            this.autocomplete("destroy");
+        }
+
+        this.autocomplete(defaults);
+        this.data('initalized', true);
+
+        this.unbind("blur.BlurInputTextBox");
+        this.bind("blur.BlurInputTextBox", function(event) {
+            var theInputBox = $(this);
+            setTimeout(function() {
+                if (!that.data('showAllClicked')) {
+                    if (theInputBox.val().length <= 0) {
+                        theInputBox.val(theInputBox.data("uiItem"));
+                    }
+                    if (theInputBox.val().length <= 0) {
+                        return;
+                    }
+                    $.fn.hAutoComplete.prototype.val.call(theInputBox, { term: theInputBox.val() });
+                } else {
+                    that.data('showAllClicked', false);
+                }
+            }, 20);
+        });
+        this.data("uiItem", that.val());
+
+    };
+
+
+    /*
     extends the jquery expressions to be able to determin if an element is in focus
     by $(element).is(':focus')
     which should return true or false
             $(this).css("z-index", zmax);
         });
     };
-
-
-    /*
-    
-    Options:
-    
-    options.additionalDataRecievedCallback = function(data)   -> For registering a callback function that retrieves additional data every time a filtering call to the server completes.
-    
-    options.extraParameters =  -> For sending additional paramters to the server each time a search operation is performed.  Things like work order number, loging name, etc...
-    {
-    additionalProperty1: 'This can be an additional property. It can be named anything as long as the server can de-serialize it.',
-    additionalProperty2: 'This can be an additional property. It can be named anything as long as the server can de-serialize it.',
-    .
-    .
-    .
-    }
-
-    options.source = 'http...' -> For specifying the URL to where the filtering AJAX call will be called out to.  An object will be serialized into JSON and posted to this URL.  The object
-    that is posted looks like this:
-    {
-    term: 'search term',   //Mandatory, will always exist
-    controlId: 'ID of the control where the search is coming from', //Mandatory, will always exist
-        
-    additionalProperty1: 'This comes from options.extraParameters',
-    additionalProperty2: 'This comes from options.extraParameters',
-    .
-    .
-    .
-    */
-    $.fn.hAutoComplete = function(options, setValue) {
-        if (options === 'set') {
-            $.fn.hAutoComplete.prototype.val.call(this, { term: setValue });
-            return;
-        }
-
-        if (typeof (options) !== 'object') {
-            var msg = "Invalid parameters passed to hAutoComplete";
-            alert(msg); throw msg;
-        }
-
-        var padding, minimumSearchLength, toolTipContainer, showallButton, defaults, that, repositionPopup, getFilterResultsFromServer, hideToolTip, showToolTip, showAllButtonId, msg, otherElementFocus;
-
-
-        that = this; //self reference variable to be used inside functions
-        $.fn.hAutoComplete.prototype.lastfocusedInputbox = null;
-        padding = 10; //How much padding to leave between text and border
-        minimumSearchLength = 1;
-        toolTipContainer = $('<div id="hAutoCompleteContainerBox" class="ui-widget ui-widget-content ui-corner-all">');
-        toolTipContainer.css({
-            position: 'absolute',
-            padding: (padding + 'px')
-        });
-        showallButton = $('<button>Get All Items</button>');
-
-        this.attr('name', ('hAutoCompleteBox_' + this.attr('id'))); //We will set the ajax name so that we will know what ajaxID to wait for if we need to
-        showAllButtonId = 'hAutoCompleteBoxClick_' + this.attr('id');
-        showallButton.attr('id', showAllButtonId);
-
-
-        toolTipContainer.append($('<span style="display:inline">Start typing the description or the part number to filter.<br /> Or click </span>'));
-        toolTipContainer.append(showallButton);
-        toolTipContainer.append($('<span style="display:inline"> if you want to see the whole list.</span>'));
-
-        //Make sure that a URL was specified
-        if ((options.source === null) || (options.source === undefined)) {
-            throw "Missing search URL for hAutoComplete.  Please specify options.source URL.";
-        }
-        //Make sure that the element has an ID associated
-        if (this.attr('id').length <= 0) {
-            msg = "An ID must be defined for hAutoComplete text boxes, this will be passed in to the controller as part of the filter";
-            alert(msg);
-            throw msg;
-        }
-
-        otherElementFocus = function(event) {
-            hideToolTip(event);
-            //event.stopPropagation(); return false;
-        };
-
-        $('body').undelegate('*', 'focus', otherElementFocus);
-        $('body').delegate('*', 'focus', otherElementFocus);
-
-        $(document).undelegate('*', 'click', otherElementFocus);
-        $(document).delegate('*', 'click', otherElementFocus);
-
-        getFilterResultsFromServer = function(searchObject, response, surpressErrors, callType) {
-            //Spawn an ajax request here to the filter controller
-            //Make sure some kind of loading screen appears.
-            if ((surpressErrors === null) || (surpressErrors === undefined)) {
-                surpressErrors = true;
-            }
-            if ((callType === null) || (callType === undefined)) {
-                callType = 'filtering';
-            }
-
-
-            $.AjaxManager.POSTJson({
-                name: that.attr('name'),
-                message: 'Filtering',
-                url: options.source,
-                growl: true,
-                single: false,
-                cache: true,
-                surpressIfError: surpressErrors,
-                dataFunc: function() {
-                    var dataPackage = {
-                        term: searchObject.term,
-                        controlId: that.attr('id'),
-                        TypeOfCall: callType
-                    };
-                    $.extend(dataPackage, options.extraParameters);
-                    return dataPackage;
-                },
-                success: function(json) {
-                    if (typeof (options.additionalDataRecievedCallback) === 'function') {
-                        options.additionalDataRecievedCallback(json);
-                    }
-                    response(json.resultList);
-                }
-            });
-        };
-
-
-
-
-        /*
-        a public function and lets user to programatically pre-fill
-        our autocomplete text input box.
-        */
-        $.fn.hAutoComplete.prototype.val = function(inputOptions) {
-            var msg, options, thisElement;
-            thisElement = $(this);
-            options = {
-                term: ''
-            };
-
-            $.extend(options, inputOptions);
-            if (options.term.length <= 0) {
-                msg = "term needs to be defined for options in hAutocomplete.val() function";
-                alert(msg); throw msg;
-            }
-
-            getFilterResultsFromServer(options, function(result) {
-                if (result.length === 1) {
-                    thisElement.data("uiItem", result[0].id);
-                    thisElement.val(result[0].id);
-                } else {
-                    thisElement.val(thisElement.data("uiItem"));
-                }
-            }, false, 'verifying');
-        };
-
-        hideToolTip = function(event) {
-            var targetElement = $(event.target);
-            if (targetElement.hasClass('ui-autocomplete-input')) {
-                return;
-            }
-            if (targetElement.attr('id').toLowerCase() === showAllButtonId.toLowerCase()) {
-                that.data('showAllClicked', true);
-                return;
-            }
-            toolTipContainer.remove();
-        };
-
-
-        showToolTip = function(textInputElement) {
-            //Make sure that the tool tip is removed from anywhere else if it is still showing.
-            $('#hAutoCompleteContainerBox').each(function() {
-                $(this).remove();
-            });
-
-
-
-            textInputElement.after(toolTipContainer);
-            toolTipContainer.show();
-
-            showallButton.button({ icons: { primary: 'ui-icon-folder-open' }, text: false });
-            showallButton.unbind('click.showallbutton');
-            showallButton.bind('click.showallbutton', function() {
-                var txtInput = toolTipContainer.data('associated');
-                txtInput.autocomplete("option", { minLength: 0 });
-                txtInput.autocomplete("search", '');
-                txtInput.autocomplete("option", { minLength: minimumSearchLength });
-            });
-            showallButton.each(function() {
-                this.tabIndex = -1;
-            });
-
-            repositionPopup(textInputElement);
-            toolTipContainer.data('associated', textInputElement);
-        };
-
-
-
-
-
-
-
-
-        this.click(function(event) {
-            event.stopPropagation();
-            return false;
-        });
-
-        repositionPopup = function(element) {
-            toolTipContainer.css({
-                width: (element.width() - padding)
-            });
-
-            toolTipContainer.css({
-                left: element.position().left
-            });
-
-            toolTipContainer.maxZIndex({ inc: 5 });
-            $('.ui-autocomplete').each(function() {
-                if ($(this).is(':visible')) {
-                    $(this).maxZIndex({ inc: 5 });
-                }
-            });
-
-        };
-
-
-        this.unbind('focus.focusToShowToolTip');
-        this.bind('focus.focusToShowToolTip', function() {
-            var element = $(this);
-            showToolTip(element);
-
-            $.fn.hAutoComplete.prototype.lastfocusedInputbox = $(this);
-        });
-
-
-
-        //The jQuery-UI autocomplete plugin default settings
-        defaults = {
-            source: getFilterResultsFromServer,
-            select: function(event, ui) {
-                var element = $(this);
-                element.data("uiItem", ui.item.id);
-                element.val(ui.item.id);
-                element.val(element.data("uiItem"));
-                toolTipContainer.remove();
-                return false;
-            },
-            close: function(event, ui) {
-                //var element = $(this);
-                //setTimeout(function() { element.val(element.data("uiItem")); }, 10);
-            },
-            open: function(event, ui) {
-                var element = $(this);
-                setTimeout(function() { element.focus(); }, 100);
-                $('.ui-autocomplete').each(function() {
-                    $(this).maxZIndex({ inc: 5 });
-                });
-            },
-            minLength: minimumSearchLength
-        };
-
-        if (this.data('initalized')) {
-            this.autocomplete("destroy");
-        }
-
-        this.autocomplete(defaults);
-        this.data('initalized', true);
-
-        this.unbind("blur.BlurInputTextBox");
-        this.bind("blur.BlurInputTextBox", function(event) {
-            var theInputBox = $(this);
-            setTimeout(function() {
-                if (!that.data('showAllClicked')) {
-                    if (theInputBox.val().length <= 0) {
-                        theInputBox.val(theInputBox.data("uiItem"));
-                    }
-                    if (theInputBox.val().length <= 0) {
-                        return;
-                    }
-                    $.fn.hAutoComplete.prototype.val.call(theInputBox, { term: theInputBox.val() });
-                } else {
-                    that.data('showAllClicked', false);
-                }
-            }, 20);
-        });
-        this.data("uiItem", that.val());
-
-    };
 } (jQuery));