Author: jcompagner
Date: Mon Jun  2 01:23:39 2008
New Revision: 662360

URL: http://svn.apache.org/viewvc?rev=662360&view=rev
Log:
throttling auto complete.
Improved current throttling that it has a extra boolean so that you can really 
throttle (as long as new events are created they are ignored within the time 
out)

Modified:
    wicket/branches/wicket-1.3.x/jdk-1.4/wicket/   (props changed)
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js

Propchange: wicket/branches/wicket-1.3.x/jdk-1.4/wicket/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jun  2 01:23:39 2008
@@ -1 +1 @@
-/wicket/trunk/wicket:647219,654055,655101,655115,655118,655295,655297,655329,656285,656380,657980,658017
+/wicket/trunk/wicket:647219,654055,655101,655115,655118,655295,655297,655329,656285,656380,657980,658017,658135

Modified: 
wicket/branches/wicket-1.3.x/jdk-1.4/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.3.x/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js?rev=662360&r1=662359&r2=662360&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
 (original)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js
 Mon Jun  2 01:23:39 2008
@@ -57,6 +57,11 @@
        // it is computed when the menu is first rendered, and then reused.
        var initialDelta = -1;
 
+       // holds a throttler, for not sending many requests if the user types
+       // too quickly.
+       var localThrottler = new Wicket.Throttler(true);
+       var throttleDelay = 300;
+
     function initialize(){
                // Remove the autocompletion menu if still present from
                // a previous call. This is required to properly register
@@ -236,6 +241,11 @@
         else{
                selected=-1;
         }
+        localThrottler.throttle(getMenuId(), throttleDelay, 
actualUpdateChoices);
+    }
+
+    function actualUpdateChoices()
+    {
         var value = wicketGet(elementId).value;
                var request = new 
Wicket.Ajax.Request(callbackUrl+"&q="+processValue(value), doUpdateChoices, 
false, true, false, "wicket-autocomplete|d");
                request.get();
@@ -380,12 +390,14 @@
                var re = /\bselected\b/gi;
         for(var i=0;i<elementCount;i++)
                {
-                       var classNames = node.className.replace(re, "");
+            var origClassNames = node.className;
+                       var classNames = origClassNames.replace(re, "");
                        if(selected==i){
                                classNames += " selected";
                                adjustScrollOffset(menu.parentNode, node);
                        }
-                       node.className = classNames;
+                       if (classNames != origClassNames)
+                node.className = classNames;
        
                        if ((cfg.maxHeight > -1) && (height < cfg.maxHeight))
                                height+=node.offsetHeight;
@@ -482,4 +494,4 @@
     }
 
     initialize();
-}
\ No newline at end of file
+}

Modified: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js?rev=662360&r1=662359&r2=662360&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
 (original)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
 Mon Jun  2 01:23:39 2008
@@ -1554,6 +1554,7 @@
        initialize: function(func) {
                this.func = func;
                this.timestamp = new Date().getTime();
+               this.timeoutVar = undefined;
        },
        
        getTimestamp: function() {
@@ -1566,13 +1567,30 @@
        
        setFunc: function(func) {
                this.func = func;
+       },
+
+       getTimeoutVar: function() {
+        return this.timeoutVar;
+       },
+
+       setTimeoutVar: function(timeoutVar) {
+        this.timeoutVar = timeoutVar;
        }
 };
 
 Wicket.Throttler = Wicket.Class.create();
 Wicket.Throttler.prototype = {
-       initialize: function() {
+
+    /* "postponeTimerOnUpdate" is an optional parameter. If it is set to true, 
then the timer is
+       reset each time the throttle function gets called. Use this behaviour 
if you want something
+       to happen at X milliseconds after the *last* call to throttle.
+       If the parameter is not set, or set to false, then the timer is not 
reset. */
+       initialize: function(postponeTimerOnUpdate) {
                this.entries = new Array();
+               if (postponeTimerOnUpdate != undefined)
+            this.postponeTimerOnUpdate = postponeTimerOnUpdate;
+        else
+            this.postponeTimerOnUpdate = false;
        },
        
        throttle: function(id, millis, func) {
@@ -1580,10 +1598,15 @@
                var me = this;
                if (entry == undefined) {
                        entry = new Wicket.ThrottlerEntry(func);
+                       entry.setTimeoutVar(window.setTimeout(function() { 
me.execute(id); }, millis));
                        this.entries[id] = entry;
-                       window.setTimeout(function() { me.execute(id); }, 
millis);
                } else {
                        entry.setFunc(func);
+            if (this.postponeTimerOnUpdate == true)
+            {
+                window.clearTimeout(entry.getTimeoutVar());
+                entry.setTimeoutVar(window.setTimeout(function() { 
me.execute(id); }, millis));
+            }
                }       
        },
        
@@ -1591,10 +1614,9 @@
                var entry = this.entries[id];
                if (entry != undefined) {
                        var func = entry.getFunc();
+            this.entries[id] = undefined;      
                        var tmp = func();
                }
-               
-               this.entries[id] = undefined;   
        }
 };
 
@@ -2067,4 +2089,4 @@
        if (e!=null) {
            e.style.display = "none";
        }
-}
\ No newline at end of file
+}


Reply via email to