Author: knopp
Date: Tue Aug 26 15:15:44 2008
New Revision: 689262

URL: http://svn.apache.org/viewvc?rev=689262&view=rev
Log:
preventDefault

Modified:
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxEventBehavior.java
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
    
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
 Tue Aug 26 15:15:44 2008
@@ -106,6 +106,10 @@
                config.append(prefix);
                config.append("';\n");
 
+               config.append("gs.defaultPageId='");
+               config.append(getPageId(component.getPage()));
+               config.append("';\n");
+               
                config.append("gs.urlParamComponentId='");
                config.append(AjaxUrlCodingStrategy.PARAM_COMPONENT_ID);
                config.append("';\n");
@@ -201,8 +205,6 @@
        {
                JSONObject o = new JSONObject();
 
-               o.put("p", getPageId(component.getPage()));
-
                if (component instanceof Page == false)
                {
                        o.put("c", component.getMarkupId());
@@ -262,7 +264,7 @@
                        o.put("f", attributes.getForm().getMarkupId());
                }
                o.put("m", attributes.isMultipart());
-               o.put("t", attributes.getRequesTimeout());
+               o.put("rt", attributes.getRequesTimeout());
                o.put("pt", attributes.getProcessingTimeout());
                o.put("t", attributes.getToken());
                o.put("r", attributes.isRemovePrevious());
@@ -273,6 +275,7 @@
                renderFunctionList(o, "be", attributes.getBeforeHandlers());
                renderFunctionList(o, "s", attributes.getSuccessHandlers());
                renderFunctionList(o, "e", attributes.getErrorHandlers());
+               renderFunctionList(o, "rqi", 
attributes.getRequestQueueItemCreationListeners());
 
                Map<String, Object> urlArguments = attributes.getUrlArguments();
                if (urlArguments != null && !urlArguments.isEmpty())

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxEventBehavior.java
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxEventBehavior.java?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxEventBehavior.java
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxEventBehavior.java
 Tue Aug 26 15:15:44 2008
@@ -59,8 +59,15 @@
                js.append(event);
                js.append("',");
                js.append(renderAttributes(component));
+               
+               boolean allowDefault = getAttributes().allowDefault(); 
+               if (allowDefault)
+               {
+                       js.append("," + allowDefault);
+               }
                js.append(")");
 
                response.renderOnDomReadyJavascript(js.toString());
        }
+       
 }

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
 Tue Aug 26 15:15:44 2008
@@ -21,18 +21,18 @@
 import org.apache.wicket.markup.html.form.Form;
 
 /**
- * Attributes for an Ajax Request. 
+ * Attributes for an Ajax Request.
  * 
  * <hr>
- *  
+ * 
  * <p>
  * Note that some of these attributes represent javascript functions. Those 
functions get a
  * <code>RequestQueueItem</code> instance as first argument. The instance 
provides access to
  * following properties that the javascript functions can use:
  * <dl>
  * <dt>attributes</dt>
- * <dd> Object with request queue item attributes. The <code>attributes</code> 
Object contains following
- * properties:
+ * <dd> Object with request queue item attributes. The <code>attributes</code> 
Object contains
+ * following properties:
  * <dl>
  * 
  * <dt>component</dt>
@@ -262,4 +262,24 @@
         * @return FunctionList or <code>null</code>
         */
        FunctionList getUrlArgumentMethods();
+
+       /**
+        * Array of javascript functions invoked when a 
<code>RequestQueueItem</code> instance is
+        * created. The <code>RequestQueueItem</code> instance will be passed 
as first argument.
+        * 
+        * @return FunctionList or <code>null</code>
+        */
+       FunctionList getRequestQueueItemCreationListeners();
+
+       /**
+        * Only applies for event behaviors. Returns whether the behavior 
should allow the default event
+        * handler to be invoked. For example if the behavior is attached to a 
link and
+        * [EMAIL PROTECTED] #allowDefault()} returns <code>false</code> (which 
is default value), the link's URL
+        * will not be followed. If [EMAIL PROTECTED] #allowDefault()} returns 
<code>true</code>, the link URL
+        * will be loaded (and the onclick handler fired if there is any).
+        * 
+        * @return <code>true</code> if the default event handler should be 
invoked,
+        *         <code>false</code> otherwise.
+        */
+       boolean allowDefault();
 }

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java
 Tue Aug 26 15:15:44 2008
