2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.tree.TreePanel
\r
11 * <p>The TreePanel provides tree-structured UI representation of tree-structured data.</p>
\r
12 * <p>{@link Ext.tree.TreeNode TreeNode}s added to the TreePanel may each contain metadata
\r
13 * used by your application in their {@link Ext.tree.TreeNode#attributes attributes} property.</p>
\r
14 * <p><b>A TreePanel must have a {@link #root} node before it is rendered.</b> This may either be
\r
15 * specified using the {@link #root} config option, or using the {@link #setRootNode} method.
\r
16 * @extends Ext.Panel
\r
18 * @cfg {Ext.tree.TreeNode} root The root node for the tree.
\r
19 * @cfg {Boolean} rootVisible false to hide the root node (defaults to true)
\r
20 * @cfg {Boolean} lines false to disable tree lines (defaults to true)
\r
21 * @cfg {Boolean} enableDD true to enable drag and drop
\r
22 * @cfg {Boolean} enableDrag true to enable just drag
\r
23 * @cfg {Boolean} enableDrop true to enable just drop
\r
24 * @cfg {Object} dragConfig Custom config to pass to the {@link Ext.tree.TreeDragZone} instance
\r
25 * @cfg {Object} dropConfig Custom config to pass to the {@link Ext.tree.TreeDropZone} instance
\r
26 * @cfg {String} ddGroup The DD group this TreePanel belongs to
\r
27 * @cfg {String} ddAppendOnly True if the tree should only allow append drops (use for trees which are sorted)
\r
28 * @cfg {Boolean} ddScroll true to enable body scrolling
\r
29 * @cfg {Boolean} containerScroll true to register this container with ScrollManager
\r
30 * @cfg {Boolean} hlDrop false to disable node highlight on drop (defaults to the value of Ext.enableFx)
\r
31 * @cfg {String} hlColor The color of the node highlight (defaults to C3DAF9)
\r
32 * @cfg {Boolean} animate true to enable animated expand/collapse (defaults to the value of Ext.enableFx)
\r
33 * @cfg {Boolean} singleExpand true if only 1 node per branch may be expanded
\r
34 * @cfg {Boolean} selModel A tree selection model to use with this TreePanel (defaults to a {@link Ext.tree.DefaultSelectionModel})
\r
35 * @cfg {Boolean} trackMouseOver False to disable mouse over highlighting
\r
36 * @cfg {Ext.tree.TreeLoader} loader A {@link Ext.tree.TreeLoader} for use with this TreePanel
\r
37 * @cfg {String} pathSeparator The token used to separate sub-paths in path strings (defaults to '/')
\r
38 * @cfg {Boolean} useArrows True to use Vista-style arrows in the tree (defaults to false)
\r
41 * @param {Object} config
\r
43 Ext.tree.TreePanel = Ext.extend(Ext.Panel, {
\r
45 animate: Ext.enableFx,
\r
48 hlDrop : Ext.enableFx,
\r
51 initComponent : function(){
\r
52 Ext.tree.TreePanel.superclass.initComponent.call(this);
\r
54 if(!this.eventModel){
\r
55 this.eventModel = new Ext.tree.TreeEventModel(this);
\r
58 // initialize the loader
\r
59 var l = this.loader;
\r
61 l = new Ext.tree.TreeLoader({
\r
62 dataUrl: this.dataUrl
\r
64 }else if(typeof l == 'object' && !l.load){
\r
65 l = new Ext.tree.TreeLoader(l);
\r
72 * The root node of this tree.
\r
73 * @type Ext.tree.TreeNode
\r
77 this.setRootNode(this.root);
\r
84 * Fires when a new child node is appended to a node in this tree.
\r
85 * @param {Tree} tree The owner tree
\r
86 * @param {Node} parent The parent node
\r
87 * @param {Node} node The newly appended node
\r
88 * @param {Number} index The index of the newly appended node
\r
93 * Fires when a child node is removed from a node in this tree.
\r
94 * @param {Tree} tree The owner tree
\r
95 * @param {Node} parent The parent node
\r
96 * @param {Node} node The child node removed
\r
101 * Fires when a node is moved to a new location in the tree
\r
102 * @param {Tree} tree The owner tree
\r
103 * @param {Node} node The node moved
\r
104 * @param {Node} oldParent The old parent of this node
\r
105 * @param {Node} newParent The new parent of this node
\r
106 * @param {Number} index The index it was moved to
\r
111 * Fires when a new child node is inserted in a node in this tree.
\r
112 * @param {Tree} tree The owner tree
\r
113 * @param {Node} parent The parent node
\r
114 * @param {Node} node The child node inserted
\r
115 * @param {Node} refNode The child node the node was inserted before
\r
119 * @event beforeappend
\r
120 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
\r
121 * @param {Tree} tree The owner tree
\r
122 * @param {Node} parent The parent node
\r
123 * @param {Node} node The child node to be appended
\r
127 * @event beforeremove
\r
128 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
\r
129 * @param {Tree} tree The owner tree
\r
130 * @param {Node} parent The parent node
\r
131 * @param {Node} node The child node to be removed
\r
135 * @event beforemovenode
\r
136 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
\r
137 * @param {Tree} tree The owner tree
\r
138 * @param {Node} node The node being moved
\r
139 * @param {Node} oldParent The parent of the node
\r
140 * @param {Node} newParent The new parent the node is moving to
\r
141 * @param {Number} index The index it is being moved to
\r
145 * @event beforeinsert
\r
146 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
\r
147 * @param {Tree} tree The owner tree
\r
148 * @param {Node} parent The parent node
\r
149 * @param {Node} node The child node to be inserted
\r
150 * @param {Node} refNode The child node the node is being inserted before
\r
155 * @event beforeload
\r
156 * Fires before a node is loaded, return false to cancel
\r
157 * @param {Node} node The node being loaded
\r
162 * Fires when a node is loaded
\r
163 * @param {Node} node The node that was loaded
\r
167 * @event textchange
\r
168 * Fires when the text for a node is changed
\r
169 * @param {Node} node The node
\r
170 * @param {String} text The new text
\r
171 * @param {String} oldText The old text
\r
175 * @event beforeexpandnode
\r
176 * Fires before a node is expanded, return false to cancel.
\r
177 * @param {Node} node The node
\r
178 * @param {Boolean} deep
\r
179 * @param {Boolean} anim
\r
181 "beforeexpandnode",
\r
183 * @event beforecollapsenode
\r
184 * Fires before a node is collapsed, return false to cancel.
\r
185 * @param {Node} node The node
\r
186 * @param {Boolean} deep
\r
187 * @param {Boolean} anim
\r
189 "beforecollapsenode",
\r
191 * @event expandnode
\r
192 * Fires when a node is expanded
\r
193 * @param {Node} node The node
\r
197 * @event disabledchange
\r
198 * Fires when the disabled status of a node changes
\r
199 * @param {Node} node The node
\r
200 * @param {Boolean} disabled
\r
204 * @event collapsenode
\r
205 * Fires when a node is collapsed
\r
206 * @param {Node} node The node
\r
210 * @event beforeclick
\r
211 * Fires before click processing on a node. Return false to cancel the default action.
\r
212 * @param {Node} node The node
\r
213 * @param {Ext.EventObject} e The event object
\r
218 * Fires when a node is clicked
\r
219 * @param {Node} node The node
\r
220 * @param {Ext.EventObject} e The event object
\r
224 * @event checkchange
\r
225 * Fires when a node with a checkbox's checked property changes
\r
226 * @param {Node} this This node
\r
227 * @param {Boolean} checked
\r
232 * Fires when a node is double clicked
\r
233 * @param {Node} node The node
\r
234 * @param {Ext.EventObject} e The event object
\r
238 * @event contextmenu
\r
239 * Fires when a node is right clicked. To display a context menu in response to this
\r
240 * event, first create a Menu object (see {@link Ext.menu.Menu} for details), then add
\r
241 * a handler for this event:<code><pre>
\r
242 new Ext.tree.TreePanel({
\r
243 title: 'My TreePanel',
\r
244 root: new Ext.tree.AsyncTreeNode({
\r
247 { text: 'Child node 1', leaf: true },
\r
248 { text: 'Child node 2', leaf: true }
\r
251 contextMenu: new Ext.menu.Menu({
\r
254 text: 'Delete Node'
\r
257 itemclick: function(item) {
\r
259 case 'delete-node':
\r
260 var n = item.parentMenu.contextNode;
\r
261 if (n.parentNode) {
\r
270 contextmenu: function(node, e) {
\r
271 // Register the context node with the menu so that a Menu Item's handler function can access
\r
272 // it via its {@link Ext.menu.BaseItem#parentMenu parentMenu} property.
\r
274 var c = node.getOwnerTree().contextMenu;
\r
275 c.contextNode = node;
\r
276 c.showAt(e.getXY());
\r
281 * @param {Node} node The node
\r
282 * @param {Ext.EventObject} e The event object
\r
286 * @event beforechildrenrendered
\r
287 * Fires right before the child nodes for a node are rendered
\r
288 * @param {Node} node The node
\r
290 "beforechildrenrendered",
\r
293 * Fires when a node starts being dragged
\r
294 * @param {Ext.tree.TreePanel} this
\r
295 * @param {Ext.tree.TreeNode} node
\r
296 * @param {event} e The raw browser event
\r
301 * Fires when a drag operation is complete
\r
302 * @param {Ext.tree.TreePanel} this
\r
303 * @param {Ext.tree.TreeNode} node
\r
304 * @param {event} e The raw browser event
\r
309 * Fires when a dragged node is dropped on a valid DD target
\r
310 * @param {Ext.tree.TreePanel} this
\r
311 * @param {Ext.tree.TreeNode} node
\r
312 * @param {DD} dd The dd it was dropped on
\r
313 * @param {event} e The raw browser event
\r
317 * @event beforenodedrop
\r
318 * Fires when a DD object is dropped on a node in this tree for preprocessing. Return false to cancel the drop. The dropEvent
\r
319 * passed to handlers has the following properties:<br />
\r
320 * <ul style="padding:5px;padding-left:16px;">
\r
321 * <li>tree - The TreePanel</li>
\r
322 * <li>target - The node being targeted for the drop</li>
\r
323 * <li>data - The drag data from the drag source</li>
\r
324 * <li>point - The point of the drop - append, above or below</li>
\r
325 * <li>source - The drag source</li>
\r
326 * <li>rawEvent - Raw mouse event</li>
\r
327 * <li>dropNode - Drop node(s) provided by the source <b>OR</b> you can supply node(s)
\r
328 * to be inserted by setting them on this object.</li>
\r
329 * <li>cancel - Set this to true to cancel the drop.</li>
\r
330 * <li>dropStatus - If the default drop action is cancelled but the drop is valid, setting this to true
\r
331 * will prevent the animated "repair" from appearing.</li>
\r
333 * @param {Object} dropEvent
\r
338 * Fires after a DD object is dropped on a node in this tree. The dropEvent
\r
339 * passed to handlers has the following properties:<br />
\r
340 * <ul style="padding:5px;padding-left:16px;">
\r
341 * <li>tree - The TreePanel</li>
\r
342 * <li>target - The node being targeted for the drop</li>
\r
343 * <li>data - The drag data from the drag source</li>
\r
344 * <li>point - The point of the drop - append, above or below</li>
\r
345 * <li>source - The drag source</li>
\r
346 * <li>rawEvent - Raw mouse event</li>
\r
347 * <li>dropNode - Dropped node(s).</li>
\r
349 * @param {Object} dropEvent
\r
353 * @event nodedragover
\r
354 * Fires when a tree node is being targeted for a drag drop, return false to signal drop not allowed. The dragOverEvent
\r
355 * passed to handlers has the following properties:<br />
\r
356 * <ul style="padding:5px;padding-left:16px;">
\r
357 * <li>tree - The TreePanel</li>
\r
358 * <li>target - The node being targeted for the drop</li>
\r
359 * <li>data - The drag data from the drag source</li>
\r
360 * <li>point - The point of the drop - append, above or below</li>
\r
361 * <li>source - The drag source</li>
\r
362 * <li>rawEvent - Raw mouse event</li>
\r
363 * <li>dropNode - Drop node(s) provided by the source.</li>
\r
364 * <li>cancel - Set this to true to signal drop not allowed.</li>
\r
366 * @param {Object} dragOverEvent
\r
370 if(this.singleExpand){
\r
371 this.on("beforeexpandnode", this.restrictExpand, this);
\r
376 proxyNodeEvent : function(ename, a1, a2, a3, a4, a5, a6){
\r
377 if(ename == 'collapse' || ename == 'expand' || ename == 'beforecollapse' || ename == 'beforeexpand' || ename == 'move' || ename == 'beforemove'){
\r
378 ename = ename+'node';
\r
380 // args inline for performance while bubbling events
\r
381 return this.fireEvent(ename, a1, a2, a3, a4, a5, a6);
\r
386 * Returns this root node for this tree
\r
389 getRootNode : function(){
\r
394 * Sets the root node for this tree during initialization.
\r
395 * @param {Node} node
\r
398 setRootNode : function(node){
\r
399 if(!node.render){ // attributes passed
\r
400 node = this.loader.createNode(node);
\r
403 node.ownerTree = this;
\r
404 node.isRoot = true;
\r
405 this.registerNode(node);
\r
406 if(!this.rootVisible){
\r
407 var uiP = node.attributes.uiProvider;
\r
408 node.ui = uiP ? new uiP(node) : new Ext.tree.RootTreeNodeUI(node);
\r
414 * Gets a node in this tree by its id
\r
415 * @param {String} id
\r
418 getNodeById : function(id){
\r
419 return this.nodeHash[id];
\r
423 registerNode : function(node){
\r
424 this.nodeHash[node.id] = node;
\r
428 unregisterNode : function(node){
\r
429 delete this.nodeHash[node.id];
\r
433 toString : function(){
\r
434 return "[Tree"+(this.id?" "+this.id:"")+"]";
\r
438 restrictExpand : function(node){
\r
439 var p = node.parentNode;
\r
441 if(p.expandedChild && p.expandedChild.parentNode == p){
\r
442 p.expandedChild.collapse();
\r
444 p.expandedChild = node;
\r
449 * Retrieve an array of checked nodes, or an array of a specific attribute of checked nodes (e.g. "id")
\r
450 * @param {String} attribute (optional) Defaults to null (return the actual nodes)
\r
451 * @param {TreeNode} startNode (optional) The node to start from, defaults to the root
\r
454 getChecked : function(a, startNode){
\r
455 startNode = startNode || this.root;
\r
457 var f = function(){
\r
458 if(this.attributes.checked){
\r
459 r.push(!a ? this : (a == 'id' ? this.id : this.attributes[a]));
\r
462 startNode.cascade(f);
\r
467 * Returns the container element for this TreePanel.
\r
468 * @return {Element} The container element for this TreePanel.
\r
470 getEl : function(){
\r
475 * Returns the default {@link Ext.tree.TreeLoader} for this TreePanel.
\r
476 * @return {Ext.tree.TreeLoader} The TreeLoader for this TreePanel.
\r
478 getLoader : function(){
\r
479 return this.loader;
\r
485 expandAll : function(){
\r
486 this.root.expand(true);
\r
490 * Collapse all nodes
\r
492 collapseAll : function(){
\r
493 this.root.collapse(true);
\r
497 * Returns the selection model used by this TreePanel.
\r
498 * @return {TreeSelectionModel} The selection model used by this TreePanel
\r
500 getSelectionModel : function(){
\r
501 if(!this.selModel){
\r
502 this.selModel = new Ext.tree.DefaultSelectionModel();
\r
504 return this.selModel;
\r
508 * Expands a specified path in this TreePanel. A path can be retrieved from a node with {@link Ext.data.Node#getPath}
\r
509 * @param {String} path
\r
510 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
\r
511 * @param {Function} callback (optional) The callback to call when the expand is complete. The callback will be called with
\r
512 * (bSuccess, oLastNode) where bSuccess is if the expand was successful and oLastNode is the last node that was expanded.
\r
514 expandPath : function(path, attr, callback){
\r
515 attr = attr || "id";
\r
516 var keys = path.split(this.pathSeparator);
\r
517 var curNode = this.root;
\r
518 if(curNode.attributes[attr] != keys[1]){ // invalid root
\r
520 callback(false, null);
\r
525 var f = function(){
\r
526 if(++index == keys.length){
\r
528 callback(true, curNode);
\r
532 var c = curNode.findChild(attr, keys[index]);
\r
535 callback(false, curNode);
\r
540 c.expand(false, false, f);
\r
542 curNode.expand(false, false, f);
\r
546 * Selects the node in this tree at the specified path. A path can be retrieved from a node with {@link Ext.data.Node#getPath}
\r
547 * @param {String} path
\r
548 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
\r
549 * @param {Function} callback (optional) The callback to call when the selection is complete. The callback will be called with
\r
550 * (bSuccess, oSelNode) where bSuccess is if the selection was successful and oSelNode is the selected node.
\r
552 selectPath : function(path, attr, callback){
\r
553 attr = attr || "id";
\r
554 var keys = path.split(this.pathSeparator);
\r
555 var v = keys.pop();
\r
556 if(keys.length > 0){
\r
557 var f = function(success, node){
\r
558 if(success && node){
\r
559 var n = node.findChild(attr, v);
\r
565 }else if(callback){
\r
566 callback(false, n);
\r
570 callback(false, n);
\r
574 this.expandPath(keys.join(this.pathSeparator), attr, f);
\r
576 this.root.select();
\r
578 callback(true, this.root);
\r
584 * Returns the underlying Element for this tree
\r
585 * @return {Ext.Element} The Element
\r
587 getTreeEl : function(){
\r
592 onRender : function(ct, position){
\r
593 Ext.tree.TreePanel.superclass.onRender.call(this, ct, position);
\r
594 this.el.addClass('x-tree');
\r
595 this.innerCt = this.body.createChild({tag:"ul",
\r
596 cls:"x-tree-root-ct " +
\r
597 (this.useArrows ? 'x-tree-arrows' : this.lines ? "x-tree-lines" : "x-tree-no-lines")});
\r
601 initEvents : function(){
\r
602 Ext.tree.TreePanel.superclass.initEvents.call(this);
\r
604 if(this.containerScroll){
\r
605 Ext.dd.ScrollManager.register(this.body);
\r
607 if((this.enableDD || this.enableDrop) && !this.dropZone){
\r
609 * The dropZone used by this tree if drop is enabled
\r
610 * @type Ext.tree.TreeDropZone
\r
612 this.dropZone = new Ext.tree.TreeDropZone(this, this.dropConfig || {
\r
613 ddGroup: this.ddGroup || "TreeDD", appendOnly: this.ddAppendOnly === true
\r
616 if((this.enableDD || this.enableDrag) && !this.dragZone){
\r
618 * The dragZone used by this tree if drag is enabled
\r
619 * @type Ext.tree.TreeDragZone
\r
621 this.dragZone = new Ext.tree.TreeDragZone(this, this.dragConfig || {
\r
622 ddGroup: this.ddGroup || "TreeDD",
\r
623 scroll: this.ddScroll
\r
626 this.getSelectionModel().init(this);
\r
630 afterRender : function(){
\r
631 Ext.tree.TreePanel.superclass.afterRender.call(this);
\r
632 this.root.render();
\r
633 if(!this.rootVisible){
\r
634 this.root.renderChildren();
\r
638 onDestroy : function(){
\r
640 this.body.removeAllListeners();
\r
641 Ext.dd.ScrollManager.unregister(this.body);
\r
643 this.dropZone.unreg();
\r
646 this.dragZone.unreg();
\r
649 this.root.destroy();
\r
650 this.nodeHash = null;
\r
651 Ext.tree.TreePanel.superclass.onDestroy.call(this);
\r
655 * @cfg {String/Number} activeItem
\r
659 * @cfg {Boolean} autoDestroy
\r
663 * @cfg {Object/String/Function} autoLoad
\r
667 * @cfg {Boolean} autoWidth
\r
671 * @cfg {Boolean/Number} bufferResize
\r
675 * @cfg {String} defaultType
\r
679 * @cfg {Object} defaults
\r
683 * @cfg {Boolean} hideBorders
\r
687 * @cfg {Mixed} items
\r
691 * @cfg {String} layout
\r
695 * @cfg {Object} layoutConfig
\r
699 * @cfg {Boolean} monitorResize
\r
715 * @method doLayout
\r
727 * @method findById
\r
731 * @method findByType
\r
735 * @method getComponent
\r
739 * @method getLayout
\r
743 * @method getUpdater
\r
763 * @event afterLayout
\r
767 * @event beforeadd
\r
771 * @event beforeremove
\r
782 * @cfg {String} allowDomMove @hide
\r
785 * @cfg {String} autoEl @hide
\r
788 * @cfg {String} applyTo @hide
\r
791 * @cfg {String} contentEl @hide
\r
794 * @cfg {String} disabledClass @hide
\r
797 * @cfg {String} elements @hide
\r
800 * @cfg {String} html @hide
\r
803 * @property disabled
\r
807 * @method applyToMarkup
\r
819 * @method setDisabled
\r
824 Ext.tree.TreePanel.nodeTypes = {};
\r
826 Ext.reg('treepanel', Ext.tree.TreePanel);