3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.tree.TreePanel"></div>/**
\r
15 * @class Ext.tree.TreePanel
\r
16 * @extends Ext.Panel
\r
17 * <p>The TreePanel provides tree-structured UI representation of tree-structured data.</p>
\r
18 * <p>{@link Ext.tree.TreeNode TreeNode}s added to the TreePanel may each contain metadata
\r
19 * used by your application in their {@link Ext.tree.TreeNode#attributes attributes} property.</p>
\r
20 * <p><b>A TreePanel must have a {@link #root} node before it is rendered.</b> This may either be
\r
21 * specified using the {@link #root} config option, or using the {@link #setRootNode} method.
\r
22 * <p>An example of tree rendered to an existing div:</p><pre><code>
\r
23 var tree = new Ext.tree.TreePanel({
\r
24 renderTo: 'tree-div',
\r
29 containerScroll: true,
\r
31 // auto create TreeLoader
\r
32 dataUrl: 'get-nodes.php',
\r
42 tree.getRootNode().expand();
\r
44 * <p>The example above would work with a data packet similar to this:</p><pre><code>
\r
47 "id": "source\/adapter",
\r
55 "id": "source\/debug.js",
\r
60 * <p>An example of tree within a Viewport:</p><pre><code>
\r
66 title: 'Navigation',
\r
71 loader: new Ext.tree.TreeLoader(),
\r
72 root: new Ext.tree.AsyncTreeNode({
\r
75 text: 'Menu Option 1',
\r
78 text: 'Menu Option 2',
\r
81 text: 'Menu Option 3',
\r
87 click: function(n) {
\r
88 Ext.Msg.alert('Navigation Tree Click', 'You clicked: "' + n.attributes.text + '"');
\r
94 // remaining code not shown ...
\r
99 * @cfg {Ext.tree.TreeNode} root The root node for the tree.
\r
100 * @cfg {Boolean} rootVisible <tt>false</tt> to hide the root node (defaults to <tt>true</tt>)
\r
101 * @cfg {Boolean} lines <tt>false</tt> to disable tree lines (defaults to <tt>true</tt>)
\r
102 * @cfg {Boolean} enableDD <tt>true</tt> to enable drag and drop
\r
103 * @cfg {Boolean} enableDrag <tt>true</tt> to enable just drag
\r
104 * @cfg {Boolean} enableDrop <tt>true</tt> to enable just drop
\r
105 * @cfg {Object} dragConfig Custom config to pass to the {@link Ext.tree.TreeDragZone} instance
\r
106 * @cfg {Object} dropConfig Custom config to pass to the {@link Ext.tree.TreeDropZone} instance
\r
107 * @cfg {String} ddGroup The DD group this TreePanel belongs to
\r
108 * @cfg {Boolean} ddAppendOnly <tt>true</tt> if the tree should only allow append drops (use for trees which are sorted)
\r
109 * @cfg {Boolean} ddScroll <tt>true</tt> to enable body scrolling
\r
110 * @cfg {Boolean} containerScroll <tt>true</tt> to register this container with ScrollManager
\r
111 * @cfg {Boolean} hlDrop <tt>false</tt> to disable node highlight on drop (defaults to the value of {@link Ext#enableFx})
\r
112 * @cfg {String} hlColor The color of the node highlight (defaults to <tt>'C3DAF9'</tt>)
\r
113 * @cfg {Boolean} animate <tt>true</tt> to enable animated expand/collapse (defaults to the value of {@link Ext#enableFx})
\r
114 * @cfg {Boolean} singleExpand <tt>true</tt> if only 1 node per branch may be expanded
\r
115 * @cfg {Object} selModel A tree selection model to use with this TreePanel (defaults to an {@link Ext.tree.DefaultSelectionModel})
\r
116 * @cfg {Boolean} trackMouseOver <tt>false</tt> to disable mouse over highlighting
\r
117 * @cfg {Ext.tree.TreeLoader} loader A {@link Ext.tree.TreeLoader} for use with this TreePanel
\r
118 * @cfg {String} pathSeparator The token used to separate sub-paths in path strings (defaults to <tt>'/'</tt>)
\r
119 * @cfg {Boolean} useArrows <tt>true</tt> to use Vista-style arrows in the tree (defaults to <tt>false</tt>)
\r
120 * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
\r
123 * @param {Object} config
\r
126 Ext.tree.TreePanel = Ext.extend(Ext.Panel, {
\r
127 rootVisible : true,
\r
128 animate: Ext.enableFx,
\r
131 hlDrop : Ext.enableFx,
\r
132 pathSeparator: "/",
\r
135 * @cfg {Array} bubbleEvents
\r
136 * <p>An array of events that, when fired, should be bubbled to any parent container.
\r
137 * Defaults to <tt>['add', 'remove']</tt>.
\r
141 initComponent : function(){
\r
142 Ext.tree.TreePanel.superclass.initComponent.call(this);
\r
144 if(!this.eventModel){
\r
145 this.eventModel = new Ext.tree.TreeEventModel(this);
\r
148 // initialize the loader
\r
149 var l = this.loader;
\r
151 l = new Ext.tree.TreeLoader({
\r
152 dataUrl: this.dataUrl,
\r
153 requestMethod: this.requestMethod
\r
155 }else if(typeof l == 'object' && !l.load){
\r
156 l = new Ext.tree.TreeLoader(l);
\r
160 this.nodeHash = {};
\r
163 * The root node of this tree.
\r
164 * @type Ext.tree.TreeNode
\r
170 this.setRootNode(r);
\r
178 * Fires when a new child node is appended to a node in this tree.
\r
179 * @param {Tree} tree The owner tree
\r
180 * @param {Node} parent The parent node
\r
181 * @param {Node} node The newly appended node
\r
182 * @param {Number} index The index of the newly appended node
\r
187 * Fires when a child node is removed from a node in this tree.
\r
188 * @param {Tree} tree The owner tree
\r
189 * @param {Node} parent The parent node
\r
190 * @param {Node} node The child node removed
\r
195 * Fires when a node is moved to a new location in the tree
\r
196 * @param {Tree} tree The owner tree
\r
197 * @param {Node} node The node moved
\r
198 * @param {Node} oldParent The old parent of this node
\r
199 * @param {Node} newParent The new parent of this node
\r
200 * @param {Number} index The index it was moved to
\r
205 * Fires when a new child node is inserted in a node in this tree.
\r
206 * @param {Tree} tree The owner tree
\r
207 * @param {Node} parent The parent node
\r
208 * @param {Node} node The child node inserted
\r
209 * @param {Node} refNode The child node the node was inserted before
\r
213 * @event beforeappend
\r
214 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
\r
215 * @param {Tree} tree The owner tree
\r
216 * @param {Node} parent The parent node
\r
217 * @param {Node} node The child node to be appended
\r
221 * @event beforeremove
\r
222 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
\r
223 * @param {Tree} tree The owner tree
\r
224 * @param {Node} parent The parent node
\r
225 * @param {Node} node The child node to be removed
\r
229 * @event beforemovenode
\r
230 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
\r
231 * @param {Tree} tree The owner tree
\r
232 * @param {Node} node The node being moved
\r
233 * @param {Node} oldParent The parent of the node
\r
234 * @param {Node} newParent The new parent the node is moving to
\r
235 * @param {Number} index The index it is being moved to
\r
239 * @event beforeinsert
\r
240 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
\r
241 * @param {Tree} tree The owner tree
\r
242 * @param {Node} parent The parent node
\r
243 * @param {Node} node The child node to be inserted
\r
244 * @param {Node} refNode The child node the node is being inserted before
\r
249 * @event beforeload
\r
250 * Fires before a node is loaded, return false to cancel
\r
251 * @param {Node} node The node being loaded
\r
256 * Fires when a node is loaded
\r
257 * @param {Node} node The node that was loaded
\r
261 * @event textchange
\r
262 * Fires when the text for a node is changed
\r
263 * @param {Node} node The node
\r
264 * @param {String} text The new text
\r
265 * @param {String} oldText The old text
\r
269 * @event beforeexpandnode
\r
270 * Fires before a node is expanded, return false to cancel.
\r
271 * @param {Node} node The node
\r
272 * @param {Boolean} deep
\r
273 * @param {Boolean} anim
\r
275 "beforeexpandnode",
\r
277 * @event beforecollapsenode
\r
278 * Fires before a node is collapsed, return false to cancel.
\r
279 * @param {Node} node The node
\r
280 * @param {Boolean} deep
\r
281 * @param {Boolean} anim
\r
283 "beforecollapsenode",
\r
285 * @event expandnode
\r
286 * Fires when a node is expanded
\r
287 * @param {Node} node The node
\r
291 * @event disabledchange
\r
292 * Fires when the disabled status of a node changes
\r
293 * @param {Node} node The node
\r
294 * @param {Boolean} disabled
\r
298 * @event collapsenode
\r
299 * Fires when a node is collapsed
\r
300 * @param {Node} node The node
\r
304 * @event beforeclick
\r
305 * Fires before click processing on a node. Return false to cancel the default action.
\r
306 * @param {Node} node The node
\r
307 * @param {Ext.EventObject} e The event object
\r
312 * Fires when a node is clicked
\r
313 * @param {Node} node The node
\r
314 * @param {Ext.EventObject} e The event object
\r
318 * @event checkchange
\r
319 * Fires when a node with a checkbox's checked property changes
\r
320 * @param {Node} this This node
\r
321 * @param {Boolean} checked
\r
325 * @event beforedblclick
\r
326 * Fires before double click processing on a node. Return false to cancel the default action.
\r
327 * @param {Node} node The node
\r
328 * @param {Ext.EventObject} e The event object
\r
333 * Fires when a node is double clicked
\r
334 * @param {Node} node The node
\r
335 * @param {Ext.EventObject} e The event object
\r
339 * @event contextmenu
\r
340 * Fires when a node is right clicked. To display a context menu in response to this
\r
341 * event, first create a Menu object (see {@link Ext.menu.Menu} for details), then add
\r
342 * a handler for this event:<pre><code>
\r
343 new Ext.tree.TreePanel({
\r
344 title: 'My TreePanel',
\r
345 root: new Ext.tree.AsyncTreeNode({
\r
348 { text: 'Child node 1', leaf: true },
\r
349 { text: 'Child node 2', leaf: true }
\r
352 contextMenu: new Ext.menu.Menu({
\r
355 text: 'Delete Node'
\r
358 itemclick: function(item) {
\r
360 case 'delete-node':
\r
361 var n = item.parentMenu.contextNode;
\r
362 if (n.parentNode) {
\r
371 contextmenu: function(node, e) {
\r
372 // Register the context node with the menu so that a Menu Item's handler function can access
\r
373 // it via its {@link Ext.menu.BaseItem#parentMenu parentMenu} property.
\r
375 var c = node.getOwnerTree().contextMenu;
\r
376 c.contextNode = node;
\r
377 c.showAt(e.getXY());
\r
382 * @param {Node} node The node
\r
383 * @param {Ext.EventObject} e The event object
\r
387 * @event beforechildrenrendered
\r
388 * Fires right before the child nodes for a node are rendered
\r
389 * @param {Node} node The node
\r
391 "beforechildrenrendered",
\r
394 * Fires when a node starts being dragged
\r
395 * @param {Ext.tree.TreePanel} this
\r
396 * @param {Ext.tree.TreeNode} node
\r
397 * @param {event} e The raw browser event
\r
402 * Fires when a drag operation is complete
\r
403 * @param {Ext.tree.TreePanel} this
\r
404 * @param {Ext.tree.TreeNode} node
\r
405 * @param {event} e The raw browser event
\r
410 * Fires when a dragged node is dropped on a valid DD target
\r
411 * @param {Ext.tree.TreePanel} this
\r
412 * @param {Ext.tree.TreeNode} node
\r
413 * @param {DD} dd The dd it was dropped on
\r
414 * @param {event} e The raw browser event
\r
418 * @event beforenodedrop
\r
419 * Fires when a DD object is dropped on a node in this tree for preprocessing. Return false to cancel the drop. The dropEvent
\r
420 * passed to handlers has the following properties:<br />
\r
421 * <ul style="padding:5px;padding-left:16px;">
\r
422 * <li>tree - The TreePanel</li>
\r
423 * <li>target - The node being targeted for the drop</li>
\r
424 * <li>data - The drag data from the drag source</li>
\r
425 * <li>point - The point of the drop - append, above or below</li>
\r
426 * <li>source - The drag source</li>
\r
427 * <li>rawEvent - Raw mouse event</li>
\r
428 * <li>dropNode - Drop node(s) provided by the source <b>OR</b> you can supply node(s)
\r
429 * to be inserted by setting them on this object.</li>
\r
430 * <li>cancel - Set this to true to cancel the drop.</li>
\r
431 * <li>dropStatus - If the default drop action is cancelled but the drop is valid, setting this to true
\r
432 * will prevent the animated "repair" from appearing.</li>
\r
434 * @param {Object} dropEvent
\r
439 * Fires after a DD object is dropped on a node in this tree. The dropEvent
\r
440 * passed to handlers has the following properties:<br />
\r
441 * <ul style="padding:5px;padding-left:16px;">
\r
442 * <li>tree - The TreePanel</li>
\r
443 * <li>target - The node being targeted for the drop</li>
\r
444 * <li>data - The drag data from the drag source</li>
\r
445 * <li>point - The point of the drop - append, above or below</li>
\r
446 * <li>source - The drag source</li>
\r
447 * <li>rawEvent - Raw mouse event</li>
\r
448 * <li>dropNode - Dropped node(s).</li>
\r
450 * @param {Object} dropEvent
\r
454 * @event nodedragover
\r
455 * Fires when a tree node is being targeted for a drag drop, return false to signal drop not allowed. The dragOverEvent
\r
456 * passed to handlers has the following properties:<br />
\r
457 * <ul style="padding:5px;padding-left:16px;">
\r
458 * <li>tree - The TreePanel</li>
\r
459 * <li>target - The node being targeted for the drop</li>
\r
460 * <li>data - The drag data from the drag source</li>
\r
461 * <li>point - The point of the drop - append, above or below</li>
\r
462 * <li>source - The drag source</li>
\r
463 * <li>rawEvent - Raw mouse event</li>
\r
464 * <li>dropNode - Drop node(s) provided by the source.</li>
\r
465 * <li>cancel - Set this to true to signal drop not allowed.</li>
\r
467 * @param {Object} dragOverEvent
\r
471 if(this.singleExpand){
\r
472 this.on("beforeexpandnode", this.restrictExpand, this);
\r
477 proxyNodeEvent : function(ename, a1, a2, a3, a4, a5, a6){
\r
478 if(ename == 'collapse' || ename == 'expand' || ename == 'beforecollapse' || ename == 'beforeexpand' || ename == 'move' || ename == 'beforemove'){
\r
479 ename = ename+'node';
\r
481 // args inline for performance while bubbling events
\r
482 return this.fireEvent(ename, a1, a2, a3, a4, a5, a6);
\r
487 * Returns this root node for this tree
\r
490 getRootNode : function(){
\r
495 * Sets the root node for this tree. If the TreePanel has already rendered a root node, the
\r
496 * previous root node (and all of its descendants) are destroyed before the new root node is rendered.
\r
497 * @param {Node} node
\r
500 setRootNode : function(node){
\r
501 Ext.destroy(this.root);
\r
502 if(!node.render){ // attributes passed
\r
503 node = this.loader.createNode(node);
\r
506 node.ownerTree = this;
\r
507 node.isRoot = true;
\r
508 this.registerNode(node);
\r
509 if(!this.rootVisible){
\r
510 var uiP = node.attributes.uiProvider;
\r
511 node.ui = uiP ? new uiP(node) : new Ext.tree.RootTreeNodeUI(node);
\r
513 if (this.innerCt) {
\r
514 this.innerCt.update('');
\r
515 this.afterRender();
\r
521 * Gets a node in this tree by its id
\r
522 * @param {String} id
\r
525 getNodeById : function(id){
\r
526 return this.nodeHash[id];
\r
530 registerNode : function(node){
\r
531 this.nodeHash[node.id] = node;
\r
535 unregisterNode : function(node){
\r
536 delete this.nodeHash[node.id];
\r
540 toString : function(){
\r
541 return "[Tree"+(this.id?" "+this.id:"")+"]";
\r
545 restrictExpand : function(node){
\r
546 var p = node.parentNode;
\r
548 if(p.expandedChild && p.expandedChild.parentNode == p){
\r
549 p.expandedChild.collapse();
\r
551 p.expandedChild = node;
\r
556 * Retrieve an array of checked nodes, or an array of a specific attribute of checked nodes (e.g. "id")
\r
557 * @param {String} attribute (optional) Defaults to null (return the actual nodes)
\r
558 * @param {TreeNode} startNode (optional) The node to start from, defaults to the root
\r
561 getChecked : function(a, startNode){
\r
562 startNode = startNode || this.root;
\r
564 var f = function(){
\r
565 if(this.attributes.checked){
\r
566 r.push(!a ? this : (a == 'id' ? this.id : this.attributes[a]));
\r
569 startNode.cascade(f);
\r
574 * Returns the container element for this TreePanel.
\r
575 * @return {Element} The container element for this TreePanel.
\r
577 getEl : function(){
\r
582 * Returns the default {@link Ext.tree.TreeLoader} for this TreePanel.
\r
583 * @return {Ext.tree.TreeLoader} The TreeLoader for this TreePanel.
\r
585 getLoader : function(){
\r
586 return this.loader;
\r
592 expandAll : function(){
\r
593 this.root.expand(true);
\r
597 * Collapse all nodes
\r
599 collapseAll : function(){
\r
600 this.root.collapse(true);
\r
604 * Returns the selection model used by this TreePanel.
\r
605 * @return {TreeSelectionModel} The selection model used by this TreePanel
\r
607 getSelectionModel : function(){
\r
608 if(!this.selModel){
\r
609 this.selModel = new Ext.tree.DefaultSelectionModel();
\r
611 return this.selModel;
\r
615 * Expands a specified path in this TreePanel. A path can be retrieved from a node with {@link Ext.data.Node#getPath}
\r
616 * @param {String} path
\r
617 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
\r
618 * @param {Function} callback (optional) The callback to call when the expand is complete. The callback will be called with
\r
619 * (bSuccess, oLastNode) where bSuccess is if the expand was successful and oLastNode is the last node that was expanded.
\r
621 expandPath : function(path, attr, callback){
\r
622 attr = attr || "id";
\r
623 var keys = path.split(this.pathSeparator);
\r
624 var curNode = this.root;
\r
625 if(curNode.attributes[attr] != keys[1]){ // invalid root
\r
627 callback(false, null);
\r
632 var f = function(){
\r
633 if(++index == keys.length){
\r
635 callback(true, curNode);
\r
639 var c = curNode.findChild(attr, keys[index]);
\r
642 callback(false, curNode);
\r
647 c.expand(false, false, f);
\r
649 curNode.expand(false, false, f);
\r
653 * 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
654 * @param {String} path
\r
655 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
\r
656 * @param {Function} callback (optional) The callback to call when the selection is complete. The callback will be called with
\r
657 * (bSuccess, oSelNode) where bSuccess is if the selection was successful and oSelNode is the selected node.
\r
659 selectPath : function(path, attr, callback){
\r
660 attr = attr || "id";
\r
661 var keys = path.split(this.pathSeparator),
\r
663 if(keys.length > 1){
\r
664 var f = function(success, node){
\r
665 if(success && node){
\r
666 var n = node.findChild(attr, v);
\r
672 }else if(callback){
\r
673 callback(false, n);
\r
677 callback(false, n);
\r
681 this.expandPath(keys.join(this.pathSeparator), attr, f);
\r
683 this.root.select();
\r
685 callback(true, this.root);
\r
691 * Returns the underlying Element for this tree
\r
692 * @return {Ext.Element} The Element
\r
694 getTreeEl : function(){
\r
699 onRender : function(ct, position){
\r
700 Ext.tree.TreePanel.superclass.onRender.call(this, ct, position);
\r
701 this.el.addClass('x-tree');
\r
702 this.innerCt = this.body.createChild({tag:"ul",
\r
703 cls:"x-tree-root-ct " +
\r
704 (this.useArrows ? 'x-tree-arrows' : this.lines ? "x-tree-lines" : "x-tree-no-lines")});
\r
708 initEvents : function(){
\r
709 Ext.tree.TreePanel.superclass.initEvents.call(this);
\r
711 if(this.containerScroll){
\r
712 Ext.dd.ScrollManager.register(this.body);
\r
714 if((this.enableDD || this.enableDrop) && !this.dropZone){
\r
716 * The dropZone used by this tree if drop is enabled (see {@link #enableDD} or {@link #enableDrop})
\r
717 * @property dropZone
\r
718 * @type Ext.tree.TreeDropZone
\r
720 this.dropZone = new Ext.tree.TreeDropZone(this, this.dropConfig || {
\r
721 ddGroup: this.ddGroup || "TreeDD", appendOnly: this.ddAppendOnly === true
\r
724 if((this.enableDD || this.enableDrag) && !this.dragZone){
\r
726 * The dragZone used by this tree if drag is enabled (see {@link #enableDD} or {@link #enableDrag})
\r
727 * @property dragZone
\r
728 * @type Ext.tree.TreeDragZone
\r
730 this.dragZone = new Ext.tree.TreeDragZone(this, this.dragConfig || {
\r
731 ddGroup: this.ddGroup || "TreeDD",
\r
732 scroll: this.ddScroll
\r
735 this.getSelectionModel().init(this);
\r
739 afterRender : function(){
\r
740 Ext.tree.TreePanel.superclass.afterRender.call(this);
\r
741 this.root.render();
\r
742 if(!this.rootVisible){
\r
743 this.root.renderChildren();
\r
747 onDestroy : function(){
\r
749 this.body.removeAllListeners();
\r
750 Ext.dd.ScrollManager.unregister(this.body);
\r
752 this.dropZone.unreg();
\r
755 this.dragZone.unreg();
\r
758 this.root.destroy();
\r
759 this.nodeHash = null;
\r
760 Ext.tree.TreePanel.superclass.onDestroy.call(this);
\r
764 * @cfg {String/Number} activeItem
\r
768 * @cfg {Boolean} autoDestroy
\r
772 * @cfg {Object/String/Function} autoLoad
\r
776 * @cfg {Boolean} autoWidth
\r
780 * @cfg {Boolean/Number} bufferResize
\r
784 * @cfg {String} defaultType
\r
788 * @cfg {Object} defaults
\r
792 * @cfg {Boolean} hideBorders
\r
796 * @cfg {Mixed} items
\r
800 * @cfg {String} layout
\r
804 * @cfg {Object} layoutConfig
\r
808 * @cfg {Boolean} monitorResize
\r
836 * @method findByType
\r
840 * @method getComponent
\r
844 * @method getLayout
\r
848 * @method getUpdater
\r
868 * @method removeAll
\r
872 * @event afterLayout
\r
880 * @event beforeremove
\r
891 * @cfg {String} allowDomMove @hide
\r
894 * @cfg {String} autoEl @hide
\r
897 * @cfg {String} applyTo @hide
\r
900 * @cfg {String} contentEl @hide
\r
903 * @cfg {String} disabledClass @hide
\r
906 * @cfg {String} elements @hide
\r
909 * @cfg {String} html @hide
\r
912 * @cfg {Boolean} preventBodyReset
\r
916 * @property disabled
\r
920 * @method applyToMarkup
\r
932 * @method setDisabled
\r
937 Ext.tree.TreePanel.nodeTypes = {};
\r
939 Ext.reg('treepanel', Ext.tree.TreePanel);</pre>