You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2012/08/30 13:59:42 UTC

svn commit: r1378899 - in /myfaces/tobago/trunk/tobago-theme: tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/ tobago...

Author: lofwyr
Date: Thu Aug 30 11:59:41 2012
New Revision: 1378899

URL: http://svn.apache.org/viewvc?rev=1378899&view=rev
Log:
TOBAGO-1184: Support for initializing components without javascript fragments:
- support for the onclick attribute of tc:button and tc:link
- support for partial rendering

Modified:
    myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ButtonRenderer.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/LinkRenderer.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/Command.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/CommandMap.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/util/HtmlRendererUtils.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ButtonRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ButtonRenderer.java?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ButtonRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ButtonRenderer.java Thu Aug 30 11:59:41 2012
@@ -76,8 +76,14 @@ public class ButtonRenderer extends Comm
 
       final String url = RenderUtils.generateUrl(facesContext, button);
       final CommandMap map = new CommandMap();
-      map.setClick(new Command(
-          button.isTransition(), button.getTarget(), url, null, null, confirmation, null));
+      final String[] partialIds
+          = HtmlRendererUtils.getComponentIdsAsList(facesContext, button, button.getRenderedPartially());
+      final Command click = new Command(
+          button.isTransition(), button.getTarget(), url, partialIds, null, confirmation, null);
+      if (button.getOnclick() != null) {
+        click.setScript(button.getOnclick());
+      }
+      map.setClick(click);
       writer.writeAttribute(DataAttributes.ACTION, map.encodeJson(), true);
 
       writer.writeAttribute(HtmlAttributes.HREF, "#", false);

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/LinkRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/LinkRenderer.java?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/LinkRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/LinkRenderer.java Thu Aug 30 11:59:41 2012
@@ -73,8 +73,14 @@ public class LinkRenderer extends Comman
 
       final String url = RenderUtils.generateUrl(facesContext, link);
       final CommandMap map = new CommandMap();
