Exemplo n.º 1
0
		private void ApplyCheckHierarchy(TreeNode n) {
			ApplyCheckHierarchyToChildren(n);
			ApplyCheckHierarchyToParents(n);
		}
Exemplo n.º 2
0
		/// <summary>
		/// Inserts a node into the tree, which must be attached. The parent's child list should not include the new node, and the list is fixed by this method. Returns the inserted element, or null if there was no physical node (eg. if the parent has never been expanded to).
		/// </summary>
		private Element InsertTreeNodeDOM(TreeNode parent, TreeNode toInsert, int position) {
			Element result = null;
			if (parent.treeIfRoot != null) {
				// Modifying the root.
				Element elem = GetElement();
				StringBuilder sb = new StringBuilder();
				AppendNodeHtml(toInsert, sb);
				result = jQuery.FromHtml(sb.ToString()).GetElement(0);
				if (position == parent.children.Count)
					elem.AppendChild(result);
				else
					elem.InsertBefore(result, elem.Children[position]);
			}
			else {
				// Not inserting at the root.
				Element parentEl = GetNodeElement(parent);
				if (parentEl == null)
					return null;

				if (parentEl.Children.Length > 1) {
					// We need to insert the new element into the parent's child list.
					Element listEl = parentEl.Children[1];
					StringBuilder sb = new StringBuilder();
					AppendNodeHtml((TreeNode)toInsert, sb);
					result = jQuery.FromHtml(sb.ToString()).GetElement(0);
					if (position == parent.children.Count)
						listEl.AppendChild(result);
					else
						listEl.InsertBefore(result, listEl.Children[position]);
				}

				if (parent.children.Count == 0)
					UpdateExpansionClasses(parentEl, parent.icon, true, parent.expanded);	// This was the first child we added.
			}

			return result;
		}
Exemplo n.º 3
0
		private void SetTreeNodeCheckStateDOM(TreeNode node, TreeNodeCheckState checkState) {
			Element nodeElem = GetNodeElement(node);
			if (nodeElem != null) {
				CheckBoxElement cb = (CheckBoxElement)jQuery.FromElement(nodeElem.Children[0]).Children("input").GetElement(0);
				cb.Indeterminate = (checkState == TreeNodeCheckState.indeterminate);
				cb.Checked = (checkState == TreeNodeCheckState.yes);
				cb.DefaultChecked = cb.Checked;
			}
		}
Exemplo n.º 4
0
		private void MakeDraggable(TreeNode node, bool enable) {
			var el = jQuery.FromElement(GetNodeElement(node).Children[0]).Children("." + ItemTextClass);
			if (enable) {
				el.Draggable(new DraggableOptions { Helper      = "clone",
				                                    AppendTo    = GetElement(),
				                                    Scroll      = true,
				                                    Containment = "parent" });
			}
			else
				((DraggableObject)el).Destroy();
		}
Exemplo n.º 5
0
		private void SetNodeTextDOM(TreeNode node, string text) {
			Element elem = GetNodeElement(node);
			if (elem != null)
				jQuery.FromElement(elem.Children[0]).Children("." + ItemTextClass).Text(text);
		}
Exemplo n.º 6
0
		protected virtual void InitDefault() {
			invisibleRoot = new TreeNode();
			invisibleRoot.treeIfRoot = this;
			invisibleRoot.expanded   = true;
			selectedNode  = null;
			enabled       = true;
			width         = 300;
			height        = 300;
			tabIndex      = 0;
		}
Exemplo n.º 7
0
		private Element GetNodeElement(TreeNode node) { return Document.GetElementById(NodeIdPrefix + Utils.ToStringInvariantInt(node.id)); }
Exemplo n.º 8
0
		public static string GetTreeNodeIcon(TreeNode node) {
			return node.icon;
		}
Exemplo n.º 9
0
		public static object GetTreeNodeData(TreeNode node) {
			return node.data;
		}
Exemplo n.º 10
0
		public static TreeNodeCheckState GetTreeNodeCheckState(TreeNode node) {
			return node.checkState;
		}
