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 2011/11/18 11:48:22 UTC

svn commit: r1203580 - in /myfaces/tobago/trunk: tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/ tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/ tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/...

Author: lofwyr
Date: Fri Nov 18 10:48:22 2011
New Revision: 1203580

URL: http://svn.apache.org/viewvc?rev=1203580&view=rev
Log:
TOBAGO-377: Improved Tree
 - change DOM for trees: children are no longer in a separate div, but the children now are holding the id of the parent. This is a preparation of TOBAGO-1049 (Tree inside of the Sheet (aka TreeTable)), because a HTML table body TBODY can only contain TR but no DIV stuff.

Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITree.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/DataAttributes.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeMenuNodeRenderer.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeNodeRenderer.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-tree.js

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITree.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITree.java?rev=1203580&r1=1203579&r2=1203580&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITree.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITree.java Fri Nov 18 10:48:22 2011
@@ -31,8 +31,10 @@ import javax.faces.context.FacesContext;
 import javax.faces.validator.Validator;
 import javax.faces.validator.ValidatorException;
 import java.io.IOException;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 
 public abstract class AbstractUITree extends javax.faces.component.UIInput implements NamingContainer, LayoutComponent {
 
@@ -45,6 +47,9 @@ public abstract class AbstractUITree ext
 
   private MixedTreeModel model;
 
+  // XXX may be removed
+  private Set<String> expandedCache;
+
   public UIComponent getRoot() {
     // find the UITreeNode in the children.
     for (UIComponent child : (List<UIComponent>) getChildren()) {
@@ -61,6 +66,7 @@ public abstract class AbstractUITree ext
   @Override
   public void encodeEnd(FacesContext facesContext) throws IOException {
     model = new MixedTreeModel();
+    expandedCache = new HashSet<String>();
     for (Object child : getChildren()) {
       if (child instanceof TreeModelBuilder) {
         TreeModelBuilder builder = (TreeModelBuilder) child;
@@ -73,12 +79,17 @@ public abstract class AbstractUITree ext
     super.encodeEnd(facesContext);
 
     model = null;
+    expandedCache = null;
   }
 
   public MixedTreeModel getModel() {
     return model;
   }
 
+  public Set<String> getExpandedCache() {
+    return expandedCache;
+  }
+
   @Override
   public boolean getRendersChildren() {
     return true;

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/DataAttributes.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/DataAttributes.java?rev=1203580&r1=1203579&r2=1203580&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/DataAttributes.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/DataAttributes.java Fri Nov 18 10:48:22 2011
@@ -28,20 +28,29 @@ public final class DataAttributes {
    * Custom disabled attribute. Use for element, that don't have the disabled attribute.
    */
   public static final String DISABLED = "data-tobago-disabled";
+
   /**
    * Alternate to the src attribute, to implement a hover effect.
    */
   public static final String SRCHOVER = "data-tobago-srchover";
+
   /**
    * Alternate to the src attribute, to implement a hover effect.
    */
   public static final String SRCDEFAULT = "data-tobago-srcdefault";
+
   /**
    * Alternate to the src attribute, icon open and close.
    */
   public static final String SRCCLOSE = "data-tobago-srcclose";
+
   /**
    * Alternate to the src attribute, icon open and close.
    */
   public static final String SRCOPEN = "data-tobago-srcopen";
+
+  /**
+   * Id of the parent node in a tree node.
+   */
+  public static final String TREEPARENT = "data-tobago-treeparent";
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeMenuNodeRenderer.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/TreeMenuNodeRenderer.java?rev=1203580&r1=1203579&r2=1203580&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeMenuNodeRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeMenuNodeRenderer.java Fri Nov 18 10:48:22 2011
@@ -108,7 +108,6 @@ public class TreeMenuNodeRenderer extend
   @Override
   public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
 
-    final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
     final UITreeNode node = (UITreeNode) component;
     final UITreeMenu tree = ComponentUtils.findAncestor(node, UITreeMenu.class);
 
@@ -121,10 +120,29 @@ public class TreeMenuNodeRenderer extend
     final boolean ie6
         = VariableResolverUtils.resolveClientProperties(facesContext).getUserAgent().equals(UserAgent.MSIE_6_0);
 
+    // XXX todo: find a better way to determine the parentId
+    final String clientId = node.getClientId(facesContext);
+    final int colon = clientId.lastIndexOf(":");
+    final int underscore = clientId.substring(0, colon).lastIndexOf("_");
+    final String parentId = root ? null : clientId.substring(0, underscore) + clientId.substring(colon);
+
+    final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
+
+    if (expanded) {
+      tree.getExpandedCache().add(id);
+    }
+
     if (showRoot || !root) {
       writer.startElement(HtmlElements.DIV, null);
       writer.writeIdAttribute(id);
       writer.writeClassAttribute(Classes.create(node));
+      writer.writeAttribute(DataAttributes.TREEPARENT, parentId, false);
+
+      if (!tree.getExpandedCache().contains(parentId)) {
+        Style style = new Style();
+        style.setDisplay(Display.NONE);
+        writer.writeStyleAttribute(style);
+      }
 
       if (folder) {
         encodeExpandedHidden(writer, node, id, expanded);
@@ -149,13 +167,6 @@ public class TreeMenuNodeRenderer extend
       writer.endElement(HtmlElements.DIV);
     }
 
-    if (folder) {
-      writer.startElement(HtmlElements.DIV, null);
-      writer.writeIdAttribute(id + ComponentUtils.SUB_SEPARATOR + "content");
-      Style contentStyle = new Style();
-      contentStyle.setDisplay(expanded ? Display.BLOCK : Display.NONE);
-      writer.writeStyleAttribute(contentStyle);
-    }
   }
 
   private void encodeExpandedHidden(
@@ -182,12 +193,4 @@ public class TreeMenuNodeRenderer extend
     writer.endElement(HtmlElements.IMG);
   }
 
-  @Override
-  public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
-    UITreeNode node = (UITreeNode) component;
-    if (node.isFolder()) {
-      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
-      writer.endElement(HtmlElements.DIV);
-    }
-  }
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeNodeRenderer.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/TreeNodeRenderer.java?rev=1203580&r1=1203579&r2=1203580&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeNodeRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TreeNodeRenderer.java Fri Nov 18 10:48:22 2011
@@ -31,6 +31,7 @@ import org.apache.myfaces.tobago.model.T
 import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
 import org.apache.myfaces.tobago.renderkit.css.Classes;
 import org.apache.myfaces.tobago.renderkit.css.Style;
+import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
@@ -126,6 +127,7 @@ public class TreeNodeRenderer extends La
 
   @Override
   public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
+
     final UITreeNode node = (UITreeNode) component;
     final AbstractUITree tree = ComponentUtils.findAncestor(node, AbstractUITree.class);
 
@@ -137,8 +139,18 @@ public class TreeNodeRenderer extends La
     // if the root is hidden, the root node must be expanded (otherwise you will see nothing)
     final boolean expanded = folder && node.isExpanded() || !showRoot && root;
 
+    // XXX todo: find a better way to determine the parentId
+    final String clientId = node.getClientId(facesContext);
+    final int colon = clientId.lastIndexOf(":");
+    final int underscore = clientId.substring(0, colon).lastIndexOf("_");
+    final String parentId = root ? null : clientId.substring(0, underscore) + clientId.substring(colon);
+
     final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
 
+    if (expanded) {
+      tree.getExpandedCache().add(id);
+    }
+
     if (showRoot || !root) {
       writer.startElement(HtmlElements.DIV, null);
 
@@ -148,6 +160,14 @@ public class TreeNodeRenderer extends La
         HtmlRendererUtils.renderDojoDndItem(node, writer, true);
       }
       writer.writeClassAttribute(Classes.create(node));
+      writer.writeAttribute(DataAttributes.TREEPARENT, parentId, false);
+
+      if (!tree.getExpandedCache().contains(parentId)) {
+        Style style = new Style();
+        style.setDisplay(Display.NONE);
+        writer.writeStyleAttribute(style);
+      }
+
       // div style (width)
       Style style = new Style(facesContext, tree);
       String widthString;
@@ -172,13 +192,6 @@ public class TreeNodeRenderer extends La
       writer.endElement(HtmlElements.DIV);
     }
 
-    if (folder) {
-      writer.startElement(HtmlElements.DIV, null);
-      writer.writeIdAttribute(id + ComponentUtils.SUB_SEPARATOR + "content");
-      Style style = new Style();
-      style.setDisplay(expanded ? Display.BLOCK : Display.NONE);
-      writer.writeStyleAttribute(style);
-    }
   }
 
   private void encodeExpandedHidden(
@@ -191,13 +204,4 @@ public class TreeNodeRenderer extends La
     writer.endElement(HtmlElements.INPUT);
   }
 
-  @Override
-  public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
-    UITreeNode node = (UITreeNode) component;
-    TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
-    boolean folder = node.isFolder();
-    if (folder) {
-      writer.endElement(HtmlElements.DIV);
-    }
-  }
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-tree.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-tree.js?rev=1203580&r1=1203579&r2=1203580&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-tree.js (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-tree.js Fri Nov 18 10:48:22 2011
@@ -20,24 +20,10 @@ Tobago.Tree = {};
 Tobago.Tree.toggleNode = function(element) {
   var src;
   var node = element.closest(".tobago-treeNode, .tobago-treeMenuNode");
-  var content = jQuery(Tobago.Utils.escapeClientId(node.attr("id") + Tobago.SUB_COMPONENT_SEP + "content"));
   var expanded = node.find(".tobago-treeMenuNode-expanded, .tobago-treeNode-expanded");
   var toggle = node.find(".tobago-treeMenuNode-toggle, .tobago-treeNode-toggle");
-  if (content.css("display") == "none") {
-    content.css("display", "block");
-    toggle.each(function() {
-      src = jQuery(this).data("tobago-srcopen");
-      if (src == null) { // use the close icon if there is no open icon
-        src = jQuery(this).data("tobago-srcclose");
-      }
-      jQuery(this).attr("src", src);
-      Tobago.fixPngAlpha(this);
-    });
-    expanded.attr("value", "true");
-    node.filter(".tobago-treeNode").addClass("tobago-treeNode-markup-expanded");
-    node.filter(".tobago-treeMenuNode").addClass("tobago-treeMenuNode-markup-expanded");
-  } else {
-    content.css("display", "none");
+  if ("true" == expanded.attr("value")) {
+    Tobago.Tree.hideChildren(node);
     toggle.each(function() {
       src = jQuery(this).data("tobago-srcclose");
       if (src == null) { // use the open icon if there is no close icon
@@ -49,9 +35,48 @@ Tobago.Tree.toggleNode = function(elemen
     expanded.attr("value", "false");
     node.filter(".tobago-treeNode").removeClass("tobago-treeNode-markup-expanded");
     node.filter(".tobago-treeMenuNode").removeClass("tobago-treeMenuNode-markup-expanded");
+  } else {
+    Tobago.Tree.showChildren(node);
+    toggle.each(function() {
+      src = jQuery(this).data("tobago-srcopen");
+      if (src == null) { // use the close icon if there is no open icon
+        src = jQuery(this).data("tobago-srcclose");
+      }
+      jQuery(this).attr("src", src);
+      Tobago.fixPngAlpha(this);
+    });
+    expanded.attr("value", "true");
+    node.filter(".tobago-treeNode").addClass("tobago-treeNode-markup-expanded");
+    node.filter(".tobago-treeMenuNode").addClass("tobago-treeMenuNode-markup-expanded");
   }
 };
 
+/**
+ * Hide all children of the node recursively.
+ * @param node A jQuery-Object as a node of the tree.
+ */
+Tobago.Tree.hideChildren = function (node) {
+  var children = node.nextAll("[data-tobago-treeparent='" + node.attr("id") + "']");
+  children.hide().each(function () {
+    Tobago.Tree.hideChildren(jQuery(this));
+  });
+};
+
+/**
+ * Show the children of the node recursively, there parents are expanded.
+ * @param node A jQuery-Object as a node of the tree.
+ */
+Tobago.Tree.showChildren = function (node) {
+  var children = node.nextAll("[data-tobago-treeparent='" + node.attr("id") + "']");
+  children.show().each(function () {
+    var child = jQuery(this);
+    var expanded = child.find(".tobago-treeMenuNode-expanded, .tobago-treeNode-expanded");
+    if ("true" == expanded.attr("value")) {
+      Tobago.Tree.showChildren(child);
+    }
+  });
+};
+
 Tobago.Tree.init = function(elements) {
 
   var listboxSelects = Tobago.Utils.selectWidthJQuery(elements, ".tobago-treeListbox").find("select");
@@ -136,5 +161,5 @@ Tobago.Tree.init = function(elements) {
     tree.find(".tobago-treeMenuNode").removeClass("tobago-treeMenuNode-markup-marked");
     node.addClass("tobago-treeMenuNode-markup-marked");
   });
-};
 
+};