-      map.setClick(new Command(
-          link.isTransition(), link.getTarget(), url, null, null, confirmation, null));
+      final String[] partialIds
+          = HtmlRendererUtils.getComponentIdsAsList(facesContext, link, link.getRenderedPartially());
+      final Command click = new Command(
+          link.isTransition(), link.getTarget(), url, partialIds, null, confirmation, null);
+      if (link.getOnclick() != null) {
+        click.setScript(link.getOnclick());
+      }
+      map.setClick(click);
       writer.writeAttribute(DataAttributes.ACTION, map.encodeJson(), true);
 
       writer.writeAttribute(HtmlAttributes.HREF, "#", false);

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/Command.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/Command.java?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/Command.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/Command.java Thu Aug 30 11:59:41 2012
@@ -28,17 +28,20 @@ public class Command {
   private Boolean transition;
   private String target;
   private String url;
-  private String partially;
+  private String[] partially;
   private String focus;
   private String confirmation;
   private Integer delay;
+  /**
+   * @deprecated
+   */
+  @Deprecated
+  private String script;
 
   public Command(
-      Boolean transition, String target, String url, String partially, String focus, String confirmation,
+      Boolean transition, String target, String url, String[] partially, String focus, String confirmation,
       Integer delay) {
-    if (!transition) { // XXX true is the default: this should be applied in the serialization method.
-      this.transition = transition;
-    }
+    this.transition = transition;
     this.target = target;
     this.url = url;
     this.partially = partially;
@@ -47,4 +50,82 @@ public class Command {
     this.delay = delay;
   }
 
+  /**
+   * @deprecated
+   */
+  @Deprecated
+  public void setScript(String script) {
+    this.script = script;
+  }
+
+  public void encodeJson(StringBuilder builder) {
+    builder.append("{");
+    int initialLength = builder.length();
+    if (transition != null && !transition) { // true is the default, so encoding is needed.
+      encodeJsonAttribute(builder, "transition", transition);
+    }
+    if (target != null) {
+      encodeJsonAttribute(builder, "target", target);
+    }
+    if (url != null) {
+      encodeJsonAttribute(builder, "url", url);
+    }
+    if (partially != null && partially.length > 0) {
+      encodeJsonAttribute(builder, "partially", partially);
+    }
+    if (focus != null) {
+      encodeJsonAttribute(builder, "focus", focus);
+    }
+    if (confirmation != null) {
+      encodeJsonAttribute(builder, "confirmation", confirmation);
+    }
+    if (delay != null) {
+      encodeJsonAttribute(builder, "delay", delay);
+    }
+    if (script != null) {
+      encodeJsonAttribute(builder, "script", script);
+    }
+
+    if (builder.length() - initialLength > 0) {
+      builder.deleteCharAt(builder.length() - 1);
+    }
+
+    builder.append("}");
+  }
+
+  private void encodeJsonAttribute(StringBuilder builder, String name, String[] value) {
+    builder.append("\"");
+    builder.append(name);
+    builder.append("\":\"");
+    boolean colon = false;
+    for (String item : value) {
+      if (colon) {
+        builder.append(",");
+      }
+      builder.append(item);
+      colon = true;
+    }
+    builder.append("\",");
+  }
+
+  private void encodeJsonAttribute(StringBuilder builder, String name, Boolean value) {
+    encodeJsonAttributeIntern(builder, name, Boolean.toString(value));
+  }
+
+  private void encodeJsonAttribute(StringBuilder builder, String name, Integer value) {
+    encodeJsonAttributeIntern(builder,  name, Integer.toString(value));
+  }
+
+  private void encodeJsonAttribute(StringBuilder builder, String name, String value) {
+    value = value.replaceAll("\\\"", "\\\\\\\"");
+    encodeJsonAttributeIntern(builder, name, value);
+  }
+
+  private void encodeJsonAttributeIntern(StringBuilder builder, String name, String value) {
+    builder.append("\"");
+    builder.append(name);
+    builder.append("\":\"");
+    builder.append(value);
+    builder.append("\",");
+  }
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/CommandMap.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/CommandMap.java?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/CommandMap.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/CommandMap.java Thu Aug 30 11:59:41 2012
@@ -19,8 +19,6 @@
 
 package org.apache.myfaces.tobago.renderkit.html;
 
-import com.google.gson.Gson;
-
 /**
  * @since 1.6.0
  */
@@ -37,6 +35,19 @@ public class CommandMap {
   }
 
   public String encodeJson() {
-    return new Gson().toJson(this);
+    StringBuilder builder = new StringBuilder();
+    builder.append("{");
+
+    if (click != null) {
+      builder.append("\"click\":");
+      click.encodeJson(builder);
+      builder.append(",");
+    }
+
+    if (builder.charAt(builder.length() - 1) == ',') {
+      builder.deleteCharAt(builder.length() - 1);
+    }
+    builder.append("}");
+    return builder.toString();
   }
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/util/HtmlRendererUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/util/HtmlRendererUtils.java?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/util/HtmlRendererUtils.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/util/HtmlRendererUtils.java Thu Aug 30 11:59:41 2012
@@ -59,6 +59,7 @@ import javax.faces.context.ResponseWrite
 import javax.faces.model.SelectItem;
 import javax.faces.model.SelectItemGroup;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -415,6 +416,20 @@ public final class HtmlRendererUtils {
     return sb.toString();
   }
 
+  public static String[] getComponentIdsAsList(
+      FacesContext context, UIComponent component, String[] componentId) {
+    List<String> result = new ArrayList<String>(componentId.length);
+    for (String id : componentId) {
+      if (!StringUtils.isBlank(id)) {
+        String clientId = getComponentId(context, component, id);
+        if (clientId != null) {
+          result.add(clientId);
+        }
+      }
+    }
+    return (String[]) result.toArray(new String[result.size()]);
+  }
+
   public static String getComponentId(FacesContext context, UIComponent component, String componentId) {
     UIComponent partiallyComponent = ComponentUtils.findComponent(component, componentId);
     if (partiallyComponent != null) {

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js?rev=1378899&r1=1378898&r2=1378899&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js Thu Aug 30 11:59:41 2012
@@ -967,6 +967,9 @@ var Tobago = {
               Tobago.reloadComponent(this, commands.click.partially, actionId, commands.click);
             } else if (commands.click.url) {
               Tobago.navigateToUrl(commands.click.url);
+            } else if (commands.click.script) { // XXX this case is deprecated.
+              // not allowed with Content Security Policy (CSP)
+                eval(commands.click.script);
             } else {
               Tobago.submitAction(this, actionId, commands.click);
             }