Author: ffacon
Date: Mon Jul  4 22:16:09 2011
New Revision: 1142826

URL: http://svn.apache.org/viewvc?rev=1142826&view=rev
Log:
TAP5-746 : Zone should include an option to periodically update itself 
Taha Hafeez : replacing Thread.sleep() with waitForCondition(prototype) . 
implementation use of js event and zone.getBody() 

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningVoid.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningZone.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningVoid.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningZone.java
Removed:
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshDemo.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshDemo.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ZoneRefresh.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/mixins/zone-refresh.js
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/ZoneRefreshTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ZoneRefresh.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ZoneRefresh.java?rev=1142826&r1=1142825&r2=1142826&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ZoneRefresh.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ZoneRefresh.java
 Mon Jul  4 22:16:09 2011
@@ -101,7 +101,7 @@ public class ZoneRefresh
          return callback.getResult();
       }
       
-      return zone;
+      return zone.getBody();
    }
 
 }
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/mixins/zone-refresh.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/mixins/zone-refresh.js?rev=1142826&r1=1142825&r2=1142826&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/mixins/zone-refresh.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/mixins/zone-refresh.js
 Mon Jul  4 22:16:09 2011
@@ -13,63 +13,45 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 // 
-if (!Tapestry.ZoneRefresh)
-{
-   Tapestry.ZoneRefresh = {};
-}
+Tapestry.ZONE_REFRESH_EVENT = "tapestry:zonerefresh";
 
 Tapestry.Initializer.zoneRefresh = function(params)
 {
-   //  Ensure a valid period. Not required as PeriodicalUpdater already takes 
care of it
-   // but will will skip unnecessary steps
-   if(params.period <= 0)
-   {
-      return;   
-   }
-         
-   // If the timer is already present, don't create a new one
-   if (Tapestry.ZoneRefresh[params.id])
+   var zoneRefreshId = params.id + "_refresh";
+   
+   // This will prevent any more instantiation of  PeriodicalExecuter
+   if($(zoneRefreshId))
    {
-      // Timer already in use
       return;
    }
+   
+   // Create a new element and place it at the end of the document. Then we use
+   // it for refreshing the zone
+   var zoneRefresh = document.createElement("div");
+   zoneRefresh.id = zoneRefreshId;
+   
+   // Link zoneRefresh element to zone
+   $T(zoneRefresh).zoneId = params.id;
+   document.body.appendChild(zoneRefresh);
+   
+   // Connect event to zone
+   Tapestry.Initializer.updateZoneOnEvent(Tapestry.ZONE_REFRESH_EVENT, 
zoneRefresh, params.id, params.URL);
 
-   // Set zone
-   var element = $(params.id);
-   $T(element).zoneId = params.id;
+   //Create timer
+   var timer = new PeriodicalExecuter(function(e)
+   {
+      zoneRefresh.fire(Tapestry.ZONE_REFRESH_EVENT);
+   }, params.period);
 
-   // Function to be called for each refresh
-   var keepUpdatingZone = function(e)
+   
+   //Clear the timer before unload
+   Event.observe(window, "beforeunload", function()
    {
-      try 
+      if(timer)
       {
-         var zoneManager = Tapestry.findZoneManager(element);
-         zoneManager.updateFromURL(params.URL);
+         timer.stop();   
       }
-      catch(e)
-      {
-         e.stop();
-         Tapestry.error(Tapestry.Messages.invocationException, {
-            fname : "Tapestry.Initializer.zoneRefresh",
-            params : params,
-            exception : e
-            });
-      }
-   };
-
-   // Create and store the executor
-   Tapestry.ZoneRefresh[params.id] = new PeriodicalExecuter(keepUpdatingZone, 
params.period);
+   })
 };
 
-// Before unload clear all the timers
-Event.observe(window, "beforeunload", function()
-{
-   if (Tapestry.ZoneRefresh)
-   {
-      for ( var propertyName in Tapestry.ZoneRefresh)
-      {
-         var property = Tapestry.ZoneRefresh[propertyName];
-         property.stop();
-      }
-   }
-});
+

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml?rev=1142826&r1=1142825&r2=1142826&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Index.tml Mon Jul  4 
22:16:09 2011
@@ -130,8 +130,12 @@
       -- error reporting for adding a duplicate mixin to a component
     </li>
     <li>
-       <a href="ZoneRefreshDemo">Zone Refresh Demo</a>
-       -- refreshes zone after 5 seconds
+       <a href="ZoneRefreshWithHandlerReturningVoid">Zone Refresh With Event 
Handler Returning Void</a>
+       -- refreshes zone with an event handler returning void
+    </li>
+        <li>
+       <a href="ZoneRefreshWithHandlerReturningZone">Zone Refresh With Event 
Handler Returning Zone</a>
+       -- refreshes zone with an event handler returning zone
     </li>
     <li>
       <a href="UnsupportedParameterBlockDemo">Unsupported Parameter Block 
