You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/11/19 17:54:13 UTC

[4/9] git commit: Rewrite Tree: re-implement leaf node selection/de-selection

Rewrite Tree: re-implement leaf node selection/de-selection


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/32e3714c
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/32e3714c
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/32e3714c

Branch: refs/heads/5.4-js-rewrite
Commit: 32e3714cca603c7f38de68b5e4f7019638c2cd3d
Parents: 623f99e
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Sun Nov 18 01:55:34 2012 +0000
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Sun Nov 18 01:55:34 2012 +0000

----------------------------------------------------------------------
 .../coffeescript/META-INF/modules/core/tree.coffee |   20 +++++++
 .../apache/tapestry5/corelib/components/Tree.java  |   41 ++++++++++++---
 .../resources/META-INF/assets/tapestry5/tree.css   |   24 ++-------
 .../apache/tapestry5/corelib/components/Tree.tml   |    1 +
 4 files changed, 59 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32e3714c/tapestry-core/src/main/coffeescript/META-INF/modules/core/tree.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/tree.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/tree.coffee
index cad398e..43d33f7 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/tree.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/tree.coffee
@@ -24,6 +24,7 @@ define ["core/dom", "core/ajax", "core/zone"],
     LOADING = "tree-children-loading"
     LOADED = "tree-children-loaded"
     EXPANDED = "t-tree-expanded"
+    SELECTED = "t-selected-leaf-node"
 
     send = (node, action, onsuccess) ->
       container = node.findContainer TREE
@@ -86,8 +87,27 @@ define ["core/dom", "core/ajax", "core/zone"],
 
       return false
 
+    toggleSelection = ->
+
+      selected = this.hasClass SELECTED
+
+      node = this.findContainer("li").findFirst("[#{NODE_ID}]")
+
+      if selected
+        this.removeClass SELECTED
+        send node, "deselect"
+      else
+        this.addClass SELECTED
+        send node, "select"
+
+      return false
+
     dom.onDocument "click", SELECTOR, clickHandler
 
+    dom.onDocument "click",
+      "#{TREE}[data-tree-node-selection-enabled] LI.t-leaf-node > .t-tree-label",
+      toggleSelection
+
 
     return null
   

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32e3714c/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Tree.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Tree.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Tree.java
index 9c98010..c718af7 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Tree.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Tree.java
@@ -36,6 +36,9 @@ import java.util.List;
  * to track which nodes have been expanded. The optional {@link TreeSelectionModel} is used to track node selections (as currently
  * implemented, only leaf nodes may be selected).
  * <p/>
+ * Tree is <em>not</em> a form control component; all changes made to the tree on the client
+ * (expansions, collapsing, and selections) are propogated immediately back to the server.
+ * <p/>
  * The Tree component uses special tricks to support recursive rendering of the Tree as necessary.
  *
  * @tapestrydoc
@@ -98,7 +101,7 @@ public class Tree
     /**
      * Optional parameter used to inform the container about the value of the currently rendering TreeNode; this
      * is often preferable to the TreeNode, and like the node parameter, is primarily used when the label parameter
-     * it bound.
+     * is bound.
      */
     @Parameter
     private Object value;
@@ -136,6 +139,15 @@ public class Tree
         }
     };
 
+    private static RenderCommand MARK_SELECTED = new RenderCommand()
+    {
+        @Override
+        public void render(MarkupWriter writer, RenderQueue queue)
+        {
+            writer.getElement().addClassName("t-selected-leaf-node");
+        }
+    };
+
     /**
      * Renders a single node (which may be the last within its containing node).
      * This is a mix of immediate rendering, and queuing up various Blocks and Render commands
@@ -159,6 +171,8 @@ public class Tree
 
                 value = node.getValue();
 
+                boolean isLeaf = node.isLeaf();
+
                 writer.element("li");
 
                 if (isLast)
@@ -166,17 +180,19 @@ public class Tree
                     writer.attributes("class", "t-last");
                 }
 
+                if (isLeaf)
+                {
+                    writer.getElement().addClassName("t-leaf-node");
+                }
+
                 Element e = writer.element("span", "class", "t-tree-icon");
 
-                if (node.isLeaf())
-                {
-                    e.addClassName("t-leaf-node");
-                } else if (!node.getHasChildren())
+                if (!isLeaf && !node.getHasChildren())
                 {
                     e.addClassName("t-empty-node");
                 }
 
-                boolean hasChildren = !node.isLeaf() && node.getHasChildren();
+                boolean hasChildren = !isLeaf && node.getHasChildren();
                 boolean expanded = hasChildren && expansionModel.isExpanded(node);
 
                 writer.attributes("data-node-id", node.getId());
@@ -201,6 +217,12 @@ public class Tree
 
                 queue.push(RENDER_CLOSE_TAG);
                 queue.push(label);
+
+                if (isLeaf && selectionModel != null && selectionModel.isSelected(node))
+                {
+                    queue.push(MARK_SELECTED);
+                }
+
                 queue.push(RENDER_LABEL_SPAN);
 
             }
@@ -267,7 +289,7 @@ public class Tree
             return doMarkCollapsed(nodeId);
         }
 
-        if (action.equalsIgnoreCase("selected"))
+        if (action.equalsIgnoreCase("select"))
         {
             return doUpdateSelected(nodeId, true);
         }
@@ -377,4 +399,9 @@ public class Tree
     {
         expansionModel.clear();
     }
+
+    public Boolean getSelectionEnabled()
+    {
+        return selectionModel != null ? true : null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32e3714c/tapestry-core/src/main/resources/META-INF/assets/tapestry5/tree.css
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/tree.css b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/tree.css
index b00a81b..07b0408 100644
--- a/tapestry-core/src/main/resources/META-INF/assets/tapestry5/tree.css
+++ b/tapestry-core/src/main/resources/META-INF/assets/tapestry5/tree.css
@@ -46,16 +46,16 @@ SPAN.t-tree-icon {
   background-position: 0px 0px;
 }
 
-SPAN.t-tree-icon.t-leaf-node {
+LI.t-leaf-node > SPAN.t-tree-icon {
   cursor: default;
   background-position: -32px -16px;
 }
 
-SPAN.t-tree-label.t-selectable {
+DIV[data-tree-node-selection-enabled] LI.t-leaf-node > .t-tree-label {
     cursor: pointer;
 }
 
-SPAN.t-tree-label.t-selected-leaf-node-label {
+SPAN.t-tree-label.t-selected-leaf-node {
   font-weight: bold;
 }
 
@@ -73,20 +73,4 @@ SPAN.t-ajax-wait {
   height: 16px;
   display: inline-block;
   background-image: url(ajax-loader.gif);
-}
-
-SPAN.t-tree-icon.t-expand {
-  width: 16px;
-  height: 16px;
-  background-image: url(expand.png);
-  display: inline-block;
-  cursor: pointer;
-}
-
-SPAN.t-tree-icon.t-collapse {
-  width: 16px;
-  height: 16px;
-  background-image: url(collapse.png);
-  display: inline-block;
-  cursor: pointer;
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32e3714c/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Tree.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Tree.tml b/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Tree.tml
index 8be548a..ce76a05 100644
--- a/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Tree.tml
+++ b/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/Tree.tml
@@ -2,6 +2,7 @@
 
     <div class="${containerClass}"
          data-component-type="core/Tree"
+         data-tree-node-selection-enabled="${selectionEnabled}"
          data-tree-action-url="${treeActionLink}">
         <t:delegate to="renderRootNodes"/>
     </div>