Author: jkuhnert
Date: Sun Nov 26 09:35:13 2006
New Revision: 479383
URL: http://svn.apache.org/viewvc?view=rev&rev=479383
Log:
Fixes TAPESTRY-1159 .
Refactored PageRenderSupportImpl some to handle removing duplicate dojo.require
statements as well as
redundant newlines / etc.
Modified:
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/Timeout.script
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/XTile.script
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/form/MaskEdit.script
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/FormSupportImpl.java
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.java
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.jwc
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.script
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/PageRenderSupportImpl.java
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/FormSupportTest.java
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/TestPageRenderSupport.java
Modified:
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/Timeout.script
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/Timeout.script?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/Timeout.script
(original)
+++
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/Timeout.script
Sun Nov 26 09:35:13 2006
@@ -74,7 +74,6 @@
}
}
</if>
-
tapestry.TimeoutProlongSession=function()
{
tapestry.TimeoutUpdateProlongSessionTime();
@@ -111,7 +110,6 @@
}
<if expression="!disableAutoProlong">
-
tapestry.TimeoutInitChangeObserver=function()
{
dojo.event.connect(document.body, "onclick", tapestry,
"TimeoutHandleOnClick");
@@ -145,7 +143,6 @@
return true;
}
</if>
-
<if expression="!disableAutoProlong">
tapestry.TimeoutInitChangeObserver();
</if>
Modified:
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/XTile.script
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/XTile.script?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/XTile.script
(original)
+++
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/ajax/XTile.script
Sun Nov 26 09:35:13 2006
@@ -64,7 +64,6 @@
requestObject.open("GET", url, true);
requestObject.send(null);
}
-
<unique>
<![CDATA[
function getRequest()
@@ -106,7 +105,5 @@
}
]]>
</unique>
-
</body>
-
</script>
Modified:
tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/form/MaskEdit.script
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-contrib/src/java/org/apache/tapestry/contrib/form/MaskEdit.script?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
Binary files - no diff available.
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/FormSupportImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/FormSupportImpl.java?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/FormSupportImpl.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/FormSupportImpl.java
Sun Nov 26 09:35:13 2006
@@ -558,14 +558,14 @@
if (fieldId != null && _form.getFocus()
&& _cycle.getAttribute(FIELD_FOCUS_ATTRIBUTE) == null) {
- _pageRenderSupport.addInitializationScript(_form,
"tapestry.form.focusField('" + fieldId + "');");
+ _pageRenderSupport.addInitializationScript(_form,
"dojo.require(\"tapestry.form\");tapestry.form.focusField('" + fieldId + "');");
_cycle.setAttribute(FIELD_FOCUS_ATTRIBUTE, Boolean.TRUE);
}
// register the validation profile with client side form manager
if (_form.isClientValidationEnabled()) {
- _pageRenderSupport.addInitializationScript(_form,
"tapestry.form.clearProfiles('"
+ _pageRenderSupport.addInitializationScript(_form,
"dojo.require(\"tapestry.form\");tapestry.form.clearProfiles('"
+ formId + "'); tapestry.form.registerProfile('" + formId
+ "',"
+ _profile.toString() + ");");
}
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.java?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.java
Sun Nov 26 09:35:13 2006
@@ -27,7 +27,6 @@
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.TapestryUtils;
import org.apache.tapestry.components.ILinkComponent;
-import org.apache.tapestry.components.LinkEventType;
/**
* Combines a link component (such as
@@ -70,16 +69,13 @@
boolean dynamic = false;
String imageId = null;
- PageRenderSupport pageRenderSupport = TapestryUtils
- .getPageRenderSupport(cycle, this);
-
- ILinkComponent serviceLink = (ILinkComponent) cycle
- .getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
+ PageRenderSupport pageRenderSupport =
TapestryUtils.getPageRenderSupport(cycle, this);
+ ILinkComponent serviceLink = (ILinkComponent)
cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
+
if (serviceLink == null)
throw new ApplicationRuntimeException(Tapestry
- .getMessage("Rollover.must-be-contained-by-link"), this,
- null, null);
+ .getMessage("Rollover.must-be-contained-by-link"), this,
null, null);
boolean linkDisabled = serviceLink.isDisabled();
@@ -107,16 +103,17 @@
if (dynamic)
{
- if (mouseOverURL == null) mouseOverURL = imageURL;
-
- if (mouseOutURL == null) mouseOutURL = imageURL;
+ if (mouseOverURL == null)
+ mouseOverURL = imageURL;
- imageId = writeScript(cycle, pageRenderSupport, serviceLink,
- mouseOverURL, mouseOutURL);
+ if (mouseOutURL == null)
+ mouseOutURL = imageURL;
+ imageId = writeScript(cycle, pageRenderSupport, serviceLink,
mouseOverURL, mouseOutURL);
+
writer.attribute("id", imageId);
}
-
+
renderInformalParameters(writer, cycle);
writer.closeTag();
@@ -127,34 +124,23 @@
public abstract IScript getScript();
- private String writeScript(IRequestCycle cycle,
- PageRenderSupport pageRenderSupport, ILinkComponent link,
+ private String writeScript(IRequestCycle cycle, PageRenderSupport
pageRenderSupport, ILinkComponent link,
String mouseOverImageURL, String mouseOutImageURL)
{
String imageId = pageRenderSupport.getUniqueString(getId());
- String preloadedMouseOverImageURL = pageRenderSupport
- .getPreloadedImageReference(this, mouseOverImageURL);
- String preloadedMouseOutImageURL = pageRenderSupport
- .getPreloadedImageReference(this, mouseOutImageURL);
-
+
+ String preloadedMouseOverImageURL =
pageRenderSupport.getPreloadedImageReference(this, mouseOverImageURL);
+ String preloadedMouseOutImageURL =
pageRenderSupport.getPreloadedImageReference(this, mouseOutImageURL);
+
Map symbols = new HashMap();
-
+
+ symbols.put("link", link);
symbols.put("imageId", imageId);
symbols.put("mouseOverImageURL", preloadedMouseOverImageURL);
symbols.put("mouseOutImageURL", preloadedMouseOutImageURL);
-
+
getScript().execute(this, cycle, pageRenderSupport, symbols);
-
- // Add attributes to the link to control mouse over/out.
- // Because the script is written before the <body> tag,
- // there won't be any timing issues (such as cause
- // bug #113893).
-
- link.addEventHandler(LinkEventType.MOUSE_OVER, "tapestry." + (String)
symbols
- .get("onMouseOverName"));
- link.addEventHandler(LinkEventType.MOUSE_OUT, "tapestry." + (String)
symbols
- .get("onMouseOutName"));
-
+
return imageId;
}
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.jwc
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.jwc?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.jwc
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.jwc
Sun Nov 26 09:35:13 2006
@@ -59,4 +59,4 @@
<inject property="script" type="script" object="Rollover.script"/>
-</component-specification>
\ No newline at end of file
+</component-specification>
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.script
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.script?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.script
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Rollover.script
Sun Nov 26 09:35:13 2006
@@ -17,7 +17,7 @@
<!DOCTYPE script PUBLIC
"-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
+ "http://tapestry.apache.org/dtd/Script_3_0.dtd">
<script>
<!--
@@ -38,7 +38,7 @@
<input-symbol key="imageId" class="java.lang.String" required="yes"/>
<input-symbol key="mouseOverImageURL" class="java.lang.String" required="yes"/>
<input-symbol key="mouseOutImageURL" class="java.lang.String" required="yes"/>
-
+<input-symbol key="link" />
<let key="onMouseOverName">
mouseOver_${imageId}
@@ -50,18 +50,21 @@
<let key="attribute">
document.getElementById('${imageId}').src
</let>
-
<body>
-tapestry.${onMouseOverName}=function()
-{
+tapestry.${onMouseOverName}=function(){
if (document.images)
${attribute} = ${mouseOverImageURL};
-};
+}
-tapestry.${onMouseOutName}=function()
-{
+tapestry.${onMouseOutName}=function(){
if (document.images)
${attribute} = ${mouseOutImageURL};
-};
+}
</body>
+<initialization>
+<unique>
+dojo.event.connect(dojo.byId("${link.clientId}"), "onmouseover", tapestry,
"${onMouseOverName}");
+dojo.event.connect(dojo.byId("${link.clientId}"), "onmouseout", tapestry,
"${onMouseOutName}");
+</unique>
+</initialization>
</script>
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/PageRenderSupportImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/PageRenderSupportImpl.java?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/PageRenderSupportImpl.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/PageRenderSupportImpl.java
Sun Nov 26 09:35:13 2006
@@ -77,6 +77,10 @@
private final String _preloadName;
+ private final RegexpMatcher _matcher = new RegexpMatcher();
+
+ private final Map _requires = new HashMap();
+
public PageRenderSupportImpl(AssetFactory assetFactory, String namespace,
Location location, ResponseBuilder builder)
{
@@ -149,14 +153,12 @@
if (!_builder.isBodyScriptAllowed(target))
return;
- String val = StringUtils.stripToEmpty(script);
- if (val.length() <= 0)
- return;
+ String val = stripDuplicateIncludes(script);
if (_bodyScript == null)
_bodyScript = new StringBuffer(val.length());
-
- _bodyScript.append(val);
+
+ _bodyScript.append("\n").append(val);
}
public void addInitializationScript(String script)
@@ -169,9 +171,7 @@
if (!_builder.isInitializationScriptAllowed(target))
return;
- String val = StringUtils.stripToEmpty(script);
- if (val.length() <= 0)
- return;
+ String val = stripDuplicateIncludes(script);
if (_initializationScript == null)
_initializationScript = new StringBuffer(val.length() + 1);
@@ -179,6 +179,38 @@
_initializationScript.append("\n").append(val);
}
+ /**
+ * Provides a mechanism to strip out duplicate dojo.require calls made in
script
+ * templates in order to reduce amount of redundant javascript written to
client.
+ *
+ * @param input The incoming script string to check for requires.
+ * @return The input string stripped of all known dojo.require calls, if
any.
+ */
+ String stripDuplicateIncludes(String input)
+ {
+ String[] lines = StringUtils.splitPreserveAllTokens(input, ';');
+
+ if (lines == null || lines.length < 1)
+ return input;
+
+ String ret = input;
+
+ for (int i=0; i < lines.length; i++) {
+ if (lines[i].indexOf("dojo.require") < 0)
+ continue;
+
+ String line = StringUtils.stripToEmpty(lines[i]);
+
+ if (_requires.containsKey(line)) {
+ ret = StringUtils.replaceOnce(ret, line+";", "");
+ } else {
+ _requires.put(line, "t");
+ }
+ }
+
+ return StringUtils.stripToEmpty(ret.trim());
+ }
+
public void addExternalScript(Resource scriptLocation)
{
addExternalScript(null, scriptLocation);
@@ -246,12 +278,15 @@
if (any(_imageInitializations))
{
- _builder.writeImageInitializations(writer,
_imageInitializations.toString(), _preloadName, cycle);
+ _builder.writeImageInitializations(writer,
StringUtils.stripToEmpty(_imageInitializations.toString())
+ , _preloadName, cycle);
}
if (any(_bodyScript))
{
- _builder.writeBodyScript(writer, _bodyScript.toString(), cycle);
+ _builder.writeBodyScript(writer,
StringUtils.stripToEmpty(_bodyScript.toString())
+ ,
+ cycle);
}
_builder.endBodyScript(writer, cycle);
@@ -268,7 +303,8 @@
if (!any(_initializationScript))
return;
- _builder.writeInitializationScript(writer,
_initializationScript.toString());
+ _builder.writeInitializationScript(writer,
StringUtils.stripToEmpty(_initializationScript.toString())
+ );
}
private boolean any(StringBuffer buffer)
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/FormSupportTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/FormSupportTest.java?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/FormSupportTest.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/FormSupportTest.java
Sun Nov 26 09:35:13 2006
@@ -211,7 +211,7 @@
trainGetFieldFocus(cycle, null);
- support.addInitializationScript(form,
"tapestry.form.focusField('wilma');");
+ support.addInitializationScript(form,
"dojo.require(\"tapestry.form\");tapestry.form.focusField('wilma');");
cycle.setAttribute(FormSupportImpl.FIELD_FOCUS_ATTRIBUTE,
Boolean.TRUE);
replay();
@@ -1073,7 +1073,7 @@
trainGetFieldFocus(cycle, null);
- support.addInitializationScript(form,
"tapestry.form.focusField('barney');");
+ support.addInitializationScript(form,
"dojo.require(\"tapestry.form\");tapestry.form.focusField('barney');");
cycle.setAttribute(FormSupportImpl.FIELD_FOCUS_ATTRIBUTE,
Boolean.TRUE);
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/TestPageRenderSupport.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/TestPageRenderSupport.java?view=diff&rev=479383&r1=479382&r2=479383
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/TestPageRenderSupport.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/TestPageRenderSupport.java
Sun Nov 26 09:35:13 2006
@@ -143,10 +143,10 @@
{ "<script type=\"text/javascript\"><!--","",
"dojo.addOnLoad(function(e) {",
"tapestry._preload = [];", "if (document.images)", "{",
- " tapestry._preload[0] = new Image();",
+ "tapestry._preload[0] = new Image();",
" tapestry._preload[0].src = \"/foo/bar.gif\";",
" tapestry._preload[1] = new Image();",
- " tapestry._preload[1].src = \"/zip/zap.png\";", "}",
"});myBodyScript();",
+ " tapestry._preload[1].src = \"/zip/zap.png\";}",
"});myBodyScript();",
"// --></script>" });
verify();
@@ -171,8 +171,8 @@
{"<script type=\"text/javascript\"><!--", "",
"dojo.addOnLoad(function(e) {",
"NAMESPACE_preload = [];", "if (document.images)", "{",
- " NAMESPACE_preload[0] = new Image();",
- " NAMESPACE_preload[0].src = \"/foo/bar.gif\";", "}",
+ "NAMESPACE_preload[0] = new Image();",
+ " NAMESPACE_preload[0].src = \"/foo/bar.gif\";}",
"});",
"// --></script>" });
@@ -235,7 +235,7 @@
verify();
}
-
+
public void testAddInitializationScript()
{
AssetFactory factory = newAssetFactory();
@@ -248,13 +248,24 @@
prs.addInitializationScript("myInitializationScript1();");
prs.addInitializationScript("myInitializationScript2();");
+ prs.addInitializationScript("dojo.require(\"dojo.event.*\");");
+
prs.addInitializationScript("dojo.require(\"dojo.widget.*\");dojo.require(\"dojo.event.*\");");
+ prs.addInitializationScript("dojo.require(\"dojo.event.*\");");
+
prs.addInitializationScript("dojo.require(\"tapestry.form\");tapestry.form.registerForm(\"valid\");\n"
+
+ "tapestry.form.focusField(\'inputEnabled\');\n" +
+
"dojo.require(\"tapestry.form\");tapestry.form.registerForm(\"form\");");
prs.writeInitializationScript(writer);
assertOutput(new String[]
{ "<script type=\"text/javascript\"><!--",
- "dojo.addOnLoad(function(e) {","",
- "myInitializationScript1();", "myInitializationScript2();});",
+ "dojo.addOnLoad(function(e) {",
+ "myInitializationScript1();", "myInitializationScript2();",
+
"dojo.require(\"dojo.event.*\");","dojo.require(\"dojo.widget.*\");",
+ "",
+ "tapestry.form.registerForm(\"valid\");",
+ "tapestry.form.focusField(\'inputEnabled\');",
+
"dojo.require(\"tapestry.form\");tapestry.form.registerForm(\"form\");});",
"// --></script>" });
verify();