myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r1130169 - in /myfaces/tomahawk/trunk/sandbox/core20/src/main: conf/META-INF/ java/org/apache/myfaces/custom/jsCallbackFunction/ java/org/apache/myfaces/custom/jsOnEventAjax/
Date Wed, 01 Jun 2011 14:50:26 GMT
Author: lu4242
Date: Wed Jun  1 14:50:25 2011
New Revision: 1130169

URL: http://svn.apache.org/viewvc?rev=1130169&view=rev
Log:
TOMAHAWK-1593 Add s:jsCallbackFunction and s:jsOnEventAjax tags

Added:
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/AbstractJsCallbackFunctionComponent.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionLibrary.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionName.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionRenderer.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/AbstractOnEventAjaxComponent.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxRenderer.java
    myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxTagHandler.java
Modified:
    myfaces/tomahawk/trunk/sandbox/core20/src/main/conf/META-INF/facelets-taglib-base.xml

Modified: myfaces/tomahawk/trunk/sandbox/core20/src/main/conf/META-INF/facelets-taglib-base.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/conf/META-INF/facelets-taglib-base.xml?rev=1130169&r1=1130168&r2=1130169&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/conf/META-INF/facelets-taglib-base.xml
(original)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/conf/META-INF/facelets-taglib-base.xml
Wed Jun  1 14:50:25 2011
@@ -17,8 +17,25 @@
  * specific language governing permissions and limitations
  * under the License.
 -->
-<!DOCTYPE facelet-taglib
-  PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
-<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
+<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
+              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
+              version="2.0">
 
