3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.tree.TreePanel
10 * <p>The TreePanel provides tree-structured UI representation of tree-structured data.</p>
11 * <p>{@link Ext.tree.TreeNode TreeNode}s added to the TreePanel may each contain metadata
12 * used by your application in their {@link Ext.tree.TreeNode#attributes attributes} property.</p>
13 * <p><b>A TreePanel must have a {@link #root} node before it is rendered.</b> This may either be
14 * specified using the {@link #root} config option, or using the {@link #setRootNode} method.
15 * <p>An example of tree rendered to an existing div:</p><pre><code>
16 var tree = new Ext.tree.TreePanel({
22 containerScroll: true,
24 // auto create TreeLoader
25 dataUrl: 'get-nodes.php',
35 tree.getRootNode().expand();
37 * <p>The example above would work with a data packet similar to this:</p><pre><code>
40 "id": "source\/adapter",
48 "id": "source\/debug.js",
53 * <p>An example of tree within a Viewport:</p><pre><code>
64 loader: new Ext.tree.TreeLoader(),
65 root: new Ext.tree.AsyncTreeNode({
68 text: 'Menu Option 1',
71 text: 'Menu Option 2',
74 text: 'Menu Option 3',
81 Ext.Msg.alert('Navigation Tree Click', 'You clicked: "' + n.attributes.text + '"');
87 // remaining code not shown ...
92 * @cfg {Ext.tree.TreeNode} root The root node for the tree.
93 * @cfg {Boolean} rootVisible <tt>false</tt> to hide the root node (defaults to <tt>true</tt>)
94 * @cfg {Boolean} lines <tt>false</tt> to disable tree lines (defaults to <tt>true</tt>)
95 * @cfg {Boolean} enableDD <tt>true</tt> to enable drag and drop
96 * @cfg {Boolean} enableDrag <tt>true</tt> to enable just drag
97 * @cfg {Boolean} enableDrop <tt>true</tt> to enable just drop
98 * @cfg {Object} dragConfig Custom config to pass to the {@link Ext.tree.TreeDragZone} instance
99 * @cfg {Object} dropConfig Custom config to pass to the {@link Ext.tree.TreeDropZone} instance
100 * @cfg {String} ddGroup The DD group this TreePanel belongs to
101 * @cfg {Boolean} ddAppendOnly <tt>true</tt> if the tree should only allow append drops (use for trees which are sorted)
102 * @cfg {Boolean} ddScroll <tt>true</tt> to enable body scrolling
103 * @cfg {Boolean} containerScroll <tt>true</tt> to register this container with ScrollManager
104 * @cfg {Boolean} hlDrop <tt>false</tt> to disable node highlight on drop (defaults to the value of {@link Ext#enableFx})
105 * @cfg {String} hlColor The color of the node highlight (defaults to <tt>'C3DAF9'</tt>)
106 * @cfg {Boolean} animate <tt>true</tt> to enable animated expand/collapse (defaults to the value of {@link Ext#enableFx})
107 * @cfg {Boolean} singleExpand <tt>true</tt> if only 1 node per branch may be expanded
108 * @cfg {Object} selModel A tree selection model to use with this TreePanel (defaults to an {@link Ext.tree.DefaultSelectionModel})
109 * @cfg {Boolean} trackMouseOver <tt>false</tt> to disable mouse over highlighting
110 * @cfg {Ext.tree.TreeLoader} loader A {@link Ext.tree.TreeLoader} for use with this TreePanel
111 * @cfg {String} pathSeparator The token used to separate sub-paths in path strings (defaults to <tt>'/'</tt>)
112 * @cfg {Boolean} useArrows <tt>true</tt> to use Vista-style arrows in the tree (defaults to <tt>false</tt>)
113 * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
116 * @param {Object} config
119 Ext.tree.TreePanel = Ext.extend(Ext.Panel, {
121 animate : Ext.enableFx,
124 hlDrop : Ext.enableFx,
128 * @cfg {Array} bubbleEvents
129 * <p>An array of events that, when fired, should be bubbled to any parent container.
130 * See {@link Ext.util.Observable#enableBubble}.
131 * Defaults to <tt>[]</tt>.
135 initComponent : function(){
136 Ext.tree.TreePanel.superclass.initComponent.call(this);
138 if(!this.eventModel){
139 this.eventModel = new Ext.tree.TreeEventModel(this);
142 // initialize the loader
145 l = new Ext.tree.TreeLoader({
146 dataUrl: this.dataUrl,
147 requestMethod: this.requestMethod
149 }else if(Ext.isObject(l) && !l.load){
150 l = new Ext.tree.TreeLoader(l);
157 * The root node of this tree.
158 * @type Ext.tree.TreeNode
172 * Fires when a new child node is appended to a node in this tree.
173 * @param {Tree} tree The owner tree
174 * @param {Node} parent The parent node
175 * @param {Node} node The newly appended node
176 * @param {Number} index The index of the newly appended node
181 * Fires when a child node is removed from a node in this tree.
182 * @param {Tree} tree The owner tree
183 * @param {Node} parent The parent node
184 * @param {Node} node The child node removed
189 * Fires when a node is moved to a new location in the tree
190 * @param {Tree} tree The owner tree
191 * @param {Node} node The node moved
192 * @param {Node} oldParent The old parent of this node
193 * @param {Node} newParent The new parent of this node
194 * @param {Number} index The index it was moved to
199 * Fires when a new child node is inserted in a node in this tree.
200 * @param {Tree} tree The owner tree
201 * @param {Node} parent The parent node
202 * @param {Node} node The child node inserted
203 * @param {Node} refNode The child node the node was inserted before
207 * @event beforeappend
208 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
209 * @param {Tree} tree The owner tree
210 * @param {Node} parent The parent node
211 * @param {Node} node The child node to be appended
215 * @event beforeremove
216 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
217 * @param {Tree} tree The owner tree
218 * @param {Node} parent The parent node
219 * @param {Node} node The child node to be removed
223 * @event beforemovenode
224 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
225 * @param {Tree} tree The owner tree
226 * @param {Node} node The node being moved
227 * @param {Node} oldParent The parent of the node
228 * @param {Node} newParent The new parent the node is moving to
229 * @param {Number} index The index it is being moved to
233 * @event beforeinsert
234 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
235 * @param {Tree} tree The owner tree
236 * @param {Node} parent The parent node
237 * @param {Node} node The child node to be inserted
238 * @param {Node} refNode The child node the node is being inserted before
244 * Fires before a node is loaded, return false to cancel
245 * @param {Node} node The node being loaded
250 * Fires when a node is loaded
251 * @param {Node} node The node that was loaded
256 * Fires when the text for a node is changed
257 * @param {Node} node The node
258 * @param {String} text The new text
259 * @param {String} oldText The old text
263 * @event beforeexpandnode
264 * Fires before a node is expanded, return false to cancel.
265 * @param {Node} node The node
266 * @param {Boolean} deep
267 * @param {Boolean} anim
271 * @event beforecollapsenode
272 * Fires before a node is collapsed, return false to cancel.
273 * @param {Node} node The node
274 * @param {Boolean} deep
275 * @param {Boolean} anim
277 'beforecollapsenode',
280 * Fires when a node is expanded
281 * @param {Node} node The node
285 * @event disabledchange
286 * Fires when the disabled status of a node changes
287 * @param {Node} node The node
288 * @param {Boolean} disabled
292 * @event collapsenode
293 * Fires when a node is collapsed
294 * @param {Node} node The node
299 * Fires before click processing on a node. Return false to cancel the default action.
300 * @param {Node} node The node
301 * @param {Ext.EventObject} e The event object
306 * Fires when a node is clicked
307 * @param {Node} node The node
308 * @param {Ext.EventObject} e The event object
312 * @event containerclick
313 * Fires when the tree container is clicked
315 * @param {Ext.EventObject} e The event object
320 * Fires when a node with a checkbox's checked property changes
321 * @param {Node} this This node
322 * @param {Boolean} checked
326 * @event beforedblclick
327 * Fires before double click processing on a node. Return false to cancel the default action.
328 * @param {Node} node The node
329 * @param {Ext.EventObject} e The event object
334 * Fires when a node is double clicked
335 * @param {Node} node The node
336 * @param {Ext.EventObject} e The event object
340 * @event containerdblclick
341 * Fires when the tree container is double clicked
343 * @param {Ext.EventObject} e The event object
348 * Fires when a node is right clicked. To display a context menu in response to this
349 * event, first create a Menu object (see {@link Ext.menu.Menu} for details), then add
350 * a handler for this event:<pre><code>
351 new Ext.tree.TreePanel({
352 title: 'My TreePanel',
353 root: new Ext.tree.AsyncTreeNode({
356 { text: 'Child node 1', leaf: true },
357 { text: 'Child node 2', leaf: true }
360 contextMenu: new Ext.menu.Menu({
366 itemclick: function(item) {
369 var n = item.parentMenu.contextNode;
379 contextmenu: function(node, e) {
380 // Register the context node with the menu so that a Menu Item's handler function can access
381 // it via its {@link Ext.menu.BaseItem#parentMenu parentMenu} property.
383 var c = node.getOwnerTree().contextMenu;
384 c.contextNode = node;
390 * @param {Node} node The node
391 * @param {Ext.EventObject} e The event object
395 * @event containercontextmenu
396 * Fires when the tree container is right clicked
398 * @param {Ext.EventObject} e The event object
400 'containercontextmenu',
402 * @event beforechildrenrendered
403 * Fires right before the child nodes for a node are rendered
404 * @param {Node} node The node
406 'beforechildrenrendered',
409 * Fires when a node starts being dragged
410 * @param {Ext.tree.TreePanel} this
411 * @param {Ext.tree.TreeNode} node
412 * @param {event} e The raw browser event
417 * Fires when a drag operation is complete
418 * @param {Ext.tree.TreePanel} this
419 * @param {Ext.tree.TreeNode} node
420 * @param {event} e The raw browser event
425 * Fires when a dragged node is dropped on a valid DD target
426 * @param {Ext.tree.TreePanel} this
427 * @param {Ext.tree.TreeNode} node
428 * @param {DD} dd The dd it was dropped on
429 * @param {event} e The raw browser event
433 * @event beforenodedrop
434 * Fires when a DD object is dropped on a node in this tree for preprocessing. Return false to cancel the drop. The dropEvent
435 * passed to handlers has the following properties:<br />
436 * <ul style="padding:5px;padding-left:16px;">
437 * <li>tree - The TreePanel</li>
438 * <li>target - The node being targeted for the drop</li>
439 * <li>data - The drag data from the drag source</li>
440 * <li>point - The point of the drop - append, above or below</li>
441 * <li>source - The drag source</li>
442 * <li>rawEvent - Raw mouse event</li>
443 * <li>dropNode - Drop node(s) provided by the source <b>OR</b> you can supply node(s)
444 * to be inserted by setting them on this object.</li>
445 * <li>cancel - Set this to true to cancel the drop.</li>
446 * <li>dropStatus - If the default drop action is cancelled but the drop is valid, setting this to true
447 * will prevent the animated 'repair' from appearing.</li>
449 * @param {Object} dropEvent
454 * Fires after a DD object is dropped on a node in this tree. The dropEvent
455 * passed to handlers has the following properties:<br />
456 * <ul style="padding:5px;padding-left:16px;">
457 * <li>tree - The TreePanel</li>
458 * <li>target - The node being targeted for the drop</li>
459 * <li>data - The drag data from the drag source</li>
460 * <li>point - The point of the drop - append, above or below</li>
461 * <li>source - The drag source</li>
462 * <li>rawEvent - Raw mouse event</li>
463 * <li>dropNode - Dropped node(s).</li>
465 * @param {Object} dropEvent
469 * @event nodedragover
470 * Fires when a tree node is being targeted for a drag drop, return false to signal drop not allowed. The dragOverEvent
471 * passed to handlers has the following properties:<br />
472 * <ul style="padding:5px;padding-left:16px;">
473 * <li>tree - The TreePanel</li>
474 * <li>target - The node being targeted for the drop</li>
475 * <li>data - The drag data from the drag source</li>
476 * <li>point - The point of the drop - append, above or below</li>
477 * <li>source - The drag source</li>
478 * <li>rawEvent - Raw mouse event</li>
479 * <li>dropNode - Drop node(s) provided by the source.</li>
480 * <li>cancel - Set this to true to signal drop not allowed.</li>
482 * @param {Object} dragOverEvent
486 if(this.singleExpand){
487 this.on('beforeexpandnode', this.restrictExpand, this);
492 proxyNodeEvent : function(ename, a1, a2, a3, a4, a5, a6){
493 if(ename == 'collapse' || ename == 'expand' || ename == 'beforecollapse' || ename == 'beforeexpand' || ename == 'move' || ename == 'beforemove'){
494 ename = ename+'node';
496 // args inline for performance while bubbling events
497 return this.fireEvent(ename, a1, a2, a3, a4, a5, a6);
502 * Returns this root node for this tree
505 getRootNode : function(){
510 * Sets the root node for this tree. If the TreePanel has already rendered a root node, the
511 * previous root node (and all of its descendants) are destroyed before the new root node is rendered.
515 setRootNode : function(node){
517 if(!node.render){ // attributes passed
518 node = this.loader.createNode(node);
521 node.ownerTree = this;
523 this.registerNode(node);
524 if(!this.rootVisible){
525 var uiP = node.attributes.uiProvider;
526 node.ui = uiP ? new uiP(node) : new Ext.tree.RootTreeNodeUI(node);
535 clearInnerCt : function(){
536 this.innerCt.update('');
540 renderRoot : function(){
542 if(!this.rootVisible){
543 this.root.renderChildren();
548 * Gets a node in this tree by its id
552 getNodeById : function(id){
553 return this.nodeHash[id];
557 registerNode : function(node){
558 this.nodeHash[node.id] = node;
562 unregisterNode : function(node){
563 delete this.nodeHash[node.id];
567 toString : function(){
568 return '[Tree'+(this.id?' '+this.id:'')+']';
572 restrictExpand : function(node){
573 var p = node.parentNode;
575 if(p.expandedChild && p.expandedChild.parentNode == p){
576 p.expandedChild.collapse();
578 p.expandedChild = node;
583 * Retrieve an array of checked nodes, or an array of a specific attribute of checked nodes (e.g. 'id')
584 * @param {String} attribute (optional) Defaults to null (return the actual nodes)
585 * @param {TreeNode} startNode (optional) The node to start from, defaults to the root
588 getChecked : function(a, startNode){
589 startNode = startNode || this.root;
592 if(this.attributes.checked){
593 r.push(!a ? this : (a == 'id' ? this.id : this.attributes[a]));
596 startNode.cascade(f);
601 * Returns the default {@link Ext.tree.TreeLoader} for this TreePanel.
602 * @return {Ext.tree.TreeLoader} The TreeLoader for this TreePanel.
604 getLoader : function(){
611 expandAll : function(){
612 this.root.expand(true);
618 collapseAll : function(){
619 this.root.collapse(true);
623 * Returns the selection model used by this TreePanel.
624 * @return {TreeSelectionModel} The selection model used by this TreePanel
626 getSelectionModel : function(){
628 this.selModel = new Ext.tree.DefaultSelectionModel();
630 return this.selModel;
634 * Expands a specified path in this TreePanel. A path can be retrieved from a node with {@link Ext.data.Node#getPath}
635 * @param {String} path
636 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
637 * @param {Function} callback (optional) The callback to call when the expand is complete. The callback will be called with
638 * (bSuccess, oLastNode) where bSuccess is if the expand was successful and oLastNode is the last node that was expanded.
640 expandPath : function(path, attr, callback){
641 if(Ext.isEmpty(path)){
643 callback(false, undefined);
648 var keys = path.split(this.pathSeparator);
649 var curNode = this.root;
650 if(curNode.attributes[attr] != keys[1]){ // invalid root
652 callback(false, null);
658 if(++index == keys.length){
660 callback(true, curNode);
664 var c = curNode.findChild(attr, keys[index]);
667 callback(false, curNode);
672 c.expand(false, false, f);
674 curNode.expand(false, false, f);
678 * Selects the node in this tree at the specified path. A path can be retrieved from a node with {@link Ext.data.Node#getPath}
679 * @param {String} path
680 * @param {String} attr (optional) The attribute used in the path (see {@link Ext.data.Node#getPath} for more info)
681 * @param {Function} callback (optional) The callback to call when the selection is complete. The callback will be called with
682 * (bSuccess, oSelNode) where bSuccess is if the selection was successful and oSelNode is the selected node.
684 selectPath : function(path, attr, callback){
685 if(Ext.isEmpty(path)){
687 callback(false, undefined);
692 var keys = path.split(this.pathSeparator),
695 var f = function(success, node){
697 var n = node.findChild(attr, v);
712 this.expandPath(keys.join(this.pathSeparator), attr, f);
716 callback(true, this.root);
722 * Returns the underlying Element for this tree
723 * @return {Ext.Element} The Element
725 getTreeEl : function(){
730 onRender : function(ct, position){
731 Ext.tree.TreePanel.superclass.onRender.call(this, ct, position);
732 this.el.addClass('x-tree');
733 this.innerCt = this.body.createChild({tag:'ul',
734 cls:'x-tree-root-ct ' +
735 (this.useArrows ? 'x-tree-arrows' : this.lines ? 'x-tree-lines' : 'x-tree-no-lines')});
739 initEvents : function(){
740 Ext.tree.TreePanel.superclass.initEvents.call(this);
742 if(this.containerScroll){
743 Ext.dd.ScrollManager.register(this.body);
745 if((this.enableDD || this.enableDrop) && !this.dropZone){
747 * The dropZone used by this tree if drop is enabled (see {@link #enableDD} or {@link #enableDrop})
749 * @type Ext.tree.TreeDropZone
751 this.dropZone = new Ext.tree.TreeDropZone(this, this.dropConfig || {
752 ddGroup: this.ddGroup || 'TreeDD', appendOnly: this.ddAppendOnly === true
755 if((this.enableDD || this.enableDrag) && !this.dragZone){
757 * The dragZone used by this tree if drag is enabled (see {@link #enableDD} or {@link #enableDrag})
759 * @type Ext.tree.TreeDragZone
761 this.dragZone = new Ext.tree.TreeDragZone(this, this.dragConfig || {
762 ddGroup: this.ddGroup || 'TreeDD',
763 scroll: this.ddScroll
766 this.getSelectionModel().init(this);
770 afterRender : function(){
771 Ext.tree.TreePanel.superclass.afterRender.call(this);
775 beforeDestroy : function(){
777 Ext.dd.ScrollManager.unregister(this.body);
778 Ext.destroy(this.dropZone, this.dragZone);
781 Ext.destroy(this.loader);
782 this.nodeHash = this.root = this.loader = null;
783 Ext.tree.TreePanel.superclass.beforeDestroy.call(this);
787 * Destroy the root node. Not included by itself because we need to pass the silent parameter.
790 destroyRoot : function(){
791 if(this.root && this.root.destroy){
792 this.root.destroy(true);
797 * @cfg {String/Number} activeItem
801 * @cfg {Boolean} autoDestroy
805 * @cfg {Object/String/Function} autoLoad
809 * @cfg {Boolean} autoWidth
813 * @cfg {Boolean/Number} bufferResize
817 * @cfg {String} defaultType
821 * @cfg {Object} defaults
825 * @cfg {Boolean} hideBorders
833 * @cfg {String} layout
837 * @cfg {Object} layoutConfig
841 * @cfg {Boolean} monitorResize
873 * @method getComponent
913 * @event beforeremove
924 * @cfg {String} allowDomMove @hide
927 * @cfg {String} autoEl @hide
930 * @cfg {String} applyTo @hide
933 * @cfg {String} contentEl @hide
936 * @cfg {Mixed} data @hide
939 * @cfg {Mixed} tpl @hide
942 * @cfg {String} tplWriteMode @hide
945 * @cfg {String} disabledClass @hide
948 * @cfg {String} elements @hide
951 * @cfg {String} html @hide
954 * @cfg {Boolean} preventBodyReset
962 * @method applyToMarkup
974 * @method setDisabled
979 Ext.tree.TreePanel.nodeTypes = {};
981 Ext.reg('treepanel', Ext.tree.TreePanel);Ext.tree.TreeEventModel = function(tree){
983 this.tree.on('render', this.initEvents, this);
986 Ext.tree.TreeEventModel.prototype = {
987 initEvents : function(){
990 if(t.trackMouseOver !== false){
993 mouseover: this.delegateOver,
994 mouseout: this.delegateOut
997 t.mon(t.getTreeEl(), {
999 click: this.delegateClick,
1000 dblclick: this.delegateDblClick,
1001 contextmenu: this.delegateContextMenu
1005 getNode : function(e){
1007 if(t = e.getTarget('.x-tree-node-el', 10)){
1008 var id = Ext.fly(t, '_treeEvents').getAttribute('tree-node-id', 'ext');
1010 return this.tree.getNodeById(id);
1016 getNodeTarget : function(e){
1017 var t = e.getTarget('.x-tree-node-icon', 1);
1019 t = e.getTarget('.x-tree-node-el', 6);
1024 delegateOut : function(e, t){
1025 if(!this.beforeEvent(e)){
1028 if(e.getTarget('.x-tree-ec-icon', 1)){
1029 var n = this.getNode(e);
1030 this.onIconOut(e, n);
1031 if(n == this.lastEcOver){
1032 delete this.lastEcOver;
1035 if((t = this.getNodeTarget(e)) && !e.within(t, true)){
1036 this.onNodeOut(e, this.getNode(e));
1040 delegateOver : function(e, t){
1041 if(!this.beforeEvent(e)){
1044 if(Ext.isGecko && !this.trackingDoc){ // prevent hanging in FF
1045 Ext.getBody().on('mouseover', this.trackExit, this);
1046 this.trackingDoc = true;
1048 if(this.lastEcOver){ // prevent hung highlight
1049 this.onIconOut(e, this.lastEcOver);
1050 delete this.lastEcOver;
1052 if(e.getTarget('.x-tree-ec-icon', 1)){
1053 this.lastEcOver = this.getNode(e);
1054 this.onIconOver(e, this.lastEcOver);
1056 if(t = this.getNodeTarget(e)){
1057 this.onNodeOver(e, this.getNode(e));
1061 trackExit : function(e){
1062 if(this.lastOverNode){
1063 if(this.lastOverNode.ui && !e.within(this.lastOverNode.ui.getEl())){
1064 this.onNodeOut(e, this.lastOverNode);
1066 delete this.lastOverNode;
1067 Ext.getBody().un('mouseover', this.trackExit, this);
1068 this.trackingDoc = false;
1073 delegateClick : function(e, t){
1074 if(this.beforeEvent(e)){
1075 if(e.getTarget('input[type=checkbox]', 1)){
1076 this.onCheckboxClick(e, this.getNode(e));
1077 }else if(e.getTarget('.x-tree-ec-icon', 1)){
1078 this.onIconClick(e, this.getNode(e));
1079 }else if(this.getNodeTarget(e)){
1080 this.onNodeClick(e, this.getNode(e));
1083 this.checkContainerEvent(e, 'click');
1087 delegateDblClick : function(e, t){
1088 if(this.beforeEvent(e)){
1089 if(this.getNodeTarget(e)){
1090 this.onNodeDblClick(e, this.getNode(e));
1093 this.checkContainerEvent(e, 'dblclick');
1097 delegateContextMenu : function(e, t){
1098 if(this.beforeEvent(e)){
1099 if(this.getNodeTarget(e)){
1100 this.onNodeContextMenu(e, this.getNode(e));
1103 this.checkContainerEvent(e, 'contextmenu');
1107 checkContainerEvent: function(e, type){
1112 this.onContainerEvent(e, type);
1115 onContainerEvent: function(e, type){
1116 this.tree.fireEvent('container' + type, this.tree, e);
1119 onNodeClick : function(e, node){
1123 onNodeOver : function(e, node){
1124 this.lastOverNode = node;
1128 onNodeOut : function(e, node){
1132 onIconOver : function(e, node){
1133 node.ui.addClass('x-tree-ec-over');
1136 onIconOut : function(e, node){
1137 node.ui.removeClass('x-tree-ec-over');
1140 onIconClick : function(e, node){
1144 onCheckboxClick : function(e, node){
1145 node.ui.onCheckChange(e);
1148 onNodeDblClick : function(e, node){
1149 node.ui.onDblClick(e);
1152 onNodeContextMenu : function(e, node){
1153 node.ui.onContextMenu(e);
1156 beforeEvent : function(e){
1157 var node = this.getNode(e);
1158 if(this.disabled || !node || !node.ui){
1165 disable: function(){
1166 this.disabled = true;
1170 this.disabled = false;
1173 * @class Ext.tree.DefaultSelectionModel
1174 * @extends Ext.util.Observable
1175 * The default single selection for a TreePanel.
1177 Ext.tree.DefaultSelectionModel = Ext.extend(Ext.util.Observable, {
1179 constructor : function(config){
1180 this.selNode = null;
1184 * @event selectionchange
1185 * Fires when the selected node changes
1186 * @param {DefaultSelectionModel} this
1187 * @param {TreeNode} node the new selection
1192 * @event beforeselect
1193 * Fires before the selected node changes, return false to cancel the change
1194 * @param {DefaultSelectionModel} this
1195 * @param {TreeNode} node the new selection
1196 * @param {TreeNode} node the old selection
1201 Ext.apply(this, config);
1202 Ext.tree.DefaultSelectionModel.superclass.constructor.call(this);
1205 init : function(tree){
1207 tree.mon(tree.getTreeEl(), 'keydown', this.onKeyDown, this);
1208 tree.on('click', this.onNodeClick, this);
1211 onNodeClick : function(node, e){
1217 * @param {TreeNode} node The node to select
1218 * @return {TreeNode} The selected node
1220 select : function(node, /* private*/ selectNextNode){
1221 // If node is hidden, select the next node in whatever direction was being moved in.
1222 if (!Ext.fly(node.ui.wrap).isVisible() && selectNextNode) {
1223 return selectNextNode.call(this, node);
1225 var last = this.selNode;
1227 node.ui.onSelectedChange(true);
1228 }else if(this.fireEvent('beforeselect', this, node, last) !== false){
1229 if(last && last.ui){
1230 last.ui.onSelectedChange(false);
1232 this.selNode = node;
1233 node.ui.onSelectedChange(true);
1234 this.fireEvent('selectionchange', this, node, last);
1241 * @param {TreeNode} node The node to unselect
1242 * @param {Boolean} silent True to stop the selectionchange event from firing.
1244 unselect : function(node, silent){
1245 if(this.selNode == node){
1246 this.clearSelections(silent);
1251 * Clear all selections
1252 * @param {Boolean} silent True to stop the selectionchange event from firing.
1254 clearSelections : function(silent){
1255 var n = this.selNode;
1257 n.ui.onSelectedChange(false);
1258 this.selNode = null;
1259 if(silent !== true){
1260 this.fireEvent('selectionchange', this, null);
1267 * Get the selected node
1268 * @return {TreeNode} The selected node
1270 getSelectedNode : function(){
1271 return this.selNode;
1275 * Returns true if the node is selected
1276 * @param {TreeNode} node The node to check
1279 isSelected : function(node){
1280 return this.selNode == node;
1284 * Selects the node above the selected node in the tree, intelligently walking the nodes
1285 * @return TreeNode The new selection
1287 selectPrevious : function(/* private */ s){
1288 if(!(s = s || this.selNode || this.lastSelNode)){
1291 // Here we pass in the current function to select to indicate the direction we're moving
1292 var ps = s.previousSibling;
1294 if(!ps.isExpanded() || ps.childNodes.length < 1){
1295 return this.select(ps, this.selectPrevious);
1297 var lc = ps.lastChild;
1298 while(lc && lc.isExpanded() && Ext.fly(lc.ui.wrap).isVisible() && lc.childNodes.length > 0){
1301 return this.select(lc, this.selectPrevious);
1303 } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
1304 return this.select(s.parentNode, this.selectPrevious);
1310 * Selects the node above the selected node in the tree, intelligently walking the nodes
1311 * @return TreeNode The new selection
1313 selectNext : function(/* private */ s){
1314 if(!(s = s || this.selNode || this.lastSelNode)){
1317 // Here we pass in the current function to select to indicate the direction we're moving
1318 if(s.firstChild && s.isExpanded() && Ext.fly(s.ui.wrap).isVisible()){
1319 return this.select(s.firstChild, this.selectNext);
1320 }else if(s.nextSibling){
1321 return this.select(s.nextSibling, this.selectNext);
1322 }else if(s.parentNode){
1324 s.parentNode.bubble(function(){
1325 if(this.nextSibling){
1326 newS = this.getOwnerTree().selModel.select(this.nextSibling, this.selectNext);
1335 onKeyDown : function(e){
1336 var s = this.selNode || this.lastSelNode;
1337 // undesirable, but required
1350 this.selectPrevious();
1354 if(s.hasChildNodes()){
1355 if(!s.isExpanded()){
1357 }else if(s.firstChild){
1358 this.select(s.firstChild, e);
1364 if(s.hasChildNodes() && s.isExpanded()){
1366 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
1367 this.select(s.parentNode, e);
1375 * @class Ext.tree.MultiSelectionModel
1376 * @extends Ext.util.Observable
1377 * Multi selection for a TreePanel.
1379 Ext.tree.MultiSelectionModel = Ext.extend(Ext.util.Observable, {
1381 constructor : function(config){
1386 * @event selectionchange
1387 * Fires when the selected nodes change
1388 * @param {MultiSelectionModel} this
1389 * @param {Array} nodes Array of the selected nodes
1393 Ext.apply(this, config);
1394 Ext.tree.MultiSelectionModel.superclass.constructor.call(this);
1397 init : function(tree){
1399 tree.mon(tree.getTreeEl(), 'keydown', this.onKeyDown, this);
1400 tree.on('click', this.onNodeClick, this);
1403 onNodeClick : function(node, e){
1404 if(e.ctrlKey && this.isSelected(node)){
1405 this.unselect(node);
1407 this.select(node, e, e.ctrlKey);
1413 * @param {TreeNode} node The node to select
1414 * @param {EventObject} e (optional) An event associated with the selection
1415 * @param {Boolean} keepExisting True to retain existing selections
1416 * @return {TreeNode} The selected node
1418 select : function(node, e, keepExisting){
1419 if(keepExisting !== true){
1420 this.clearSelections(true);
1422 if(this.isSelected(node)){
1423 this.lastSelNode = node;
1426 this.selNodes.push(node);
1427 this.selMap[node.id] = node;
1428 this.lastSelNode = node;
1429 node.ui.onSelectedChange(true);
1430 this.fireEvent('selectionchange', this, this.selNodes);
1436 * @param {TreeNode} node The node to unselect
1438 unselect : function(node){
1439 if(this.selMap[node.id]){
1440 node.ui.onSelectedChange(false);
1441 var sn = this.selNodes;
1442 var index = sn.indexOf(node);
1444 this.selNodes.splice(index, 1);
1446 delete this.selMap[node.id];
1447 this.fireEvent('selectionchange', this, this.selNodes);
1452 * Clear all selections
1454 clearSelections : function(suppressEvent){
1455 var sn = this.selNodes;
1457 for(var i = 0, len = sn.length; i < len; i++){
1458 sn[i].ui.onSelectedChange(false);
1462 if(suppressEvent !== true){
1463 this.fireEvent('selectionchange', this, this.selNodes);
1469 * Returns true if the node is selected
1470 * @param {TreeNode} node The node to check
1473 isSelected : function(node){
1474 return this.selMap[node.id] ? true : false;
1478 * Returns an array of the selected nodes
1481 getSelectedNodes : function(){
1482 return this.selNodes.concat([]);
1485 onKeyDown : Ext.tree.DefaultSelectionModel.prototype.onKeyDown,
1487 selectNext : Ext.tree.DefaultSelectionModel.prototype.selectNext,
1489 selectPrevious : Ext.tree.DefaultSelectionModel.prototype.selectPrevious
1491 * @class Ext.data.Tree
1492 * @extends Ext.util.Observable
1493 * Represents a tree data structure and bubbles all the events for its nodes. The nodes
1494 * in the tree have most standard DOM functionality.
1496 * @param {Node} root (optional) The root node
1498 Ext.data.Tree = Ext.extend(Ext.util.Observable, {
1500 constructor: function(root){
1503 * The root node for this tree
1508 this.setRootNode(root);
1513 * Fires when a new child node is appended to a node in this tree.
1514 * @param {Tree} tree The owner tree
1515 * @param {Node} parent The parent node
1516 * @param {Node} node The newly appended node
1517 * @param {Number} index The index of the newly appended node
1522 * Fires when a child node is removed from a node in this tree.
1523 * @param {Tree} tree The owner tree
1524 * @param {Node} parent The parent node
1525 * @param {Node} node The child node removed
1530 * Fires when a node is moved to a new location in the tree
1531 * @param {Tree} tree The owner tree
1532 * @param {Node} node The node moved
1533 * @param {Node} oldParent The old parent of this node
1534 * @param {Node} newParent The new parent of this node
1535 * @param {Number} index The index it was moved to
1540 * Fires when a new child node is inserted in a node in this tree.
1541 * @param {Tree} tree The owner tree
1542 * @param {Node} parent The parent node
1543 * @param {Node} node The child node inserted
1544 * @param {Node} refNode The child node the node was inserted before
1548 * @event beforeappend
1549 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
1550 * @param {Tree} tree The owner tree
1551 * @param {Node} parent The parent node
1552 * @param {Node} node The child node to be appended
1556 * @event beforeremove
1557 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
1558 * @param {Tree} tree The owner tree
1559 * @param {Node} parent The parent node
1560 * @param {Node} node The child node to be removed
1565 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
1566 * @param {Tree} tree The owner tree
1567 * @param {Node} node The node being moved
1568 * @param {Node} oldParent The parent of the node
1569 * @param {Node} newParent The new parent the node is moving to
1570 * @param {Number} index The index it is being moved to
1574 * @event beforeinsert
1575 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
1576 * @param {Tree} tree The owner tree
1577 * @param {Node} parent The parent node
1578 * @param {Node} node The child node to be inserted
1579 * @param {Node} refNode The child node the node is being inserted before
1583 Ext.data.Tree.superclass.constructor.call(this);
1587 * @cfg {String} pathSeparator
1588 * The token used to separate paths in node ids (defaults to '/').
1593 proxyNodeEvent : function(){
1594 return this.fireEvent.apply(this, arguments);
1598 * Returns the root node for this tree.
1601 getRootNode : function(){
1606 * Sets the root node for this tree.
1607 * @param {Node} node
1610 setRootNode : function(node){
1612 node.ownerTree = this;
1614 this.registerNode(node);
1619 * Gets a node in this tree by its id.
1620 * @param {String} id
1623 getNodeById : function(id){
1624 return this.nodeHash[id];
1628 registerNode : function(node){
1629 this.nodeHash[node.id] = node;
1633 unregisterNode : function(node){
1634 delete this.nodeHash[node.id];
1637 toString : function(){
1638 return "[Tree"+(this.id?" "+this.id:"")+"]";
1643 * @class Ext.data.Node
1644 * @extends Ext.util.Observable
1645 * @cfg {Boolean} leaf true if this node is a leaf and does not have children
1646 * @cfg {String} id The id for this node. If one is not specified, one is generated.
1648 * @param {Object} attributes The attributes/config for the node
1650 Ext.data.Node = Ext.extend(Ext.util.Observable, {
1652 constructor: function(attributes){
1654 * The attributes supplied for the node. You can use this property to access any custom attributes you supplied.
1657 this.attributes = attributes || {};
1658 this.leaf = this.attributes.leaf;
1660 * The node id. @type String
1662 this.id = this.attributes.id;
1664 this.id = Ext.id(null, "xnode-");
1665 this.attributes.id = this.id;
1668 * All child nodes of this node. @type Array
1670 this.childNodes = [];
1672 * The parent node for this node. @type Node
1674 this.parentNode = null;
1676 * The first direct child node of this node, or null if this node has no child nodes. @type Node
1678 this.firstChild = null;
1680 * The last direct child node of this node, or null if this node has no child nodes. @type Node
1682 this.lastChild = null;
1684 * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node
1686 this.previousSibling = null;
1688 * The node immediately following this node in the tree, or null if there is no sibling node. @type Node
1690 this.nextSibling = null;
1695 * Fires when a new child node is appended
1696 * @param {Tree} tree The owner tree
1697 * @param {Node} this This node
1698 * @param {Node} node The newly appended node
1699 * @param {Number} index The index of the newly appended node
1704 * Fires when a child node is removed
1705 * @param {Tree} tree The owner tree
1706 * @param {Node} this This node
1707 * @param {Node} node The removed node
1712 * Fires when this node is moved to a new location in the tree
1713 * @param {Tree} tree The owner tree
1714 * @param {Node} this This node
1715 * @param {Node} oldParent The old parent of this node
1716 * @param {Node} newParent The new parent of this node
1717 * @param {Number} index The index it was moved to
1722 * Fires when a new child node is inserted.
1723 * @param {Tree} tree The owner tree
1724 * @param {Node} this This node
1725 * @param {Node} node The child node inserted
1726 * @param {Node} refNode The child node the node was inserted before
1730 * @event beforeappend
1731 * Fires before a new child is appended, return false to cancel the append.
1732 * @param {Tree} tree The owner tree
1733 * @param {Node} this This node
1734 * @param {Node} node The child node to be appended
1736 "beforeappend" : true,
1738 * @event beforeremove
1739 * Fires before a child is removed, return false to cancel the remove.
1740 * @param {Tree} tree The owner tree
1741 * @param {Node} this This node
1742 * @param {Node} node The child node to be removed
1744 "beforeremove" : true,
1747 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.
1748 * @param {Tree} tree The owner tree
1749 * @param {Node} this This node
1750 * @param {Node} oldParent The parent of this node
1751 * @param {Node} newParent The new parent this node is moving to
1752 * @param {Number} index The index it is being moved to
1754 "beforemove" : true,
1756 * @event beforeinsert
1757 * Fires before a new child is inserted, return false to cancel the insert.
1758 * @param {Tree} tree The owner tree
1759 * @param {Node} this This node
1760 * @param {Node} node The child node to be inserted
1761 * @param {Node} refNode The child node the node is being inserted before
1763 "beforeinsert" : true
1765 this.listeners = this.attributes.listeners;
1766 Ext.data.Node.superclass.constructor.call(this);
1770 fireEvent : function(evtName){
1771 // first do standard event for this node
1772 if(Ext.data.Node.superclass.fireEvent.apply(this, arguments) === false){
1775 // then bubble it up to the tree if the event wasn't cancelled
1776 var ot = this.getOwnerTree();
1778 if(ot.proxyNodeEvent.apply(ot, arguments) === false){
1786 * Returns true if this node is a leaf
1789 isLeaf : function(){
1790 return this.leaf === true;
1794 setFirstChild : function(node){
1795 this.firstChild = node;
1799 setLastChild : function(node){
1800 this.lastChild = node;
1805 * Returns true if this node is the last child of its parent
1808 isLast : function(){
1809 return (!this.parentNode ? true : this.parentNode.lastChild == this);
1813 * Returns true if this node is the first child of its parent
1816 isFirst : function(){
1817 return (!this.parentNode ? true : this.parentNode.firstChild == this);
1821 * Returns true if this node has one or more child nodes, else false.
1824 hasChildNodes : function(){
1825 return !this.isLeaf() && this.childNodes.length > 0;
1829 * Returns true if this node has one or more child nodes, or if the <tt>expandable</tt>
1830 * node attribute is explicitly specified as true (see {@link #attributes}), otherwise returns false.
1833 isExpandable : function(){
1834 return this.attributes.expandable || this.hasChildNodes();
1838 * Insert node(s) as the last child node of this node.
1839 * @param {Node/Array} node The node or Array of nodes to append
1840 * @return {Node} The appended node if single append, or null if an array was passed
1842 appendChild : function(node){
1844 if(Ext.isArray(node)){
1846 }else if(arguments.length > 1){
1849 // if passed an array or multiple args do them one by one
1851 for(var i = 0, len = multi.length; i < len; i++) {
1852 this.appendChild(multi[i]);
1855 if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){
1858 var index = this.childNodes.length;
1859 var oldParent = node.parentNode;
1860 // it's a move, make sure we move it cleanly
1862 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){
1865 oldParent.removeChild(node);
1867 index = this.childNodes.length;
1869 this.setFirstChild(node);
1871 this.childNodes.push(node);
1872 node.parentNode = this;
1873 var ps = this.childNodes[index-1];
1875 node.previousSibling = ps;
1876 ps.nextSibling = node;
1878 node.previousSibling = null;
1880 node.nextSibling = null;
1881 this.setLastChild(node);
1882 node.setOwnerTree(this.getOwnerTree());
1883 this.fireEvent("append", this.ownerTree, this, node, index);
1885 node.fireEvent("move", this.ownerTree, node, oldParent, this, index);
1892 * Removes a child node from this node.
1893 * @param {Node} node The node to remove
1894 * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.
1895 * @return {Node} The removed node
1897 removeChild : function(node, destroy){
1898 var index = this.childNodes.indexOf(node);
1902 if(this.fireEvent("beforeremove", this.ownerTree, this, node) === false){
1906 // remove it from childNodes collection
1907 this.childNodes.splice(index, 1);
1910 if(node.previousSibling){
1911 node.previousSibling.nextSibling = node.nextSibling;
1913 if(node.nextSibling){
1914 node.nextSibling.previousSibling = node.previousSibling;
1917 // update child refs
1918 if(this.firstChild == node){
1919 this.setFirstChild(node.nextSibling);
1921 if(this.lastChild == node){
1922 this.setLastChild(node.previousSibling);
1925 this.fireEvent("remove", this.ownerTree, this, node);
1935 clear : function(destroy){
1936 // clear any references from the node
1937 this.setOwnerTree(null, destroy);
1938 this.parentNode = this.previousSibling = this.nextSibling = null;
1940 this.firstChild = this.lastChild = null;
1945 * Destroys the node.
1947 destroy : function(/* private */ silent){
1949 * Silent is to be used in a number of cases
1950 * 1) When setRootNode is called.
1951 * 2) When destroy on the tree is called
1952 * 3) For destroying child nodes on a node
1954 if(silent === true){
1955 this.purgeListeners();
1957 Ext.each(this.childNodes, function(n){
1960 this.childNodes = null;
1967 * Inserts the first node before the second node in this nodes childNodes collection.
1968 * @param {Node} node The node to insert
1969 * @param {Node} refNode The node to insert before (if null the node is appended)
1970 * @return {Node} The inserted node
1972 insertBefore : function(node, refNode){
1973 if(!refNode){ // like standard Dom, refNode can be null for append
1974 return this.appendChild(node);
1977 if(node == refNode){
1981 if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){
1984 var index = this.childNodes.indexOf(refNode);
1985 var oldParent = node.parentNode;
1986 var refIndex = index;
1988 // when moving internally, indexes will change after remove
1989 if(oldParent == this && this.childNodes.indexOf(node) < index){
1993 // it's a move, make sure we move it cleanly
1995 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
1998 oldParent.removeChild(node);
2001 this.setFirstChild(node);
2003 this.childNodes.splice(refIndex, 0, node);
2004 node.parentNode = this;
2005 var ps = this.childNodes[refIndex-1];
2007 node.previousSibling = ps;
2008 ps.nextSibling = node;
2010 node.previousSibling = null;
2012 node.nextSibling = refNode;
2013 refNode.previousSibling = node;
2014 node.setOwnerTree(this.getOwnerTree());
2015 this.fireEvent("insert", this.ownerTree, this, node, refNode);
2017 node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);
2023 * Removes this node from its parent
2024 * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.
2025 * @return {Node} this
2027 remove : function(destroy){
2028 if (this.parentNode) {
2029 this.parentNode.removeChild(this, destroy);
2035 * Removes all child nodes from this node.
2036 * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.
2037 * @return {Node} this
2039 removeAll : function(destroy){
2040 var cn = this.childNodes,
2043 this.removeChild(n, destroy);
2049 * Returns the child node at the specified index.
2050 * @param {Number} index
2053 item : function(index){
2054 return this.childNodes[index];
2058 * Replaces one child node in this node with another.
2059 * @param {Node} newChild The replacement node
2060 * @param {Node} oldChild The node to replace
2061 * @return {Node} The replaced node
2063 replaceChild : function(newChild, oldChild){
2064 var s = oldChild ? oldChild.nextSibling : null;
2065 this.removeChild(oldChild);
2066 this.insertBefore(newChild, s);
2071 * Returns the index of a child node
2072 * @param {Node} node
2073 * @return {Number} The index of the node or -1 if it was not found
2075 indexOf : function(child){
2076 return this.childNodes.indexOf(child);
2080 * Returns the tree this node is in.
2083 getOwnerTree : function(){
2084 // if it doesn't have one, look for one
2085 if(!this.ownerTree){
2089 this.ownerTree = p.ownerTree;
2095 return this.ownerTree;
2099 * Returns depth of this node (the root node has a depth of 0)
2102 getDepth : function(){
2105 while(p.parentNode){
2113 setOwnerTree : function(tree, destroy){
2114 // if it is a move, we need to update everyone
2115 if(tree != this.ownerTree){
2117 this.ownerTree.unregisterNode(this);
2119 this.ownerTree = tree;
2120 // If we're destroying, we don't need to recurse since it will be called on each child node
2121 if(destroy !== true){
2122 Ext.each(this.childNodes, function(n){
2123 n.setOwnerTree(tree);
2127 tree.registerNode(this);
2133 * Changes the id of this node.
2134 * @param {String} id The new id for the node.
2136 setId: function(id){
2138 var t = this.ownerTree;
2140 t.unregisterNode(this);
2142 this.id = this.attributes.id = id;
2144 t.registerNode(this);
2146 this.onIdChange(id);
2151 onIdChange: Ext.emptyFn,
2154 * Returns the path for this node. The path can be used to expand or select this node programmatically.
2155 * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)
2156 * @return {String} The path
2158 getPath : function(attr){
2159 attr = attr || "id";
2160 var p = this.parentNode;
2161 var b = [this.attributes[attr]];
2163 b.unshift(p.attributes[attr]);
2166 var sep = this.getOwnerTree().pathSeparator;
2167 return sep + b.join(sep);
2171 * Bubbles up the tree from this node, calling the specified function with each node. The arguments to the function
2172 * will be the args provided or the current node. If the function returns false at any point,
2173 * the bubble is stopped.
2174 * @param {Function} fn The function to call
2175 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
2176 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
2178 bubble : function(fn, scope, args){
2181 if(fn.apply(scope || p, args || [p]) === false){
2189 * Cascades down the tree from this node, calling the specified function with each node. The arguments to the function
2190 * will be the args provided or the current node. If the function returns false at any point,
2191 * the cascade is stopped on that branch.
2192 * @param {Function} fn The function to call
2193 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
2194 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
2196 cascade : function(fn, scope, args){
2197 if(fn.apply(scope || this, args || [this]) !== false){
2198 var cs = this.childNodes;
2199 for(var i = 0, len = cs.length; i < len; i++) {
2200 cs[i].cascade(fn, scope, args);
2206 * Interates the child nodes of this node, calling the specified function with each node. The arguments to the function
2207 * will be the args provided or the current node. If the function returns false at any point,
2208 * the iteration stops.
2209 * @param {Function} fn The function to call
2210 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node in the iteration.
2211 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
2213 eachChild : function(fn, scope, args){
2214 var cs = this.childNodes;
2215 for(var i = 0, len = cs.length; i < len; i++) {
2216 if(fn.apply(scope || cs[i], args || [cs[i]]) === false){
2223 * Finds the first child that has the attribute with the specified value.
2224 * @param {String} attribute The attribute name
2225 * @param {Mixed} value The value to search for
2226 * @param {Boolean} deep (Optional) True to search through nodes deeper than the immediate children
2227 * @return {Node} The found child or null if none was found
2229 findChild : function(attribute, value, deep){
2230 return this.findChildBy(function(){
2231 return this.attributes[attribute] == value;
2236 * Finds the first child by a custom function. The child matches if the function passed returns <code>true</code>.
2237 * @param {Function} fn A function which must return <code>true</code> if the passed Node is the required Node.
2238 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the Node being tested.
2239 * @param {Boolean} deep (Optional) True to search through nodes deeper than the immediate children
2240 * @return {Node} The found child or null if none was found
2242 findChildBy : function(fn, scope, deep){
2243 var cs = this.childNodes,
2248 for(; i < len; i++){
2250 if(fn.call(scope || n, n) === true){
2253 res = n.findChildBy(fn, scope, deep);
2264 * Sorts this nodes children using the supplied sort function.
2265 * @param {Function} fn A function which, when passed two Nodes, returns -1, 0 or 1 depending upon required sort order.
2266 * @param {Object} scope (optional)The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
2268 sort : function(fn, scope){
2269 var cs = this.childNodes;
2270 var len = cs.length;
2272 var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
2274 for(var i = 0; i < len; i++){
2276 n.previousSibling = cs[i-1];
2277 n.nextSibling = cs[i+1];
2279 this.setFirstChild(n);
2282 this.setLastChild(n);
2289 * Returns true if this node is an ancestor (at any point) of the passed node.
2290 * @param {Node} node
2293 contains : function(node){
2294 return node.isAncestor(this);
2298 * Returns true if the passed node is an ancestor (at any point) of this node.
2299 * @param {Node} node
2302 isAncestor : function(node){
2303 var p = this.parentNode;
2313 toString : function(){
2314 return "[Node"+(this.id?" "+this.id:"")+"]";
2317 * @class Ext.tree.TreeNode
2318 * @extends Ext.data.Node
2319 * @cfg {String} text The text for this node
2320 * @cfg {Boolean} expanded true to start the node expanded
2321 * @cfg {Boolean} allowDrag False to make this node undraggable if {@link #draggable} = true (defaults to true)
2322 * @cfg {Boolean} allowDrop False if this node cannot have child nodes dropped on it (defaults to true)
2323 * @cfg {Boolean} disabled true to start the node disabled
2324 * @cfg {String} icon The path to an icon for the node. The preferred way to do this
2325 * is to use the cls or iconCls attributes and add the icon via a CSS background image.
2326 * @cfg {String} cls A css class to be added to the node
2327 * @cfg {String} iconCls A css class to be added to the nodes icon element for applying css background images
2328 * @cfg {String} href URL of the link used for the node (defaults to #)
2329 * @cfg {String} hrefTarget target frame for the link
2330 * @cfg {Boolean} hidden True to render hidden. (Defaults to false).
2331 * @cfg {String} qtip An Ext QuickTip for the node
2332 * @cfg {Boolean} expandable If set to true, the node will always show a plus/minus icon, even when empty
2333 * @cfg {String} qtipCfg An Ext QuickTip config for the node (used instead of qtip)
2334 * @cfg {Boolean} singleClickExpand True for single click expand on this node
2335 * @cfg {Function} uiProvider A UI <b>class</b> to use for this node (defaults to Ext.tree.TreeNodeUI)
2336 * @cfg {Boolean} checked True to render a checked checkbox for this node, false to render an unchecked checkbox
2337 * (defaults to undefined with no checkbox rendered)
2338 * @cfg {Boolean} draggable True to make this node draggable (defaults to false)
2339 * @cfg {Boolean} isTarget False to not allow this node to act as a drop target (defaults to true)
2340 * @cfg {Boolean} allowChildren False to not allow this node to have child nodes (defaults to true)
2341 * @cfg {Boolean} editable False to not allow this node to be edited by an {@link Ext.tree.TreeEditor} (defaults to true)
2343 * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node
2345 Ext.tree.TreeNode = Ext.extend(Ext.data.Node, {
2347 constructor : function(attributes){
2348 attributes = attributes || {};
2349 if(Ext.isString(attributes)){
2350 attributes = {text: attributes};
2352 this.childrenRendered = false;
2353 this.rendered = false;
2354 Ext.tree.TreeNode.superclass.constructor.call(this, attributes);
2355 this.expanded = attributes.expanded === true;
2356 this.isTarget = attributes.isTarget !== false;
2357 this.draggable = attributes.draggable !== false && attributes.allowDrag !== false;
2358 this.allowChildren = attributes.allowChildren !== false && attributes.allowDrop !== false;
2361 * Read-only. The text for this node. To change it use <code>{@link #setText}</code>.
2364 this.text = attributes.text;
2366 * True if this node is disabled.
2369 this.disabled = attributes.disabled === true;
2371 * True if this node is hidden.
2374 this.hidden = attributes.hidden === true;
2379 * Fires when the text for this node is changed
2380 * @param {Node} this This node
2381 * @param {String} text The new text
2382 * @param {String} oldText The old text
2386 * @event beforeexpand
2387 * Fires before this node is expanded, return false to cancel.
2388 * @param {Node} this This node
2389 * @param {Boolean} deep
2390 * @param {Boolean} anim
2394 * @event beforecollapse
2395 * Fires before this node is collapsed, return false to cancel.
2396 * @param {Node} this This node
2397 * @param {Boolean} deep
2398 * @param {Boolean} anim
2403 * Fires when this node is expanded
2404 * @param {Node} this This node
2408 * @event disabledchange
2409 * Fires when the disabled status of this node changes
2410 * @param {Node} this This node
2411 * @param {Boolean} disabled
2416 * Fires when this node is collapsed
2417 * @param {Node} this This node
2421 * @event beforeclick
2422 * Fires before click processing. Return false to cancel the default action.
2423 * @param {Node} this This node
2424 * @param {Ext.EventObject} e The event object
2429 * Fires when this node is clicked
2430 * @param {Node} this This node
2431 * @param {Ext.EventObject} e The event object
2435 * @event checkchange
2436 * Fires when a node with a checkbox's checked property changes
2437 * @param {Node} this This node
2438 * @param {Boolean} checked
2442 * @event beforedblclick
2443 * Fires before double click processing. Return false to cancel the default action.
2444 * @param {Node} this This node
2445 * @param {Ext.EventObject} e The event object
2450 * Fires when this node is double clicked
2451 * @param {Node} this This node
2452 * @param {Ext.EventObject} e The event object
2456 * @event contextmenu
2457 * Fires when this node is right clicked
2458 * @param {Node} this This node
2459 * @param {Ext.EventObject} e The event object
2463 * @event beforechildrenrendered
2464 * Fires right before the child nodes for this node are rendered
2465 * @param {Node} this This node
2467 'beforechildrenrendered'
2470 var uiClass = this.attributes.uiProvider || this.defaultUI || Ext.tree.TreeNodeUI;
2473 * Read-only. The UI for this node
2476 this.ui = new uiClass(this);
2479 preventHScroll : true,
2481 * Returns true if this node is expanded
2484 isExpanded : function(){
2485 return this.expanded;
2489 * Returns the UI object for this node.
2490 * @return {TreeNodeUI} The object which is providing the user interface for this tree
2491 * node. Unless otherwise specified in the {@link #uiProvider}, this will be an instance
2492 * of {@link Ext.tree.TreeNodeUI}
2498 getLoader : function(){
2500 return this.loader || ((owner = this.getOwnerTree()) && owner.loader ? owner.loader : (this.loader = new Ext.tree.TreeLoader()));
2504 setFirstChild : function(node){
2505 var of = this.firstChild;
2506 Ext.tree.TreeNode.superclass.setFirstChild.call(this, node);
2507 if(this.childrenRendered && of && node != of){
2508 of.renderIndent(true, true);
2511 this.renderIndent(true, true);
2516 setLastChild : function(node){
2517 var ol = this.lastChild;
2518 Ext.tree.TreeNode.superclass.setLastChild.call(this, node);
2519 if(this.childrenRendered && ol && node != ol){
2520 ol.renderIndent(true, true);
2523 this.renderIndent(true, true);
2527 // these methods are overridden to provide lazy rendering support
2529 appendChild : function(n){
2530 if(!n.render && !Ext.isArray(n)){
2531 n = this.getLoader().createNode(n);
2533 var node = Ext.tree.TreeNode.superclass.appendChild.call(this, n);
2534 if(node && this.childrenRendered){
2537 this.ui.updateExpandIcon();
2542 removeChild : function(node, destroy){
2543 this.ownerTree.getSelectionModel().unselect(node);
2544 Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments);
2545 // only update the ui if we're not destroying
2547 var rendered = node.ui.rendered;
2548 // if it's been rendered remove dom node
2552 if(rendered && this.childNodes.length < 1){
2553 this.collapse(false, false);
2555 this.ui.updateExpandIcon();
2557 if(!this.firstChild && !this.isHiddenRoot()){
2558 this.childrenRendered = false;
2565 insertBefore : function(node, refNode){
2567 node = this.getLoader().createNode(node);
2569 var newNode = Ext.tree.TreeNode.superclass.insertBefore.call(this, node, refNode);
2570 if(newNode && refNode && this.childrenRendered){
2573 this.ui.updateExpandIcon();
2578 * Sets the text for this node
2579 * @param {String} text
2581 setText : function(text){
2582 var oldText = this.text;
2583 this.text = this.attributes.text = text;
2584 if(this.rendered){ // event without subscribing
2585 this.ui.onTextChange(this, text, oldText);
2587 this.fireEvent('textchange', this, text, oldText);
2591 * Sets the icon class for this node.
2592 * @param {String} cls
2594 setIconCls : function(cls){
2595 var old = this.attributes.iconCls;
2596 this.attributes.iconCls = cls;
2598 this.ui.onIconClsChange(this, cls, old);
2603 * Sets the tooltip for this node.
2604 * @param {String} tip The text for the tip
2605 * @param {String} title (Optional) The title for the tip
2607 setTooltip : function(tip, title){
2608 this.attributes.qtip = tip;
2609 this.attributes.qtipTitle = title;
2611 this.ui.onTipChange(this, tip, title);
2616 * Sets the icon for this node.
2617 * @param {String} icon
2619 setIcon : function(icon){
2620 this.attributes.icon = icon;
2622 this.ui.onIconChange(this, icon);
2627 * Sets the href for the node.
2628 * @param {String} href The href to set
2629 * @param {String} (Optional) target The target of the href
2631 setHref : function(href, target){
2632 this.attributes.href = href;
2633 this.attributes.hrefTarget = target;
2635 this.ui.onHrefChange(this, href, target);
2640 * Sets the class on this node.
2641 * @param {String} cls
2643 setCls : function(cls){
2644 var old = this.attributes.cls;
2645 this.attributes.cls = cls;
2647 this.ui.onClsChange(this, cls, old);
2652 * Triggers selection of this node
2654 select : function(){
2655 var t = this.getOwnerTree();
2657 t.getSelectionModel().select(this);
2662 * Triggers deselection of this node
2663 * @param {Boolean} silent (optional) True to stop selection change events from firing.
2665 unselect : function(silent){
2666 var t = this.getOwnerTree();
2668 t.getSelectionModel().unselect(this, silent);
2673 * Returns true if this node is selected
2676 isSelected : function(){
2677 var t = this.getOwnerTree();
2678 return t ? t.getSelectionModel().isSelected(this) : false;
2683 * @param {Boolean} deep (optional) True to expand all children as well
2684 * @param {Boolean} anim (optional) false to cancel the default animation
2685 * @param {Function} callback (optional) A callback to be called when
2686 * expanding this node completes (does not wait for deep expand to complete).
2687 * Called with 1 parameter, this node.
2688 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
2690 expand : function(deep, anim, callback, scope){
2692 if(this.fireEvent('beforeexpand', this, deep, anim) === false){
2695 if(!this.childrenRendered){
2696 this.renderChildren();
2698 this.expanded = true;
2699 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
2700 this.ui.animExpand(function(){
2701 this.fireEvent('expand', this);
2702 this.runCallback(callback, scope || this, [this]);
2704 this.expandChildNodes(true, true);
2706 }.createDelegate(this));
2710 this.fireEvent('expand', this);
2711 this.runCallback(callback, scope || this, [this]);
2714 this.runCallback(callback, scope || this, [this]);
2717 this.expandChildNodes(true);
2721 runCallback : function(cb, scope, args){
2722 if(Ext.isFunction(cb)){
2723 cb.apply(scope, args);
2727 isHiddenRoot : function(){
2728 return this.isRoot && !this.getOwnerTree().rootVisible;
2732 * Collapse this node.
2733 * @param {Boolean} deep (optional) True to collapse all children as well
2734 * @param {Boolean} anim (optional) false to cancel the default animation
2735 * @param {Function} callback (optional) A callback to be called when
2736 * expanding this node completes (does not wait for deep expand to complete).
2737 * Called with 1 parameter, this node.
2738 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
2740 collapse : function(deep, anim, callback, scope){
2741 if(this.expanded && !this.isHiddenRoot()){
2742 if(this.fireEvent('beforecollapse', this, deep, anim) === false){
2745 this.expanded = false;
2746 if((this.getOwnerTree().animate && anim !== false) || anim){
2747 this.ui.animCollapse(function(){
2748 this.fireEvent('collapse', this);
2749 this.runCallback(callback, scope || this, [this]);
2751 this.collapseChildNodes(true);
2753 }.createDelegate(this));
2757 this.fireEvent('collapse', this);
2758 this.runCallback(callback, scope || this, [this]);
2760 }else if(!this.expanded){
2761 this.runCallback(callback, scope || this, [this]);
2764 var cs = this.childNodes;
2765 for(var i = 0, len = cs.length; i < len; i++) {
2766 cs[i].collapse(true, false);
2772 delayedExpand : function(delay){
2773 if(!this.expandProcId){
2774 this.expandProcId = this.expand.defer(delay, this);
2779 cancelExpand : function(){
2780 if(this.expandProcId){
2781 clearTimeout(this.expandProcId);
2783 this.expandProcId = false;
2787 * Toggles expanded/collapsed state of the node
2789 toggle : function(){
2798 * Ensures all parent nodes are expanded, and if necessary, scrolls
2799 * the node into view.
2800 * @param {Function} callback (optional) A function to call when the node has been made visible.
2801 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
2803 ensureVisible : function(callback, scope){
2804 var tree = this.getOwnerTree();
2805 tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){
2806 var node = tree.getNodeById(this.id); // Somehow if we don't do this, we lose changes that happened to node in the meantime
2807 tree.getTreeEl().scrollChildIntoView(node.ui.anchor);
2808 this.runCallback(callback, scope || this, [this]);
2809 }.createDelegate(this));
2813 * Expand all child nodes
2814 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
2816 expandChildNodes : function(deep, anim) {
2817 var cs = this.childNodes,
2820 for (i = 0; i < len; i++) {
2821 cs[i].expand(deep, anim);
2826 * Collapse all child nodes
2827 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
2829 collapseChildNodes : function(deep){
2830 var cs = this.childNodes;
2831 for(var i = 0, len = cs.length; i < len; i++) {
2832 cs[i].collapse(deep);
2837 * Disables this node
2839 disable : function(){
2840 this.disabled = true;
2842 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
2843 this.ui.onDisableChange(this, true);
2845 this.fireEvent('disabledchange', this, true);
2851 enable : function(){
2852 this.disabled = false;
2853 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
2854 this.ui.onDisableChange(this, false);
2856 this.fireEvent('disabledchange', this, false);
2860 renderChildren : function(suppressEvent){
2861 if(suppressEvent !== false){
2862 this.fireEvent('beforechildrenrendered', this);
2864 var cs = this.childNodes;
2865 for(var i = 0, len = cs.length; i < len; i++){
2868 this.childrenRendered = true;
2872 sort : function(fn, scope){
2873 Ext.tree.TreeNode.superclass.sort.apply(this, arguments);
2874 if(this.childrenRendered){
2875 var cs = this.childNodes;
2876 for(var i = 0, len = cs.length; i < len; i++){
2883 render : function(bulkRender){
2884 this.ui.render(bulkRender);
2886 // make sure it is registered
2887 this.getOwnerTree().registerNode(this);
2888 this.rendered = true;
2890 this.expanded = false;
2891 this.expand(false, false);
2897 renderIndent : function(deep, refresh){
2899 this.ui.childIndent = null;
2901 this.ui.renderIndent();
2902 if(deep === true && this.childrenRendered){
2903 var cs = this.childNodes;
2904 for(var i = 0, len = cs.length; i < len; i++){
2905 cs[i].renderIndent(true, refresh);
2910 beginUpdate : function(){
2911 this.childrenRendered = false;
2914 endUpdate : function(){
2915 if(this.expanded && this.rendered){
2916 this.renderChildren();
2921 destroy : function(silent){
2922 if(silent === true){
2923 this.unselect(true);
2925 Ext.tree.TreeNode.superclass.destroy.call(this, silent);
2926 Ext.destroy(this.ui, this.loader);
2927 this.ui = this.loader = null;
2931 onIdChange : function(id){
2932 this.ui.onIdChange(id);
2936 Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode;/**
2937 * @class Ext.tree.AsyncTreeNode
2938 * @extends Ext.tree.TreeNode
2939 * @cfg {TreeLoader} loader A TreeLoader to be used by this node (defaults to the loader defined on the tree)
2941 * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node
2943 Ext.tree.AsyncTreeNode = function(config){
2944 this.loaded = config && config.loaded === true;
2945 this.loading = false;
2946 Ext.tree.AsyncTreeNode.superclass.constructor.apply(this, arguments);
2949 * Fires before this node is loaded, return false to cancel
2950 * @param {Node} this This node
2952 this.addEvents('beforeload', 'load');
2955 * Fires when this node is loaded
2956 * @param {Node} this This node
2959 * The loader used by this node (defaults to using the tree's defined loader)
2964 Ext.extend(Ext.tree.AsyncTreeNode, Ext.tree.TreeNode, {
2965 expand : function(deep, anim, callback, scope){
2966 if(this.loading){ // if an async load is already running, waiting til it's done
2969 if(!this.loading){ // done loading
2970 clearInterval(timer);
2971 this.expand(deep, anim, callback, scope);
2973 }.createDelegate(this);
2974 timer = setInterval(f, 200);
2978 if(this.fireEvent("beforeload", this) === false){
2981 this.loading = true;
2982 this.ui.beforeLoad(this);
2983 var loader = this.loader || this.attributes.loader || this.getOwnerTree().getLoader();
2985 loader.load(this, this.loadComplete.createDelegate(this, [deep, anim, callback, scope]), this);
2989 Ext.tree.AsyncTreeNode.superclass.expand.call(this, deep, anim, callback, scope);
2993 * Returns true if this node is currently loading
2996 isLoading : function(){
2997 return this.loading;
3000 loadComplete : function(deep, anim, callback, scope){
3001 this.loading = false;
3003 this.ui.afterLoad(this);
3004 this.fireEvent("load", this);
3005 this.expand(deep, anim, callback, scope);
3009 * Returns true if this node has been loaded
3012 isLoaded : function(){
3016 hasChildNodes : function(){
3017 if(!this.isLeaf() && !this.loaded){
3020 return Ext.tree.AsyncTreeNode.superclass.hasChildNodes.call(this);
3025 * Trigger a reload for this node
3026 * @param {Function} callback
3027 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this Node.
3029 reload : function(callback, scope){
3030 this.collapse(false, false);
3031 while(this.firstChild){
3032 this.removeChild(this.firstChild).destroy();
3034 this.childrenRendered = false;
3035 this.loaded = false;
3036 if(this.isHiddenRoot()){
3037 this.expanded = false;
3039 this.expand(false, false, callback, scope);
3043 Ext.tree.TreePanel.nodeTypes.async = Ext.tree.AsyncTreeNode;/**
3044 * @class Ext.tree.TreeNodeUI
3045 * This class provides the default UI implementation for Ext TreeNodes.
3046 * The TreeNode UI implementation is separate from the
3047 * tree implementation, and allows customizing of the appearance of
3050 * If you are customizing the Tree's user interface, you
3051 * may need to extend this class, but you should never need to instantiate this class.<br>
3053 * This class provides access to the user interface components of an Ext TreeNode, through
3054 * {@link Ext.tree.TreeNode#getUI}
3056 Ext.tree.TreeNodeUI = Ext.extend(Object, {
3058 constructor : function(node){
3064 ecc: 'x-tree-ec-icon x-tree-elbow',
3065 emptyIcon: Ext.BLANK_IMAGE_URL
3070 removeChild : function(node){
3072 this.ctNode.removeChild(node.ui.getEl());
3077 beforeLoad : function(){
3078 this.addClass("x-tree-node-loading");
3082 afterLoad : function(){
3083 this.removeClass("x-tree-node-loading");
3087 onTextChange : function(node, text, oldText){
3089 this.textNode.innerHTML = text;
3094 onIconClsChange : function(node, cls, oldCls){
3096 Ext.fly(this.iconNode).replaceClass(oldCls, cls);
3101 onIconChange : function(node, icon){
3103 //'<img src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" : ""),(a.iconCls ? " "+a.iconCls : ""),'" unselectable="on" />',
3104 var empty = Ext.isEmpty(icon);
3105 this.iconNode.src = empty ? this.emptyIcon : icon;
3106 Ext.fly(this.iconNode)[empty ? 'removeClass' : 'addClass']('x-tree-node-inline-icon');
3111 onTipChange : function(node, tip, title){
3113 var hasTitle = Ext.isDefined(title);
3114 if(this.textNode.setAttributeNS){
3115 this.textNode.setAttributeNS("ext", "qtip", tip);
3117 this.textNode.setAttributeNS("ext", "qtitle", title);
3120 this.textNode.setAttribute("ext:qtip", tip);
3122 this.textNode.setAttribute("ext:qtitle", title);
3129 onHrefChange : function(node, href, target){
3131 this.anchor.href = this.getHref(href);
3132 if(Ext.isDefined(target)){
3133 this.anchor.target = target;
3139 onClsChange : function(node, cls, oldCls){
3141 Ext.fly(this.elNode).replaceClass(oldCls, cls);
3146 onDisableChange : function(node, state){
3147 this.disabled = state;
3148 if (this.checkbox) {
3149 this.checkbox.disabled = state;
3151 this[state ? 'addClass' : 'removeClass']('x-tree-node-disabled');
3155 onSelectedChange : function(state){
3158 this.addClass("x-tree-selected");
3161 this.removeClass("x-tree-selected");
3166 onMove : function(tree, node, oldParent, newParent, index, refNode){
3167 this.childIndent = null;
3169 var targetNode = newParent.ui.getContainer();
3170 if(!targetNode){//target not rendered
3171 this.holder = document.createElement("div");
3172 this.holder.appendChild(this.wrap);
3175 var insertBefore = refNode ? refNode.ui.getEl() : null;
3177 targetNode.insertBefore(this.wrap, insertBefore);
3179 targetNode.appendChild(this.wrap);
3181 this.node.renderIndent(true, oldParent != newParent);
3186 * Adds one or more CSS classes to the node's UI element.
3187 * Duplicate classes are automatically filtered out.
3188 * @param {String/Array} className The CSS class to add, or an array of classes
3190 addClass : function(cls){
3192 Ext.fly(this.elNode).addClass(cls);
3197 * Removes one or more CSS classes from the node's UI element.
3198 * @param {String/Array} className The CSS class to remove, or an array of classes
3200 removeClass : function(cls){
3202 Ext.fly(this.elNode).removeClass(cls);
3207 remove : function(){
3209 this.holder = document.createElement("div");
3210 this.holder.appendChild(this.wrap);
3215 fireEvent : function(){
3216 return this.node.fireEvent.apply(this.node, arguments);
3220 initEvents : function(){
3221 this.node.on("move", this.onMove, this);
3223 if(this.node.disabled){
3224 this.onDisableChange(this.node, true);
3226 if(this.node.hidden){
3229 var ot = this.node.getOwnerTree();
3230 var dd = ot.enableDD || ot.enableDrag || ot.enableDrop;
3231 if(dd && (!this.node.isRoot || ot.rootVisible)){
3232 Ext.dd.Registry.register(this.elNode, {
3234 handles: this.getDDHandles(),
3241 getDDHandles : function(){
3242 return [this.iconNode, this.textNode, this.elNode];
3249 this.node.hidden = true;
3251 this.wrap.style.display = "none";
3259 this.node.hidden = false;
3261 this.wrap.style.display = "";
3266 onContextMenu : function(e){
3267 if (this.node.hasListener("contextmenu") || this.node.getOwnerTree().hasListener("contextmenu")) {
3270 this.fireEvent("contextmenu", this.node, e);
3275 onClick : function(e){
3280 if(this.fireEvent("beforeclick", this.node, e) !== false){
3281 var a = e.getTarget('a');
3282 if(!this.disabled && this.node.attributes.href && a){
3283 this.fireEvent("click", this.node, e);
3285 }else if(a && e.ctrlKey){
3293 if(this.node.attributes.singleClickExpand && !this.animating && this.node.isExpandable()){
3297 this.fireEvent("click", this.node, e);
3304 onDblClick : function(e){
3309 if(this.fireEvent("beforedblclick", this.node, e) !== false){
3313 if(!this.animating && this.node.isExpandable()){
3316 this.fireEvent("dblclick", this.node, e);
3320 onOver : function(e){
3321 this.addClass('x-tree-node-over');
3324 onOut : function(e){
3325 this.removeClass('x-tree-node-over');
3329 onCheckChange : function(){
3330 var checked = this.checkbox.checked;
3332 this.checkbox.defaultChecked = checked;
3333 this.node.attributes.checked = checked;
3334 this.fireEvent('checkchange', this.node, checked);
3338 ecClick : function(e){
3339 if(!this.animating && this.node.isExpandable()){
3345 startDrop : function(){
3346 this.dropping = true;
3349 // delayed drop so the click event doesn't get fired on a drop
3350 endDrop : function(){
3351 setTimeout(function(){
3352 this.dropping = false;
3353 }.createDelegate(this), 50);
3357 expand : function(){
3358 this.updateExpandIcon();
3359 this.ctNode.style.display = "";
3364 if(!this.node.preventHScroll){
3365 try{this.anchor.focus();
3369 var noscroll = this.node.getOwnerTree().getTreeEl().dom;
3370 var l = noscroll.scrollLeft;
3371 this.anchor.focus();
3372 noscroll.scrollLeft = l;
3378 * Sets the checked status of the tree node to the passed value, or, if no value was passed,
3379 * toggles the checked status. If the node was rendered with no checkbox, this has no effect.
3380 * @param {Boolean} value (optional) The new checked status.
3382 toggleCheck : function(value){
3383 var cb = this.checkbox;
3385 cb.checked = (value === undefined ? !cb.checked : value);
3386 this.onCheckChange();
3398 animExpand : function(callback){
3399 var ct = Ext.get(this.ctNode);
3401 if(!this.node.isExpandable()){
3402 this.updateExpandIcon();
3403 this.ctNode.style.display = "";
3404 Ext.callback(callback);
3407 this.animating = true;
3408 this.updateExpandIcon();
3411 callback : function(){
3412 this.animating = false;
3413 Ext.callback(callback);
3416 duration: this.node.ownerTree.duration || .25
3421 highlight : function(){
3422 var tree = this.node.getOwnerTree();
3423 Ext.fly(this.wrap).highlight(
3424 tree.hlColor || "C3DAF9",
3425 {endColor: tree.hlBaseColor}
3430 collapse : function(){
3431 this.updateExpandIcon();
3432 this.ctNode.style.display = "none";
3436 animCollapse : function(callback){
3437 var ct = Ext.get(this.ctNode);
3438 ct.enableDisplayMode('block');
3441 this.animating = true;
3442 this.updateExpandIcon();
3445 callback : function(){
3446 this.animating = false;
3447 Ext.callback(callback);
3450 duration: this.node.ownerTree.duration || .25
3455 getContainer : function(){
3460 * Returns the element which encapsulates this node.
3461 * @return {HtmlElement} The DOM element. The default implementation uses a <code><li></code>.
3468 appendDDGhost : function(ghostNode){
3469 ghostNode.appendChild(this.elNode.cloneNode(true));
3473 getDDRepairXY : function(){
3474 return Ext.lib.Dom.getXY(this.iconNode);
3478 onRender : function(){
3483 render : function(bulkRender){
3484 var n = this.node, a = n.attributes;
3485 var targetNode = n.parentNode ?
3486 n.parentNode.ui.getContainer() : n.ownerTree.innerCt.dom;
3489 this.rendered = true;
3491 this.renderElements(n, a, targetNode, bulkRender);
3494 this.onTipChange(n, a.qtip, a.qtipTitle);
3495 }else if(a.qtipCfg){
3496 a.qtipCfg.target = Ext.id(this.textNode);
3497 Ext.QuickTips.register(a.qtipCfg);
3500 if(!this.node.expanded){
3501 this.updateExpandIcon(true);
3504 if(bulkRender === true) {
3505 targetNode.appendChild(this.wrap);
3511 renderElements : function(n, a, targetNode, bulkRender){
3512 // add some indent caching, this helps performance when rendering a large tree
3513 this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';
3515 var cb = Ext.isBoolean(a.checked),
3517 href = this.getHref(a.href),
3518 buf = ['<li class="x-tree-node"><div ext:tree-node-id="',n.id,'" class="x-tree-node-el x-tree-node-leaf x-unselectable ', a.cls,'" unselectable="on">',
3519 '<span class="x-tree-node-indent">',this.indentMarkup,"</span>",
3520 '<img alt="" src="', this.emptyIcon, '" class="x-tree-ec-icon x-tree-elbow" />',
3521 '<img alt="" src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" : ""),(a.iconCls ? " "+a.iconCls : ""),'" unselectable="on" />',
3522 cb ? ('<input class="x-tree-node-cb" type="checkbox" ' + (a.checked ? 'checked="checked" />' : '/>')) : '',
3523 '<a hidefocus="on" class="x-tree-node-anchor" href="',href,'" tabIndex="1" ',
3524 a.hrefTarget ? ' target="'+a.hrefTarget+'"' : "", '><span unselectable="on">',n.text,"</span></a></div>",
3525 '<ul class="x-tree-node-ct" style="display:none;"></ul>',
3528 if(bulkRender !== true && n.nextSibling && (nel = n.nextSibling.ui.getEl())){
3529 this.wrap = Ext.DomHelper.insertHtml("beforeBegin", nel, buf);
3531 this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf);
3534 this.elNode = this.wrap.childNodes[0];
3535 this.ctNode = this.wrap.childNodes[1];
3536 var cs = this.elNode.childNodes;
3537 this.indentNode = cs[0];
3538 this.ecNode = cs[1];
3539 this.iconNode = cs[2];
3542 this.checkbox = cs[3];
3544 this.checkbox.defaultChecked = this.checkbox.checked;
3547 this.anchor = cs[index];
3548 this.textNode = cs[index].firstChild;
3552 * @private Gets a normalized href for the node.
3553 * @param {String} href
3555 getHref : function(href){
3556 return Ext.isEmpty(href) ? (Ext.isGecko ? '' : '#') : href;
3560 * Returns the <a> element that provides focus for the node's UI.
3561 * @return {HtmlElement} The DOM anchor element.
3563 getAnchor : function(){
3568 * Returns the text node.
3569 * @return {HtmlNode} The DOM text node.
3571 getTextEl : function(){
3572 return this.textNode;
3576 * Returns the icon <img> element.
3577 * @return {HtmlElement} The DOM image element.
3579 getIconEl : function(){
3580 return this.iconNode;
3584 * Returns the checked status of the node. If the node was rendered with no
3585 * checkbox, it returns false.
3586 * @return {Boolean} The checked flag.
3588 isChecked : function(){
3589 return this.checkbox ? this.checkbox.checked : false;
3593 updateExpandIcon : function(){
3598 cls = n.isLast() ? "x-tree-elbow-end" : "x-tree-elbow",
3599 hasChild = n.hasChildNodes();
3600 if(hasChild || n.attributes.expandable){
3603 c1 = "x-tree-node-collapsed";
3604 c2 = "x-tree-node-expanded";
3607 c1 = "x-tree-node-expanded";
3608 c2 = "x-tree-node-collapsed";
3611 this.removeClass("x-tree-node-leaf");
3612 this.wasLeaf = false;
3614 if(this.c1 != c1 || this.c2 != c2){
3615 Ext.fly(this.elNode).replaceClass(c1, c2);
3616 this.c1 = c1; this.c2 = c2;
3620 Ext.fly(this.elNode).replaceClass("x-tree-node-expanded", "x-tree-node-collapsed");
3623 this.wasLeaf = true;
3626 var ecc = "x-tree-ec-icon "+cls;
3627 if(this.ecc != ecc){
3628 this.ecNode.className = ecc;
3635 onIdChange: function(id){
3637 this.elNode.setAttribute('ext:tree-node-id', id);
3642 getChildIndent : function(){
3643 if(!this.childIndent){
3647 if(!p.isRoot || (p.isRoot && p.ownerTree.rootVisible)){
3649 buf.unshift('<img alt="" src="'+this.emptyIcon+'" class="x-tree-elbow-line" />');
3651 buf.unshift('<img alt="" src="'+this.emptyIcon+'" class="x-tree-icon" />');
3656 this.childIndent = buf.join("");
3658 return this.childIndent;
3662 renderIndent : function(){
3665 p = this.node.parentNode;
3667 indent = p.ui.getChildIndent();
3669 if(this.indentMarkup != indent){ // don't rerender if not required
3670 this.indentNode.innerHTML = indent;
3671 this.indentMarkup = indent;
3673 this.updateExpandIcon();
3677 destroy : function(){
3679 Ext.dd.Registry.unregister(this.elNode.id);
3682 Ext.each(['textnode', 'anchor', 'checkbox', 'indentNode', 'ecNode', 'iconNode', 'elNode', 'ctNode', 'wrap', 'holder'], function(el){
3684 Ext.fly(this[el]).remove();
3693 * @class Ext.tree.RootTreeNodeUI
3694 * This class provides the default UI implementation for <b>root</b> Ext TreeNodes.
3695 * The RootTreeNode UI implementation allows customizing the appearance of the root tree node.<br>
3697 * If you are customizing the Tree's user interface, you
3698 * may need to extend this class, but you should never need to instantiate this class.<br>
3700 Ext.tree.RootTreeNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
3702 render : function(){
3704 var targetNode = this.node.ownerTree.innerCt.dom;
3705 this.node.expanded = true;
3706 targetNode.innerHTML = '<div class="x-tree-root-node"></div>';
3707 this.wrap = this.ctNode = targetNode.firstChild;
3710 collapse : Ext.emptyFn,
3711 expand : Ext.emptyFn
3713 * @class Ext.tree.TreeLoader
3714 * @extends Ext.util.Observable
3715 * A TreeLoader provides for lazy loading of an {@link Ext.tree.TreeNode}'s child
3716 * nodes from a specified URL. The response must be a JavaScript Array definition
3717 * whose elements are node definition objects. e.g.:
3721 text: 'A leaf Node',
3725 text: 'A folder Node',
3728 text: 'A child Node',
3734 * A server request is sent, and child nodes are loaded only when a node is expanded.
3735 * The loading node's id is passed to the server under the parameter name "node" to
3736 * enable the server to produce the correct child nodes.
3738 * To pass extra parameters, an event handler may be attached to the "beforeload"
3739 * event, and the parameters specified in the TreeLoader's baseParams property:
3741 myTreeLoader.on("beforeload", function(treeLoader, node) {
3742 this.baseParams.category = node.attributes.category;
3745 * This would pass an HTTP parameter called "category" to the server containing
3746 * the value of the Node's "category" attribute.
3748 * Creates a new Treeloader.
3749 * @param {Object} config A config object containing config properties.
3751 Ext.tree.TreeLoader = function(config){
3752 this.baseParams = {};
3753 Ext.apply(this, config);
3758 * Fires before a network request is made to retrieve the Json text which specifies a node's children.
3759 * @param {Object} This TreeLoader object.
3760 * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
3761 * @param {Object} callback The callback function specified in the {@link #load} call.
3766 * Fires when the node has been successfuly loaded.
3767 * @param {Object} This TreeLoader object.
3768 * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
3769 * @param {Object} response The response object containing the data from the server.
3773 * @event loadexception
3774 * Fires if the network request failed.
3775 * @param {Object} This TreeLoader object.
3776 * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
3777 * @param {Object} response The response object containing the data from the server.
3781 Ext.tree.TreeLoader.superclass.constructor.call(this);
3782 if(Ext.isString(this.paramOrder)){
3783 this.paramOrder = this.paramOrder.split(/[\s,|]/);
3787 Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
3789 * @cfg {String} dataUrl The URL from which to request a Json string which
3790 * specifies an array of node definition objects representing the child nodes
3794 * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
3797 * @cfg {String} url Equivalent to {@link #dataUrl}.
3800 * @cfg {Boolean} preloadChildren If set to true, the loader recursively loads "children" attributes when doing the first load on nodes.
3803 * @cfg {Object} baseParams (optional) An object containing properties which
3804 * specify HTTP parameters to be passed to each request for child nodes.
3807 * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
3808 * created by this loader. If the attributes sent by the server have an attribute in this object,
3809 * they take priority.
3812 * @cfg {Object} uiProviders (optional) An object containing properties which
3813 * specify custom {@link Ext.tree.TreeNodeUI} implementations. If the optional
3814 * <i>uiProvider</i> attribute of a returned child node is a string rather
3815 * than a reference to a TreeNodeUI implementation, then that string value
3816 * is used as a property name in the uiProviders object.
3821 * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
3822 * child nodes before loading.
3827 * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. Only used when using directFn.
3828 * Specifies the params in the order in which they must be passed to the server-side Direct method
3829 * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
3830 * comma, or pipe. For example,
3831 * any of the following would be acceptable:<pre><code>
3832 nodeParameter: 'node',
3833 paramOrder: ['param1','param2','param3']
3834 paramOrder: 'node param1 param2 param3'
3835 paramOrder: 'param1,node,param2,param3'
3836 paramOrder: 'param1|param2|param|node'
3839 paramOrder: undefined,
3842 * @cfg {Boolean} paramsAsHash Only used when using directFn.
3843 * Send parameters as a collection of named arguments (defaults to <tt>false</tt>). Providing a
3844 * <tt>{@link #paramOrder}</tt> nullifies this configuration.
3846 paramsAsHash: false,
3849 * @cfg {String} nodeParameter The name of the parameter sent to the server which contains
3850 * the identifier of the node. Defaults to <tt>'node'</tt>.
3852 nodeParameter: 'node',
3855 * @cfg {Function} directFn
3856 * Function to call when executing a request.
3858 directFn : undefined,
3861 * Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor.
3862 * This is called automatically when a node is expanded, but may be used to reload
3863 * a node (or append new children if the {@link #clearOnLoad} option is false.)
3864 * @param {Ext.tree.TreeNode} node
3865 * @param {Function} callback Function to call after the node has been loaded. The
3866 * function is passed the TreeNode which was requested to be loaded.
3867 * @param {Object} scope The scope (<code>this</code> reference) in which the callback is executed.
3868 * defaults to the loaded TreeNode.
3870 load : function(node, callback, scope){
3871 if(this.clearOnLoad){
3872 while(node.firstChild){
3873 node.removeChild(node.firstChild);
3876 if(this.doPreload(node)){ // preloaded json children
3877 this.runCallback(callback, scope || node, [node]);
3878 }else if(this.directFn || this.dataUrl || this.url){
3879 this.requestData(node, callback, scope || node);
3883 doPreload : function(node){
3884 if(node.attributes.children){
3885 if(node.childNodes.length < 1){ // preloaded?
3886 var cs = node.attributes.children;
3888 for(var i = 0, len = cs.length; i < len; i++){
3889 var cn = node.appendChild(this.createNode(cs[i]));
3890 if(this.preloadChildren){
3901 getParams: function(node){
3902 var bp = Ext.apply({}, this.baseParams),
3903 np = this.nodeParameter,
3904 po = this.paramOrder;
3906 np && (bp[ np ] = node.id);
3909 var buf = [node.id];
3911 // reset 'buf' if the nodeParameter was included in paramOrder
3912 if(np && po.indexOf(np) > -1){
3916 for(var i = 0, len = po.length; i < len; i++){
3917 buf.push(bp[ po[i] ]);
3919 }else if(this.paramsAsHash){
3928 requestData : function(node, callback, scope){
3929 if(this.fireEvent("beforeload", this, node, callback) !== false){
3931 var args = this.getParams(node);
3932 args.push(this.processDirectResponse.createDelegate(this, [{callback: callback, node: node, scope: scope}], true));
3933 this.directFn.apply(window, args);
3935 this.transId = Ext.Ajax.request({
3936 method:this.requestMethod,
3937 url: this.dataUrl||this.url,
3938 success: this.handleResponse,
3939 failure: this.handleFailure,
3941 argument: {callback: callback, node: node, scope: scope},
3942 params: this.getParams(node)
3946 // if the load is cancelled, make sure we notify
3947 // the node that we are done
3948 this.runCallback(callback, scope || node, []);
3952 processDirectResponse: function(result, response, args){
3953 if(response.status){
3954 this.handleResponse({
3955 responseData: Ext.isArray(result) ? result : null,
3956 responseText: result,
3960 this.handleFailure({
3967 runCallback: function(cb, scope, args){
3968 if(Ext.isFunction(cb)){
3969 cb.apply(scope, args);
3973 isLoading : function(){
3974 return !!this.transId;
3978 if(this.isLoading()){
3979 Ext.Ajax.abort(this.transId);
3984 * <p>Override this function for custom TreeNode node implementation, or to
3985 * modify the attributes at creation time.</p>
3986 * Example:<pre><code>
3987 new Ext.tree.TreePanel({
3989 loader: new Ext.tree.TreeLoader({
3991 createNode: function(attr) {
3992 // Allow consolidation consignments to have
3993 // consignments dropped into them.
3994 if (attr.isConsolidation) {
3995 attr.iconCls = 'x-consol',
3996 attr.allowDrop = true;
3998 return Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
4004 * @param attr {Object} The attributes from which to create the new node.
4006 createNode : function(attr){
4007 // apply baseAttrs, nice idea Corey!
4009 Ext.applyIf(attr, this.baseAttrs);
4011 if(this.applyLoader !== false && !attr.loader){
4014 if(Ext.isString(attr.uiProvider)){
4015 attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
4018 return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr);
4021 new Ext.tree.TreeNode(attr) :
4022 new Ext.tree.AsyncTreeNode(attr);
4026 processResponse : function(response, node, callback, scope){
4027 var json = response.responseText;
4029 var o = response.responseData || Ext.decode(json);
4031 for(var i = 0, len = o.length; i < len; i++){
4032 var n = this.createNode(o[i]);
4034 node.appendChild(n);
4038 this.runCallback(callback, scope || node, [node]);
4040 this.handleFailure(response);
4044 handleResponse : function(response){
4045 this.transId = false;
4046 var a = response.argument;
4047 this.processResponse(response, a.node, a.callback, a.scope);
4048 this.fireEvent("load", this, a.node, response);
4051 handleFailure : function(response){
4052 this.transId = false;
4053 var a = response.argument;
4054 this.fireEvent("loadexception", this, a.node, response);
4055 this.runCallback(a.callback, a.scope || a.node, [a.node]);
4058 destroy : function(){
4060 this.purgeListeners();
4063 * @class Ext.tree.TreeFilter
4064 * Note this class is experimental and doesn't update the indent (lines) or expand collapse icons of the nodes
4065 * @param {TreePanel} tree
4066 * @param {Object} config (optional)
4068 Ext.tree.TreeFilter = function(tree, config){
4071 Ext.apply(this, config);
4074 Ext.tree.TreeFilter.prototype = {
4081 * Filter the data by a specific attribute.
4082 * @param {String/RegExp} value Either string that the attribute value
4083 * should start with or a RegExp to test against the attribute
4084 * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
4085 * @param {TreeNode} startNode (optional) The node to start the filter at.
4087 filter : function(value, attr, startNode){
4088 attr = attr || "text";
4090 if(typeof value == "string"){
4091 var vlen = value.length;
4092 // auto clear empty filter
4093 if(vlen == 0 && this.clearBlank){
4097 value = value.toLowerCase();
4099 return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
4101 }else if(value.exec){ // regex?
4103 return value.test(n.attributes[attr]);
4106 throw 'Illegal filter type, must be string or regex';
4108 this.filterBy(f, null, startNode);
4112 * Filter by a function. The passed function will be called with each
4113 * node in the tree (or from the startNode). If the function returns true, the node is kept
4114 * otherwise it is filtered. If a node is filtered, its children are also filtered.
4115 * @param {Function} fn The filter function
4116 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
4118 filterBy : function(fn, scope, startNode){
4119 startNode = startNode || this.tree.root;
4123 var af = this.filtered, rv = this.reverse;
4124 var f = function(n){
4131 var m = fn.call(scope || n, n);
4139 startNode.cascade(f);
4142 if(typeof id != "function"){
4144 if(n && n.parentNode){
4145 n.parentNode.removeChild(n);
4153 * Clears the current filter. Note: with the "remove" option
4154 * set a filter cannot be cleared.
4158 var af = this.filtered;
4160 if(typeof id != "function"){
4171 * @class Ext.tree.TreeSorter
4172 * Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
4173 * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
4174 * Example usage:<br />
4176 new Ext.tree.TreeSorter(myTree, {
4179 sortType: function(node) {
4180 // sort by a custom, typed attribute:
4181 return parseInt(node.id, 10);
4186 * @param {TreePanel} tree
4187 * @param {Object} config
4189 Ext.tree.TreeSorter = Ext.extend(Object, {
4191 constructor: function(tree, config){
4193 * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
4196 * @cfg {String} property The named attribute on the node to sort by (defaults to "text"). Note that this
4197 * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
4200 * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
4203 * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
4206 * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
4209 * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting. The function
4210 * will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return
4211 * the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
4212 * a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
4213 * example usage. Note that if a sortType is specified, any {@link #property} config will be ignored.
4216 Ext.apply(this, config);
4219 beforechildrenrendered: this.doSort,
4220 append: this.updateSort,
4221 insert: this.updateSort,
4222 textchange: this.updateSortParent
4225 var desc = this.dir && this.dir.toLowerCase() == 'desc',
4226 prop = this.property || 'text';
4227 sortType = this.sortType;
4228 folderSort = this.folderSort;
4229 caseSensitive = this.caseSensitive === true;
4230 leafAttr = this.leafAttr || 'leaf';
4232 if(Ext.isString(sortType)){
4233 sortType = Ext.data.SortTypes[sortType];
4235 this.sortFn = function(n1, n2){
4236 var attr1 = n1.attributes,
4237 attr2 = n2.attributes;
4240 if(attr1[leafAttr] && !attr2[leafAttr]){
4243 if(!attr1[leafAttr] && attr2[leafAttr]){
4247 var prop1 = attr1[prop],
4248 prop2 = attr2[prop],
4249 v1 = sortType ? sortType(prop1) : (caseSensitive ? prop1 : prop1.toUpperCase());
4250 v2 = sortType ? sortType(prop2) : (caseSensitive ? prop2 : prop2.toUpperCase());
4253 return desc ? 1 : -1;
4255 return desc ? -1 : 1;
4261 doSort : function(node){
4262 node.sort(this.sortFn);
4265 updateSort : function(tree, node){
4266 if(node.childrenRendered){
4267 this.doSort.defer(1, this, [node]);
4271 updateSortParent : function(node){
4272 var p = node.parentNode;
4273 if(p && p.childrenRendered){
4274 this.doSort.defer(1, this, [p]);
4278 * @class Ext.tree.TreeDropZone
4279 * @extends Ext.dd.DropZone
4281 * @param {String/HTMLElement/Element} tree The {@link Ext.tree.TreePanel} for which to enable dropping
4282 * @param {Object} config
4284 if(Ext.dd.DropZone){
4286 Ext.tree.TreeDropZone = function(tree, config){
4288 * @cfg {Boolean} allowParentInsert
4289 * Allow inserting a dragged node between an expanded parent node and its first child that will become a
4290 * sibling of the parent when dropped (defaults to false)
4292 this.allowParentInsert = config.allowParentInsert || false;
4294 * @cfg {String} allowContainerDrop
4295 * True if drops on the tree container (outside of a specific tree node) are allowed (defaults to false)
4297 this.allowContainerDrop = config.allowContainerDrop || false;
4299 * @cfg {String} appendOnly
4300 * True if the tree should only allow append drops (use for trees which are sorted, defaults to false)
4302 this.appendOnly = config.appendOnly || false;
4304 Ext.tree.TreeDropZone.superclass.constructor.call(this, tree.getTreeEl(), config);
4306 * The TreePanel for this drop zone
4307 * @type Ext.tree.TreePanel
4312 * Arbitrary data that can be associated with this tree and will be included in the event object that gets
4313 * passed to any nodedragover event handler (defaults to {})
4314 * @type Ext.tree.TreePanel
4317 this.dragOverData = {};
4319 this.lastInsertClass = "x-tree-no-status";
4322 Ext.extend(Ext.tree.TreeDropZone, Ext.dd.DropZone, {
4324 * @cfg {String} ddGroup
4325 * A named drag drop group to which this object belongs. If a group is specified, then this object will only
4326 * interact with other drag drop objects in the same group (defaults to 'TreeDD').
4331 * @cfg {String} expandDelay
4332 * The delay in milliseconds to wait before expanding a target tree node while dragging a droppable node
4333 * over the target (defaults to 1000)
4338 expandNode : function(node){
4339 if(node.hasChildNodes() && !node.isExpanded()){
4340 node.expand(false, null, this.triggerCacheRefresh.createDelegate(this));
4345 queueExpand : function(node){
4346 this.expandProcId = this.expandNode.defer(this.expandDelay, this, [node]);
4350 cancelExpand : function(){
4351 if(this.expandProcId){
4352 clearTimeout(this.expandProcId);
4353 this.expandProcId = false;
4358 isValidDropPoint : function(n, pt, dd, e, data){
4359 if(!n || !data){ return false; }
4360 var targetNode = n.node;
4361 var dropNode = data.node;
4362 // default drop rules
4363 if(!(targetNode && targetNode.isTarget && pt)){
4366 if(pt == "append" && targetNode.allowChildren === false){
4369 if((pt == "above" || pt == "below") && (targetNode.parentNode && targetNode.parentNode.allowChildren === false)){
4372 if(dropNode && (targetNode == dropNode || dropNode.contains(targetNode))){
4376 var overEvent = this.dragOverData;
4377 overEvent.tree = this.tree;
4378 overEvent.target = targetNode;
4379 overEvent.data = data;
4380 overEvent.point = pt;
4381 overEvent.source = dd;
4382 overEvent.rawEvent = e;
4383 overEvent.dropNode = dropNode;
4384 overEvent.cancel = false;
4385 var result = this.tree.fireEvent("nodedragover", overEvent);
4386 return overEvent.cancel === false && result !== false;
4390 getDropPoint : function(e, n, dd){
4393 return tn.allowChildren !== false ? "append" : false; // always append for root
4395 var dragEl = n.ddel;
4396 var t = Ext.lib.Dom.getY(dragEl), b = t + dragEl.offsetHeight;
4397 var y = Ext.lib.Event.getPageY(e);
4398 var noAppend = tn.allowChildren === false || tn.isLeaf();
4399 if(this.appendOnly || tn.parentNode.allowChildren === false){
4400 return noAppend ? false : "append";
4402 var noBelow = false;
4403 if(!this.allowParentInsert){
4404 noBelow = tn.hasChildNodes() && tn.isExpanded();
4406 var q = (b - t) / (noAppend ? 2 : 3);
4407 if(y >= t && y < (t + q)){
4409 }else if(!noBelow && (noAppend || y >= b-q && y <= b)){
4417 onNodeEnter : function(n, dd, e, data){
4418 this.cancelExpand();
4421 onContainerOver : function(dd, e, data) {
4422 if (this.allowContainerDrop && this.isValidDropPoint({ ddel: this.tree.getRootNode().ui.elNode, node: this.tree.getRootNode() }, "append", dd, e, data)) {
4423 return this.dropAllowed;
4425 return this.dropNotAllowed;
4429 onNodeOver : function(n, dd, e, data){
4430 var pt = this.getDropPoint(e, n, dd);
4433 // auto node expand check
4434 if(!this.expandProcId && pt == "append" && node.hasChildNodes() && !n.node.isExpanded()){
4435 this.queueExpand(node);
4436 }else if(pt != "append"){
4437 this.cancelExpand();
4440 // set the insert point style on the target node
4441 var returnCls = this.dropNotAllowed;
4442 if(this.isValidDropPoint(n, pt, dd, e, data)){
4447 returnCls = n.node.isFirst() ? "x-tree-drop-ok-above" : "x-tree-drop-ok-between";
4448 cls = "x-tree-drag-insert-above";
4449 }else if(pt == "below"){
4450 returnCls = n.node.isLast() ? "x-tree-drop-ok-below" : "x-tree-drop-ok-between";
4451 cls = "x-tree-drag-insert-below";
4453 returnCls = "x-tree-drop-ok-append";
4454 cls = "x-tree-drag-append";
4456 if(this.lastInsertClass != cls){
4457 Ext.fly(el).replaceClass(this.lastInsertClass, cls);
4458 this.lastInsertClass = cls;
4466 onNodeOut : function(n, dd, e, data){
4467 this.cancelExpand();
4468 this.removeDropIndicators(n);
4472 onNodeDrop : function(n, dd, e, data){
4473 var point = this.getDropPoint(e, n, dd);
4474 var targetNode = n.node;
4475 targetNode.ui.startDrop();
4476 if(!this.isValidDropPoint(n, point, dd, e, data)){
4477 targetNode.ui.endDrop();
4480 // first try to find the drop node
4481 var dropNode = data.node || (dd.getTreeNode ? dd.getTreeNode(data, targetNode, point, e) : null);
4482 return this.processDrop(targetNode, data, point, dd, e, dropNode);
4485 onContainerDrop : function(dd, e, data){
4486 if (this.allowContainerDrop && this.isValidDropPoint({ ddel: this.tree.getRootNode().ui.elNode, node: this.tree.getRootNode() }, "append", dd, e, data)) {
4487 var targetNode = this.tree.getRootNode();
4488 targetNode.ui.startDrop();
4489 var dropNode = data.node || (dd.getTreeNode ? dd.getTreeNode(data, targetNode, 'append', e) : null);
4490 return this.processDrop(targetNode, data, 'append', dd, e, dropNode);
4496 processDrop: function(target, data, point, dd, e, dropNode){
4508 var retval = this.tree.fireEvent("beforenodedrop", dropEvent);
4509 if(retval === false || dropEvent.cancel === true || !dropEvent.dropNode){
4510 target.ui.endDrop();
4511 return dropEvent.dropStatus;
4514 target = dropEvent.target;
4515 if(point == 'append' && !target.isExpanded()){
4516 target.expand(false, null, function(){
4517 this.completeDrop(dropEvent);
4518 }.createDelegate(this));
4520 this.completeDrop(dropEvent);
4526 completeDrop : function(de){
4527 var ns = de.dropNode, p = de.point, t = de.target;
4528 if(!Ext.isArray(ns)){
4532 for(var i = 0, len = ns.length; i < len; i++){
4535 t.parentNode.insertBefore(n, t);
4536 }else if(p == "below"){
4537 t.parentNode.insertBefore(n, t.nextSibling);
4543 if(Ext.enableFx && this.tree.hlDrop){
4547 this.tree.fireEvent("nodedrop", de);
4551 afterNodeMoved : function(dd, data, e, targetNode, dropNode){
4552 if(Ext.enableFx && this.tree.hlDrop){
4553 dropNode.ui.focus();
4554 dropNode.ui.highlight();
4556 this.tree.fireEvent("nodedrop", this.tree, targetNode, data, dd, e);
4560 getTree : function(){
4565 removeDropIndicators : function(n){
4568 Ext.fly(el).removeClass([
4569 "x-tree-drag-insert-above",
4570 "x-tree-drag-insert-below",
4571 "x-tree-drag-append"]);
4572 this.lastInsertClass = "_noclass";
4577 beforeDragDrop : function(target, e, id){
4578 this.cancelExpand();
4583 afterRepair : function(data){
4584 if(data && Ext.enableFx){
4585 data.node.ui.highlight();
4592 * @class Ext.tree.TreeDragZone
4593 * @extends Ext.dd.DragZone
4595 * @param {String/HTMLElement/Element} tree The {@link Ext.tree.TreePanel} for which to enable dragging
4596 * @param {Object} config
4598 if(Ext.dd.DragZone){
4599 Ext.tree.TreeDragZone = function(tree, config){
4600 Ext.tree.TreeDragZone.superclass.constructor.call(this, tree.innerCt, config);
4602 * The TreePanel for this drag zone
4603 * @type Ext.tree.TreePanel
4609 Ext.extend(Ext.tree.TreeDragZone, Ext.dd.DragZone, {
4611 * @cfg {String} ddGroup
4612 * A named drag drop group to which this object belongs. If a group is specified, then this object will only
4613 * interact with other drag drop objects in the same group (defaults to 'TreeDD').
4618 onBeforeDrag : function(data, e){
4620 return n && n.draggable && !n.disabled;
4624 onInitDrag : function(e){
4625 var data = this.dragData;
4626 this.tree.getSelectionModel().select(data.node);
4627 this.tree.eventModel.disable();
4628 this.proxy.update("");
4629 data.node.ui.appendDDGhost(this.proxy.ghost.dom);
4630 this.tree.fireEvent("startdrag", this.tree, data.node, e);
4634 getRepairXY : function(e, data){
4635 return data.node.ui.getDDRepairXY();
4639 onEndDrag : function(data, e){
4640 this.tree.eventModel.enable.defer(100, this.tree.eventModel);
4641 this.tree.fireEvent("enddrag", this.tree, data.node, e);
4645 onValidDrop : function(dd, e, id){
4646 this.tree.fireEvent("dragdrop", this.tree, this.dragData.node, dd, e);
4651 beforeInvalidDrop : function(e, id){
4652 // this scrolls the original position back into view
4653 var sm = this.tree.getSelectionModel();
4654 sm.clearSelections();
4655 sm.select(this.dragData.node);
4659 afterRepair : function(){
4660 if (Ext.enableFx && this.tree.hlDrop) {
4661 Ext.Element.fly(this.dragData.ddel).highlight(this.hlColor || "c3daf9");
4663 this.dragging = false;
4667 * @class Ext.tree.TreeEditor
4668 * @extends Ext.Editor
4669 * Provides editor functionality for inline tree node editing. Any valid {@link Ext.form.Field} subclass can be used
4670 * as the editor field.
4672 * @param {TreePanel} tree
4673 * @param {Object} fieldConfig (optional) Either a prebuilt {@link Ext.form.Field} instance or a Field config object
4674 * that will be applied to the default field instance (defaults to a {@link Ext.form.TextField}).
4675 * @param {Object} config (optional) A TreeEditor config object
4677 Ext.tree.TreeEditor = function(tree, fc, config){
4679 var field = fc.events ? fc : new Ext.form.TextField(fc);
4681 Ext.tree.TreeEditor.superclass.constructor.call(this, field, config);
4686 tree.on('render', this.initEditor, this);
4688 this.initEditor(tree);
4692 Ext.extend(Ext.tree.TreeEditor, Ext.Editor, {
4694 * @cfg {String} alignment
4695 * The position to align to (see {@link Ext.Element#alignTo} for more details, defaults to "l-l").
4701 * @cfg {Boolean} hideEl
4702 * True to hide the bound element while the editor is displayed (defaults to false)
4707 * CSS class to apply to the editor (defaults to "x-small-editor x-tree-editor")
4709 cls: "x-small-editor x-tree-editor",
4711 * @cfg {Boolean} shim
4712 * True to shim the editor if selects/iframes could be displayed beneath it (defaults to false)
4718 * @cfg {Number} maxWidth
4719 * The maximum width in pixels of the editor field (defaults to 250). Note that if the maxWidth would exceed
4720 * the containing tree element's size, it will be automatically limited for you to the container width, taking
4721 * scroll and client offsets into account prior to each edit.
4725 * @cfg {Number} editDelay The number of milliseconds between clicks to register a double-click that will trigger
4726 * editing on the current node (defaults to 350). If two clicks occur on the same node within this time span,
4727 * the editor for the node will display, otherwise it will be processed as a regular click.
4731 initEditor : function(tree){
4734 beforeclick: this.beforeNodeClick,
4735 dblclick : this.onNodeDblClick
4740 complete : this.updateNode,
4741 beforestartedit: this.fitToTree,
4742 specialkey : this.onSpecialKey
4745 this.on('startedit', this.bindScroll, this, {delay:10});
4749 fitToTree : function(ed, el){
4750 var td = this.tree.getTreeEl().dom, nd = el.dom;
4751 if(td.scrollLeft > nd.offsetLeft){ // ensure the node left point is visible
4752 td.scrollLeft = nd.offsetLeft;
4756 (td.clientWidth > 20 ? td.clientWidth : td.offsetWidth) - Math.max(0, nd.offsetLeft-td.scrollLeft) - /*cushion*/5);
4757 this.setSize(w, '');
4761 * Edit the text of the passed {@link Ext.tree.TreeNode TreeNode}.
4762 * @param node {Ext.tree.TreeNode} The TreeNode to edit. The TreeNode must be {@link Ext.tree.TreeNode#editable editable}.
4764 triggerEdit : function(node, defer){
4765 this.completeEdit();
4766 if(node.attributes.editable !== false){
4768 * The {@link Ext.tree.TreeNode TreeNode} this editor is bound to. Read-only.
4769 * @type Ext.tree.TreeNode
4770 * @property editNode
4772 this.editNode = node;
4773 if(this.tree.autoScroll){
4774 Ext.fly(node.ui.getEl()).scrollIntoView(this.tree.body);
4776 var value = node.text || '';
4777 if (!Ext.isGecko && Ext.isEmpty(node.text)){
4778 node.setText(' ');
4780 this.autoEditTimer = this.startEdit.defer(this.editDelay, this, [node.ui.textNode, value]);
4786 bindScroll : function(){
4787 this.tree.getTreeEl().on('scroll', this.cancelEdit, this);
4791 beforeNodeClick : function(node, e){
4792 clearTimeout(this.autoEditTimer);
4793 if(this.tree.getSelectionModel().isSelected(node)){
4795 return this.triggerEdit(node);
4799 onNodeDblClick : function(node, e){
4800 clearTimeout(this.autoEditTimer);
4804 updateNode : function(ed, value){
4805 this.tree.getTreeEl().un('scroll', this.cancelEdit, this);
4806 this.editNode.setText(value);
4810 onHide : function(){
4811 Ext.tree.TreeEditor.superclass.onHide.call(this);
4813 this.editNode.ui.focus.defer(50, this.editNode.ui);
4818 onSpecialKey : function(field, e){
4823 }else if(k == e.ENTER && !e.hasModifier()){
4825 this.completeEdit();
4829 onDestroy : function(){
4830 clearTimeout(this.autoEditTimer);
4831 Ext.tree.TreeEditor.superclass.onDestroy.call(this);
4832 var tree = this.tree;
4833 tree.un('beforeclick', this.beforeNodeClick, this);
4834 tree.un('dblclick', this.onNodeDblClick, this);