You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/06/07 05:49:06 UTC

svn commit: r664273 - /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java

Author: lu4242
Date: Fri Jun  6 20:49:06 2008
New Revision: 664273

URL: http://svn.apache.org/viewvc?rev=664273&view=rev
Log:
TOMAHAWK-1167 s:toggleLink page jumps and onclick fix

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java?rev=664273&r1=664272&r2=664273&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/toggle/ToggleLinkRenderer.java Fri Jun  6 20:49:06 2008
@@ -19,14 +19,24 @@
 package org.apache.myfaces.custom.toggle;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Iterator;
 
 import javax.faces.component.UIComponent;
+import javax.faces.component.UIOutput;
+import javax.faces.component.UIParameter;
 import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.component.UserRoleUtils;
 import org.apache.myfaces.renderkit.html.ext.HtmlLinkRenderer;
+import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
 
 /**
  * Renderer for component HtmlAjaxChildComboBox
@@ -42,6 +52,172 @@
 public class ToggleLinkRenderer extends HtmlLinkRenderer {
     public static final int DEFAULT_MAX_SUGGESTED_ITEMS = 200;
 
+    protected void renderOutputLinkStart(FacesContext facesContext,
+            UIOutput output) throws IOException
+    {
+        if (UserRoleUtils.isEnabledOnUserRole(output))
+        {        
+            ResponseWriter writer = facesContext.getResponseWriter();
+    
+            //calculate href
+            String href = org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils
+                    .getStringValue(facesContext, output);
+            
+            if (href == null)
+            {
+                href = "";
+            }
+            if ("".equals(href) || "#".equals(href))
+            {
+                //If no value defined the default is #componentId where
+                //componentId is the clientId of the component to be 
+                //shown. This avoid that the page jumps to top on every click.
+                ToggleLink toggleLink = (ToggleLink) output;
+                String[] componentsToToggle = toggleLink.getFor().split(",");
+                for (int i = 0; i < componentsToToggle.length; i++) {
+                    String componentId = componentsToToggle[i].trim();
+                    UIComponent componentToShow = toggleLink.findComponent(componentId);
+                    if (componentToShow != null){
+                        href = "#"+ componentToShow.getClientId(facesContext);
+                        break;
+                    }
+                }                
+            }
+            if (getChildCount(output) > 0)
+            {
+                StringBuffer hrefBuf = new StringBuffer(href);
+                addChildParametersToHref(facesContext, output, hrefBuf, (href
+                        .indexOf('?') == -1), //first url parameter?
+                        writer.getCharacterEncoding());
+                href = hrefBuf.toString();
+            }
+            href = facesContext.getExternalContext().encodeResourceURL(href); //TODO: or encodeActionURL ?
+    
+            String clientId = output.getClientId(facesContext);
+    
+            //write anchor
+            writer.startElement(HTML.ANCHOR_ELEM, output);
+            writer.writeAttribute(HTML.ID_ATTR, clientId, null);
+            writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
+            writer.writeURIAttribute(HTML.HREF_ATTR, href, null);
+            
+            HtmlRendererUtils
+                    .renderHTMLAttributes(
+                            writer,
+                            output,
+                            org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.ANCHOR_PASSTHROUGH_ATTRIBUTES_WITHOUT_ONCLICK_WITHOUT_STYLE);
+            HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR,
+                    output.getAttributes().get(HTML.STYLE_ATTR));
+            HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR,
+                    output.getAttributes().get(HTML.STYLE_CLASS_ATTR));
+            
+            HtmlRendererUtils.renderHTMLAttribute(writer, HTML.ONCLICK_ATTR, HTML.ONCLICK_ATTR, 
+                    buildOnclickToggleFunction(facesContext,output));
+            
+            writer.flush();
+        }
+    }
+    
+    private String buildOnclickToggleFunction(FacesContext facesContext,
+            UIOutput output) throws IOException
+    {
+        ToggleLink toggleLink = (ToggleLink) output;
+        String[] componentsToToggle = toggleLink.getFor().split(",");
+        StringBuffer idsToShow = new StringBuffer();
+        for (int i = 0; i < componentsToToggle.length; i++) {
+            String componentId = componentsToToggle[i].trim();
+            UIComponent componentToShow = toggleLink.findComponent(componentId);
+            if (componentToShow == null) {
+                Log log = LogFactory.getLog(ToggleLinkRenderer.class);
+                log.error("Unable to find component with id " + componentId);
+                continue;
+            }
+            if( idsToShow.length() > 0 )
+                idsToShow.append( ',' );
+            idsToShow.append( componentToShow.getClientId(facesContext) );
+        }
+        
+        String outputOnclick = toggleLink.getOnclick();
+        StringBuffer onClick = new StringBuffer();
+        
+        if (outputOnclick != null)
+        {
+            onClick.append("var cf = function(){");
+            onClick.append(outputOnclick);
+            onClick.append('}');
+            onClick.append(';');
+            onClick.append("var oamSF = function(){");            
+        }
+        
+        onClick.append(getToggleJavascriptFunctionName(facesContext, toggleLink) + "('"+idsToShow+"')");
+        
+        if (outputOnclick != null)
+        {
+            onClick.append('}');
+            onClick.append(';');
+            onClick.append("return (cf()==false)? false : oamSF();");        
+        }
+
+        return onClick.toString();
+    }
+    
+    //This method is copied from HtmlLinkRendererBase.  
+    private void addChildParametersToHref(FacesContext facesContext,
+            UIComponent linkComponent, StringBuffer hrefBuf,
+            boolean firstParameter, String charEncoding) throws IOException
+    {
+        boolean strictXhtmlLinks = MyfacesConfig.getCurrentInstance(
+                facesContext.getExternalContext()).isStrictXhtmlLinks();
+        for (Iterator it = getChildren(linkComponent).iterator(); it.hasNext();)
+        {
+            UIComponent child = (UIComponent) it.next();
+            if (child instanceof UIParameter)
+            {
+                String name = ((UIParameter) child).getName();
+                Object value = ((UIParameter) child).getValue();
+                addParameterToHref(name, value, hrefBuf, firstParameter,
+                        charEncoding, strictXhtmlLinks);
+                firstParameter = false;
+            }
+        }
+    }
+
+    //This method is copied from HtmlLinkRendererBase.     
+    private static void addParameterToHref(String name, Object value,
+            StringBuffer hrefBuf, boolean firstParameter, String charEncoding,
+            boolean strictXhtmlLinks) throws UnsupportedEncodingException
+    {
+        if (name == null)
+        {
+            throw new IllegalArgumentException(
+                    "Unnamed parameter value not allowed within command link.");
+        }
+
+        if (firstParameter)
+        {
+            hrefBuf.append('?');
+        }
+        else
+        {
+            if (strictXhtmlLinks)
+            {
+                hrefBuf.append("&amp;");
+            }
+            else
+            {
+                hrefBuf.append('&');
+            }
+        }
+
+        hrefBuf.append(URLEncoder.encode(name, charEncoding));
+        hrefBuf.append('=');
+        if (value != null)
+        {
+            //UIParameter is no ConvertibleValueHolder, so no conversion possible
+            hrefBuf.append(URLEncoder.encode(value.toString(), charEncoding));
+        }
+    }
+            
     public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
         RendererUtils.checkParamValidity(context, component, ToggleLink.class);
 
@@ -58,22 +234,6 @@
         if(toggleLink.isDisabled())
         	return;
 
-        String[] componentsToToggle = toggleLink.getFor().split(",");
-        StringBuffer idsToShow = new StringBuffer();
-        for (int i = 0; i < componentsToToggle.length; i++) {
-            String componentId = componentsToToggle[i].trim();
-            UIComponent componentToShow = toggleLink.findComponent(componentId);
-            if (component == null) {
-                Log log = LogFactory.getLog(ToggleLinkRenderer.class);
-                log.error("Unable to find component with id " + componentId);
-                continue;
-            }
-            if( idsToShow.length() > 0 )
-            	idsToShow.append( ',' );
-            idsToShow.append( componentToShow.getClientId(context) );
-        }
-
-        toggleLink.setOnclick(getToggleJavascriptFunctionName(context, toggleLink) + "('"+idsToShow+"')");
 
         super.encodeBegin(context, component);
     }