Author: jcompagner
Date: Fri Nov 25 12:45:08 2011
New Revision: 1206142

URL: http://svn.apache.org/viewvc?rev=1206142&view=rev
Log:
Wicket autocomplete keeps working on the replaced element when somehow the 
element is replaced by another ajax request when it is shown.
Issue: WICKET-4261

Modified:
    
wicket/branches/wicket-1.4.x/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js

Modified: 
wicket/branches/wicket-1.4.x/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js?rev=1206142&r1=1206141&r2=1206142&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
 Fri Nov 25 12:45:08 2011
@@ -53,6 +53,7 @@ Wicket.AutoComplete=function(elementId, 
        var objonchange;
        var objonchangeoriginal;
        var objonfocus;
+       var initialElement; 
        
        // holds the eventual margins, padding, etc. of the menu container.
        // it is computed when the menu is first rendered, and then reused.
@@ -77,17 +78,20 @@ Wicket.AutoComplete=function(elementId, 
        var throttleDelay = cfg.throttleDelay;
 
     function initialize(){
+       var isShowing = false;
                // Remove the autocompletion menu if still present from
                // a previous call. This is required to properly register
                // the mouse event handler again (using the new stateful 
'mouseactive'
                // variable which just gets created)
         var choiceDiv=document.getElementById(getMenuId());
         if (choiceDiv != null) {
+               isShowing = choiceDiv.showingAutocomplete;
             choiceDiv.parentNode.parentNode.removeChild(choiceDiv.parentNode);
         } 
                
         var obj=wicketGet(elementId);
-
+               initialElement = obj;
+               
         objonkeydown=obj.onkeydown;
         objonblur=obj.onblur;
         objonkeyup=obj.onkeyup;
@@ -118,6 +122,7 @@ Wicket.AutoComplete=function(elementId, 
                 return killEvent(event);
             }
             if (!ignoreOneFocusGain && cfg.showListOnFocusGain && visible==0) {
+               getAutocompleteMenu().showingAutocomplete = true;
                 if (cfg.showCompleteListOnFocusGain) {
                     updateChoices(true);
                 } else {
@@ -209,6 +214,29 @@ Wicket.AutoComplete=function(elementId, 
             }
                        if(typeof objonkeypress=="function") return 
objonkeypress.apply(this,[event]);
         }
+        if (Wicket.Focus.getFocusedElement() === obj && isShowing == true)
+        {
+               // element already has focus, we should show list
+               if (cfg.showListOnFocusGain) {
+                if (cfg.showCompleteListOnFocusGain) {
+                    updateChoices(true);
+                } else {
+                    updateChoices();
+                }
+            }
+        }
+    }
+    
+    function clearMenu()
+    {
+       // Remove the autocompletion menu if still present from
+               // a previous call. This is required to properly register
+               // the mouse event handler again (using the new stateful 
'mouseactive'
+               // variable which just gets created)
+        var choiceDiv=document.getElementById(getMenuId());
+        if (choiceDiv != null) {
+            choiceDiv.parentNode.parentNode.removeChild(choiceDiv.parentNode);
+        } 
     }
     
     function setSelected(newSelected) {
@@ -555,13 +583,19 @@ Wicket.AutoComplete=function(elementId, 
     }
     
     function doUpdateChoices(resp){
-    
-       // check if the input hasn't been cleared in the meanwhile
+       
+       getAutocompleteMenu().showingAutocomplete = false;
+       
+       // check if the input hasn't been cleared in the meanwhile or has been 
replaced by ajax
        var input=wicketGet(elementId);
-               if ((Wicket.Focus.getFocusedElement() != input) || 
!cfg.showListOnEmptyInput && (input.value==null || input.value=="")) {
+               if ((input != initialElement) || 
((Wicket.Focus.getFocusedElement() != input) || !cfg.showListOnEmptyInput && 
(input.value==null || input.value==""))) {
                        hideAutoComplete();
                        Wicket.Ajax.invokePostCallHandlers();
                        hideIndicator();
+                       if (input != initialElement)
+                       {
+                               clearMenu();
+                       }
                        return;
                }
 


Reply via email to