Author: pyaschenko
Date: 2009-07-08 08:34:18 -0400 (Wed, 08 Jul 2009)
New Revision: 14850
Modified:
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
branches/community/3.3.X/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
branches/community/3.3.X/ui/componentControl/src/main/templates/htmlComponentControl.jspx
branches/community/3.3.X/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java
Log:
https://jira.jboss.org/jira/browse/RF-7474
Modified:
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java
===================================================================
---
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java 2009-07-08
11:53:55 UTC (rev 14849)
+++
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/component/UIComponentControl.java 2009-07-08
12:34:18 UTC (rev 14850)
@@ -68,14 +68,31 @@
JSFunction invocation = new
JSFunction("Richfaces.componentControl.performOperation");
invocation.addParameter(new JSReference("event"));
+ invocation.addParameter(getEvent());
invocation.addParameter(targetId);
invocation.addParameter(getOperation());
- invocation.addParameter(new JSReference("{" + getEncodedParametersMap()
+ "}"));
- invocation.addParameter(Boolean.valueOf(isDisableDefault()));
+ addOptions(invocation);
return invocation.toScript();
}
+ public void addOptions(JSFunction function) {
+ String params = getEncodedParametersMap();
+
+ if (params.length()!=0) {
+ function.addParameter(new
JSReference("function(){return{"+params+"}}"));
+ }
+ String event = (String) getEvent();
+ boolean disableDefault = isDisableDefault();
+ boolean isOnContextMenu = ("contextmenu".equalsIgnoreCase(event) ||
"oncontextmenu".equalsIgnoreCase(event));
+ if ( isOnContextMenu ^ disableDefault ) {
+ if (params.length()==0) {
+ function.addParameter(new JSReference("{}"));
+ }
+ function.addParameter(disableDefault);
+ }
+ }
+
public String getEncodedParametersMap() {
StringBuilder result = new StringBuilder();
Modified:
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java
===================================================================
---
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2009-07-08
11:53:55 UTC (rev 14849)
+++
branches/community/3.3.X/ui/componentControl/src/main/java/org/richfaces/renderkit/ComponentControlRendererBase.java 2009-07-08
12:34:18 UTC (rev 14850)
@@ -21,14 +21,19 @@
package org.richfaces.renderkit;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
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.javascript.JSFunction;
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.JSReference;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
import org.ajax4jsf.renderkit.RendererUtils;
import org.ajax4jsf.resource.InternetResource;
@@ -89,81 +94,6 @@
}
/**
- * 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 || 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");
- }
-
- /**
* Gets additional scripts.
*
* @return array of resources
@@ -181,4 +111,118 @@
private static String convertToString(Object obj ) {
return ( obj == null ? "" : obj.toString() );
}
-}
+
+ private static String convertToString(List array, String separator ) {
+ StringBuilder sb = new StringBuilder();
+ boolean close = false;
+ Iterator<Object> i = array.iterator();
+ while (i.hasNext())
+ {
+ Object item = i.next();
+ if (close) {
+ sb.append(separator);
+ } else {
+ close = true;
+ }
+ sb.append(item);
+ }
+ return sb.toString();
+ }
+
+ public void getScript(FacesContext context, UIComponent component) throws IOException
{
+
+ UIComponentControl componentControl = (UIComponentControl) component;
+ ResponseWriter writer = context.getResponseWriter();
+
+ Map<String, Object> attributes = component.getAttributes();
+
+ JSFunctionDefinition function = null;
+ List<JSFunction> functionArray = new ArrayList<JSFunction>();
+
+ String name = convertToString(attributes.get("name"));
+ String attachTo = convertToString(attributes.get("attachTo"));
+ String forAttr = convertToString(attributes.get("for"));
+ forAttr = replaceClientIds(context, component, forAttr);
+ String operation = convertToString(attributes.get("operation"));
+ String attachTiming = componentControl.getAttachTiming();
+ checkValidity(componentControl.getClientId(context), name, attachTiming, forAttr,
operation);
+ String event = convertToString(attributes.get("event"));
+
+ String performScript = null;
+
+ if (!"".equals(name.trim())) {
+ JSFunction subFunction = new
JSFunction("Richfaces.componentControl.performOperation");
+ subFunction.addParameter(new JSReference("cevent"));
+ subFunction.addParameter(event);
+ subFunction.addParameter(forAttr);
+ subFunction.addParameter(operation);
+ componentControl.addOptions(subFunction);
+
+ function = new JSFunctionDefinition("cevent");
+ function.setName(name);
+ function.addToBody(subFunction);
+ performScript = function.toString();
+ }
+
+ if (attachTo != null && attachTo.trim().length() != 0 &&
!"#".equals(attachTo)) {
+
+ 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;
+ }
+
+ 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++) {
+ JSFunction mainFunction = null;
+ if (isOnLoad) {
+ mainFunction = new JSFunction("jQuery(document).ready");
+ } else if (isOnAvailable) {
+ UIComponent target =
RendererUtils.getInstance().findComponentFor(context, component, result[i]);
+ String clientId = (target != null) ? target.getClientId(context) :
result[i];
+ mainFunction = new
JSFunction("Richfaces.onAvailable",clientId);
+ } else if (isImmediate) {
+ }
+
+ if (mainFunction!=null)
+ {
+ JSFunction subFunction = new
JSFunction("Richfaces.componentControl.attachEvent");
+ subFunction.addParameter(replaceClientIds(context, component,
result[i]));
+ subFunction.addParameter(event);
+ subFunction.addParameter(forAttr);
+ subFunction.addParameter(operation);
+ componentControl.addOptions(subFunction);
+
+ function = new JSFunctionDefinition("");
+ function.addToBody(subFunction);
+
+ mainFunction.addParameter(function);
+ functionArray.add(mainFunction);
+ }
+ }
+ }
+
+ if (!functionArray.isEmpty() || performScript!=null) {
+ writer.startElement("script", componentControl);
+ getUtils().writeAttribute(writer, "type",
"text/javascript");
+ writer.writeText("//", null);
+ writer.write("<![CDATA[\n");
+ if (performScript!= null) {
+ writer.write(performScript);
+ writer.write(";");
+ }
+ if (!functionArray.isEmpty()) {
+ writer.write(convertToString(functionArray, ";"));
+ writer.write(";");
+ }
+ writer.writeText("\n//", null);
+ writer.write("]]>");
+ writer.endElement("script");
+ }
+ }
+}
\ No newline at end of file
Modified:
branches/community/3.3.X/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js
===================================================================
---
branches/community/3.3.X/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2009-07-08
11:53:55 UTC (rev 14849)
+++
branches/community/3.3.X/ui/componentControl/src/main/resources/org/richfaces/renderkit/html/script/controlUtils.js 2009-07-08
12:34:18 UTC (rev 14850)
@@ -31,7 +31,7 @@
Richfaces.componentControl.attachEvent = function(attachTo, aevent, forAttr, operation,
params, disableDefault) {
jQuery(attachTo).bind(Richfaces.effectEventOnOut(aevent),function(cevent) {
- Richfaces.componentControl.performOperation(cevent, forAttr, operation, params,
disableDefault);
+ Richfaces.componentControl.performOperation(cevent, aevent, forAttr, operation, params,
disableDefault);
}).each(function() {
Richfaces.componentControl.applyDecorations(this, forAttr, function(element) {
//TODO: handle component decoration
@@ -39,10 +39,19 @@
});
};
-Richfaces.componentControl.performOperation = function( cevent, forAttr, operation,
params, disableDefault) {
+Richfaces.componentControl.checkDisableDefault = function (ename, disableDefault) {
+ if (disableDefault==undefined) {
+ var en=ename.toLowerCase();
+ return (en=="oncontextmenu" || en=="contextmenu" ? true : false);
+ } else {
+ return disableDefault;
+ }
+}
+
+Richfaces.componentControl.performOperation = function( cevent, aevent, forAttr,
operation, params, disableDefault) {
// stop event before event isn't extended by prototype
- if (disableDefault) {
+ if (Richfaces.componentControl.checkDisableDefault(aevent, disableDefault)) {
var event = jQuery.event.fix(cevent);
event.stopPropagation();
event.preventDefault();
Modified:
branches/community/3.3.X/ui/componentControl/src/main/templates/htmlComponentControl.jspx
===================================================================
---
branches/community/3.3.X/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2009-07-08
11:53:55 UTC (rev 14849)
+++
branches/community/3.3.X/ui/componentControl/src/main/templates/htmlComponentControl.jspx 2009-07-08
12:34:18 UTC (rev 14850)
@@ -10,48 +10,13 @@
component="org.richfaces.component.UIComponentControl"
<h:scripts>/org/richfaces/renderkit/html/scripts/jquery/jquery.js,/org/richfaces/renderkit/html/script/controlUtils.js</h:scripts>
-
- <c:set var="event"
value="#{component.attributes['event']}"/>
- <c:set var="forAttr"
value="#{component.attributes['for']}"/>
- <c:set var="attachTiming"
value="#{component.attributes['attachTiming']}"/>
- <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.encodedParametersMap}"/>
-
-
- <f:clientid var="clientId"/>
- <jsp:scriptlet>
+
+<f:clientid var="clientId"/>
+
+<jsp:scriptlet>
<![CDATA[
- String event = (String) variables.getVariable("event");
- String forAttr = (String) variables.getVariable("forAttr");
- String attachTiming = (String) variables.getVariable("attachTiming");
- String attachTo = (String) variables.getVariable("attachTo");
- String name = (String) variables.getVariable("name");
- String operation = (String) variables.getVariable("operation");
-
- checkValidity(clientId, name, attachTiming, forAttr, operation);
- variables.setVariable("forAttr", getUtils().escapeJavaScript(
- replaceClientIds(context, component, forAttr)));
- variables.setVariable("attachTo", replaceClientIds(context, component,
attachTo));
+ getScript(context, component);
]]>
</jsp:scriptlet>
-
-<jsp:scriptlet><![CDATA[ if (! "".equals(name.trim()) ) {
]]></jsp:scriptlet>
-<script type="text/javascript">
- //<![CDATA[
-function #{name}(cevent) {
- Richfaces.componentControl.performOperation(
- cevent, '#{forAttr}', '#{operation}', function() { return {#{params}};
}, #{component.disableDefault} );
-}
-//]]>
-</script>
-<jsp:scriptlet><![CDATA[ } ]]></jsp:scriptlet>
-
-<jsp:scriptlet><![CDATA[ if (attachTo != null &&
attachTo.trim().length() != 0 && !"#".equals(attachTo)) { ]]>
- attachEventAccordingToTimingOption(context, component);
-<![CDATA[ } ]]>
-</jsp:scriptlet>
-
</f:root>
Modified:
branches/community/3.3.X/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java
===================================================================
---
branches/community/3.3.X/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java 2009-07-08
11:53:55 UTC (rev 14849)
+++
branches/community/3.3.X/ui/componentControl/src/test/java/org/richfaces/component/ComponentControlTest.java 2009-07-08
12:34:18 UTC (rev 14850)
@@ -158,11 +158,11 @@
assertEquals(6, params.length);
assertTrue(params[0].trim().endsWith("event"));
- assertEquals("'#button'", params[1].trim());
- assertEquals("'testOperation'", params[2].trim());
- assertTrue(params[3].trim().startsWith("{"));
- assertTrue(params[4].trim().endsWith("}"));
- assertTrue(params[5].trim().endsWith("false)"));
+ assertEquals("'onclick'", params[1].trim());
+ assertEquals("'#button'", params[2].trim());
+ assertEquals("'testOperation'", params[3].trim());
+ assertTrue(params[4].trim().startsWith("function(){return{"));
+ assertTrue(params[5].trim().endsWith("}})"));
}
public void testParametersMap() throws Exception {