Exemplo n.º 11
0
		public static string GetTreeNodeText(TreeNode node) {
			return node.text;
		}
Exemplo n.º 12
0
		public static bool IsTreeNodeExpanded(TreeNode node) {
			return node.expanded;
		}
Exemplo n.º 13
0
		public static void SetTreeNodeExpanded(TreeNode node, bool expanded, bool applyToAllChildren) {
			if (node.children.Count > 0) {
				if (applyToAllChildren) {
					for (int i = 0; i < node.children.Count; i++)
						SetTreeNodeExpanded(node.children[i], expanded, true);
				}

				if (node.treeIfRoot == null) {
					#if CLIENT
						// Don't do this for the invisible root (it is always expanded).
						Tree tree = GetTree(node);
						if (tree != null && tree.isAttached)
							tree.DoSetTreeNodeExpanded(node, expanded, false);
						else
							node.expanded = expanded;
					#else
						node.expanded = expanded;
					#endif
				}
			}
			else {
				if (node.treeIfRoot == null)
					node.expanded = expanded;
			}
		}
Exemplo n.º 14
0
		public static void SetTreeNodeCheckState(TreeNode node, TreeNodeCheckState check) {
			Tree tree = GetTree(node);
			#if CLIENT
				if (tree != null && tree.isAttached && tree.hasChecks)
					tree.SetTreeNodeCheckStateDOM(node, check);
			#endif

			node.checkState = check;
			if (tree != null && tree.autoCheckHierarchy)
				tree.ApplyCheckHierarchy(node);

			#if CLIENT
				if (tree != null)
					tree.OnNodeChecked(new TreeNodeEventArgs(node));
			#endif
		}
Exemplo n.º 15
0
		private bool SetSelection(TreeNode newSelection, bool raiseSelectionChanging, bool raiseSelectionChanged) {
			if (newSelection != null)
				EnsureExpandedTo(newSelection);

			#if CLIENT
				if (raiseSelectionChanging) {
					if (!RaiseSelectionChanging(newSelection))
						return false;
				}

				if (isAttached) {
					if (selectedNode != null) {
						// Remove the previous selection
						var jq = jQuery.FromElement(GetNodeElement(selectedNode).Children[0]);
						Element d = jq.Children("." + ItemTextClass).GetElement(0);
						d.ClassName = ItemTextClass;
						if (enableDragDrop)
							MakeDraggable(selectedNode, false);
					}

					if (newSelection != null) {
						var jq = jQuery.FromElement(GetNodeElement(newSelection).Children[0]);
						Element d = jq.Children("." + ItemTextClass).GetElement(0);
						d.ClassName = ItemTextClass + " " + SelectedNodeClass;
						if (enableDragDrop)
							MakeDraggable(newSelection, true);
						EnsureVisible(newSelection);
					}
				}
			#endif

			selectedNode = newSelection;

			#if CLIENT
				if (raiseSelectionChanged)
					OnSelectionChanged(EventArgs.Empty);
			#endif
			return true;
		}
Exemplo n.º 16
0
		public static List<TreeNode> GetTreeNodeChildren(TreeNode node) {
			#if SERVER
				return node.children.ToList();
			#endif
			#if CLIENT
				return node.children.Clone();
			#endif
		}
