Author: vbaranov
Date: 2008-04-15 06:04:40 -0400 (Tue, 15 Apr 2008)
New Revision: 7833
Modified:
trunk/ui/componentControl/src/main/config/component/componentControl.xml
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
trunk/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
trunk/ui/componentControl/src/main/templates/htmlComponentControl.jspx
Log:
http://jira.jboss.com/jira/browse/RF-1531
Modified: trunk/ui/componentControl/src/main/config/component/componentControl.xml
===================================================================
--- trunk/ui/componentControl/src/main/config/component/componentControl.xml 2008-04-15
09:59:49 UTC (rev 7832)
+++ trunk/ui/componentControl/src/main/config/component/componentControl.xml 2008-04-15
10:04:40 UTC (rev 7833)
@@ -105,7 +105,7 @@
<description>
Defines an Id of a component or HTML element listened by the control component
</description>
- <defaultvalue>"immediate"</defaultvalue>
+ <defaultvalue>"onavailable"</defaultvalue>
</property>
<property hidden="true">
Modified:
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
===================================================================
---
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2008-04-15
09:59:49 UTC (rev 7832)
+++
trunk/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2008-04-15
10:04:40 UTC (rev 7833)
@@ -1,38 +1,160 @@
package org.richfaces.renderkit;
+import java.io.IOException;
+
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import org.ajax4jsf.renderkit.ComponentVariables;
+import org.ajax4jsf.renderkit.ComponentsVariableResolver;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
+import org.ajax4jsf.renderkit.RendererUtils;
+import org.ajax4jsf.resource.InternetResource;
import org.richfaces.component.UIComponentControl;
import org.richfaces.component.util.HtmlUtil;
public class ComponentControlRendererBase extends HeaderResourcesRendererBase {
- protected Class getComponentClass() {
- return UIComponentControl.class;
+ /**
+ * Constant for "immediate" attach timing option
+ */
+ private static final String IMMEDIATE = "immediate";
+
+ /**
+ * Constant for "onAvailable" attach timing option
+ */
+ private static final String ON_AVAILABLE = "onavailable";
+
+ /**
+ * Constant for "onload" attach timing option
+ */
+ private static final String ON_LOAD = "onload";
+
+ protected Class<UIComponentControl> getComponentClass() {
+ return UIComponentControl.class;
+ }
+
+ /**
+ * Additional scripts.
+ */
+ private final InternetResource[] additionalScripts = { new
org.ajax4jsf.javascript.PrototypeScript(),
+ new org.ajax4jsf.javascript.AjaxScript(),
getResource("/org/richfaces/renderkit/html/scripts/available.js") };
+
+ /**
+ * Perform validation of the component control configuration. Throws FacesException
in case validation fails.
+ * @param clientId - id of the component
+ * @param name - component name
+ * @param attachTiming - timing options
+ * @param forAttr - client ids of target components
+ * @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 + "'");
}
- protected void checkValidity(String clientId, String name, String attachTiming, String
forAttr, String operation) {
+ if (operation == null || operation.trim().length() == 0) {
+ throw new FacesException("The operation attribute of the controlComponent
(id='" + clientId
+ + "') must be specified");
+ }
+ }
- if ( ! "onload".equals(attachTiming) &&
- ! "immediate".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',
'onload', 'onJScall'");
- }
+ protected String replaceClientIds(FacesContext context, UIComponent component, String
selector) {
+ return HtmlUtil.expandIdSelector(HtmlUtil.idsToIdSelector(selector), component,
context);
+ }
- if (operation==null || operation.trim().length() == 0) {
- throw new FacesException(
- "The operation attribute of the controlComponent
(id='"+clientId+"') must be specified" );
- }
+ /**
+ * Prepare Java script according to the timing option of the component control
+ * and write it to the ResponceWriter
+ * @param context - FacesContext
+ * @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;
}
- protected String replaceClientIds(FacesContext context, UIComponent component, String
selector) {
- return HtmlUtil.expandIdSelector(HtmlUtil.idsToIdSelector(selector), component,
context);
+ 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(replaceClientIds(context, component, result[i]));
+ writer.write(attachEventBodyEnd.toString());
+
+ if (isOnLoad || isOnAvailable) {
+ writer.write(");");
+ }
}
+ writer.writeText("//", null);
+ writer.write("]]>");
+ writer.endElement("script");
+ }
+
+ /**
+ * Gets additional scripts.
+ *
+ * @return array of resources
+ */
+ protected InternetResource[] getScripts() {
+ return additionalScripts;
+ }
+
+ /**
+ * Returns String representation of object. If object is null,
+ * returns empty String.
+ * @param obj - object
+ * @return String representation of object.
+ */
+ private static String convertToString(Object obj ) {
+ return ( obj == null ? "" : obj.toString() );
+ }
}
Modified:
trunk/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
===================================================================
---
trunk/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2008-04-15
09:59:49 UTC (rev 7832)
+++
trunk/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2008-04-15
10:04:40 UTC (rev 7833)
@@ -30,18 +30,13 @@
};
Richfaces.componentControl.attachEvent = function(attachTo, aevent, forAttr, operation,
params, disableDefault) {
- var pattern = /\s*,\s*/;
- // "attachTo" attribute may contain several ids splitted by ","
- result = attachTo.split(pattern);
- for(i=0; i < result.length; i++) {
- jQuery(result[i]).bind(Richfaces.effectEventOnOut(aevent),function(cevent) {
- Richfaces.componentControl.performOperation(cevent, forAttr, operation, params,
disableDefault);
- }).each(function() {
- Richfaces.componentControl.applyDecorations(this, forAttr, function(element) {
- //TODO: handle component decoration
- });
+ jQuery(attachTo).bind(Richfaces.effectEventOnOut(aevent),function(cevent) {
+ Richfaces.componentControl.performOperation(cevent, forAttr, operation, params,
disableDefault);
+ }).each(function() {
+ Richfaces.componentControl.applyDecorations(this, forAttr, function(element) {
+ //TODO: handle component decoration
});
- }
+ });
};
Richfaces.componentControl.performOperation = function( cevent, forAttr, operation,
params, disableDefault) {
Modified: trunk/ui/componentControl/src/main/templates/htmlComponentControl.jspx
===================================================================
--- trunk/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2008-04-15
09:59:49 UTC (rev 7832)
+++ trunk/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2008-04-15
10:04:40 UTC (rev 7833)
@@ -48,31 +48,9 @@
<jsp:scriptlet><![CDATA[ } ]]></jsp:scriptlet>
-<jsp:scriptlet><![CDATA[ if ( "immediate".equals(attachTiming)
&& ! "#".equals(attachTo) ) { ]]></jsp:scriptlet>
-<script type="text/javascript">
- //<![CDATA[
- {
+<jsp:scriptlet><![CDATA[ if (! "#".equals(attachTo) ) { ]]>
+ attachEventAccordingToTimingOption(context, component);
+<![CDATA[ } ]]>
+</jsp:scriptlet>
-Richfaces.componentControl.attachEvent(
- '#{attachTo}', '#{event}', '#{forAttr}', '#{operation}',
function() { return {#{params}}; }, #{component.disableDefault} );
-
-}
-//]]>
-</script>
-<jsp:scriptlet><![CDATA[ } ]]></jsp:scriptlet>
-
-
-
-<jsp:scriptlet><![CDATA[ if ( "onload".equals(attachTiming) &&
! "#".equals(attachTo) ) {]]></jsp:scriptlet>
-<script type="text/javascript">
- //<![CDATA[
- jQuery(document).ready(function() {
- Richfaces.componentControl.attachEvent(
- '#{attachTo}', '#{event}', '#{forAttr}', '#{operation}',
function() { return {#{params}}; }, #{component.disableDefault} );
- });
-
-//]]>
-</script>
-<jsp:scriptlet><![CDATA[ } ]]></jsp:scriptlet>
-
</f:root>
\ No newline at end of file