@@ -26,21 +26,22 @@
  * delegating the calls to another [EMAIL PROTECTED] AjaxRequestAttributes} 
instance if one is specified.
  * <p>
  * To extend attributes from behavior or component the following pattern can 
be used:
+ * 
  * <pre>
- *      // add a precondition to super attirbutes
- *      class MyBehavior extends AjaxBehavior
- *      {
- *          public AjaxRequestAttributes getAttributes()
+ * // add a precondition to super attirbutes
+ * class MyBehavior extends AjaxBehavior
+ * {
+ *     public AjaxRequestAttributes getAttributes()
  *          {
  *              return new AjaxRequestAttributesImpl(super.getAttributes) 
  *              {
  *                  public FunctionList getPreconditions()
  *                  {
- *                      return 
super.getPreconditions().add("function(requestQueueItem) { return true; }";);
+ *                      return 
super.getPreconditions().add(&quot;function(requestQueueItem) { return true; 
}&quot;;);
  *                  }
  *              }
  *          }
- *      }              
+ * }
  * </pre>
  * 
  * @author Matej Knopp
@@ -247,4 +248,23 @@
                        return null;
                }
        }
+
+       public FunctionList getRequestQueueItemCreationListeners()
+       {
+               FunctionList result = null;
+               if (delegate != null)
+               {
+                       result = 
delegate.getRequestQueueItemCreationListeners();
+               }
+               if (result == null)
+               {
+                       result = new FunctionList();
+               }
+               return result;
+       }
+
+       public boolean allowDefault()
+       {
+               return false;
+       }
 }

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
 Tue Aug 26 15:15:44 2008
@@ -145,7 +145,8 @@
                trace: function() { },
                debug: function() { },
                info: function() { },
-               error: function() { }
+               error: function() { },
+               warn: function() { }
        }               
        
        var FirebugLogger =
@@ -195,6 +196,10 @@
                error: function() 
                {
                        console.error.apply(this, FirebugLogger.arg("ERROR", 
arguments));
+               },
+               warn: function() 
+               {
+                       console.warn.apply(this, FirebugLogger.arg("WARN", 
arguments));
                }
        }
        
@@ -210,12 +215,15 @@
                        console.info = console.log;
                if (typeof(console.debug) == "undefined")
                        console.debug = console.log;
+               if (typeof(console.warn) == "undefined")
+                       console.warn = console.log;
        }                               
        
        var logConfig = 
        { 
-               disableAll: false, trace: true, debug: true, info: true, error: 
true, 
-               "trace:GarbageCollector": false, "trace:Contribution":false, 
"trace:Events":true 
+               disableAll: false, trace: true, debug: true, info: true, error: 
true, warn: true, 
+               "trace:GarbageCollector": false, "trace:Contribution":false, 
"trace:Events":false, "trace:Focus":false,
+               "trace:RequestQueue": false, "trace:General":false, 
"trace:Throttler": true
        };
        
        W.Log  = 
@@ -240,6 +248,11 @@
                        if (!logConfig.disableAll && logConfig.error && 
logConfig[arguments[0]] != false && logConfig["error:" + arguments[0]] != false)
                                logger.error.apply(this, arguments);
                },
+               warn: function()
+               {
+                       if (!logConfig.disableAll && logConfig.warn && 
logConfig[arguments[0]] != false && logConfig["warn:" + arguments[0]] != false)
+                               logger.warn.apply(this, arguments);
+               },
                setLogger: function(newLogger)
                {
                        logger = newLogger;
@@ -764,8 +777,21 @@
                                return;
                        }
                        var entry = this.entries[token];