+    <function>
+        <description>Retrieve the function name to be generated and used. A component
instance is required to found the component that should
+        be instance of AbstractJsCallbackFunctionComponent pointed by 'expr' property using
a UIComponent.findComponent call.</description>
+        <function-name>jsCallbackFunctionName</function-name>
+        <function-class>org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunctionLibrary</function-class>
+        <function-signature>java.lang.String jsCallbackFunctionName(javax.faces.component.UIComponent,
java.lang.String)</function-signature>
+    </function>
+    
+    <function>
+        <description>Encode the passed string into a valid javascript function name</description>
+        <function-name>generateJsFunctionName</function-name>
+        <function-class>org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunctionLibrary</function-class>
+        <function-signature>java.lang.String generateJsFunctionName(java.lang.String)</function-signature>
+    </function>
+
+    
 </facelet-taglib>
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/AbstractJsCallbackFunctionComponent.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/AbstractJsCallbackFunctionComponent.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/AbstractJsCallbackFunctionComponent.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/AbstractJsCallbackFunctionComponent.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsCallbackFunction;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.behavior.ClientBehaviorHolder;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
+
+/**
+ * This component creates a function inside an inline &lt;script&gt; tag,
+ * with a function that can be referenced later using getFunctionName() method
+ * inside this component instance or the EL function #{s:jsCallbackFunctionName(UIComponent)}.
+ * <p> 
+ * Inside the function, the following code is added:
+ * </p>
+ * <code>
+ * generatedFunctionNameUsingClientIdAndEventName = function (... arguments ...){
+ *   ... jsStartContent ...
+ *   ... clientBehavior scripts ...  
+ *   ... jsEndContent ...
+ * }
+ * </code>
+ * <p> This is useful in situations where the context where this script is
+ * rendered is important, and it is not possible to put the scripts on static 
+ * javascript files.</p>
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+@JSFComponent(name="s:jsCallbackFunction",
+    clazz = "org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunctionComponent",
+    tagClass = "org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunctionComponentTag")
+public abstract class AbstractJsCallbackFunctionComponent extends UIComponentBase 
+    implements ClientBehaviorHolder, JsCallbackFunctionName
+{
+    public static final String COMPONENT_FAMILY = "org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunction";
+    public static final String COMPONENT_TYPE = "org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunction";
+    public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunction";
+    
+    @Override
+    public String getFamily()
+    {
+        return COMPONENT_FAMILY;
+    }
+    
+    @Override
+    public String getDefaultEventName()
+    {
+        return getEventName();
+    }
+    
+    @Override
+    public Collection<String> getEventNames()
+    {
+        List<String> eventNames = new ArrayList<String>(1);
+        eventNames.add(getEventName());
+        return eventNames;
+    }
+    
+    public String getFunctionName()
+    {
+        return getFunctionName(getFacesContext());
+    }
+    
+    public String getFunctionName(FacesContext context)
+    {
+        return (this.getFunctionPrefix() == null ? "" : this.getFunctionPrefix() + '_' )+
+            this.getEventName()+"_"+
+                JavascriptUtils.getValidJavascriptName(
+                        this.getClientId(context),false);
+    }
+
+    /**
+     * Indicates the eventName this callback is used for. The value of this property
+     * is returned in getDefaultEventName(), so all client behaviors will be attached
+     * to this event automatically.
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getEventName();
+    
+    /**
+     * Define a prefix that will be appended to the functionName. 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getFunctionPrefix();
+    
+    /**
+     * Define the arguments the callback functions has
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getArguments();
+    
+    /**
+     * Script fragment to be added at the beginning of the function, before all
+     * clientBehaviors (if applies)
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsStartContent();
+    
+    /**
+     * Script fragment to be added at the beginning of the function, after all
+     * clientBehaviors (if applies)
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsEndContent();
+    
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionLibrary.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionLibrary.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionLibrary.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionLibrary.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsCallbackFunction;
+
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
+
+public final class JsCallbackFunctionLibrary
+{
+    public JsCallbackFunctionLibrary()
+    {
+    }
+    
+    public static String jsCallbackFunctionName(UIComponent c, String expr)
+    {
+        UIComponent foundComponent = null;
+        if (c != null)
+        {
+            foundComponent = c.findComponent(expr);
+        }
+        if (foundComponent == null)
+        {
+            foundComponent = FacesContext.getCurrentInstance().getViewRoot().findComponent(expr);
+        }
+        if (foundComponent == null)
+        {
+            throw new FacesException("Cannot found target component:" + expr);
+        }
+        JsCallbackFunctionName jsFunctionCallback = (JsCallbackFunctionName) foundComponent;
+        
+        return jsFunctionCallback.getFunctionName();
+    }
+    
+    public static String generateJsFunctionName(String name)
+    {
+        return JavascriptUtils.getValidJavascriptName(name, true);
+    }
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionName.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionName.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionName.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionName.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsCallbackFunction;
+
+import javax.faces.context.FacesContext;
+
+public interface JsCallbackFunctionName
+{
+    public String getFunctionName();
+    
+    public String getFunctionName(FacesContext context);
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionRenderer.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionRenderer.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsCallbackFunction/JsCallbackFunctionRenderer.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsCallbackFunction;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehavior;
+import javax.faces.component.behavior.ClientBehaviorContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.Renderer;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
+import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils.ScriptContext;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
+
+/**
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+@JSFRenderer(renderKitId="HTML_BASIC", 
+        family="org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunction",
+        type="org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunction")
+public class JsCallbackFunctionRenderer extends Renderer
+{
+    @Override
+    public void encodeEnd(FacesContext facesContext, UIComponent component)
+            throws IOException
+    {
+        AbstractJsCallbackFunctionComponent jsFunctionCallback = (AbstractJsCallbackFunctionComponent)
component;
+        ResponseWriter writer = facesContext.getResponseWriter();
+        
+        writer.startElement(HTML.SPAN_ELEM, jsFunctionCallback);
+        writer.writeAttribute(HTML.ID_ATTR,  jsFunctionCallback.getClientId(facesContext),
null);
+        writer.startElement(HTML.SCRIPT_ELEM, jsFunctionCallback);
+
+        String functionName = jsFunctionCallback.getFunctionName(facesContext);
+        
+        Map<String,List<ClientBehavior>> clientBehaviors = jsFunctionCallback.getClientBehaviors();
+        
+        ClientBehaviorContext cbcContext = ClientBehaviorContext
+            .createClientBehaviorContext(facesContext, jsFunctionCallback,
+                jsFunctionCallback.getEventName(), jsFunctionCallback.getClientId(facesContext),
+                null);
+
+        ScriptContext script = new ScriptContext(
+                MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isPrettyHtml());
+        
+        script.append(functionName);
+        script.append(" = ");
+        script.append(" function(");
+        if (jsFunctionCallback.getArguments() != null)
+        {
+            script.append(jsFunctionCallback.getArguments());
+        }
+        script.append(')');
+        script.append('{');
+        
+        if (jsFunctionCallback.getJsStartContent() != null)
+        {
+            script.append(jsFunctionCallback.getJsStartContent());
+        }
+        
+        if (jsFunctionCallback.getEventName() != null)
+        {
+            List<ClientBehavior> cbList = clientBehaviors.get(jsFunctionCallback.getEventName());
+            if (cbList != null)
+            {
+                for (ClientBehavior cb : cbList)
+                {
+                    script.append(cb.getScript(cbcContext));
+                    script.append(';');
+                }
+            }
+        }
+        
+        if (jsFunctionCallback.getJsEndContent() != null)
+        {
+            script.append(jsFunctionCallback.getJsEndContent());
+        }
+        
+        script.append('}');
+        
+        writer.writeText(script.toString(), null);
+        
+        writer.endElement(HTML.SCRIPT_ELEM);
+        writer.endElement(HTML.SPAN_ELEM);
+    }
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/AbstractOnEventAjaxComponent.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/AbstractOnEventAjaxComponent.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/AbstractOnEventAjaxComponent.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/AbstractOnEventAjaxComponent.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsOnEventAjax;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.behavior.ClientBehaviorHolder;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
+import org.apache.myfaces.custom.jsCallbackFunction.JsCallbackFunctionName;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
+
+/**
+ * This component creates a f:ajax callback inside an inline &lt;script&gt; tag,
+ * with a function that can be referenced later using getFunctionName() method
+ * inside this component instance or the EL function #{s:jsCallbackFunctionName(UIComponent)}.
+ * <p> 
+ * Inside the function, the following code is added:
+ * </p>
+ * <code>
+ * generatedFunctionNameUsingClientIdAndEventName = function (... arguments ...){
+ *   ... jsStartContent ...
+ *   if(e.status == '...'){
+ *     ... jsBeforeClientBehaviors ...
+ *     ... clientBehavior scripts ... 
+ *     ... jsAfterClientBehaviors ...
+ *   }
+ *   ... jsEndContent ...
+ * }
+ * </code>
+ * <p> This is useful in situations where the context where this script is
+ * rendered is important, and it is not possible to put the scripts on static 
+ * javascript files.</p>
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+@JSFComponent(
+        name="s:jsOnEventAjax",
+        clazz="org.apache.myfaces.custom.jsOnEventAjax.OnEventAjaxComponent",
+        tagHandler="org.apache.myfaces.custom.jsOnEventAjax.OnEventAjaxTagHandler")
+public abstract class AbstractOnEventAjaxComponent extends UIComponentBase 
+    implements ClientBehaviorHolder, JsCallbackFunctionName
+{
+    public static final String COMPONENT_FAMILY = "org.apache.myfaces.custom.jsOnEventAjax.OnEventAjax";
+    public static final String COMPONENT_TYPE = "org.apache.myfaces.custom.jsOnEventAjax.OnEventAjax";
// for unit tests
+    public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.custom.jsOnEventAjax.OnEventAjax";
+    
+    @Override
+    public String getFamily()
+    {
+        return COMPONENT_FAMILY;
+    }
+
+    @Override
+    public String getDefaultEventName()
+    {
+        return getEventName();
+    }
+    
+    @Override
+    public Collection<String> getEventNames()
+    {
+        List<String> eventNames = new ArrayList<String>(1);
+        eventNames.add(getEventName());
+        return eventNames;
+    }
+
+    public String getFunctionName()
+    {
+        return getFunctionName(getFacesContext());
+    }
+    
+    public String getFunctionName(FacesContext context)
+    {
+        return (this.getFunctionPrefix() == null ? "" : this.getFunctionPrefix() + '_' )+
+            this.getEventName()+"_"+
+                JavascriptUtils.getValidJavascriptName(
+                        this.getClientId(context),false);
+    }
+    
+    /**
+     * Indicates the eventName this callback is used for. The value of this property
+     * is returned in getDefaultEventName(), so all client behaviors will be attached
+     * to this event automatically.
+     * 
+     * @return
+     */
+    @JSFProperty(required=true)
+    public abstract String getEventName();    
+    
+    /**
+     * by default 'success'
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getStatus();
+
+    /**
+     * Define a prefix that will be appended to the functionName. 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getFunctionPrefix();    
+
+    /**
+     * Javascript code to put inside the function, but before check for status 
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsStartContent();
+    
+    /**
+     * Javascript code to put inside the function, but after check for status 
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsEndContent();
+    
+    /**
+     * Script fragment to be added at the beginning of the function, before all
+     * clientBehaviors (if applies)
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsBeforeClientBehaviors();
+    
+    /**
+     * Script fragment to be added at the beginning of the function, after all
+     * clientBehaviors (if applies)
+     * 
+     * @return
+     */
+    @JSFProperty
+    public abstract String getJsAfterClientBehaviors();
+
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxRenderer.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxRenderer.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxRenderer.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsOnEventAjax;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehavior;
+import javax.faces.component.behavior.ClientBehaviorContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.Renderer;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
+import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils.ScriptContext;
+
+@JSFRenderer(family="org.apache.myfaces.custom.jsOnEventAjax.OnEventAjax",
+             renderKitId="HTML_BASIC",
+             type="org.apache.myfaces.custom.jsOnEventAjax.OnEventAjax")
+public class OnEventAjaxRenderer extends Renderer
+{
+
+    @Override
+    public void encodeEnd(FacesContext facesContext, UIComponent component)
+            throws IOException
+    {
+        //check for NP
+        super.encodeEnd(facesContext, component);
+        
+        AbstractOnEventAjaxComponent onEventAjaxComponent = (AbstractOnEventAjaxComponent)
component;
+        
+        //UIComponent target = ( onEventAjaxComponent.getForId() != null ) ? 
+        //        onEventAjaxComponent.findComponent(onEventAjaxComponent.getForId()) : 
+        //            onEventAjaxComponent;
+
+        ResponseWriter writer = facesContext.getResponseWriter();
+        writer.startElement(HTML.SPAN_ELEM, component);
+        writer.writeAttribute(HTML.ID_ATTR,  onEventAjaxComponent.getClientId(facesContext),
null);
+        
+        Map<String,List<ClientBehavior>> clientBehaviors = onEventAjaxComponent.getClientBehaviors();
+
+        String eventName = onEventAjaxComponent.getEventName();
+        String functionName = onEventAjaxComponent.getFunctionName(facesContext);
+        ClientBehaviorContext cbcContext = ClientBehaviorContext
+            .createClientBehaviorContext(facesContext, onEventAjaxComponent,
+                    eventName, onEventAjaxComponent.getClientId(facesContext),
+                    null);
+        
+        writer.startElement(HTML.SCRIPT_ELEM, component);
+        writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
+        writer.writeText(getOnEventFunction(facesContext, cbcContext, onEventAjaxComponent,
clientBehaviors, eventName, onEventAjaxComponent, functionName), null);        
+        writer.endElement(HTML.SCRIPT_ELEM);
+        
+        writer.endElement(HTML.SPAN_ELEM);
+    }
+    
+    protected String getOnEventFunction(FacesContext facesContext, ClientBehaviorContext
cbcContext, AbstractOnEventAjaxComponent c, 
+                                        Map<String,List<ClientBehavior>> clientBehaviors,
String eventName, 
+                                        UIComponent target, String functionName)
+    {
+        ScriptContext script = new ScriptContext(
+                MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isPrettyHtml());
+
+        script.append(functionName);
+        script.append(" = ");
+        script.append(" function(e)");
+        script.append('{');
+
+        if (c.getJsStartContent() != null)
+        {
+            script.append(c.getJsStartContent());
+        }
+        
+        script.append("if(e.status == '");
+        script.append(c.getStatus() == null ? "success" : c.getStatus());
+        script.append("'){");
+        
+        if (c.getJsBeforeClientBehaviors() != null)
+        {
+            script.append(c.getJsBeforeClientBehaviors());
+        }
+        
+        for (ClientBehavior cb : clientBehaviors.get(eventName))
+        {
+            script.append(cb.getScript(cbcContext));
+            script.append(';');
+        }
+
+        if (c.getJsAfterClientBehaviors() != null)
+        {
+            script.append(c.getJsAfterClientBehaviors());
+        }
+        
+        script.append('}');
+
+        if (c.getJsEndContent() != null)
+        {
+            script.append(c.getJsEndContent());
+        }
+        
+        script.append('}');
+        
+        return script.toString();
+    }
+}