Exemplo n.º 17
0
		private void AppendNodeHtml(TreeNode n, StringBuilder sb) {
			bool hasChildren = n.children != null && n.children.Count > 0;
			string suffix = (hasChildren ? (n.expanded ? ExpandedSuffix : CollapsedSuffix) : LeafSuffix);
            string blankImageUrl = uiService.BlankImageUrl;

			sb.Append("<div class=\"" + ContainerClass + " " + ContainerClass + suffix + "\" id=\"" + NodeIdPrefix + Utils.ToStringInvariantInt(n.id) + "\">"
			        +     "<div class=\"" + NodeClass + " " + NodeClass + suffix + "\">"
			        +         "<span class=\"" + ExpandCollapseClass + " " + ExpandCollapseClass + suffix + "\"><img src=\"" + blankImageUrl + "\" alt=\"\"/></span>"
			        +         "<span class=\"" + IconClass + " " + IconClass + suffix + " " + n.icon + " " + n.icon + suffix + "\"><img src=\"" + blankImageUrl + "\" alt=\"\"/></span>");
			if (hasChecks)
				sb.Append(    "<input type=\"checkbox\"" + (n.checkState == TreeNodeCheckState.yes ? " checked=\"checked\"" : "") + (n.checkState == TreeNodeCheckState.yes ? " defaultChecked=\"defaultChecked\"" : "") + (n.checkState == TreeNodeCheckState.indeterminate ? " indeterminate=\"indeterminate\"" : "") + "class=\"" + CheckBoxClass + "\" tabindex=\"-1\"/>");
			sb.Append(        "<span class=\"" + ItemTextClass + (n == selectedNode ? " " + SelectedNodeClass : "") + "\">" + Utils.HtmlEncode(n.text) + "</span>");
			sb.Append(    "</div>");
			if (hasChildren && n.expanded)
				AppendNestedListHtml(n.children, sb);
			sb.Append("</div>");
		}
Exemplo n.º 18
0
		public static bool HasChildren(TreeNode node) {
			return node.children.Count > 0;
		}
Exemplo n.º 19
0
		protected virtual void InitConfig(JsDictionary config) {
			id                 = (string)config["id"];
			width              = (int)config["width"];
			height             = (int)config["height"];
			tabIndex           = (int)config["tabIndex"];
			hasChecks          = (bool)config["hasChecks"];
			enabled            = (bool)config["enabled"];
			invisibleRoot      = (TreeNode)config["invisibleRoot"];
			enableDragDrop     = (bool)config["enableDragDrop"];
			autoCheckHierarchy = (bool)config["autoCheckHierarchy"];
			nextNodeId         = (int)config["nextNodeId"];

			FixTreeAfterDeserialize(invisibleRoot);
			invisibleRoot.treeIfRoot = this;
			invisibleRoot.parent     = null;
			
			int[] selectionPath = (int[])config["selectionPath"];
			selectedNode = selectionPath != null ? FollowTreeNodePath(invisibleRoot, selectionPath) : null;

			Attach();
		}
Exemplo n.º 20
0
		public static TreeNode GetTreeNodeParent(TreeNode node) {
			return node.parent;
		}
Exemplo n.º 21
0
		private void EnsureVisible(TreeNode n) {
			Element treeEl = GetElement(), nodeEl = GetNodeElement(n);
			var treeJq = jQuery.FromElement(treeEl);
			var nodeJq = jQuery.FromElement(nodeEl);
			int offsetTop = nodeJq.GetOffset().Top - treeJq.GetOffset().Top, scrollTop = treeJq.GetScrollTop(), nHeight = nodeJq.Children(":eq(0)").GetOuterHeight(), treeHeight = treeEl.ClientHeight;

			if (offsetTop < 0) {
				treeJq.ScrollTop(scrollTop + offsetTop);
			}
			else if (offsetTop + nHeight > treeHeight) {
				treeJq.ScrollTop(scrollTop + offsetTop + nHeight - treeHeight);
			}
		}
Exemplo n.º 22
0
		private static void FindTreeNodesRecursive(TreeNode n, TreeNodeFindPredicate predicate, List<TreeNode> arr) {
			if (predicate(n))
				arr.Add(n);
			for (int i = 0; i < n.children.Count; i++)
				FindTreeNodesRecursive(n.children[i], predicate, arr);
		}
