tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject [9/16] git commit: Start to refactor module and script loading logic from DocumentLinker to ModuleManager
Date Fri, 06 Jul 2012 00:38:00 GMT
Start to refactor module and script loading logic from DocumentLinker to ModuleManager


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/153e2d2e
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/153e2d2e
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/153e2d2e

Branch: refs/heads/5.4-js-rewrite
Commit: 153e2d2e6fdd9afaa693a42a987cb9ed42d3aba4
Parents: 7edf894
Author: Howard M. Lewis Ship <hlship@apache.org>
Authored: Thu Jul 5 12:37:52 2012 -0700
Committer: Howard M. Lewis Ship <hlship@apache.org>
Committed: Thu Jul 5 12:37:52 2012 -0700

----------------------------------------------------------------------
 .../internal/services/DocumentLinkerImpl.java      |   17 +---
 .../services/javascript/ModuleManagerImpl.java     |   17 ++++-
 .../apache/tapestry5/services/TapestryModule.java  |    2 +-
 .../services/javascript/ModuleManager.java         |    9 +-
 .../services/DocumentLinkerImplTest.groovy         |   59 +++++++--------
 5 files changed, 50 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/153e2d2e/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
index 14cae57..c87e153 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
@@ -14,7 +14,6 @@
 
 package org.apache.tapestry5.internal.services;
 
-import org.apache.tapestry5.Asset;
 import org.apache.tapestry5.dom.Document;
 import org.apache.tapestry5.dom.Element;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -47,23 +46,18 @@ public class DocumentLinkerImpl implements DocumentLinker
 
     private boolean hasDynamicScript;
 
-    private final Asset requireJS;
-
     /**
      * @param moduleManager
      *         used to identify the root folder for dynamically loaded modules
-     * @param requireJS
-     *         asset for the library
      * @param omitGeneratorMetaTag
      *         via symbol configuration
      * @param tapestryVersion
      *         version of Tapestry framework (for meta tag)
      * @param compactJSON
      */
