Author: nbelaevski
Date: 2007-11-14 15:22:08 -0500 (Wed, 14 Nov 2007)
New Revision: 3997
Modified:
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/sandbox/component/UIComponentControl.java
trunk/sandbox/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
trunk/sandbox/ui/componentControl/src/main/templates/htmlComponentControl.jspx
Log:
- componentControl code reviewed
- event parameters added
- parameters encoding added
Modified:
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
===================================================================
---
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2007-11-14
19:06:26 UTC (rev 3996)
+++
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2007-11-14
20:22:08 UTC (rev 3997)
@@ -4,55 +4,36 @@
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
-import org.ajax4jsf.javascript.JSEncoder;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
-import org.richfaces.sandbox.component.UIComponentControl;
import org.richfaces.component.util.HtmlUtil;
+import org.richfaces.sandbox.component.UIComponentControl;
public class ComponentControlRendererBase extends HeaderResourcesRendererBase {
- protected Class getComponentClass() {
- return UIComponentControl.class;
- }
+ protected Class getComponentClass() {
+ return UIComponentControl.class;
+ }
-
-
protected void checkValidity(String clientId, String name, String attachTiming, String
forAttr, String operation) {
-
- if ( ! "onJScall".equals(attachTiming) &&
- ! "onload".equals(attachTiming) &&
+
+ if ( ! "onload".equals(attachTiming) &&
! "immediate".equals(attachTiming) ) {
throw new FacesException(
- "The attachTiming attribute of the controlComponent
(id='"+clientId+"') has an invalid value:'"+ attachTiming +
+ "The attachTiming attribute of the controlComponent
(id='"+clientId+"') has an invalid value:'"+ attachTiming +
"'. It may have only the following values: 'immediate',
'onload', 'onJScall'");
}
-
- if ( name == null ) {
- throw new FacesException(
- "The name attribute of the controlComponent
(id='"+clientId+"') might not be null" );
-
- }
- if ( "".equals(operation.trim()) || operation==null ) {
+ if (operation==null || "".equals(operation.trim())) {
throw new FacesException(
"The operation attribute of the controlComponent
(id='"+clientId+"') must be specified" );
}
-
-
- if ( "".equals(name.trim()) &&
"onJScall".equals(attachTiming) ) {
- throw new FacesException(
- "The name attribute of the controlComponent
(id='"+clientId+"') must be specified when timing attribute equals to
'onJScall'" );
- }
-
+
}
+
protected String replaceClientIds(FacesContext context, UIComponent component, String
selector) {
return HtmlUtil.expandIdSelector(selector, component, context);
}
-
-
-
-
}
Modified:
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/sandbox/component/UIComponentControl.java
===================================================================
---
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/sandbox/component/UIComponentControl.java 2007-11-14
19:06:26 UTC (rev 3996)
+++
trunk/sandbox/ui/componentControl/src/main/java/org/richfaces/sandbox/component/UIComponentControl.java 2007-11-14
20:22:08 UTC (rev 3997)
@@ -4,15 +4,22 @@
package org.richfaces.sandbox.component;
-import org.ajax4jsf.Messages;
-import org.ajax4jsf.component.EventValueBinding;
+import java.util.Iterator;
+
+import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
+import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
-import javax.faces.component.UIComponent;
+import javax.faces.el.ValueBinding;
+
+import org.ajax4jsf.Messages;
import org.ajax4jsf.component.AjaxSupport;
+import org.ajax4jsf.component.EventValueBinding;
+import org.ajax4jsf.component.JavaScriptParameter;
+import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.ajax4jsf.renderkit.RendererUtils;
import org.richfaces.component.util.HtmlUtil;
/**
@@ -20,66 +27,109 @@
*
*/
public abstract class UIComponentControl extends UIComponentBase implements AjaxSupport
{
-
+
public static final String COMPONENT_TYPE =
"org.richfaces.sandbox.ComponentControl";
-
+
public static final String COMPONENT_FAMILY =
"org.richfaces.sandbox.ComponentControl";
private static final Log log = LogFactory.getLog(UIComponentControl.class);
-
- /**
- * @return JavaScript eventString. Rebuild on every call, since
- * can be in loop ( as in dataTable ) with different parameters.
- */
- public String getEventString()
- {
- StringBuffer buildOnEvent = new StringBuffer();
-
- String targetId="#" + getFor();
+ /**
+ * @return JavaScript eventString. Rebuild on every call, since
+ * can be in loop ( as in dataTable ) with different parameters.
+ */
+ public String getEventString()
+ {
+ StringBuffer buildOnEvent = new StringBuffer();
+
+ String targetId="#" + getFor();
+
targetId=HtmlUtil.expandIdSelector(targetId, this, FacesContext.getCurrentInstance());
- buildOnEvent.append("Richfaces.componentControl.performOperation(
null,");
- buildOnEvent.append("'" + targetId + "',");
- buildOnEvent.append("'" + getOperation() + "',");
- buildOnEvent.append("{" + getParams() + "});");
-
- return buildOnEvent.toString();
+ buildOnEvent.append("Richfaces.componentControl.performOperation(event, ");
+ buildOnEvent.append("'" + targetId + "',");
+ buildOnEvent.append("'" + getOperation() + "',");
+ buildOnEvent.append("{" + getEncodedParametersMap() + "});");
+ return buildOnEvent.toString();
- }
-
-
- public abstract String getEvent();
- public abstract void setEvent(String event);
- public abstract String getFor();
- public abstract void setFor(String value);
+ }
- public abstract String getParams();
- public abstract void setParams(String value);
+ public String getEncodedParametersMap() {
+ StringBuffer result = new StringBuffer();
+
+ boolean shouldClose = false;
- public abstract String getOperation();
- public abstract void setOperation(String value);
+ String params = this.getParams();
+ if (params != null && params.trim().length() != 0) {
+ result.append(params);
+ shouldClose = true;
+ }
+
+ for (Iterator it = this.getChildren().iterator(); it.hasNext();) {
+ UIComponent child = (UIComponent) it.next();
+ if (child instanceof UIParameter) {
+ String name = ((UIParameter) child).getName();
+ Object value = ((UIParameter) child).getValue();
+ if (null == name) {
+ FacesContext context = FacesContext.getCurrentInstance();
+ throw new IllegalArgumentException(Messages.getMessage(
+ Messages.UNNAMED_PARAMETER_ERROR, this
+ .getClientId(context)));
+ }
+
+ boolean escape = true;
+ if (child instanceof JavaScriptParameter) {
+ JavaScriptParameter actionParam = (JavaScriptParameter) child;
+ escape = !actionParam.isNoEscape();
+ }
+
+ if (shouldClose) {
+ result.append(", ");
+ }
- public abstract String getAttachTo();
- public abstract void setAttachTo(String value);
-
+ ScriptUtils.addEncodedString(result, name);
+ result.append(": ");
+ result.append(ScriptUtils.toScript(escape ? value : new
JSReference(value.toString())));
+
+ shouldClose = true;
+ }
+ }
+
+ return result.toString();
+ }
+
+ public abstract String getEvent();
+ public abstract void setEvent(String event);
+
+ public abstract String getFor();
+ public abstract void setFor(String value);
+
+ public abstract String getParams();
+ public abstract void setParams(String value);
+
+ public abstract String getOperation();
+ public abstract void setOperation(String value);
+
+ public abstract String getAttachTo();
+ public abstract void setAttachTo(String value);
+
protected String replaceClientIds(FacesContext context, UIComponent component, String
selector) {
return HtmlUtil.expandIdSelector(selector, component, context);
}
-
- /**
- * After nornal setting <code>parent</code> property in case of
- * created component set Ajax properties for parent.
- * @see
javax.faces.component.UIComponentBase#setParent(javax.faces.component.UIComponent)
- */
- public void setParent(UIComponent parent)
- {
- super.setParent(parent);
- if (null != parent && parent.getFamily() != null ) {
+
+ /**
+ * After nornal setting <code>parent</code> property in case of
+ * created component set Ajax properties for parent.
+ * @see
javax.faces.component.UIComponentBase#setParent(javax.faces.component.UIComponent)
+ */
+ public void setParent(UIComponent parent)
+ {
+ super.setParent(parent);
+ if (null != parent && parent.getFamily() != null ) {
if (log.isDebugEnabled()) {
log.debug(Messages.getMessage(Messages.CALLED_SET_PARENT,
parent.getClass().getName()));
}
@@ -96,14 +146,24 @@
}
}
- }
+ }
- public void setParentProperties(UIComponent parent){
- if (getAttachTo() == "" && getEvent() !="" ) {
- parent.setValueBinding(getEvent(), new EventValueBinding(this));
+ public void setParentProperties(UIComponent parent){
+ String event = getEvent();
+ String attachTo = getAttachTo();
+
+ if (event != null && event.length() != 0) {
+ if (attachTo == null || attachTo.length() == 0) {
+ parent.setValueBinding(event, new EventValueBinding(this));
+ } else {
+ ValueBinding vb = parent.getValueBinding(event);
+ if (vb instanceof EventValueBinding) {
+ //TODO check if that's EventValueBinding for us
+ parent.setValueBinding(event, null);
+ }
+ }
+ }
}
- }
-
}
Modified:
trunk/sandbox/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
===================================================================
---
trunk/sandbox/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2007-11-14
19:06:26 UTC (rev 3996)
+++
trunk/sandbox/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2007-11-14
20:22:08 UTC (rev 3997)
@@ -6,8 +6,8 @@
Richfaces.componentControl = {};
}
-Richfaces.componentControl.attachEvent = function(attachTo, cevent, aevent, forAttr,
operation, params) {
- jQuery(attachTo).bind(Richfaces.effectEventOnOut(aevent), function() {
+Richfaces.componentControl.attachEvent = function(attachTo, aevent, forAttr, operation,
params) {
+ jQuery(attachTo).bind(Richfaces.effectEventOnOut(aevent), function(cevent) {
jQuery( forAttr)[0].component[operation](cevent, params);
});
};
Modified: trunk/sandbox/ui/componentControl/src/main/templates/htmlComponentControl.jspx
===================================================================
---
trunk/sandbox/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2007-11-14
19:06:26 UTC (rev 3996)
+++
trunk/sandbox/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2007-11-14
20:22:08 UTC (rev 3997)
@@ -17,7 +17,7 @@
<c:set var="attachTo"
value="#{component.attributes['attachTo']}"/>
<c:set var="name"
value="#{component.attributes['name']}"/>
<c:set var="operation"
value="#{component.attributes['operation']}"/>
- <c:set var="params"
value="#{component.attributes['params']}"/>
+ <c:set var="params"
value="#{component.encodedParametersMap}"/>
<f:clientid var="clientId"/>
@@ -39,9 +39,9 @@
<jsp:scriptlet><![CDATA[ if (! "".equals(name.trim()) ) {
]]></jsp:scriptlet>
<script type="text/javascript">
//<![CDATA[
-function #{name}() {
+function #{name}(cevent) {
Richfaces.componentControl.performOperation(
- null, '#{forAttr}', '#{operation}', {#{params}} );
+ cevent, '#{forAttr}', '#{operation}', {#{params}} );
}
//]]>
</script>
@@ -54,7 +54,7 @@
{
Richfaces.componentControl.attachEvent(
- '#{attachTo}', null, '#{event}', '#{forAttr}',
'#{operation}', {#{params}} );
+ '#{attachTo}', '#{event}', '#{forAttr}', '#{operation}',
{#{params}} );
}
//]]>
@@ -68,7 +68,7 @@
//<![CDATA[
jQuery(document).ready(function() {
Richfaces.componentControl.attachEvent(
- '#{attachTo}', null, '#{event}', '#{forAttr}',
'#{operation}', {#{params}} );
+ '#{attachTo}', '#{event}', '#{forAttr}', '#{operation}',
{#{params}} );
});
//]]>