You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2010/02/05 19:56:28 UTC

svn commit: r907053 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/services/ main/java/org/apache/tapestry5/services/ main/java/org/apache/tapestry5/test/ main/resources/org/apache/tapestry5/ test/java/org/apa...

Author: hlship
Date: Fri Feb  5 18:56:27 2010
New Revision: 907053

URL: http://svn.apache.org/viewvc?rev=907053&view=rev
Log:
Record ClientBehaviorSupportImpl around JavascriptSupport instead of RenderSupport

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/test/TapestryTestCase.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java?rev=907053&r1=907052&r2=907053&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImpl.java Fri Feb  5 18:56:27 2010
@@ -1,10 +1,10 @@
-// Copyright 2007, 2008, 2009 The Apache Software Foundation
+// Copyright 2007, 2008, 2009, 2010 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
+// 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,
@@ -16,81 +16,59 @@
 
 import org.apache.tapestry5.Field;
 import org.apache.tapestry5.Link;
-import org.apache.tapestry5.RenderSupport;
 import org.apache.tapestry5.corelib.data.InsertPosition;
-import org.apache.tapestry5.ioc.internal.util.Defense;
 import org.apache.tapestry5.json.JSONArray;
 import org.apache.tapestry5.json.JSONObject;
 import org.apache.tapestry5.services.ClientBehaviorSupport;
