tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r581620 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/ main/java/org/apache/tapestry/corelib/components/ main/java/org/apache/tapestry/internal/services/ main/java/org/apache/tapestry/services/ test/java/or...
Date Wed, 03 Oct 2007 15:09:26 GMT
Author: hlship
Date: Wed Oct  3 08:09:23 2007
New Revision: 581620

URL: http://svn.apache.org/viewvc?rev=581620&view=rev
Log:
TAPESTRY-1793: JavaScript integration/abstract layer to support multiple client-side frameworks

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/PageRenderSupport.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/TapestryUtils.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Form.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/PageRenderSupportImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/TapestryUtilsTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/PageRenderSupportImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/PageRenderSupport.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/PageRenderSupport.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/PageRenderSupport.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/PageRenderSupport.java
Wed Oct  3 08:09:23 2007
@@ -15,6 +15,8 @@
 package org.apache.tapestry;
 
 import org.apache.tapestry.ioc.internal.util.IdAllocator;
+import org.apache.tapestry.ioc.services.SymbolSource;
+import org.apache.tapestry.services.AssetSource;
 
 /**
  * Provides support to all components that render. This is primarily about generating unique
@@ -45,8 +47,9 @@
     void addScriptLink(Asset... scriptAssets);
 
     /**
-     * Used to add scripts that are stored on the classpath. Each element has symbol expanded,
then
-     * is converted to an asset and added as a script link.
+     * Used to add scripts that are stored on the classpath. Each element has
+     * {@linkplain SymbolSource symbols expanded}, then is
+     * {@linkplain AssetSource converted to an asset} and added as a script link.
      * 
      * @param classpaths
      *            array of paths. Symbols in the paths are expanded, then the paths are each

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/TapestryUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/TapestryUtils.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/TapestryUtils.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/TapestryUtils.java
Wed Oct  3 08:09:23 2007
@@ -14,6 +14,11 @@
 
 package org.apache.tapestry;
 
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.tapestry.ioc.internal.util.CollectionFactory;
+
 /**
  * Utilities often needed when building Tapestry applications.
  */
@@ -58,5 +63,46 @@
         result.append(APOS);
 
         return result.toString();