-                       if (entry == null)
+                       
+                       var e = this.executionTimes[token];                     
                
+                       if (!postponeTimerOnUpdate && e != null)
                        {
+                               // get millis to reflect the real time after 
last invocation update
+                               var now = new Date().getTime();
+                               var d = now - e;
+                               if (d < millis)
+                               {
+                                       millis -= d;
+                               }
+                       }
+                       
+                       if (entry == null)
+                       {       
                                entry = new ThrottlerEntry(func);
                                entry.timeout = 
window.setTimeout(bind(function() { this.execute(token) }, this), millis);
                                this.entries[token] = entry;
@@ -776,8 +802,8 @@
                                entry.func = func;
                                if (postponeTimerOnUpdate)
                                {
-                                       window.clearTimeout(entry.timeout);
-                                       entry.timeout = 
window.setTimeout(bind(this.execute, this), millis);
+                                       window.clearTimeout(entry.timeout);     
                        
+                                       entry.timeout = 
window.setTimeout(bind(function() { this.execute(token) }, this), millis);
                                        log.trace("Throttler", "Postponing 
throttle, token:", token, ", millis:", millis, ", func:", func);
                                }
                                else
@@ -796,7 +822,12 @@
                        if (e == null || (e + millis) < now)
                        {
                                log.trace("Throttler", "Executing function 
immediately, token:", token, ", millis:", millis, ", func:", func);
-                               this.executionTimes[token] = now;
+                               this.executionTimes[token] = now;               
                
+                               var e = this.entries[token];
+                               if (e != null)
+                               {
+                                       window.clearTimeout(e.timeout);
+                               }
                                this.entries[token] = null;
                                func();
                                return true;
@@ -1345,7 +1376,7 @@
         *                                        negative impact on error 
detection.
         *                                        (doesn't work with current 
YUI 3 PR1 release)
         *                                         
-        *   t, requestTimeout       - Integer    Timeout in milliseconds for 
the AJAX request. This only 
+        *   rt, requestTimeout       - Integer   Timeout in milliseconds for 
the AJAX request. This only 
         *                                        involves the actual 
communication and not the processing 
         *                                        afterwards. Can be null in 
which case the default request
         *                                        timeout will be used.
@@ -1426,6 +1457,10 @@
      *                                        URL arguments. Each of the 
methods will get this 
      *                                        RequestQueueItem passed and must 
return a 
      *                                        Map<String, String> (Object).
+     *                                        
+     *   rqi, requestQueueItem  - Method(s)   Optional. Method or array of 
methods that will be invoked
+     *                                        after the RequestQueueItem 
instance is created. The methods 
+     *                                        will get the request queue 
instance passed as first argument.
         */
        var RequestQueueItem = function(attributes)
        {
@@ -1460,7 +1495,7 @@
                        component:            a.component          || a.c    || 
null,
                        formId:               a.formId             || a.f    || 
null,
                        multipart:          b(a.multipart          || a.m),
-                       requestTimeout:       a.requestTimeout     || a.t    || 
gs.defaultRequestTimeout,
+                       requestTimeout:       a.requestTimeout     || a.rt   || 
gs.defaultRequestTimeout,
                        processingTimeout:    a.processingTimeout  || a.pt   || 
gs.defaultProcessingTimeout,
                        pageId:               a.pageId             || a.p    || 
gs.defaultPageId,
                        listenerInterface:    a.listenerInterface  || a.l    || 
null,
@@ -1474,16 +1509,37 @@
                        successHandlers:    m(a.successHandlers    || a.s,   
gs.successHandlers),
                        errorHandlers:      m(a.errorHandlers      || a.e,   
gs.errorHandlers),
                        urlArguments:         a.urlArguments       || a.u    || 
null,
-                       urlArgumentMethods: m(a.urlArgumentMethods || a.ua,  
gs.urlArgumentMethods)
+                       urlArgumentMethods: m(a.urlArgumentMethods || a.ua,  
gs.urlArgumentMethods),
+                       requestQueueItem:   m(a.requestQueueItem   || a.rqi, 
gs.requestQueueItem)
                }
                
-               log.trace("RequestQueue", "Creating New Item", this.attributes);
+               log.trace("RequestQueue", "Creating New Item", 
this.attributes);                                
        }
                
        W.RequestQueueItem = RequestQueueItem;          
        
        RequestQueueItem.prototype = 
-       {
+       {               
+               init: function()
+               {
+                       if (this.initialized != true)
+                       {
+                               this.initialized = true;                        
+                               var l = this.attributes.requestQueueItem;
+                               for (var i = 0; i < l.length; ++i)
+                               {
+                                       var handler = l[i];
+                                       try 
+                                       {
+                                               handler(this);
+                                       } 
+                                       catch (exception)
+                                       {
+                                               log.error("RequestQueue", 
"Error invoking RequestQueueItem creation listener", exception);
+                                       }
+                               }
+                       }
+               },
                checkPreconditions: function() 
                {                       
                        var res = iterateArray(this.attributes.preconditions, 
bind(function(precondition) 
@@ -1972,6 +2028,9 @@
                                log.error("RequestQueue", "Item ", item, " must 
contain attributes.");
                                return;
                        }
+                       
+                       item.init();
+                       
                        var a = item.attributes;                        
                        if (a.throttlePostpone == true && a.throttle == null) 
                        {
@@ -2101,6 +2160,7 @@
                errorHandlers: [],
                urlPostProcessors: [],
                urlArgumentMethods: [ defaultArgumentMethod ],
+               requestQueueItem: [],
                urlPrefix: "INVALID_URL_PREFIX",
                urlParamComponentId: "INVALID_COMPONENT_ID_PARAM",
                urlParamTimestamp: "INVALID_TIMESTAMP_PARAM",
@@ -2374,7 +2434,7 @@
                return element.wicketEventHandlers;
        }
        
-       W.e = function(event, attributes)
+       W.e = function(event, attributes, allowDefault)
        {
                var element;
                if (attributes.c == null)
@@ -2403,6 +2463,10 @@
                        var item = new RequestQueueItem(attributes);
                        item.event = event;
                        W.ajax.requestQueue.add(item);
+                       if (allowDefault != true)
+                       {
+                               event.preventDefault();
+                       }
                }
                
                handle = Y.on(event, f, element);

Modified: 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java?rev=689262&r1=689261&r2=689262&view=diff
==============================================================================
--- 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java
 (original)
+++ 
wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java
 Tue Aug 26 15:15:44 2008
@@ -18,6 +18,8 @@
 
 import org.apache.wicket.Component;
 import org.apache.wicket.ajaxng.AjaxEventBehavior;
+import org.apache.wicket.ajaxng.AjaxRequestAttributes;
+import org.apache.wicket.ajaxng.AjaxRequestAttributesImpl;
 import org.apache.wicket.ajaxng.request.AjaxRequestTarget;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.link.AbstractLink;
@@ -29,6 +31,8 @@
  * @since 1.2
  * 
  * @author Igor Vaynberg (ivaynberg)
+ * @author Matej Knopp
+ * @param <T> 
  * 
  */
 public abstract class AjaxLink<T> extends AbstractLink implements IAjaxLink
@@ -71,9 +75,20 @@
                        {
                                return isLinkEnabled();
                        }
+                       
+                       @Override
+                       public AjaxRequestAttributes getAttributes()
+                       {
+                               return AjaxLink.this.getAttributes();
+                       }
                });
        }
 
+       public AjaxRequestAttributes getAttributes()
+       {
+               return new AjaxRequestAttributesImpl();
+       }
+       
        @Override
        protected void onComponentTag(ComponentTag tag)
        {


Reply via email to