-    public DocumentLinkerImpl(ModuleManager moduleManager, Asset requireJS, boolean omitGeneratorMetaTag,
String tapestryVersion, boolean compactJSON)
+    public DocumentLinkerImpl(ModuleManager moduleManager, boolean omitGeneratorMetaTag,
String tapestryVersion, boolean compactJSON)
     {
         this.moduleManager = moduleManager;
-        this.requireJS = requireJS;
         this.omitGeneratorMetaTag = omitGeneratorMetaTag;
 
         tapestryBanner = String.format("Apache Tapestry Framework (version %s)", tapestryVersion);
@@ -181,11 +175,8 @@ public class DocumentLinkerImpl implements DocumentLinker
         if (!rootElementName.equals("html"))
             throw new RuntimeException(String.format("The root element of the rendered document
was <%s>, not <html>. A root element of <html> is needed when linking JavaScript
and stylesheet resources.", rootElementName));
 
-        Element head = findOrCreateElement(root, "head", true);
-
         // TAPESTRY-2364
 
-
         addScriptsToEndOfBody(findOrCreateElement(root, "body", false));
     }
 
@@ -208,7 +199,9 @@ public class DocumentLinkerImpl implements DocumentLinker
         // Create the element is it is missing.
 
         if (container == null)
+        {
             container = atTop ? root.elementAt(0, childElement) : root.element(childElement);
+        }
 
         return container;
     }
@@ -227,9 +220,7 @@ public class DocumentLinkerImpl implements DocumentLinker
         // Eventually, (nearly) everything will be loaded as modules.
         // TODO: Do we need to include type="text/javascript"?
 
-        body.element("script", "src", requireJS.toClientURL());
-
-        moduleManager.writeConfiguration(body.element("script", "type", "text/javascript"));
+        moduleManager.writeInitialization(body);
 
         // Next, include all stacks and individual JavaScript files *after* RequireJS.
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/153e2d2e/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
index a4a1708..6f57358 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/ModuleManagerImpl.java
@@ -14,6 +14,9 @@
 
 package org.apache.tapestry5.internal.services.javascript;
 
+import org.apache.tapestry5.Asset;
+import org.apache.tapestry5.SymbolConstants;
+import org.apache.tapestry5.annotations.Path;
 import org.apache.tapestry5.dom.Element;
 import org.apache.tapestry5.func.F;
 import org.apache.tapestry5.func.Worker;
@@ -34,6 +37,8 @@ public class ModuleManagerImpl implements ModuleManager
 {
     private final String requireConfig;
 
+    private final Asset requireJS;
+
     private final Map<String, Resource> configuration;
 
     // Library names, sorted by order of descending length.
@@ -46,8 +51,12 @@ public class ModuleManagerImpl implements ModuleManager
     // Note: ConcurrentHashMap does not support null as a value, alas. We use classpathRoot
as a null.
     private final Map<String, Resource> cache = CollectionFactory.newConcurrentMap();
 
-    public ModuleManagerImpl(AssetPathConstructor constructor, final ComponentClassResolver
resolver, AssetSource assetSource, Map<String, Resource> configuration)
+    public ModuleManagerImpl(AssetPathConstructor constructor, final ComponentClassResolver
resolver, AssetSource assetSource,
+                             @Path("${" + SymbolConstants.REQUIRE_JS + "}")
+                             Asset requireJS,
+                             Map<String, Resource> configuration)
     {
+        this.requireJS = requireJS;
         this.configuration = configuration;
         String baseURL = constructor.constructAssetPath("module-root", "");
 
@@ -85,9 +94,11 @@ public class ModuleManagerImpl implements ModuleManager
     }
 
     @Override
-    public void writeConfiguration(Element scriptElement)
+    public void writeInitialization(Element body)
     {
-        scriptElement.raw(requireConfig);
+        body.element("script", "src", requireJS.toClientURL());
+
+        body.element("script", "type", "text/javascript").raw(requireConfig);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/153e2d2e/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
index 70f4014..24176f2 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
@@ -1820,7 +1820,7 @@ public final class TapestryModule
         {
             public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
             {
-                DocumentLinkerImpl linker = new DocumentLinkerImpl(moduleManager, requireJS,
omitGeneratorMeta, tapestryVersion, compactJSON);
+                DocumentLinkerImpl linker = new DocumentLinkerImpl(moduleManager, omitGeneratorMeta,
tapestryVersion, compactJSON);
 
                 environment.push(DocumentLinker.class, linker);
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/153e2d2e/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ModuleManager.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ModuleManager.java
b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ModuleManager.java
index e131d22..d534907 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ModuleManager.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/ModuleManager.java
@@ -33,13 +33,14 @@ public interface ModuleManager
 {
     /**
      * Invoked by the internal {@link org.apache.tapestry5.internal.services.DocumentLinker}
service to write the configuration
-     * of the module system into the page. This is the necessary initialization of the client-side
{@code require} object, including
+     * of the module system into the page, including the tag to load the RequireJS library,
and the
+     * necessary initialization of the client-side {@code require} object, including
      * (critically) its baseUrl property.
      *
-     * @param scriptElement
-     *         {@code <script>} element to write configuration should be written (using
{@link Element#raw(String)}
+     * @param body
+     *         {@code <body>} element of the page, to which new {@code <script>>}
element(s) will be added.
      */
-    void writeConfiguration(Element scriptElement);
+    void writeInitialization(Element body);
 
     /**
      * Given a module name (which may be a path of names separated by slashes), locates the
corresponding {@link Resource}.

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/153e2d2e/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.groovy
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.groovy
b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.groovy
index f241b52..2dc55d6 100644
--- a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.groovy
+++ b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.groovy
@@ -1,7 +1,6 @@
 package org.apache.tapestry5.internal.services;
 
 
-import org.apache.tapestry5.Asset
 import org.apache.tapestry5.dom.Document
 import org.apache.tapestry5.dom.Element
 import org.apache.tapestry5.dom.XMLMarkupModel
@@ -30,7 +29,7 @@ class DocumentLinkerImplTest extends InternalBaseTestCase {
 
         document.newRootElement("not-html").text("not an HTML document")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         // Only checked if there's something to link.
 
@@ -53,7 +52,7 @@ class DocumentLinkerImplTest extends InternalBaseTestCase {
 
         document.newRootElement("not-html").text("not an HTML document")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         // Only checked if there's something to link.
 
@@ -74,7 +73,7 @@ class DocumentLinkerImplTest extends InternalBaseTestCase {
     void missing_root_element_is_a_noop() {
         Document document = new Document()
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         linker.addScriptLink("foo.js")
         linker.addScript(InitializationPriority.NORMAL, "doSomething();")
@@ -90,7 +89,7 @@ class DocumentLinkerImplTest extends InternalBaseTestCase {
 
         document.newRootElement("html").element("body").element("p").text("Ready to be updated
with scripts.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -102,7 +101,7 @@ class DocumentLinkerImplTest extends InternalBaseTestCase {
 
         check document, '''
 <?xml version="1.0"?>
-<html><head/><body><p>Ready to be updated with scripts.</p><script
src="/js/require.js"/><script type="text/javascript">require.config();
+<html><body><p>Ready to be updated with scripts.</p><script src="/js/require.js"/><script
type="text/javascript">require.config();
 </script><script src="foo.js" type="text/javascript"/><script src="bar/baz.js"
type="text/javascript"/><script type="text/javascript">Tapestry.onDOMLoaded(function()
{
 pageInitialization();
 });
@@ -120,7 +119,7 @@ pageInitialization();
 
         document.newRootElement("html").element("body").element("p").text("Ready to be marked
with generator meta.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, false, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, false, "1.2.3", true)
 
         linker.updateDocument(document)
 
@@ -139,7 +138,7 @@ pageInitialization();
 
         document.newRootElement("no_html").text("Generator meta only added if root is html
tag.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, false, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, false, "1.2.3", true)
 
         linker.updateDocument(document)
 
@@ -156,7 +155,7 @@ pageInitialization();
 
         document.newRootElement("html").element("body").element("p").text("Ready to be updated
with styles.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, mockRequireJS(), true, "1.2.3",
true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         linker.addStylesheetLink(new StylesheetLink("foo.css"))
         linker.addStylesheetLink(new StylesheetLink("bar/baz.css", new StylesheetOptions("print")))
@@ -176,7 +175,7 @@ pageInitialization();
         document.newRootElement("html").element("head").comment(" existing head ").container.element("body").text(
             "body content")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, mockRequireJS(), true, "1.2.3",
true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         linker.addStylesheetLink(new StylesheetLink("foo.css"))
 
@@ -194,7 +193,7 @@ pageInitialization();
 
         document.newRootElement("html").element("body").element("p").text("Ready to be updated
with scripts.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -204,7 +203,7 @@ pageInitialization();
         linker.updateDocument(document)
 
         check document, '''
-<html><head></head><body><p>Ready to be updated with scripts.</p><script
src="/js/require.js"></script><script type="text/javascript">require.config();
+<html><body><p>Ready to be updated with scripts.</p><script src="/js/require.js"></script><script
type="text/javascript">require.config();
 </script><script type="text/javascript">doSomething();
 doSomethingElse();
 </script></body></html>
@@ -222,7 +221,7 @@ doSomethingElse();
 
         document.newRootElement("html").element("notbody").element("p").text("Ready to be
updated with scripts.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         linker.addScriptLink("foo.js")
 
@@ -230,7 +229,7 @@ doSomethingElse();
 
         check document, '''
 <?xml version="1.0"?>
-<html><head/><notbody><p>Ready to be updated with scripts.</p></notbody><body><script/><script
type="text/javascript">require.config();
+<html><notbody><p>Ready to be updated with scripts.</p></notbody><body><script
src="/js/require.js"/><script type="text/javascript">require.config();
 </script><script src="foo.js" type="text/javascript"/></body></html>
 '''
     }
@@ -241,7 +240,7 @@ doSomethingElse();
 
         document.newRootElement("html").element("body").element("p").text("Ready to be updated
with scripts.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -250,7 +249,7 @@ doSomethingElse();
         linker.updateDocument(document)
 
         check document, '''
-<html><head></head><body><p>Ready to be updated with scripts.</p><script
src="/js/require.js"></script><script type="text/javascript">require.config();
+<html><body><p>Ready to be updated with scripts.</p><script src="/js/require.js"></script><script
type="text/javascript">require.config();
 </script><script type="text/javascript">for (var i = 0; i < 5; i++)  { doIt(i);
}
 </script></body></html>
 '''
@@ -264,7 +263,7 @@ doSomethingElse();
 
         document.newRootElement("html").element("body").element("p").text("Ready to be updated
with scripts.")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -287,7 +286,7 @@ doSomethingElse();
         head.element("meta")
         head.element("script")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -314,7 +313,7 @@ doSomethingElse();
 
         head.element("meta")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", false)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
false)
 
         replay()
 
@@ -358,7 +357,7 @@ require(["core/pageinit"], function (pageinit) {
         head.element("meta")
         head.element("script")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -384,7 +383,7 @@ require(["core/pageinit"], function (pageinit) {
 
         document.newRootElement("html")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, null, true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         linker.addStylesheetLink(new StylesheetLink("everybody.css"))
         linker.addStylesheetLink(new StylesheetLink("just_ie.css", new StylesheetOptions().withCondition("IE")))
@@ -406,7 +405,7 @@ require(["core/pageinit"], function (pageinit) {
 
         document.newRootElement("html")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, mockRequireJS(), true, "1.2.3",
true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(null, true, "1.2.3", true)
 
         linker.addStylesheetLink(new StylesheetLink("whatever.css"))
         linker.addStylesheetLink(new StylesheetLink("insertion-point.css", new StylesheetOptions().asAjaxInsertionPoint()))
@@ -426,7 +425,7 @@ require(["core/pageinit"], function (pageinit) {
 
         head.element("meta")
 
-        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), mockRequireJS(),
true, "1.2.3", true)
+        DocumentLinkerImpl linker = new DocumentLinkerImpl(mockModuleManager(), true, "1.2.3",
true)
 
         replay()
 
@@ -455,8 +454,10 @@ require(["core/pageinit"], function (pageinit) {
         return new ModuleManager() {
 
             @Override
-            void writeConfiguration(Element scriptElement) {
-                scriptElement.raw("require.config();\n")
+            void writeInitialization(Element body) {
+                // Placeholder for the real code, inside ModuleManagerImpl
+                body.element("script", "src", "/js/require.js");
+                body.element("script", "type", "text/javascript").raw("require.config();\n")
             }
 
             @Override
@@ -465,12 +466,4 @@ require(["core/pageinit"], function (pageinit) {
             }
         };
     }
-
-    private Asset mockRequireJS() {
-        Asset mock = newMock(Asset.class)
-
-        expect(mock.toClientURL()).andReturn("/js/require.js").once()
-
-        return mock;
-    }
 }


Mime
View raw message