Added: myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxTagHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxTagHandler.java?rev=1130169&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxTagHandler.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core20/src/main/java/org/apache/myfaces/custom/jsOnEventAjax/OnEventAjaxTagHandler.java
Wed Jun  1 14:50:25 2011
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.jsOnEventAjax;
+
+import javax.faces.component.UIComponent;
+import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
+
+@JSFFaceletTag
+public class OnEventAjaxTagHandler extends ComponentHandler
+{
+
+    public OnEventAjaxTagHandler(ComponentConfig config)
+    {
+        super(config);
+        //just check if attribute has been defined
+        getRequiredAttribute("eventName");
+    }
+
+    @Override
+    public void onComponentCreated(FaceletContext ctx, UIComponent c,
+            UIComponent parent)
+    {
+        /*
+        c.subscribeToEvent(PostAddToViewEvent.class, new SuscribeSetOnEventAjaxListener());
+        */
+    }
+    
+    /*
+    public static final class SetOnEventAjaxListener 
+        implements ComponentSystemEventListener, StateHolder
+    {
+        private String eventName;
+        
+        public SetOnEventAjaxListener()
+        {
+        }
+        
+        public SetOnEventAjaxListener(String eventName)
+        {
+            this.eventName = eventName;
+        }
+
+        public void processEvent(ComponentSystemEvent event)
+        {
+            ClientBehaviorHolder target = (ClientBehaviorHolder) event.getComponent();
+            
+            List<ClientBehavior> list = target.getClientBehaviors().get(this.eventName);
+            
+            if (list != null)
+            {
+                for (ClientBehavior cb : list)
+                {
+                    if (cb instanceof AjaxBehavior)
+                    {
+                        String functionName = 
+                                "tomahawkOnEventAjax_"+eventName+"_"+JavascriptUtils.getValidJavascriptName(event.getComponent().getClientId(),false);
+                        
+                        ((AjaxBehavior) cb).setOnevent(functionName);
+                    }
+                }
+            }
+        }
+
+        public boolean isTransient()
+        {
+            return false;
+        }
+
+        public void setTransient(boolean arg0)
+        {
+        }
+        
+        public void restoreState(FacesContext context, Object state)
+        {
+            this.eventName = (String) state;
+        }
+
+        public Object saveState(FacesContext context)
+        {
+            return eventName;
+        }
+    }
+
+    public static final class SuscribeSetOnEventAjaxListener 
+        implements ComponentSystemEventListener
+    {
+        //Required because Mojarra call PostAddToViewEvent
+        private transient boolean processed = false;
+        
+        public void processEvent(ComponentSystemEvent event)
+        {
+            AbstractOnEventAjaxComponent c = (AbstractOnEventAjaxComponent) event.getComponent();
+            
+            UIComponent target = ( c.getForId() != null ) ? 
+                    c.findComponent(c.getForId()) : 
+                        null;
+
+            if (target != null && !processed)
+            {
+                target.subscribeToEvent(PreRenderComponentEvent.class, new SetOnEventAjaxListener(c.getEventName()));
+                processed = true;
+            }
+        }
+    }
+    */
+}



Mime
View raw message