Author: pyaschenko
Date: 2009-07-16 09:20:08 -0400 (Thu, 16 Jul 2009)
New Revision: 14940
Modified:
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/component/UIEffect.java
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/renderkit/EffectRendererBase.java
branches/community/3.3.X/ui/effect/src/main/resources/org/richfaces/renderkit/html/script/processEffect.js
branches/community/3.3.X/ui/effect/src/main/templates/effect.jspx
Log:
https://jira.jboss.org/jira/browse/RF-7391
Modified:
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/component/UIEffect.java
===================================================================
---
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/component/UIEffect.java 2009-07-16
13:12:10 UTC (rev 14939)
+++
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/component/UIEffect.java 2009-07-16
13:20:08 UTC (rev 14940)
@@ -21,12 +21,13 @@
package org.richfaces.component;
-import java.util.Iterator;
+import java.util.HashMap;
+import java.util.Map;
+
import org.ajax4jsf.Messages;
import org.ajax4jsf.component.EventValueBinding;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
-import javax.faces.component.NamingContainer;
import javax.faces.context.FacesContext;
import org.ajax4jsf.component.AjaxSupport;
import org.apache.commons.logging.Log;
@@ -52,39 +53,45 @@
*/
public String getEventString()
{
- StringBuffer buildOnEvent = new StringBuffer();
+ StringBuffer buildOnEvent = new
StringBuffer("Richfaces.processEffect(");
+ StringBuffer options = new StringBuffer();
+ StringBuffer extendStr = new StringBuffer("Object.extend({targetId:");
- String targetId=getTargetId();
- String targetPart = "{}";
+ String targetId=getTargetId();
UIComponent targetComponent=null;
- if (!"".equals(targetId)) {
- targetComponent=RendererUtils.getInstance().findComponentFor(this,targetId);
- }
- if (targetComponent!=null) {
- targetPart =
"{targetId:'"+targetComponent.getClientId(FacesContext.getCurrentInstance())+"'}";
- } else {
- // it might be html tag id or DOM object
- targetPart = targetId != ""
- ? "typeof
"+targetId+"=='object'?{targetId:"+targetId+"}:{targetId:$('"+targetId+"')}"
: "{}" ;
- }
-
- String type=getType();
- String typePart =
type!=""?"{type:'"+type+"'}":"{}";
-
- String params=getParams();
- String paramsPart =
params!=""?"{"+params+"}":"{}";
-
-
-
buildOnEvent.append("Richfaces.processEffect(Object.extend(Object.extend({targetId:this},"+targetPart+"),"+
- "Object.extend("+typePart+","+paramsPart+") ) )");
+ if (!"".equals(targetId)) {
+ targetComponent=RendererUtils.getInstance().findComponentFor(this,targetId);
+ }
+ if (targetComponent!=null) {
+ targetId =
"'"+targetComponent.getClientId(FacesContext.getCurrentInstance())+"'";
+ } else {
+ // it might be html tag id or DOM object
+ targetId = !"".equals(targetId) ? "typeof
"+targetId+"=='object'?"+targetId+":$('"+targetId+"')"
: "";
+ }
+
+ extendStr.append("".equals(targetId) ? "this" : targetId);
+
+ String type=getType();
+ if (!"".equals(type)) {
+ options.append("type:'" + type);
+ options.append('\'');
+ }
+
+ String params=getParams();
+ if (!"".equals(params)) {
+ if (options.length() != 0) {
+ options.append(',');
+ }
+ options.append(params);
+ }
+
+ buildOnEvent.append(extendStr);
+ buildOnEvent.append("},{");
+ buildOnEvent.append(options);
+ buildOnEvent.append("}));");
- return buildOnEvent.toString();
-
-//if (typeof '' == 'object') {pm.attachId= '';if
(''=='') pm.targetId=''; };
-
-
-
+ return buildOnEvent.toString();
}
public abstract String getEvent();
Modified:
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/renderkit/EffectRendererBase.java
===================================================================
---
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/renderkit/EffectRendererBase.java 2009-07-16
13:12:10 UTC (rev 14939)
+++
branches/community/3.3.X/ui/effect/src/main/java/org/richfaces/renderkit/EffectRendererBase.java 2009-07-16
13:20:08 UTC (rev 14940)
@@ -21,14 +21,20 @@
package org.richfaces.renderkit;
import java.io.IOException;
-import java.util.regex.Pattern;
+import java.util.HashMap;
+import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
-import org.ajax4jsf.javascript.JSEncoder;
+import org.ajax4jsf.javascript.JSFunction;
+import org.ajax4jsf.javascript.JSLiteral;
+import org.ajax4jsf.javascript.JSReference;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
import org.ajax4jsf.renderkit.RendererUtils;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.renderkit.RendererUtils.ScriptHashVariableWrapper;
import org.richfaces.component.UIEffect;
import org.richfaces.json.JSONTokener;
@@ -46,19 +52,6 @@
protected Class getComponentClass() {
return UIEffect.class;
}
-
- private static final Pattern VARIABLE_PATTERN =
Pattern.compile("^\\s*[_,A-Z,a-z]\\w*(?:\\.[_,A-Z,a-z]\\w*)*\\s*$");
-
- public String convertElementParameter(Object parameter) {
- if (parameter==null)
- return "''";
- String s = parameter.toString();
- if (VARIABLE_PATTERN.matcher(s).matches()) {
- return "typeof "+s+" == \"object\" ? "+s+" :
$('"+s+"')";
- } else {
- return "'"+s+"'";
- }
- }
public String convertParameters(FacesContext context, UIEffect effect) throws
IOException {
String params = effect.getParams();
@@ -149,20 +142,76 @@
}
}
- public String escapeJavaScript(Object s) {
- if (s != null) {
- JSEncoder encoder = new JSEncoder();
- StringBuffer result = new StringBuffer();
- String string = s.toString();
- int length = string.length();
+ private String findComponentId(String id, FacesContext context, UIComponent component)
{
+ String result = null;
+ if (! "".equals(id)) {
+ UIComponent comp = getUtils().findComponentFor(component,id);
+ if (comp != null) {
+ String cid= comp.getClientId(context);
+ result = cid;
+ }
+ }
+ return result;
+ }
+
+ public void writeScript (FacesContext context, UIComponent component) throws IOException
{
+
+ Map<String, Object> attributes = component.getAttributes();
+ String attachObj = "";
+ String attachId = "";
+
+ String id = (String) attributes.get("for");
+ if (! "".equals(id)) {
+ attachId = findComponentId(id, context, component);
+ if (attachId == null) {
+ // if no corresponded component id, may be it is non-jsf id.
+ // So, returning the id as is
+ attachId = id;
+ // it might be the DOM object
+ attachObj = id;
+ }
+ }
+
+ String event = (String)attributes.get("event");
+ Boolean needsFunction = new Boolean(!
"".equals(attributes.get("name")) &&
"".equals(event));
+ Boolean needsObserver = new Boolean(! "".equals(event) && !
"".equals(attachId) );
+
+ if (needsFunction || needsObserver) {
+
+ String targetObj = "";
+ String targetId = "";
- for (int i = 0; i < length; i++) {
- result.append(encoder.encode(string.charAt(i)));
+ id = (String) attributes.get("targetId");
+ targetId = findComponentId(id, context, component);
+ if (targetId == null) {
+ // if no corresponded component id, may be it is non-jsf id.
+ // So, returning the id as is
+ targetId = id;
+ // it might be the DOM object
+ targetObj = id;
}
-
- return result.toString();
- } else {
- return null;
+
+ Map<String, Object> options = new HashMap<String, Object>();
+ getUtils().addToScriptHash(options, "targetObj", targetObj, null ,
ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "attachObj", attachObj, null ,
ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "targetId", targetId, null ,
ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "attachId", attachId, null ,
ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "type",
attributes.get("type"), null , ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "event",
attributes.get("event"), null , ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "name",
attributes.get("name"), null , ScriptHashVariableWrapper.DEFAULT);
+ getUtils().addToScriptHash(options, "params", new
JSLiteral(convertParameters(context, (UIEffect)component)), null ,
ScriptHashVariableWrapper.DEFAULT);
+
+ JSFunction function = new JSFunction("Richfaces.effect.create");
+ if (!options.isEmpty()) {
+ function.addParameter(options);
+ }
+
+ ResponseWriter writer = context.getResponseWriter();
+
+ writer.startElement(HTML.SCRIPT_ELEM, component);
+ getUtils().writeAttribute(writer, HTML.TYPE_ATTR, "text/javascript");
+ writer.writeText(function.toScript(), component, null);
+ writer.endElement(HTML.SCRIPT_ELEM);
}
}
}
Modified:
branches/community/3.3.X/ui/effect/src/main/resources/org/richfaces/renderkit/html/script/processEffect.js
===================================================================
---
branches/community/3.3.X/ui/effect/src/main/resources/org/richfaces/renderkit/html/script/processEffect.js 2009-07-16
13:12:10 UTC (rev 14939)
+++
branches/community/3.3.X/ui/effect/src/main/resources/org/richfaces/renderkit/html/script/processEffect.js 2009-07-16
13:20:08 UTC (rev 14940)
@@ -2,10 +2,73 @@
window.Richfaces = {};
}
-Richfaces.processEffect = function(pm) {
- new Effect[pm.type]($(pm.targetId),pm);
+Richfaces.processEffect = function(params) {
+ new Effect[params.type]($(params.targetId),params);
};
Richfaces.effectEventOnOut = function(ename) {
return ename.substr(0,2) == 'on' ? ename.substr(2) : ename;
};
+
+if (!Richfaces.effect) {
+ Richfaces.effect={};
+}
+
+Richfaces.effect.create = function (options) {
+ /* options:
+ event,
+ name,
+ targetId,
+ attachId,
+ attachObj,
+ targetObj,
+ type,
+ params */
+
+ if (!options) options = {};
+
+ var params = options.params || {};
+ options.params = null;
+
+ var attachObj;
+ var targetObj;
+ var targetId = options.targetId;
+ var attachId = options.attachId;
+
+ if (options.attachObj) {
+ try {
+ attachObj = eval(options.attachObj);
+ } catch (e) {}
+
+ if (typeof attachObj == 'object') {
+ attachId = attachObj;
+ }
+ }
+
+ if (options.targetObj) {
+ try {
+ targetObj = eval(options.targetObj);
+ } catch (e) {}
+
+ if (typeof targetObj == 'object') targetId = targetObj;
+ }
+
+ if (!targetId) targetId = attachId;
+
+ if (!params.targetId) params.targetId = targetId;
+ params.type = options.type;
+
+ if (!options.event) {
+ // create user function
+ with (window) {
+ eval(options.name + "=function(){return
Richfaces.processEffect(Object.extend(this,arguments[0]||{}));}.bind(params)"); // ??
or just use params instead of this and binding
+ }
+ } else {
+ // attach eventListener
+ var ename = Richfaces.effectEventOnOut(options.event || "");
+ if (ename) {
+ var bindedFunction = function(event){ return Richfaces.processEffect(this);
}.bindAsEventListener(params);
+ Event.observe(attachId, ename, bindedFunction, params.useCapture||false);
+ }
+ }
+}
Modified: branches/community/3.3.X/ui/effect/src/main/templates/effect.jspx
===================================================================
--- branches/community/3.3.X/ui/effect/src/main/templates/effect.jspx 2009-07-16 13:12:10
UTC (rev 14939)
+++ branches/community/3.3.X/ui/effect/src/main/templates/effect.jspx 2009-07-16 13:20:08
UTC (rev 14940)
@@ -8,106 +8,9 @@
class="org.richfaces.renderkit.html.EffectRenderer"
baseclass="org.richfaces.renderkit.EffectRendererBase"
component="org.richfaces.component.UIEffect">
- <f:clientid var="clientId"/>
-
- <c:set var="for"
value="#{component.attributes['for']}"/>
- <c:set var="event"
value="#{component.attributes['event']}"/>
- <c:set var="name"
value="#{component.attributes['name']}"/>
- <c:set var="type"
value="#{component.attributes['type']}"/>
- <c:set var="targetId"
value="#{component.attributes['targetId']}"/>
- <c:set var="params" value="#{this:convertParameters(context,
component)}"/>
<h:scripts>new
org.ajax4jsf.javascript.PrototypeScript(),scripts/scriptaculous/effects.js,/org/richfaces/renderkit/html/script/processEffect.js</h:scripts>
-
-<jsp:scriptlet>
-<![CDATA[
- String sid = (String) variables.getVariable("for");
- variables.setVariable("attachObj","''");
- if (! "".equals(sid)) {
- UIComponent forcomp = getUtils().findComponentFor((UIComponent)component,sid);
- if (forcomp != null) {
- String cid= forcomp.getClientId(context);
- variables.setVariable("forPart",
- "{targetId:'"+cid+"',attachId:'"+cid+"'}");
- } else {
- // if no corresponded component id, may be it is non-jsf id.
- // So, returning the id as is
-
variables.setVariable("forPart","{targetId:'"+sid+"',attachId:'"+sid+"'}");
- // it might be the DOM object
- variables.setVariable("attachObj",sid);
- }
- } else {
- variables.setVariable("forPart","{}");
- }
- String tid = (String) variables.getVariable("targetId");
- variables.setVariable("targetObj","''");
- if (! "".equals(tid)) {
- UIComponent targetcomp = getUtils().findComponentFor(component,tid);
- if (targetcomp != null) {
- variables.setVariable("targetPart","{targetId:'"+targetcomp.getClientId(context)+"'}");
- } else {
- // if no corresponded component id, may be it is non-jsf id.
- // So, returning the id as is
-
variables.setVariable("targetPart","{targetId:'"+tid+"'}");
- variables.setVariable("targetObj",tid);
- }
- } else {
- variables.setVariable("targetPart","{}");
- }
+ <f:call name="writeScript"/>
- String type = (String) variables.getVariable("type");
- if (!"".equals(type)) {
- variables.setVariable("typePart","{type:'"+type+"'}");
- } else {
- variables.setVariable("typePart","{}");
- }
-
- String event = (String) variables.getVariable("event");
- String name = (String)variables.getVariable("name");
- Boolean needsFunction = new Boolean(! "".equals(name) &&
"".equals(event));
- variables.setVariable("needsFunction",needsFunction);
-
- Boolean needsObserver = new Boolean(! "".equals(event) && !
"".equals(sid) );
- variables.setVariable("needsObserver",needsObserver);
-]]>
-</jsp:scriptlet>
-<c:if test="#{needsFunction}">
-<script type="text/javascript"
x:passThruWithExclusions="name,type,for">
- //<![CDATA[
-#{name} = function () { return Richfaces.processEffect(Object.extend(Object.extend(
Object.extend(#{typePart},#{forPart}), Object.extend (#{targetPart},#{params})),
arguments[0]||{})); };
-//]]>
-</script>
-</c:if>
-<c:if test="#{needsObserver}">
-<script type="text/javascript"
x:passThruWithExclusions="name,type,for">
- //<![CDATA[
-{
-var pm = Object.extend( Object.extend(#{typePart},#{forPart}), #{targetPart} );
-/* pm.id can have a special meaning, let's check */
-//pm.id = #{this:convertElementParameter(pmId)};
-var attachObj;
-try {
- attachObj = eval('#{this:escapeJavaScript(attachObj)}');
-} catch (e) {
-}
-
-var targetObj;
-try {
- targetObj = eval('#{this:escapeJavaScript(targetObj)}');
-} catch (e) {
-}
-
-if (typeof attachObj == 'object') {pm.attachId = attachObj;if (targetObj ==
'') pm.targetId = attachObj; };
-if (typeof targetObj == 'object') pm.targetId = targetObj;
-
-pm = Object.extend(pm, #{params});
-var ename = Richfaces.effectEventOnOut('#{event}');
-var bindedFunction = function(event){ return Richfaces.processEffect(this);
}.bindAsEventListener(pm);
-Event.observe(pm.attachId,ename, bindedFunction, pm.useCapture||false);
-}
-//]]>
-</script>
-</c:if>
-
</f:root>