Author: andyhot
Date: Tue Feb 13 10:22:49 2007
New Revision: 507137

URL: http://svn.apache.org/viewvc?view=rev&rev=507137
Log:
JS support for new 'AjaxStatus' component

Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/core.js
    tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/fx.js

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/core.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/core.js?view=diff&rev=507137&r1=507136&r2=507137
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/core.js 
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/core.js Tue Feb 
13 10:22:49 2007
@@ -25,8 +25,9 @@
        version:"4.1.2",
        scriptInFlight:false, // whether or not javascript is currently being 
eval'd, default false
        ScriptFragment:'(?:<script.*?>)((\n|.|\r)*?)(?:<\/script>)', // regexp 
for script elements
-       
-       /**
+    requestsInFlight:0, // how many ajax requests are currently in progress
+
+    /**
         * Function: bind
         * 
         * Core XHR bind function for tapestry internals. The 
@@ -60,8 +61,9 @@
                        parms.encoding="UTF-8";
                        parms.load=(function(){tapestry.load.apply(this, 
arguments);});
                }
-               
-               dojo.io.queueBind(parms);
+
+        tapestry.requestsInFlight++;
+        dojo.io.queueBind(parms);
        },
        
        /**
@@ -75,7 +77,8 @@
         *      <tapestry.bind>
         */
        error:function(type, exception, http, kwArgs){
-               dojo.log.exception("Error received in IO response.", exception);
+        tapestry.requestsInFlight--;
+        dojo.log.exception("Error received in IO response.", exception);
        },
        
        /**
@@ -95,15 +98,16 @@
         * 
         */
        load:function(type, data, http, kwArgs){
-               dojo.log.debug("tapestry.load() Response recieved.", data);
-               if (!data) {
+               dojo.log.debug("tapestry.load() Response received.", data);
+        tapestry.requestsInFlight--;
+        if (!data) {
                        dojo.log.warn("No data received in response.");
                        return;
                }
                
                var resp=data.getElementsByTagName("ajax-response");
                if (!resp || resp.length < 1 || !resp[0].childNodes) {
-                       dojo.log.warn("No ajax-response elements recieved.");
+                       dojo.log.warn("No ajax-response elements received.");
                        return; 
                }
                
@@ -169,8 +173,9 @@
        },
        
        loadJson:function(type, data, http, kwArgs){
-               dojo.log.debug("tapestry.loadJson() Response recieved.", data);
-       },
+        dojo.log.debug("tapestry.loadJson() Response received.", data);
+        tapestry.requestsInFlight--;
+    },
        
        /**
         * Function: loadContent
@@ -359,7 +364,16 @@
                
                tapestry.bind(url, content, isJson);
                return false;
-       }
+       },
+
+    /**
+        * Function: isServingRequests
+        *
+        * Utility used to find out if there are any ajax requests in progress.
+        */
+    isServingRequests:function(){
+           return (tapestry.requestsInFlight > 0);
+    }
 }
 
 /**

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/fx.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/fx.js?view=diff&rev=507137&r1=507136&r2=507137
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/fx.js (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/fx.js Tue Feb 
13 10:22:49 2007
@@ -1,5 +1,6 @@
 dojo.provide("tapestry.fx");
 
+dojo.require("dojo.logging.Logger");
 dojo.require("tapestry.core");
 
 /**
@@ -15,7 +16,9 @@
     // property: postEffects
     // Contains a reference to all registered post-effects, i.e. effects that 
are
     // executed when new content arrives through an XHR response.
-    postEffects:{},    
+    postEffects:{},
+    // property: ajaxStatusAction
+    ajaxStatusAction:'loading',
     
     /**
      * Function: attachPreEffect
@@ -76,14 +79,49 @@
         this.preEffects={};
         this.postEffects={};
     },
+
+    /**
+     * Function: attachAjaxStatus
+     * Allows specifying a dom node that will be shown or hidden while ajax 
requests
+     * are in progress or have finished.
+     * Alternatively, one can specify a custom
+     * function which will get invoked when an ajax request starts or ends - 
the first
+     * argument to that function will be a boolean corresponding to wheather 
the status
+     * element should be showing or not.
+     *
+     * Parameters:
+     *  a1 - The dom id to show - hide, or the function to invoke when ajax 
starts or ends.
+     */
+    attachAjaxStatus:function(a1){
+        dojo.log.debug("Attaching ajax status listener");
+        if (dojo.lang.isString(a1)) {
+            tapestry.fx.ajaxStatusAction =
+                function(bShow){if (bShow) dojo.html.show(a1); else 
dojo.html.hide(a1);};
+        }
+        else if (dojo.lang.isFunction(a1)) {
+            tapestry.fx.ajaxStatusAction = a1;
+        }
+        else {
+            dojo.log.warn("Argument to tapestry.fx.attachAjaxStatus should be 
either a string or a function");
+            return;
+        }
+        dojo.event.connectOnce(tapestry, "bind", 
tapestry.fx._processAjaxStatus);
+        dojo.event.connectOnce(tapestry, "error", 
tapestry.fx._processAjaxStatus);
+        dojo.event.connectOnce(tapestry, "load", 
tapestry.fx._processAjaxStatus);
+        dojo.event.connectOnce(tapestry, "loadJson", 
tapestry.fx._processAjaxStatus);
+    },
+
+    _processAjaxStatus:function(){
+        tapestry.fx.ajaxStatusAction.apply(this, 
[tapestry.isServingRequests()]);
+    },
     
     _initPreEffects:function(){
-        dojo.debug("Advising tapestry.linkOnClick");
+        dojo.log.debug("Advising tapestry.linkOnClick");
         dojo.event.connectAround(tapestry, "linkOnClick", tapestry.fx, 
"_applyPreEffects");
     },
     
     _initPostEffects:function(){
-        dojo.debug("Advising tapestry.loadContent");
+        dojo.log.debug("Advising tapestry.loadContent");
         dojo.event.connectAround(tapestry, "loadContent", tapestry.fx, 
"_applyPostEffects");
     },
     
@@ -91,7 +129,7 @@
         var id = miObj.args[1];        
         var effect = this.preEffects[id];
         if (effect){
-            dojo.debug("Found pre-effect:", effect, id);
+            dojo.log.debug("Found pre-effect:", effect, id);
                        
             var anim = effect.animation();
             
@@ -114,7 +152,7 @@
         var id = miObj.args[0];
         var effect = this.postEffects[id];
         if (effect){
-            dojo.debug("Found post-effect:", effect, id);
+            dojo.log.debug("Found post-effect:", effect, id);
             
             var ret = miObj.proceed();
             


Reply via email to