+    }
+
+    /**
+     * Joins together several strings, sorting them alphabetically and separating them with
spaces.
+     * This is often used when setting the CSS class attribute of an element.
+     */
+    public static String join(String... values)
+    {
+        List<String> list = CollectionFactory.newList(values);
+
+        return sortAndJoin(list);
+    }
+
+    /**
+     * Joins together several strings, sorting them alphabetically and separating them with
spaces.
+     * This is often used when setting the CSS class attribute of an element.
+     */
+    public static String join(List<String> values)
+    {
+        List<String> copy = CollectionFactory.newList(values);
+
+        return sortAndJoin(copy);
+    }
+
+    private static String sortAndJoin(List<String> list)
+    {
+        Collections.sort(list);
+
+        StringBuilder builder = new StringBuilder(10 * list.size());
+
+        String sep = "";
+
+        for (String name : list)
+        {
+            builder.append(sep);
+            builder.append(name);
+
+            sep = " ";
+        }
+
+        return builder.toString();
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Form.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Form.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Form.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Form.java
Wed Oct  3 08:09:23 2007
@@ -21,7 +21,6 @@
 import java.io.ObjectInputStream;
 import java.util.List;
 
-import org.apache.tapestry.Asset;
 import org.apache.tapestry.ClientElement;
 import org.apache.tapestry.ComponentAction;
 import org.apache.tapestry.ComponentEventHandler;
@@ -38,7 +37,6 @@
 import org.apache.tapestry.annotations.Inject;
 import org.apache.tapestry.annotations.Mixin;
 import org.apache.tapestry.annotations.Parameter;
-import org.apache.tapestry.annotations.Path;
 import org.apache.tapestry.annotations.Persist;
 import org.apache.tapestry.corelib.internal.FormSupportImpl;
 import org.apache.tapestry.corelib.mixins.RenderInformals;
@@ -167,18 +165,6 @@
 
     private Element _div;
 
-    @Inject
-    @Path("${tapestry.scriptaculous}/prototype.js")
-    private Asset _prototype;
-
-    @Inject
-    @Path("${tapestry.scriptaculous}/scriptaculous.js")
-    private Asset _scriptaculous;
-
-    @Inject
-    @Path("classpath:/org/apache/tapestry/tapestry.js")
-    private Asset _tapestry;
-
     // Collects a stream of component actions. Each action goes in as a UTF string (the component
     // component id), followed by a ComponentAction
 
@@ -255,11 +241,7 @@
         writer.end(); // div
 
         if (_clientValidation)
-        {
-            _pageRenderSupport.addScriptLink(_prototype, _scriptaculous, _tapestry);
-
             _pageRenderSupport.addScript(format("Tapestry.registerForm('%s');", _name));
-        }
 
         _environment.peek(Heartbeat.class).begin();
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/PageRenderSupportImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/PageRenderSupportImpl.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/PageRenderSupportImpl.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/PageRenderSupportImpl.java
Wed Oct  3 08:09:23 2007
@@ -17,6 +17,9 @@
 import static java.lang.String.format;
 import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.apache.tapestry.Asset;
 import org.apache.tapestry.PageRenderSupport;
 import org.apache.tapestry.ioc.internal.util.IdAllocator;
@@ -33,12 +36,30 @@
 
     private final AssetSource _assetSource;
 
+    private final List<String> _coreScripts;
+
+    private boolean _coreAssetsAdded;
+
+    /**
+     * @param builder
+     *            Used to assemble JavaScript includes and snippets
+     * @param symbolSource
+     *            Used to example symbols (in
+     *            {@linkplain #addClasspathScriptLink(String...) in classpath scripts)
+     * @param assetSource
+     *            Used to convert classpath scripts to {@link Asset}s
+     * @param coreScripts
+     *            core scripts (evaluated as classpaths scripts) that are added to any page
that
+     *            includes a script link or script block
+     */
     public PageRenderSupportImpl(DocumentScriptBuilder builder, SymbolSource symbolSource,
-            AssetSource assetSource)
+            AssetSource assetSource, String... coreScripts)
     {
         _builder = builder;
         _symbolSource = symbolSource;
         _assetSource = assetSource;
+
+        _coreScripts = Arrays.asList(coreScripts);
     }
 
     public String allocateClientId(String id)
@@ -48,6 +69,8 @@
 
     public void addScriptLink(Asset... scriptAssets)
     {
+        addCore();
+
         for (Asset asset : scriptAssets)
         {
             notNull(asset, "scriptAsset");
@@ -58,21 +81,38 @@
 
     public void addClasspathScriptLink(String... classpaths)
     {
+        addCore();
+
         for (String path : classpaths)
-        {
-            String expanded = _symbolSource.expandSymbols(path);
+            addScriptLinkFromClasspath(path);
+    }
+
+    private void addScriptLinkFromClasspath(String path)
+    {
+        String expanded = _symbolSource.expandSymbols(path);
 
-            Asset asset = _assetSource.findAsset(null, expanded, null);
+        Asset asset = _assetSource.findAsset(null, expanded, null);
 
-            _builder.addScriptLink(asset.toClientURL());
-        }
+        _builder.addScriptLink(asset.toClientURL());
     }
 
     public void addScript(String format, Object... arguments)
     {
+        addCore();
+
         String script = format(format, arguments);
 
         _builder.addScript(script);
     }
 
+    private void addCore()
+    {
+        if (!_coreAssetsAdded)
+        {
+            for (String path : _coreScripts)
+                addScriptLinkFromClasspath(path);
+
+            _coreAssetsAdded = true;
+        }
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
Wed Oct  3 08:09:23 2007
@@ -1462,7 +1462,13 @@
 
                 environment.push(DocumentScriptBuilder.class, builder);
                 environment.push(PageRenderSupport.class, new PageRenderSupportImpl(builder,
-                        symbolSource, assetSource));
+                        symbolSource, assetSource,
+
+                        // Core scripts added to any page that uses scripting
+                        
+                        "${tapestry.scriptaculous}/prototype.js",
+                        "${tapestry.scriptaculous}/scriptaculous.js",
+                        "org/apache/tapestry/tapestry.js"));
             }
         });
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/TapestryUtilsTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/TapestryUtilsTest.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/TapestryUtilsTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/TapestryUtilsTest.java
Wed Oct  3 08:09:23 2007
@@ -14,6 +14,9 @@
 
 package org.apache.tapestry;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.testng.Assert;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
