Author: Alex.Kolonitsky
Date: 2009-02-16 05:38:02 -0500 (Mon, 16 Feb 2009)
New Revision: 12667
Modified:
trunk/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
trunk/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java
trunk/ui/core/src/main/java/org/ajax4jsf/component/UISelector.java
trunk/ui/core/src/main/java/org/ajax4jsf/renderkit/AjaxCommandRendererBase.java
Log:
Component control generates unnecessary JavaScript on page when attachTo attribute is not
defined
https://jira.jboss.org/jira/browse/RF-2302
Modified:
trunk/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java
===================================================================
---
trunk/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java 2009-02-16
10:21:25 UTC (rev 12666)
+++
trunk/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java 2009-02-16
10:38:02 UTC (rev 12667)
@@ -57,8 +57,7 @@
* @return JavaScript eventString. Rebuild on every call, since
* can be in loop ( as in dataTable ) with different parameters.
*/
- public String getEventString()
- {
+ public String getEventString() {
String targetId = HtmlUtil.idsToIdSelector(getFor());
targetId = HtmlUtil.expandIdSelector(targetId, this,
FacesContext.getCurrentInstance());
@@ -71,8 +70,6 @@
invocation.addParameter(Boolean.valueOf(isDisableDefault()));
return invocation.toScript();
-
-
}
public String getEncodedParametersMap() {
Modified:
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
===================================================================
---
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2009-02-16
10:21:25 UTC (rev 12666)
+++
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2009-02-16
10:38:02 UTC (rev 12667)
@@ -54,7 +54,7 @@
private static final String ON_LOAD = "onload";
protected Class<UIComponentControl> getComponentClass() {
- return UIComponentControl.class;
+ return UIComponentControl.class;
}
/**
@@ -72,20 +72,20 @@
* @param operation - operation performed on target components
*/
protected void checkValidity(String clientId, String name, String attachTiming,
String forAttr, String operation) {
- if (!ON_LOAD.equals(attachTiming) && !IMMEDIATE.equals(attachTiming) &&
!ON_AVAILABLE.equals(attachTiming)) {
- throw new FacesException("The attachTiming attribute of the controlComponent
(id='" + clientId
- + "') has an invalid value:'" + attachTiming + "'. It
may have only the following values: '"
- + IMMEDIATE + "', '" + ON_LOAD + "', '" +
ON_AVAILABLE + "'");
- }
-
- if (operation == null || operation.trim().length() == 0) {
- throw new FacesException("The operation attribute of the controlComponent
(id='" + clientId
- + "') must be specified");
- }
+ if (!ON_LOAD.equals(attachTiming) && !IMMEDIATE.equals(attachTiming)
&& !ON_AVAILABLE.equals(attachTiming)) {
+ throw new FacesException("The attachTiming attribute of the
controlComponent (id='" + clientId
+ + "') has an invalid value:'" + attachTiming + "'.
It may have only the following values: '"
+ + IMMEDIATE + "', '" + ON_LOAD + "', '" +
ON_AVAILABLE + "'");
+ }
+
+ if (operation == null || operation.trim().length() == 0) {
+ throw new FacesException("The operation attribute of the controlComponent
(id='" + clientId
+ + "') must be specified");
+ }
}
protected String replaceClientIds(FacesContext context, UIComponent component, String
selector) {
- return HtmlUtil.expandIdSelector(HtmlUtil.idsToIdSelector(selector), component,
context);
+ return HtmlUtil.expandIdSelector(HtmlUtil.idsToIdSelector(selector), component,
context);
}
/**
@@ -95,68 +95,72 @@
* @param component - component control
* @throws IOException - is thrown in case of writing to ResponceWriter exception
*/
- protected void attachEventAccordingToTimingOption(FacesContext context, UIComponent
component) throws IOException {
- if (!(component instanceof UIComponentControl)) {
- return;
- }
-
- UIComponentControl componentControl = (UIComponentControl) component;
- String attachTo = componentControl.getAttachTo();
- String attachTiming = componentControl.getAttachTiming();
- boolean isImmediate = attachTiming.equals(IMMEDIATE);
- boolean isOnLoad = attachTiming.equals(ON_LOAD);
- boolean isOnAvailable = attachTiming.equals(ON_AVAILABLE);
-
- if (!(isImmediate || isOnLoad || isOnAvailable)) {
- // unknown value of property "attachTiming"
- return;
- }
-
- ResponseWriter writer = context.getResponseWriter();
- ComponentVariables variables = ComponentsVariableResolver.getVariables(this,
componentControl);
-
- writer.startElement("script", componentControl);
- getUtils().writeAttribute(writer, "type", "text/javascript");
- writer.writeText("//", null);
- writer.write("<![CDATA[");
-
- String attachEventBodyStart =
"\n{\n Richfaces.componentControl.attachEvent('";
- StringBuilder attachEventBodyEnd = new StringBuilder();
- attachEventBodyEnd.append("', '");
- attachEventBodyEnd.append(convertToString(variables.getVariable("event")));
- attachEventBodyEnd.append("', '");
- attachEventBodyEnd.append(convertToString(variables.getVariable("forAttr")));
- attachEventBodyEnd.append("', '");
- attachEventBodyEnd.append(convertToString(variables.getVariable("operation")));
- attachEventBodyEnd.append("', function() { return {");
- attachEventBodyEnd.append(convertToString(variables.getVariable("params")));
- attachEventBodyEnd.append("}; }, ");
- attachEventBodyEnd.append(convertToString(componentControl.isDisableDefault()));
- attachEventBodyEnd.append(");\n }");
-
- String pattern = "\\s*,\\s*";
- // "attachTo" attribute may contain several ids splitted by ","
- String[] result = attachTo.split(pattern);
- for (int i = 0; i < result.length; i++) {
- if (isOnLoad) {
- writer.write("\n jQuery(document).ready(function()");
- } else if (isOnAvailable) {
- UIComponent target = RendererUtils.getInstance().findComponentFor(context, component,
result[i]);
- String clientId = (target != null) ? target.getClientId(context) : result[i];
- writer.write("\n Richfaces.onAvailable('" + clientId + "',
function()");
- } else if (isImmediate) {
- }
- writer.write(attachEventBodyStart);
- writer.write(getUtils().escapeJavaScript(replaceClientIds(context, component,
result[i])));
- writer.write(attachEventBodyEnd.toString());
-
- if (isOnLoad || isOnAvailable) {
- writer.write(");");
- }
- }
- writer.writeText("//", null);
- writer.write("]]>");
- writer.endElement("script");
+ protected void attachEventAccordingToTimingOption(FacesContext context,
+ UIComponent component) throws IOException {
+ if (!(component instanceof UIComponentControl)) {
+ return;
+ }
+
+ UIComponentControl componentControl = (UIComponentControl) component;
+ String attachTo = componentControl.getAttachTo();
+ String attachTiming = componentControl.getAttachTiming();
+ boolean isImmediate = attachTiming.equals(IMMEDIATE);
+ boolean isOnLoad = attachTiming.equals(ON_LOAD);
+ boolean isOnAvailable = attachTiming.equals(ON_AVAILABLE);
+
+ if (!(isImmediate || isOnLoad || isOnAvailable || attachTo == null ||
attachTo.length() == 0)) {
+ // unknown value of property "attachTiming"
+ return;
+ }
+
+ ResponseWriter writer = context.getResponseWriter();
+ ComponentVariables variables =
+ ComponentsVariableResolver.getVariables(this, componentControl);
+
+ writer.startElement("script", componentControl);
+ getUtils().writeAttribute(writer, "type",
"text/javascript");
+ writer.writeText("//", null);
+ writer.write("<![CDATA[");
+
+ String attachEventBodyStart =
"\n{\n Richfaces.componentControl.attachEvent('";
+ StringBuilder attachEventBodyEnd = new StringBuilder();
+ attachEventBodyEnd.append("', '");
+
attachEventBodyEnd.append(convertToString(variables.getVariable("event")));
+ attachEventBodyEnd.append("', '");
+
attachEventBodyEnd.append(convertToString(variables.getVariable("forAttr")));
+ attachEventBodyEnd.append("', '");
+
attachEventBodyEnd.append(convertToString(variables.getVariable("operation")));
+ attachEventBodyEnd.append("', function() { return {");
+
attachEventBodyEnd.append(convertToString(variables.getVariable("params")));
+ attachEventBodyEnd.append("}; }, ");
+ attachEventBodyEnd.append(convertToString(componentControl.isDisableDefault()));
+ attachEventBodyEnd.append(");\n }");
+
+ String pattern = "\\s*,\\s*";
+ // "attachTo" attribute may contain several ids splitted by
","
+ String[] result = attachTo.split(pattern);
+ for (int i = 0; i < result.length; i++) {
+ if (isOnLoad) {
+ writer.write("\n jQuery(document).ready(function()");
+ } else if (isOnAvailable) {
+ UIComponent target = RendererUtils.getInstance()
+ .findComponentFor(context, component, result[i]);
+ String clientId = (target != null) ? target.getClientId(context) :
result[i];
+ writer.write("\n Richfaces.onAvailable('" + clientId +
"', function()");
+ } else if (isImmediate) {
+ }
+
+ writer.write(attachEventBodyStart);
+ writer.write(getUtils().escapeJavaScript(replaceClientIds(context, component,
result[i])));
+ writer.write(attachEventBodyEnd.toString());
+
+ if (isOnLoad || isOnAvailable) {
+ writer.write(");");
+ }
+ }
+ writer.writeText("//", null);
+ writer.write("]]>");
+ writer.endElement("script");
}
/**
@@ -165,7 +169,7 @@
* @return array of resources
*/
protected InternetResource[] getScripts() {
- return additionalScripts;
+ return additionalScripts;
}
/**
@@ -175,6 +179,6 @@
* @return String representation of object.
*/
private static String convertToString(Object obj ) {
- return ( obj == null ? "" : obj.toString() );
+ return ( obj == null ? "" : obj.toString() );
}
}
Modified:
trunk/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java
===================================================================
---
trunk/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java 2009-02-16
10:21:25 UTC (rev 12666)
+++
trunk/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java 2009-02-16
10:38:02 UTC (rev 12667)
@@ -21,8 +21,9 @@
package org.richfaces.component;
+import java.util.List;
+
import javax.faces.FacesException;
-import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
@@ -30,8 +31,8 @@
import javax.faces.component.html.HtmlForm;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
-import org.richfaces.component.UIComponentControl;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
@@ -44,8 +45,7 @@
*
* @param testName name of the test case
*/
- public ComponentControlTest( String testName )
- {
+ public ComponentControlTest (String testName) {
super( testName );
}
@@ -109,7 +109,40 @@
String eventString = htmlInput.getAttributeValue("onclick");
assertNotNull(eventString);
assertTrue(eventString.contains("Richfaces.componentControl.performOperation"));
- String [] params = eventString.split(",");
+
+ eventStringParams(eventString);
+ }
+
+ public void testAttachString() throws Exception {
+ HtmlPage page = renderView();
+ assertNotNull(page);
+
+ List<?> scripts = page.getHtmlElementsByName("script");
+ String attachTo = componentControl.getAttachTo();
+ assertTrue("If on componentControl defined attachTo attr, on page must be
presented attach script",
+ scripts.isEmpty() && (attachTo == null || attachTo.length() ==
0));
+
+ String eventString = null;
+ for (Object obj : scripts) {
+ HtmlElement element = (HtmlElement) obj;
+ if
(element.asText().contains("Richfaces.componentControl.attachEvent")) {
+ eventString = element.asText();
+ break;
+ }
+ }
+
+ if (attachTo == null || attachTo.length() == 0) {
+ assertNull(eventString);
+ } else {
+ assertNotNull(eventString);
+
assertTrue(eventString.contains("Richfaces.componentControl.attachEvent"));
+
+ eventStringParams(eventString);
+ }
+ }
+
+ private void eventStringParams(String eventString) {
+ String [] params = eventString.split(",");
assertEquals(6, params.length);
assertTrue(params[0].trim().endsWith("event"));
@@ -118,8 +151,6 @@
assertTrue(params[3].trim().startsWith("{"));
assertTrue(params[4].trim().endsWith("}"));
assertTrue(params[5].trim().endsWith("false)"));
-
- assertTrue( true );
}
public void testParametersMap() throws Exception {
Modified: trunk/ui/core/src/main/java/org/ajax4jsf/component/UISelector.java
===================================================================
--- trunk/ui/core/src/main/java/org/ajax4jsf/component/UISelector.java 2009-02-16 10:21:25
UTC (rev 12666)
+++ trunk/ui/core/src/main/java/org/ajax4jsf/component/UISelector.java 2009-02-16 10:38:02
UTC (rev 12667)
@@ -47,7 +47,7 @@
private static final AjaxChildrenRenderer childrenRenderer = new AjaxChildrenRenderer()
{
- protected Class getComponentClass() {
+ protected Class<? extends UIComponent> getComponentClass() {
return UISelector.class;
}
@@ -74,20 +74,19 @@
* @see
org.ajax4jsf.framework.ajax.AjaxChildrenEncoder#encodeAjaxChild(javax.faces.context.FacesContext,
* java.lang.String, java.util.Set, java.util.Set)
*/
- public void encodeAjaxChild(FacesContext context, String path, Set ids,
- Set renderedAreas) throws IOException {
+ public void encodeAjaxChild(FacesContext context, String path,
+ Set<String> ids, Set<String> renderedAreas) throws IOException {
if (getChildCount() != 1) {
- throw new FacesException(
- "Selector component must have one, and only one, child");
+ throw new FacesException("Selector component must have one, and only one,
child");
}
UIComponent child = (UIComponent) getChildren().get(0);
- Set ajaxKeys = getAjaxKeys();
+ Set<Object> ajaxKeys = getAjaxKeys();
if (null != ajaxKeys) {
String iterationProperty = getIterationProperty();
try {
Object savedKey = PropertyUtils.getProperty(child,
iterationProperty);
- for (Iterator iter = ajaxKeys.iterator(); iter.hasNext();) {
+ for (Iterator<Object> iter = ajaxKeys.iterator(); iter.hasNext();) {
Object key = (Object) iter.next();
PropertyUtils.setProperty(child, iterationProperty, key);
if (true) {
Modified: trunk/ui/core/src/main/java/org/ajax4jsf/renderkit/AjaxCommandRendererBase.java
===================================================================
---
trunk/ui/core/src/main/java/org/ajax4jsf/renderkit/AjaxCommandRendererBase.java 2009-02-16
10:21:25 UTC (rev 12666)
+++
trunk/ui/core/src/main/java/org/ajax4jsf/renderkit/AjaxCommandRendererBase.java 2009-02-16
10:38:02 UTC (rev 12667)
@@ -125,7 +125,7 @@
return false;
}
String clientId = uiComponent.getClientId(facesContext);
- Map paramMap = facesContext.getExternalContext()
+ Map<String, String> paramMap = facesContext.getExternalContext()
.getRequestParameterMap();
Object value = paramMap.get(clientId);
boolean submitted = null != value;