Demo</a>

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningVoid.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningVoid.tml?rev=1142826&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningVoid.tml
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningVoid.tml
 Mon Jul  4 22:16:09 2011
@@ -0,0 +1,12 @@
+<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_1_0.xsd'>
+   <head>
+   </head>
+   
+   <body>
+      Using zone with event handler returning void : 
+      <span t:type='zone' t:mixins='zonerefresh' t:period='2' id='zone' 
t:id='zone'>
+         ${counter}
+      </span>
+   </body>
+   
+</html>
\ No newline at end of file

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningZone.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningZone.tml?rev=1142826&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningZone.tml
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ZoneRefreshWithHandlerReturningZone.tml
 Mon Jul  4 22:16:09 2011
@@ -0,0 +1,15 @@
+<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_1_0.xsd'>
+   <head>
+   </head>
+
+   <body>
+      Using zone with event handler returning zone :
+      <span t:type='zone' t:mixins='zonerefresh' t:period='2' id='zone' 
t:id='zone'>
+         ${counter}
+      </span>
+      <br />
+      <span t:type='zone' id='zone2' t:id='zone2'>
+         and another zone ${counter}
+      </span>
+   </body>
+</html>
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/ZoneRefreshTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/ZoneRefreshTest.java?rev=1142826&r1=1142825&r2=1142826&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/ZoneRefreshTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/ZoneRefreshTest.java
 Mon Jul  4 22:16:09 2011
@@ -20,37 +20,33 @@ import org.testng.annotations.Test;
 public class ZoneRefreshTest extends SeleniumTestCase
 {
    @Test
-   public void test_if_zone_with_void_event_handler_works() throws Exception
+   public void test_if_zone_with_event_handler_returning_void_works() throws 
Exception
    {
       openBaseURL();
-      clickAndWait("link=Zone Refresh Demo");
+      clickAndWait("link=Zone Refresh With Event Handler Returning Void");
       checkZoneValues("zone", 3);
    }
-   
-   @Test
-   public void test_if_zone_with_single_zone_event_handler_works() throws 
Exception
-   {
-      openBaseURL();
-      clickAndWait("link=Zone Refresh Demo");
-      checkZoneValues("zone2", 3);
-   }
-   
+
    @Test
-   public void test_if_zone_with_multiple_zone_event_handler_works() throws 
Exception
+   public void test_if_zone_with_event_handler_returning_zone_works() throws 
Exception
    {
       openBaseURL();
-      clickAndWait("link=Zone Refresh Demo");
-      checkZoneValues("zone4", 3);
+      clickAndWait("link=Zone Refresh With Event Handler Returning Zone");
+      checkZoneValues("zone", 3);
    }
-   
 
    private void checkZoneValues(String zone, int times) throws Exception
    {
-      Thread.sleep(300);
-      for(int i = 0; i < times; ++i)
+      for(int i = 1; i <= times; ++i)
       {
+         //Wait for ajax call to begin
+         
waitForCondition("selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount
 != 0", "20000");
+         
+         //Wait for ajax call from end
+         
waitForCondition("selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount
 == 0", "20000");
+         
+         //Check the value changed
          assertText(zone, String.valueOf(i));
-         Thread.sleep(1000);
       }
    }
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningVoid.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningVoid.java?rev=1142826&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningVoid.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningVoid.java
 Mon Jul  4 22:16:09 2011
@@ -0,0 +1,35 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package org.apache.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+
+public class ZoneRefreshWithHandlerReturningVoid
+{
+   @Persist
+   @Property
+   private int counter;
+   
+   void setupRender()
+   {
+      counter = 0;
+   }
+   
+   void onRefreshFromZone(){
+      counter++;
+   }
+
+}
+

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningZone.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningZone.java?rev=1142826&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningZone.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ZoneRefreshWithHandlerReturningZone.java
 Mon Jul  4 22:16:09 2011
@@ -0,0 +1,45 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package org.apache.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.ajax.MultiZoneUpdate;
+import org.apache.tapestry5.annotations.InjectComponent;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.corelib.components.Zone;
+
+public class ZoneRefreshWithHandlerReturningZone
+{
+   @Persist
+   @Property
+   private int counter;
+   
+   @InjectComponent
+   private Zone zone;
+   
+   @InjectComponent
+   private Zone zone2;
+
+   
+   void setupRender()
+   {
+      counter = 0;
+   }
+   
+   Object onRefreshFromZone(){
+      counter++;
+      return new MultiZoneUpdate(zone).add(zone2);
+   }
+
+}


Reply via email to