Exemplo n.º 23
0
		/// <summary>
		/// Removes a node from the tree, which must be attached. Does not fix the node's parent's child list. Returns the removed element, or null if there was no physical node (eg. if the node has never been expanded to).
		/// </summary>
		private Element RemoveTreeNodeDOM(TreeNode node) {
			if (selectedNode != null && (selectedNode == node || TreeNodeIsChildOf(selectedNode, node))) {
				TreeNode newSelection;

				int childIndex = GetTreeNodeChildIndex(node);
				if (childIndex < node.parent.children.Count - 1)
					newSelection = (TreeNode)node.parent.children[childIndex + 1];
				else if (node.parent.children.Count > 1)
					newSelection = (TreeNode)node.parent.children[node.parent.children.Count - 2];	// - 2 because our caller will remove the last node.
				else if (node.parent.treeIfRoot == null)
					newSelection = node.parent;
				else
					newSelection = null;

				SetSelection(newSelection, false, true);
			}
		
			Element elem = GetNodeElement(node);
			if (elem == null)
				return null;
			Element list = elem.ParentNode;
			elem.ParentNode.RemoveChild(elem);
			if (node.parent.children.Count == 1 && node.parent.treeIfRoot == null)	// In case we are removing the last child, also remove the child list.
				list.ParentNode.RemoveChild(list);
			return elem;
		}
Exemplo n.º 24
0
		public static List<TreeNode> FindTreeNodes(TreeNode root, TreeNodeFindPredicate predicate) {
			List<TreeNode> result = new List<TreeNode>();
			FindTreeNodesRecursive(root, predicate, result);
			return result;
		}
Exemplo n.º 25
0
		private void SetNodeIconDOM(TreeNode node, string text) {
			Element elem = GetNodeElement(node);
			if (elem != null) {
				string suffix = (node.children.Count > 0 ? (node.expanded ? ExpandedSuffix : CollapsedSuffix) : LeafSuffix);
				Element iconEl = jQuery.FromElement(elem.Children[0]).Children("." + IconClass).GetElement(0);
				iconEl.ClassName = (IconClass + " " + IconClass + suffix + " " + node.icon + " " + node.icon + suffix);
			}
		}
Exemplo n.º 26
0
		public TreeDragDropCompletedEventArgs(TreeNode draggedNode, TreeNode newParent, int positionWithinNewParent) {
			this.DraggedNode             = draggedNode;
			this.NewParent               = newParent;
			this.PositionWithinNewParent = positionWithinNewParent;
		}
Exemplo n.º 27
0
		internal TreeNode() {
			this.id         = Tree.nextNodeId++;
			this.text       = null;
			this.data       = null;
			this.icon       = Tree.DefaultIcon;
			this.children   = new List<TreeNode>();
			this.expanded   = false;
			this.checkState = TreeNodeCheckState.no;
			this.treeIfRoot = null;
			this.parent     = null;
		}
Exemplo n.º 28
0
		public TreeDropTarget(TreeNode node, bool above) {
			this.node  = node;
			this.above = above;
		}
Exemplo n.º 29
0
		private void DoSetTreeNodeExpanded(TreeNode node, bool expanded, bool doItEvenIfNoChildren) {
			node.expanded = expanded;
			if (doItEvenIfNoChildren || node.children.Count > 0) {
				Element elem = GetNodeElement(node);
				if (elem != null) {
					if (elem.Children.Length > 1) {
						// The list exists - update its display state.
						elem.Children[1].Style.Display = expanded ? "" : "none";
					}
					else {
						if (expanded) {
							// Expanding and the list does not exist - add it
							StringBuilder sb = new StringBuilder();
							AppendNestedListHtml(node.children, sb);
							jQuery.FromHtml(sb.ToString()).AppendTo(jQuery.FromElement(elem));
						}
					}

					UpdateExpansionClasses(elem, node.icon, node.children.Count > 0, expanded);
				}
			}

			if (selectedNode != null && !expanded && TreeNodeIsChildOf(selectedNode, node)) {
				SetSelection(node, false, true);
			}
		}
Exemplo n.º 30
0
		private void ApplyCheckHierarchyToParents(TreeNode node) {
			for (TreeNode n = node.parent; n != null && n.treeIfRoot == null; n = n.parent) {
				n.checkState = FindCheckStateFromChildren(n);
				#if CLIENT
					if (isAttached)
						SetTreeNodeCheckStateDOM(n, n.checkState);
					OnNodeChecked(new TreeNodeEventArgs(n));
				#endif
			}
		}