You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/02/05 19:56:22 UTC
svn commit: r907052 [1/3] - in /tapestry/tapestry5/trunk/tapestry-core/src:
main/java/org/apache/tapestry5/ main/java/org/apache/tapestry5/corelib/
main/java/org/apache/tapestry5/internal/services/
main/java/org/apache/tapestry5/services/ main/java/org...
Author: hlship
Date: Fri Feb 5 18:56:21 2010
New Revision: 907052
URL: http://svn.apache.org/viewvc?rev=907052&view=rev
Log:
Re-implement most of RenderSupportImpl in terms of calls to the JavascriptSupport Environmental, and change the relationship between JavascriptSupport and DocumentLink w.r.t. the block of initialization Javascript.
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java (with props)
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/RenderSupport.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RenderSupportImpl.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/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/TriggerDemo.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RenderSupportImplTest.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/javascript/JavaScriptSupportImplTest.java
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/TriggerDemo.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/add_script.txt
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/add_script_in_development_mode.txt
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/add_script_links.txt
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/add_script_links_at_top.txt
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/empty_document_with_scripts_at_top.txt
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/script_written_raw.txt
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/RenderSupport.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/RenderSupport.java?rev=907052&r1=907051&r2=907052&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/RenderSupport.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/RenderSupport.java Fri Feb 5 18:56:21 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
+// Copyright 2006, 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,
@@ -14,10 +14,13 @@
package org.apache.tapestry5;
+import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.AssetSource;
+import org.apache.tapestry5.services.EnvironmentalShadowBuilder;
+import org.apache.tapestry5.services.javascript.JavascriptSupport;
/**
* Provides support to all components that render. This is primarily about generating unique client-side ids (very
@@ -26,24 +29,36 @@
* <p/>
* When rendering, a <script> block will be added to the bottom of the page (just before the </body> tag).
* The scripting statements added to this block will be executed, on the client, only once the page has fully loaded.
+ * <p>
+ * The methods in this interface are largely being replaced with a new interface, {@link JavascriptSupport}.
+ * <p>
+ * RenderSupport is normally accessed within a component by using the {@link Environmental} annotation on a component
+ * field. In addition, RenderSupport may also be accessed as a service (the service
+ * {@linkplain EnvironmentalShadowBuilder internally delegates to the current environmental instance}), which is useful
+ * for service-layer objects.
*/
public interface RenderSupport
{
/**
* Allocates a unique id based on the component's id. In some cases, the return value will not precisely match the
* input value (an underscore and a unique index value may be appended).
- *
- * @param id the component id from which a unique id will be generated
+ *
+ * @param id
+ * the component id from which a unique id will be generated
* @return a unique id for this rendering of the page
* @see org.apache.tapestry5.ioc.internal.util.IdAllocator
+ * @see JavascriptSupport#allocateClientId(ComponentResources)
+ * @deprecated Use {@link JavascriptSupport#allocateClientId(String)} instead
*/
String allocateClientId(String id);
/**
* As with {@link #allocateClientId(String)} but uses the id of the component extracted from the resources.
- *
- * @param resources of the component which requires an id
+ *
+ * @param resources
+ * of the component which requires an id
* @return a unique id for this rendering of the page
+ * @deprecated Use {@link JavascriptSupport#allocateClientId(ComponentResources)} instead
*/
String allocateClientId(ComponentResources resources);
@@ -51,25 +66,32 @@
* Adds one or more new script assets to the page. Assets are added uniquely, and appear as <script> elements
* just inside the <body> element of the rendered page. Duplicate requests to add the same script are quietly
* ignored.
- *
- * @param scriptAssets asset to the script to add
+ *
+ * @param scriptAssets
+ * asset to the script to add
+ * @deprecated Use {@link JavascriptSupport#importJavascriptLibrary(Asset)} instead
*/
void addScriptLink(Asset... scriptAssets);
/**
- * Adds some number of script links as strings representations of URLs. The scripts are passed down to the client
- * as-is. Typically, this is used to reference a script stored outside the web application entirely.
- *
- * @param scriptURLs URL strings of scripts
+ * Adds some number of script links as strings representations of URLs. The scripts are passed down to the client
+ * as-is. Typically, this is used to reference a script stored outside the web application entirely.
+ *
+ * @param scriptURLs
+ * URL strings of scripts
+ * @deprecated Use {@link JavascriptSupport#importJavascriptLibrary(Asset) instead}
+ * @throws RuntimeException
+ * <strong>always</strong> as of 5.2.0
*/
void addScriptLink(String... scriptURLs);
/**
* 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 converted into an
- * asset.
+ *
+ * @param classpaths
+ * array of paths. Symbols in the paths are expanded, then the paths are each converted into an
+ * asset.
*/
void addClasspathScriptLink(String... classpaths);
@@ -77,72 +99,96 @@
* Adds a link to a CSS stylesheet. As with JavaScript libraries, each stylesheet is added at most once. Stylesheets
* added this way will be ordered before any other content, in the <head> element of the document. The
* <head> element will be created, if necessary.
- *
- * @param stylesheet the asset referencing the stylesheet
- * @param media the media value for the stylesheet, or null to not specify a specific media type
+ *
+ * @param stylesheet
+ * the asset referencing the stylesheet
+ * @param media
+ * the media value for the stylesheet, or null to not specify a specific media type
*/
void addStylesheetLink(Asset stylesheet, String media);
/**
* Adds a stylesheet as a URL. See notes in {@link #addScriptLink(String[])}.
- *
- * @param stylesheetURL URL string of stylesheet
- * @param media media value fo the stylesheet, or null to not specify a specific media type
+ *
+ * @param stylesheetURL
+ * URL string of stylesheet
+ * @param media
+ * media value fo the stylesheet, or null to not specify a specific media type
*/
void addStylesheetLink(String stylesheetURL, String media);
/**
* Adds a script statement to the page's script block. A newline will be added after the script statement.
- *
- * @param script text to be added to the script block
+ *
+ * @param script
+ * text to be added to the script block
+ * @deprecated Use {@link JavascriptSupport#addScript(String, Object...)} instead
*/
void addScript(String script);
/**
- * Adds a script statement to the page's script block. The parameters are passed to {@link String#format(String,
- * Object[])} before being added to the script block. A newline will be added after the formatted statement.
- *
- * @param format base string format, to be passed through String.format
- * @param arguments additional arguments formatted to form the final script
+ * Adds a script statement to the page's script block. The parameters are passed to
+ * {@link String#format(String, Object[])} before being added to the script block. A newline will be added after the
+ * formatted statement.
+ *
+ * @param format
+ * base string format, to be passed through String.format
+ * @param arguments
+ * additional arguments formatted to form the final script
+ * @deprecated Use {@link JavascriptSupport#addScript(String, Object...)} instead
*/
void addScript(String format, Object... arguments);
/**
- * Add an initialization call.
- *
- * @param functionName the name of the function (on the client-side Tapestry.Initializer object) to invoke.
- * @param parameterList list of parameters for the method invocation.
+ * Add an initialization call. This method is deprecated and, although it still works, it now generates
+ * very verbose, inefficient client-side Javascript.
+ *
+ * @param functionName
+ * the name of the function (on the client-side Tapestry.Initializer object) to invoke.
+ * @param parameterList
+ * list of parameters for the method invocation.
* @see #addScript(String, Object[])
+ * @deprecated Use {@link JavascriptSupport#addInitializerCall(String, JSONObject)} instead (which may require
+ * changes to your JavaScript initializer function)
*/
void addInit(String functionName, JSONArray parameterList);
/**
* Alternate version of {@link #addInit(String, org.apache.tapestry5.json.JSONArray)} where just a single object is
* passed.
- *
- * @param functionName the name of the function (on the client-side Tapestry object) to invoke.
- * @param parameter the object to pass to the function
+ *
+ * @param functionName
+ * the name of the function (on the client-side Tapestry object) to invoke.
+ * @param parameter
+ * the object to pass to the function
*/
void addInit(String functionName, JSONObject parameter);
/**
* Alternate version of {@link #addInit(String, org.apache.tapestry5.json.JSONArray)} where one or more strings are
- * passed. A single string is added to the initialization call as itself; otherwise, the parameters are combined to
- * form a {@link JSONArray}.
- *
- * @param functionName the name of the function (on the client-side Tapestry object) to invoke.
+ * passed. A single string is added to the initialization call as itself; otherwise, the parameters are combined to
+ * form a {@link JSONArray}. This method is deprecated and, although it still works, it now generates
+ * very verbose, inefficient client-side Javascript.
+ *
+ * @param functionName
+ * the name of the function (on the client-side Tapestry object) to invoke.
* @param parameters
+ * @deprecated Use {@link JavascriptSupport#addInitializerCall(String, JSONObject)} instead (which may require
+ * changes to your JavaScript initializer function)
*/
void addInit(String functionName, String... parameters);
/**
* Invoked to set focus on a rendered field. Takes into account priority, meaning that a field with errors will take
- * precendence over a merely required field, and over a field that is optional. The value {@link
- * org.apache.tapestry5.FieldFocusPriority#OVERRIDE} can be used to force a particular field to receive focus.
- *
- * @param priority focus is set only if the provided priority is greater than the current priority
- * @param fieldId id of client-side element to take focus
+ * precendence over a merely required field, and over a field that is optional. The value
+ * {@link org.apache.tapestry5.FieldFocusPriority#OVERRIDE} can be used to force a particular field to receive
+ * focus.
+ *
+ * @param priority
+ * focus is set only if the provided priority is greater than the current priority
+ * @param fieldId
+ * id of client-side element to take focus
*/
void autofocus(FieldFocusPriority priority, String fieldId);
}
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java?rev=907052&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java Fri Feb 5 18:56:21 2010
@@ -0,0 +1,37 @@
+// Copyright 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
+//
+// 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.corelib;
+
+import org.apache.tapestry5.EventConstants;
+import org.apache.tapestry5.corelib.components.LinkSubmit;
+import org.apache.tapestry5.corelib.components.Submit;
+
+/**
+ * Defines the client and server-side behavior of a {@link Submit} or {@link LinkSubmit} component.
+ *
+ * @since 5.2.0
+ */
+public enum SubmitMode
+{
+ /** Submit the form normally, with full validation on the client and server side. */
+ NORMAL,
+
+ /**
+ * Cancel the form; bypass client-side form validation. Validation still occurs
+ * on the server, though generally the data is discarded by the event listener
+ * (listening to the Submit component's {@link EventConstants#SELECTED} event).
+ */
+ CANCEL;
+}
Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/SubmitMode.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java?rev=907052&r1=907051&r2=907052&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java Fri Feb 5 18:56:21 2010
@@ -4,7 +4,7 @@
// 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,
@@ -55,16 +55,21 @@
private final int contextPathLength;
/**
- * @param productionMode via symbol configuration
- * @param omitGeneratorMetaTag via symbol configuration
- * @param tapestryVersion version of Tapestry framework (for meta tag)
- * @param combineScripts if true, individual JavaScript assets will be combined into a single virtual asset
- * @param contextPath {@link org.apache.tapestry5.services.Request#getContextPath()}
- * @param clientDataEncoder used to encode data for the combined virtual asset
+ * @param productionMode
+ * via symbol configuration
+ * @param omitGeneratorMetaTag
+ * via symbol configuration
+ * @param tapestryVersion
+ * version of Tapestry framework (for meta tag)
+ * @param combineScripts
+ * if true, individual JavaScript assets will be combined into a single virtual asset
+ * @param contextPath
+ * {@link org.apache.tapestry5.services.Request#getContextPath()}
+ * @param clientDataEncoder
+ * used to encode data for the combined virtual asset
*/
- public DocumentLinkerImpl(boolean productionMode, boolean omitGeneratorMetaTag,
- String tapestryVersion, boolean combineScripts, String contextPath,
- ClientDataEncoder clientDataEncoder)
+ public DocumentLinkerImpl(boolean productionMode, boolean omitGeneratorMetaTag, String tapestryVersion,
+ boolean combineScripts, String contextPath, ClientDataEncoder clientDataEncoder)
{
this.combineScripts = combineScripts;
this.clientDataEncoder = clientDataEncoder;
@@ -81,7 +86,8 @@
public void addStylesheetLink(String styleURL, String media)
{
- if (stylesheets.contains(styleURL)) return;
+ if (stylesheets.contains(styleURL))
+ return;
includedStylesheets.add(new IncludedStylesheet(styleURL, media));
@@ -90,7 +96,8 @@
public void addScriptLink(String scriptURL)
{
- if (scripts.contains(scriptURL)) return;
+ if (scripts.contains(scriptURL))
+ return;
scripts.add(scriptURL);
@@ -102,16 +109,14 @@
public void addScript(String script)
{
- if (InternalUtils.isBlank(script)) return;
-
scriptBlock.append(script);
- scriptBlock.append("\n");
}
/**
* Updates the supplied Document, possibly adding <head> or <body> elements.
- *
- * @param document to be updated
+ *
+ * @param document
+ * to be updated
*/
public void updateDocument(Document document)
{
@@ -119,21 +124,20 @@
// If the document failed to render at all, that's a different problem and is reported elsewhere.
- if (root == null) return;
+ if (root == null)
+ return;
if (!stylesheets.isEmpty())
addStylesheetsToHead(root, includedStylesheets);
- //only add the generator meta only to html documents
+ // only add the generator meta only to html documents
boolean isHtmlRoot = root.getName().equals("html");
if (!omitGeneratorMetaTag && isHtmlRoot)
{
Element head = findOrCreateElement(root, "head", true);
- head.element("meta",
- "name", "generator",
- "content", tapestryBanner);
+ head.element("meta", "name", "generator", "content", tapestryBanner);
}
addScriptElements(root);
@@ -141,7 +145,8 @@
private void addScriptElements(Element root)
{
- if (scripts.isEmpty() && scriptBlock.length() == 0) return;
+ if (scripts.isEmpty() && scriptBlock.length() == 0)
+ return;
// This only applies when the document is an HTML document. This may need to change in the
// future, perhaps configurable, to allow for html and xhtml and perhaps others. Does SVG
@@ -175,8 +180,9 @@
/**
* Adds the dynamic script block, which is, ultimately, a call to the client-side Tapestry.onDOMLoaded() function.
- *
- * @param body element to add the dynamic scripting to
+ *
+ * @param body
+ * element to add the dynamic scripting to
*/
protected void addDynamicScriptBlock(Element body)
{
@@ -189,19 +195,17 @@
if (developmentMode)
e.raw("Tapestry.DEBUG_ENABLED = true;\n");
- e.raw("Tapestry.onDOMLoaded(function() {\n");
-
e.raw(scriptBlock.toString());
-
- e.raw("});\n");
}
}
/**
* Adds a script link for each included script to the bottom of the container (the <head>).
- *
- * @param container element to add the script links to
- * @param scripts scripts to add
+ *
+ * @param container
+ * element to add the script links to
+ * @param scripts
+ * scripts to add
*/
protected void addScriptLinksForIncludedScripts(Element container, List<String> scripts)
{
@@ -216,12 +220,11 @@
else
{
for (String scriptURL : scripts)
- scriptContainer.element("script",
- "type", "text/javascript",
- "src", scriptURL);
+ scriptContainer.element("script", "type", "text/javascript", "src", scriptURL);
}
- if (existing != null) scriptContainer.moveBefore(existing);
+ if (existing != null)
+ scriptContainer.moveBefore(existing);
scriptContainer.pop();
}
@@ -238,17 +241,17 @@
for (String scriptURL : scripts)
{
- // Each scriptURL will be prefixed with the context path, which isn't needed to build the combined virtual
+ // Each scriptURL will be prefixed with the context path, which isn't needed to build the combined
+ // virtual
// asset (in fact, it gets in the way).
stream.writeUTF(scriptURL.substring(contextPathLength));
}
- String virtualURL = fullAssetPrefix + RequestConstants.VIRTUAL_FOLDER + dataSink.getEncodedClientData() + ".js";
+ String virtualURL = fullAssetPrefix + RequestConstants.VIRTUAL_FOLDER + dataSink.getEncodedClientData()
+ + ".js";
- container.element("script",
- "type", "text/javascript",
- "src", virtualURL);
+ container.element("script", "type", "text/javascript", "src", virtualURL);
}
catch (IOException ex)
{
@@ -256,19 +259,21 @@
}
}
-
/**
* Locates the head element under the root ("html") element, creating it if necessary, and adds the stylesheets to
* it.
- *
- * @param root element of document
- * @param stylesheets to add to the document
+ *
+ * @param root
+ * element of document
+ * @param stylesheets
+ * to add to the document
*/
protected void addStylesheetsToHead(Element root, List<IncludedStylesheet> stylesheets)
{
int count = stylesheets.size();
- if (count == 0) return;
+ if (count == 0)
+ return;
// This only applies when the document is an HTML document. This may need to change in the
// future, perhaps configurable, to allow for html and xhtml and perhaps others. Does SVG
@@ -276,8 +281,9 @@
String rootElementName = root.getName();
- // Not an html document, don't add anything.
- if (!rootElementName.equals("html")) return;
+ // Not an html document, don't add anything.
+ if (!rootElementName.equals("html"))
+ return;
Element head = findOrCreateElement(root, "head", true);
@@ -303,7 +309,8 @@
{
Element e = (Element) n;
- if (e.getName().equalsIgnoreCase(elementName)) return e;
+ if (e.getName().equalsIgnoreCase(elementName))
+ return e;
}
}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RenderSupportImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RenderSupportImpl.java?rev=907052&r1=907051&r2=907052&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RenderSupportImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RenderSupportImpl.java Fri Feb 5 18:56:21 2010
@@ -4,7 +4,7 @@
// 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,
@@ -19,107 +19,75 @@
import org.apache.tapestry5.FieldFocusPriority;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.ioc.internal.util.Defense;
-import org.apache.tapestry5.ioc.internal.util.IdAllocator;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.AssetSource;
-import org.apache.tapestry5.services.ClientInfrastructure;
+import org.apache.tapestry5.services.javascript.JavascriptSupport;
public class RenderSupportImpl implements RenderSupport
{
- private final IdAllocator idAllocator;
-
private final DocumentLinker linker;
private final SymbolSource symbolSource;
private final AssetSource assetSource;
- private final ClientInfrastructure clientInfrastructure;
-
- private boolean stackAssetsAdded;
-
- private final JSONObject init = new JSONObject();
-
private FieldFocusPriority focusPriority;
private String focusFieldId;
- /**
- * @param linker 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 org.apache.tapestry5.Asset}s
- * @param coreScripts core scripts (evaluated as classpaths scripts) that are added to any page that includes a
- * script link or script block
- * @param javascriptStack
- */
- public RenderSupportImpl(DocumentLinker linker, SymbolSource symbolSource,
- AssetSource assetSource, ClientInfrastructure clientInfrastructure)
- {
- this(linker, symbolSource, assetSource, new IdAllocator(), clientInfrastructure);
- }
+ private final JavascriptSupport javascriptSupport;
/**
- * @param linker 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 org.apache.tapestry5.Asset}s
- * @param idAllocator Used to allocate unique client ids during the render
- * @param coreScripts core scripts (evaluated as classpaths scripts) that are added to any page that includes a
- * script link or script block
- * @param javascriptStack
+ * @param linker
+ * Used to assemble JavaScript includes and snippets
+ * @param symbolSource
+ * Used to example symbols (in {@linkplain #addClasspathScriptLink(String...)}
+ * @param assetSource
+ * Used to convert classpath scripts to {@link org.apache.tapestry5.Asset}s
+ * @param javascriptSupport
+ * Used to add JavaScript libraries and blocks of initialization JavaScript to the rendered page
+ * @param ClientInfrastructure
+ * Identifies which JavaScript libraries and stylesheets are needed in a full page render
*/
-
- public RenderSupportImpl(DocumentLinker linker, SymbolSource symbolSource,
- AssetSource assetSource, IdAllocator idAllocator,
- ClientInfrastructure clientInfrastructure)
-
+ public RenderSupportImpl(DocumentLinker linker, SymbolSource symbolSource, AssetSource assetSource,
+ JavascriptSupport javascriptSupport)
{
this.linker = linker;
this.symbolSource = symbolSource;
this.assetSource = assetSource;
- this.idAllocator = idAllocator;
- this.clientInfrastructure = clientInfrastructure;
+ this.javascriptSupport = javascriptSupport;
}
public String allocateClientId(String id)
{
- return idAllocator.allocateId(id);
+ return javascriptSupport.allocateClientId(id);
}
public String allocateClientId(ComponentResources resources)
{
- return allocateClientId(resources.getId());
+ return javascriptSupport.allocateClientId(resources);
}
public void addScriptLink(Asset... scriptAssets)
{
- addStack();
-
for (Asset asset : scriptAssets)
{
Defense.notNull(asset, "scriptAsset");
- linker.addScriptLink(asset.toClientURL());
+ javascriptSupport.importJavascriptLibrary(asset);
}
}
public void addScriptLink(String... scriptURLs)
{
- addStack();
-
- for (String url : scriptURLs)
- {
- linker.addScriptLink(url);
- }
+ throw new RuntimeException(
+ "RenderSupport.addScriptLink(String...) is no longer supported, starting in Tapestry 5.2.");
}
public void addClasspathScriptLink(String... classpaths)
{
- addStack();
-
for (String path : classpaths)
addScriptLinkFromClasspath(path);
}
@@ -130,23 +98,17 @@
Asset asset = assetSource.getAsset(null, expanded, null);
- linker.addScriptLink(asset.toClientURL());
+ addScriptLink(asset);
}
public void addScript(String script)
{
- Defense.notBlank(script, "script");
-
- addStack();
-
- linker.addScript(script);
+ javascriptSupport.addScript(script);
}
public void addScript(String format, Object... arguments)
{
- Defense.notNull(format, "format");
-
- addScript(String.format(format, arguments));
+ javascriptSupport.addScript(format, arguments);
}
public void addInit(String functionName, JSONArray parameterList)
@@ -156,17 +118,11 @@
public void addInit(String functionName, JSONObject parameter)
{
- addInitFunctionInvocation(functionName, parameter);
+ javascriptSupport.addInitializerCall(functionName, parameter);
}
public void addInit(String functionName, String... parameters)
{
- if (parameters.length == 1)
- {
- addInitFunctionInvocation(functionName, parameters[0]);
- return;
- }
-
JSONArray array = new JSONArray();
for (String parameter : parameters)
@@ -174,7 +130,7 @@
array.put(parameter);
}
- addInitFunctionInvocation(functionName, array);
+ addInit(functionName, array);
}
public void autofocus(FieldFocusPriority priority, String fieldId)
@@ -189,20 +145,21 @@
}
}
+ /**
+ * For the few existing places that use the old variations of addInit(), passing a list of
+ * strings or a JSONArray, the end result is a bit inefficient. We end up generating lots
+ * of calls to Tapestry.init, with no attempt to aggregate them. Most of the time, the init
+ * occurs with a JSONObject (the "spec") and is handled by JavascriptSupport.
+ */
private void addInitFunctionInvocation(String functionName, Object parameters)
{
Defense.notBlank(functionName, "functionName");
Defense.notNull(parameters, "parameters");
- JSONArray invocations = init.has(functionName) ? init.getJSONArray(functionName) : null;
+ JSONArray list = new JSONArray().put(parameters);
+ JSONObject wrapper = new JSONObject().put(functionName, list);
- if (invocations == null)
- {
- invocations = new JSONArray();
- init.put(functionName, invocations);
- }
-
- invocations.put(parameters);
+ addScript("Tapestry.init(%s);", wrapper);
}
/**
@@ -214,11 +171,6 @@
{
addScript("$('%s').activate();", focusFieldId);
}
-
- if (init.length() > 0)
- {
- addScript("Tapestry.init(%s);", init);
- }
}
public void addStylesheetLink(Asset stylesheet, String media)
@@ -228,22 +180,8 @@
linker.addStylesheetLink(stylesheet.toClientURL(), media);
}
-
public void addStylesheetLink(String stylesheetURL, String media)
{
linker.addStylesheetLink(stylesheetURL, media);
}
-
- private void addStack()
- {
- if (!stackAssetsAdded)
- {
- for (Asset script : clientInfrastructure.getJavascriptStack())
- {
- linker.addScriptLink(script.toClientURL());
- }
-
- stackAssetsAdded = true;
- }
- }
}