@@ -27,7 +30,7 @@
     }
 
     @DataProvider(name = "string_quoting_input")
-    public Object[][] inputs()
+    public Object[][] string_quoting_input()
     {
         return new Object[][]
         {
@@ -36,5 +39,31 @@
                 { "regexp: \\d{4}", "'regexp: \\\\d{4}'" },
 
         };
+    }
+
+    @Test(dataProvider = "join_input")
+    public void join_array(String[] inputs, String expected)
+    {
+        assertEquals(TapestryUtils.join(inputs), expected);
+    }
+
+    @Test(dataProvider = "join_input")
+    public void join_list(String[] inputs, String expected)
+    {
+        List<String> list = Arrays.asList(inputs);
+
+        assertEquals(TapestryUtils.join(list), expected);
+    }
+
+    @DataProvider(name = "join_input")
+    public Object[][] join_input()
+    {
+        return new Object[][]
+        {
+        { new String[0], "" },
+        { new String[]
+        { "fred" }, "fred" },
+        { new String[]
+        { "fred", "barney", "wilma" }, "barney fred wilma" } };
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/PageRenderSupportImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/PageRenderSupportImplTest.java?rev=581620&r1=581619&r2=581620&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/PageRenderSupportImplTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/PageRenderSupportImplTest.java
Wed Oct  3 08:09:23 2007
@@ -23,6 +23,12 @@
 
 public class PageRenderSupportImplTest extends InternalBaseTestCase
 {
+    private static final String CORE_ASSET_PATH_UNEXPANDED = "${core}";
+
+    private static final String CORE_ASSET_PATH = "/org/apache/tapestry/core/core.png";
+
+    private static final String CORE_ASSET_URL = "/assets/core/core.png";
+
     private static final String ASSET_URL = "/assets/foo/bar.pdf";
 
     @Test
@@ -37,6 +43,36 @@
         replay();
 
         PageRenderSupport support = new PageRenderSupportImpl(builder, null, null);
+
+        support.addScriptLink(asset);
+
+        verify();
+    }
+
+    @Test
+    public void core_assets_added()
+    {
+        getMocksControl().checkOrder(true);
+
+        Asset coreAsset = mockAsset();
+        DocumentScriptBuilder builder = mockDocumentScriptBuilder();
+        Asset asset = mockAsset();
+        AssetSource assetSource = mockAssetSource();
+        SymbolSource symbolSource = mockSymbolSource();
+
+        train_expandSymbols(symbolSource, CORE_ASSET_PATH_UNEXPANDED, CORE_ASSET_PATH);
+        train_findAsset(assetSource, null, CORE_ASSET_PATH, null, coreAsset);
+
+        train_toClientURL(coreAsset, CORE_ASSET_URL);
+        builder.addScriptLink(CORE_ASSET_URL);
+
+        train_toClientURL(asset, ASSET_URL);
+        builder.addScriptLink(ASSET_URL);
+
+        replay();
+
+        PageRenderSupport support = new PageRenderSupportImpl(builder, symbolSource, assetSource,
+                CORE_ASSET_PATH_UNEXPANDED);
 
         support.addScriptLink(asset);
 



Mime
View raw message