+import org.apache.tapestry5.services.javascript.JavascriptSupport;
 
 public class ClientBehaviorSupportImpl implements ClientBehaviorSupport
 {
-    private final RenderSupport renderSupport;
+    private final JavascriptSupport javascriptSupport;
 
     private final JSONObject validations = new JSONObject();
 
-    public ClientBehaviorSupportImpl(RenderSupport renderSupport)
+    public ClientBehaviorSupportImpl(JavascriptSupport javascriptSupport)
     {
-        this.renderSupport = renderSupport;
+        this.javascriptSupport = javascriptSupport;
     }
 
     public void addZone(String clientId, String showFunctionName, String updateFunctionName)
     {
-        JSONObject spec = new JSONObject();
+        JSONObject spec = new JSONObject("element", clientId);
 
         addFunction(spec, "show", showFunctionName);
         addFunction(spec, "update", updateFunctionName);
 
-        addElementInit("zone", clientId, spec);
+        javascriptSupport.addInitializerCall("zone", spec);
     }
 
-    private void addElementInit(String functionName, String clientId, JSONObject spec)
-    {
-        Defense.notBlank(clientId, "clientId");
-
-        if (spec.length() == 0)
-        {
-            renderSupport.addInit(functionName, clientId);
-            return;
-        }
-
-        spec.put("element", clientId);
-
-        renderSupport.addInit(functionName, spec);
-    }
-
-
     private void addFunction(JSONObject spec, String key, String showFunctionName)
     {
-        if (showFunctionName != null) spec.put(key, showFunctionName.toLowerCase());
+        if (showFunctionName != null)
+            spec.put(key, showFunctionName.toLowerCase());
     }
 
     public void linkZone(String linkId, String elementId, Link eventLink)
     {
-        JSONArray spec = new JSONArray();
-        spec.put(linkId);
-        spec.put(elementId);
-        spec.put(eventLink.toAbsoluteURI());
+        JSONObject spec = new JSONObject("linkId", linkId, "zoneId", elementId, "url", eventLink.toAbsoluteURI());
 
-        renderSupport.addInit("linkZone", spec);
+        javascriptSupport.addInitializerCall("linkZone", spec);
     }
 
     public void addFormFragment(String clientId, String showFunctionName, String hideFunctionName)
     {
-        JSONObject spec = new JSONObject();
+        JSONObject spec = new JSONObject("element", clientId);
 
         addFunction(spec, "show", showFunctionName);
         addFunction(spec, "hide", hideFunctionName);
 
-        addElementInit("formFragment", clientId, spec);
+        javascriptSupport.addInitializerCall("formFragment", spec);
     }
 
     public void addFormInjector(String clientId, Link link, InsertPosition insertPosition, String showFunctionName)
     {
-        JSONObject spec = new JSONObject();
-        spec.put("element", clientId);
-
-        spec.put("url", link.toAbsoluteURI());
+        JSONObject spec = new JSONObject("element", clientId, "url", link.toAbsoluteURI());
 
         if (insertPosition == InsertPosition.BELOW)
             spec.put("below", true);
@@ -99,17 +77,17 @@
 
         // Always has at least two properties.
 
-        renderSupport.addInit("formInjector", spec);
+        javascriptSupport.addInitializerCall("formInjector", spec);
     }
 
-
     public void addValidation(Field field, String validationName, String message, Object constraint)
     {
         String fieldId = field.getClientId();
 
         JSONArray specs;
 
-        if (validations.has(fieldId)) specs = validations.getJSONArray(fieldId);
+        if (validations.has(fieldId))
+            specs = validations.getJSONArray(fieldId);
         else
         {
             specs = new JSONArray();
@@ -121,7 +99,8 @@
         thisSpec.put(validationName);
         thisSpec.put(message);
 
-        if (constraint != null) thisSpec.put(constraint);
+        if (constraint != null)
+            thisSpec.put(constraint);
 
         specs.put(thisSpec);
     }
@@ -132,15 +111,7 @@
      */
     public void commit()
     {
-        for (String field : validations.keys())
-        {
-            JSONArray specs = validations.getJSONArray(field);
-
-            JSONArray parameters = new JSONArray();
-            parameters.put(field);
-            parameters.put(specs);
-
-            renderSupport.addInit("validate", parameters);
-        }
+        if (validations.length() != 0)
+            javascriptSupport.addInitializerCall("validate", validations);
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=907053&r1=907052&r2=907053&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Fri Feb  5 18:56:27 2010
@@ -1958,9 +1958,9 @@
         {
             public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
             {
-                RenderSupport renderSupport = environment.peekRequired(RenderSupport.class);
+                JavascriptSupport javascriptSupport = environment.peekRequired(JavascriptSupport.class);
 
-                ClientBehaviorSupportImpl clientBehaviorSupport = new ClientBehaviorSupportImpl(renderSupport);
+                ClientBehaviorSupportImpl clientBehaviorSupport = new ClientBehaviorSupportImpl(javascriptSupport);
 
                 environment.push(ClientBehaviorSupport.class, clientBehaviorSupport);
 
@@ -2008,7 +2008,7 @@
         configuration.add("JavascriptSupport", javascriptSupport, "after:DocumentLinker");
         configuration.add("RenderSupport", renderSupport, "after:JavascriptSupport");
         configuration.add("InjectDefaultStyleheet", injectDefaultStylesheet, "after:RenderSupport");
-        configuration.add("ClientBehaviorSupport", clientBehaviorSupport, "after:RenderSupport");
+        configuration.add("ClientBehaviorSupport", clientBehaviorSupport, "after:JavascriptSupport");
         configuration.add("Heartbeat", heartbeat, "after:RenderSupport");
         configuration.add("DefaultValidationDecorator", defaultValidationDecorator, "after:Heartbeat");
     }
@@ -2105,9 +2105,9 @@
         {
             public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
             {
-                RenderSupport renderSupport = environment.peekRequired(RenderSupport.class);
+                JavascriptSupport javascriptSupport = environment.peekRequired(JavascriptSupport.class);
 
-                ClientBehaviorSupportImpl support = new ClientBehaviorSupportImpl(renderSupport);
+                ClientBehaviorSupportImpl support = new ClientBehaviorSupportImpl(javascriptSupport);
 
                 environment.push(ClientBehaviorSupport.class, support);
 
@@ -2154,7 +2154,7 @@
         configuration.add("DocumentLinker", documentLinker);
         configuration.add("JavascriptSupport", javascriptSupport, "after:DocumentLinker");
         configuration.add("RenderSupport", renderSupport, "after:JavascriptSupport");
-        configuration.add("ClientBehaviorSupport", clientBehaviorSupport, "after:RenderSupport");
+        configuration.add("ClientBehaviorSupport", clientBehaviorSupport, "after:JavascriptSupport");
         configuration.add("Heartbeat", heartbeat, "after:RenderSupport");
         configuration.add("DefaultValidationDecorator", defaultValidationDecorator, "after:Heartbeat");
     }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/test/TapestryTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/test/TapestryTestCase.java?rev=907053&r1=907052&r2=907053&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/test/TapestryTestCase.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/test/TapestryTestCase.java Fri Feb  5 18:56:27 2010
@@ -1283,4 +1283,13 @@
     
         return asset;
     }
+
+    protected final Link mockLink(String absoluteURI)
+    {
+        Link link = mockLink();
+    
+        expect(link.toAbsoluteURI()).andReturn(absoluteURI).atLeastOnce();
+    
+        return link;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js?rev=907053&r1=907052&r2=907053&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js Fri Feb  5 18:56:27 2010
@@ -753,13 +753,13 @@
     /**
      * Convert a form or link into a trigger of an Ajax update that
      * updates the indicated Zone.
-     * @param element id or instance of <form> or <a> element
-     * @param zoneId id of the element to update when link clicked or form submitted
-     * @param url absolute component event request URL
+     * @param spec.linkId id or instance of <form> or <a> element
+     * @param spec.zoneId id of the element to update when link clicked or form submitted
+     * @param spec.url absolute component event request URL
      */
-    linkZone : function(element, zoneId, url)
+    linkZone : function(spec)
     {
-    	Tapestry.Initializer.updateZoneOnEvent("click", element, zoneId, url);
+    	Tapestry.Initializer.updateZoneOnEvent("click", spec.linkId, spec.zoneId, spec.url);
     },
     
     updateZoneOnEvent : function(eventName, element, zoneId, url)
@@ -828,41 +828,49 @@
         });
     },
 
-    validate : function (field, specs)
+    /**
+     * Keys in the masterSpec are ids of field control elements. Value is a list of validation specs.
+     * Each validation spec is a 2 or 3 element array.
+     */
+    validate : function (masterSpec)
     {
-        field = $(field);
-
-        // Force the creation of the form and field event managers.
+    	$H(masterSpec).each(function (pair) { 
+    		
+    		var field = $(pair.key);
+    		
+    	      // Force the creation of the form and field event managers.
 
-        $(field.form).getFormEventManager();
-        $(field).getFieldEventManager();
+            $(field.form).getFormEventManager();
+            $(field).getFieldEventManager();
 
-        specs.each(function(spec)
-        {
-            // spec is a 2 or 3 element array.
-            // validator function name, message, optional constraint
+            // Each pair value is an array of specs
+            $A(pair.value).each(function(spec)
+            {
+                // spec is a 2 or 3 element array.
+                // validator function name, message, optional constraint
 
-            var name = spec[0];
-            var message = spec[1];
-            var constraint = spec[2];
+                var name = spec[0];
+                var message = spec[1];
+                var constraint = spec[2];
 
-            var vfunc = Tapestry.Validator[name];
+                var vfunc = Tapestry.Validator[name];
 
-            if (vfunc == undefined)
-            {
-                Tapestry.error(Tapestry.Messages.missingValidator, {
-                    name:name,
-                    fieldName:field.id
-                });
-                return;
-            }
+                if (vfunc == undefined)
+                {
+                    Tapestry.error(Tapestry.Messages.missingValidator, {
+                        name:name,
+                        fieldName:field.id
+                    });
+                    return;
+                }
 
-            // Pass the extend field, the provided message, and the constraint object
-            // to the Tapestry.Validator function, so that it can, typically, invoke
-            // field.addValidator().
+                // Pass the extended field, the provided message, and the constraint object
+                // to the Tapestry.Validator function, so that it can, typically, invoke
+                // field.addValidator().
 
-            vfunc.call(this, field, message, constraint);
-        });
+                vfunc.call(this, field, message, constraint);
+            });
+    	});
     },
 
     zone : function(spec)
@@ -1387,12 +1395,7 @@
     // show: name of Tapestry.ElementEffect function used to reveal the zone if hidden
     // update: name of Tapestry.ElementEffect function used to highlight the zone after it is updated
     initialize: function(spec)
-    {
-        if (Object.isString(spec))
-            spec = {
-                element: spec
-            }
-
+    {        
         this.element = $(spec.element);
         this.showFunc = Tapestry.ElementEffect[spec.show] || Tapestry.ElementEffect.show;
         this.updateFunc = Tapestry.ElementEffect[spec.update] || Tapestry.ElementEffect.highlight;
@@ -1484,11 +1487,6 @@
 
     initialize: function(spec)
     {
-        if (Object.isString(spec))
-            spec = {
-                element: spec
-            };
-
         this.element = $(spec.element);
 
         $T(this.element).formFragment = this;

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImplTest.java?rev=907053&r1=907052&r2=907053&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ClientBehaviorSupportImplTest.java Fri Feb  5 18:56:27 2010
@@ -1,10 +1,10 @@
-// Copyright 2007, 2008 The Apache Software Foundation
+// Copyright 2007, 2008, 2010 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
+// 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,
@@ -15,9 +15,8 @@
 package org.apache.tapestry5.internal.services;
 
 import org.apache.tapestry5.Link;
-import org.apache.tapestry5.RenderSupport;
-import org.apache.tapestry5.json.JSONArray;
 import org.apache.tapestry5.json.JSONObject;
+import org.apache.tapestry5.services.javascript.JavascriptSupport;
 import org.apache.tapestry5.test.TapestryTestCase;
 import org.testng.annotations.Test;
 
@@ -26,105 +25,81 @@
     @Test
     public void no_changes()
     {
-        RenderSupport support = mockRenderSupport();
-
-        replay();
-
-        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(support);
+        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(null);
 
         setup.commit();
-
-        verify();
     }
 
     @Test
     public void add_links()
     {
         Link link1 = mockLink("/link1");
-        Link link2 = mockLink("/link2");
-        RenderSupport support = mockRenderSupport();
+        JavascriptSupport js = mockJavascriptSupport();
 
-        support.addInit("linkZone", new JSONArray("['client1', 'zone1', '/link1']"));
-        support.addInit("linkZone", new JSONArray("['client2', 'zone2', '/link2']"));
+        js.addInitializerCall("linkZone", new JSONObject("linkId", "client1", "zoneId", "zone1", "url", "/link1"));
 
         replay();
 
-        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(support);
+        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(js);
 
         setup.linkZone("client1", "zone1", link1);
-        setup.linkZone("client2", "zone2", link2);
 
         setup.commit();
 
         verify();
     }
 
-    private Link mockLink(String absoluteURI)
-    {
-        Link link = mockLink();
-
-        expect(link.toAbsoluteURI()).andReturn(absoluteURI).atLeastOnce();
-
-        return link;
-    }
-
     @Test
     public void add_zones()
     {
-        RenderSupport support = mockRenderSupport();
+        JavascriptSupport js = mockJavascriptSupport();
 
-        support.addInit("zone", "client1");
-        support.addInit("zone", "client2");
+        js.addInitializerCall("zone", new JSONObject("element", "client1"));
+        js.addInitializerCall("zone", new JSONObject("element", "client2"));
 
         replay();
 
-        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(support);
+        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(js);
 
         setup.addZone("client1", null, null);
         setup.addZone("client2", null, null);
 
-        setup.commit();
-
         verify();
     }
 
     @Test
     public void zones_with_functions()
     {
-        RenderSupport support = mockRenderSupport();
+        JavascriptSupport js = mockJavascriptSupport();
 
-        support.addInit("zone", new JSONObject("{'element':'client1', 'show':'showme' }"));
-        support.addInit("zone", new JSONObject("{'element':'client2', 'update':'updateme' }"));
+        js.addInitializerCall("zone", new JSONObject("{'element':'client1', 'show':'showme' }"));
+        js.addInitializerCall("zone", new JSONObject("{'element':'client2', 'update':'updateme' }"));
 
         replay();
 
-        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(support);
+        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(js);
 
         setup.addZone("client1", "showme", null);
         setup.addZone("client2", null, "updateme");
 
-        setup.commit();
-
         verify();
     }
 
     @Test
     public void zone_function_names_are_converted_to_lower_case()
     {
-        RenderSupport support = mockRenderSupport();
+        JavascriptSupport js = mockJavascriptSupport();
 
-        support.addInit("zone", new JSONObject("{'element':'client1', 'show':'showme' }"));
-        support.addInit("zone", new JSONObject("{'element':'client2', 'update':'updateme' }"));
+        js.addInitializerCall("zone", new JSONObject("{'element':'client1', 'show':'showme' }"));
+        js.addInitializerCall("zone", new JSONObject("{'element':'client2', 'update':'updateme' }"));
 
         replay();
 
-        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(support);
+        ClientBehaviorSupportImpl setup = new ClientBehaviorSupportImpl(js);
 
         setup.addZone("client1", "ShowMe", null);
         setup.addZone("client2", null, "UpdateMe");
 
-        setup.commit();
-
         verify();
     }
 }