1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.tree.TreeNode |
---|
11 | * @extends Ext.data.Node |
---|
12 | * @cfg {String} text The text for this node |
---|
13 | * @cfg {Boolean} expanded true to start the node expanded |
---|
14 | * @cfg {Boolean} allowDrag False to make this node undraggable if {@link #draggable} = true (defaults to true) |
---|
15 | * @cfg {Boolean} allowDrop False if this node cannot have child nodes dropped on it (defaults to true) |
---|
16 | * @cfg {Boolean} disabled true to start the node disabled |
---|
17 | * @cfg {String} icon The path to an icon for the node. The preferred way to do this |
---|
18 | * is to use the cls or iconCls attributes and add the icon via a CSS background image. |
---|
19 | * @cfg {String} cls A css class to be added to the node |
---|
20 | * @cfg {String} iconCls A css class to be added to the nodes icon element for applying css background images |
---|
21 | * @cfg {String} href URL of the link used for the node (defaults to #) |
---|
22 | * @cfg {String} hrefTarget target frame for the link |
---|
23 | * @cfg {String} qtip An Ext QuickTip for the node |
---|
24 | * @cfg {Boolean} expandable If set to true, the node will always show a plus/minus icon, even when empty |
---|
25 | * @cfg {String} qtipCfg An Ext QuickTip config for the node (used instead of qtip) |
---|
26 | * @cfg {Boolean} singleClickExpand True for single click expand on this node |
---|
27 | * @cfg {Function} uiProvider A UI <b>class</b> to use for this node (defaults to Ext.tree.TreeNodeUI) |
---|
28 | * @cfg {Boolean} checked True to render a checked checkbox for this node, false to render an unchecked checkbox |
---|
29 | * (defaults to undefined with no checkbox rendered) |
---|
30 | * @cfg {Boolean} draggable True to make this node draggable (defaults to false) |
---|
31 | * @cfg {Boolean} isTarget False to not allow this node to act as a drop target (defaults to true) |
---|
32 | * @cfg {Boolean} allowChildren False to not allow this node to have child nodes (defaults to true) |
---|
33 | * @constructor |
---|
34 | * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node |
---|
35 | */ |
---|
36 | Ext.tree.TreeNode = function(attributes){ |
---|
37 | attributes = attributes || {}; |
---|
38 | if(typeof attributes == "string"){ |
---|
39 | attributes = {text: attributes}; |
---|
40 | } |
---|
41 | this.childrenRendered = false; |
---|
42 | this.rendered = false; |
---|
43 | Ext.tree.TreeNode.superclass.constructor.call(this, attributes); |
---|
44 | this.expanded = attributes.expanded === true; |
---|
45 | this.isTarget = attributes.isTarget !== false; |
---|
46 | this.draggable = attributes.draggable !== false && attributes.allowDrag !== false; |
---|
47 | this.allowChildren = attributes.allowChildren !== false && attributes.allowDrop !== false; |
---|
48 | |
---|
49 | /** |
---|
50 | * Read-only. The text for this node. To change it use setText(). |
---|
51 | * @type String |
---|
52 | */ |
---|
53 | this.text = attributes.text; |
---|
54 | /** |
---|
55 | * True if this node is disabled. |
---|
56 | * @type Boolean |
---|
57 | */ |
---|
58 | this.disabled = attributes.disabled === true; |
---|
59 | |
---|
60 | this.addEvents( |
---|
61 | /** |
---|
62 | * @event textchange |
---|
63 | * Fires when the text for this node is changed |
---|
64 | * @param {Node} this This node |
---|
65 | * @param {String} text The new text |
---|
66 | * @param {String} oldText The old text |
---|
67 | */ |
---|
68 | "textchange", |
---|
69 | /** |
---|
70 | * @event beforeexpand |
---|
71 | * Fires before this node is expanded, return false to cancel. |
---|
72 | * @param {Node} this This node |
---|
73 | * @param {Boolean} deep |
---|
74 | * @param {Boolean} anim |
---|
75 | */ |
---|
76 | "beforeexpand", |
---|
77 | /** |
---|
78 | * @event beforecollapse |
---|
79 | * Fires before this node is collapsed, return false to cancel. |
---|
80 | * @param {Node} this This node |
---|
81 | * @param {Boolean} deep |
---|
82 | * @param {Boolean} anim |
---|
83 | */ |
---|
84 | "beforecollapse", |
---|
85 | /** |
---|
86 | * @event expand |
---|
87 | * Fires when this node is expanded |
---|
88 | * @param {Node} this This node |
---|
89 | */ |
---|
90 | "expand", |
---|
91 | /** |
---|
92 | * @event disabledchange |
---|
93 | * Fires when the disabled status of this node changes |
---|
94 | * @param {Node} this This node |
---|
95 | * @param {Boolean} disabled |
---|
96 | */ |
---|
97 | "disabledchange", |
---|
98 | /** |
---|
99 | * @event collapse |
---|
100 | * Fires when this node is collapsed |
---|
101 | * @param {Node} this This node |
---|
102 | */ |
---|
103 | "collapse", |
---|
104 | /** |
---|
105 | * @event beforeclick |
---|
106 | * Fires before click processing. Return false to cancel the default action. |
---|
107 | * @param {Node} this This node |
---|
108 | * @param {Ext.EventObject} e The event object |
---|
109 | */ |
---|
110 | "beforeclick", |
---|
111 | /** |
---|
112 | * @event click |
---|
113 | * Fires when this node is clicked |
---|
114 | * @param {Node} this This node |
---|
115 | * @param {Ext.EventObject} e The event object |
---|
116 | */ |
---|
117 | "click", |
---|
118 | /** |
---|
119 | * @event checkchange |
---|
120 | * Fires when a node with a checkbox's checked property changes |
---|
121 | * @param {Node} this This node |
---|
122 | * @param {Boolean} checked |
---|
123 | */ |
---|
124 | "checkchange", |
---|
125 | /** |
---|
126 | * @event dblclick |
---|
127 | * Fires when this node is double clicked |
---|
128 | * @param {Node} this This node |
---|
129 | * @param {Ext.EventObject} e The event object |
---|
130 | */ |
---|
131 | "dblclick", |
---|
132 | /** |
---|
133 | * @event contextmenu |
---|
134 | * Fires when this node is right clicked |
---|
135 | * @param {Node} this This node |
---|
136 | * @param {Ext.EventObject} e The event object |
---|
137 | */ |
---|
138 | "contextmenu", |
---|
139 | /** |
---|
140 | * @event beforechildrenrendered |
---|
141 | * Fires right before the child nodes for this node are rendered |
---|
142 | * @param {Node} this This node |
---|
143 | */ |
---|
144 | "beforechildrenrendered" |
---|
145 | ); |
---|
146 | |
---|
147 | var uiClass = this.attributes.uiProvider || this.defaultUI || Ext.tree.TreeNodeUI; |
---|
148 | |
---|
149 | /** |
---|
150 | * Read-only. The UI for this node |
---|
151 | * @type TreeNodeUI |
---|
152 | */ |
---|
153 | this.ui = new uiClass(this); |
---|
154 | }; |
---|
155 | Ext.extend(Ext.tree.TreeNode, Ext.data.Node, { |
---|
156 | preventHScroll: true, |
---|
157 | /** |
---|
158 | * Returns true if this node is expanded |
---|
159 | * @return {Boolean} |
---|
160 | */ |
---|
161 | isExpanded : function(){ |
---|
162 | return this.expanded; |
---|
163 | }, |
---|
164 | |
---|
165 | /** |
---|
166 | * Returns the UI object for this node. |
---|
167 | * @return {TreeNodeUI} The object which is providing the user interface for this tree |
---|
168 | * node. Unless otherwise specified in the {@link #uiProvider}, this will be an instance |
---|
169 | * of {@link Ext.tree.TreeNodeUI} |
---|
170 | */ |
---|
171 | getUI : function(){ |
---|
172 | return this.ui; |
---|
173 | }, |
---|
174 | |
---|
175 | getLoader : function(){ |
---|
176 | var owner; |
---|
177 | return this.loader || ((owner = this.getOwnerTree()) && owner.loader ? owner.loader : new Ext.tree.TreeLoader()); |
---|
178 | }, |
---|
179 | |
---|
180 | // private override |
---|
181 | setFirstChild : function(node){ |
---|
182 | var of = this.firstChild; |
---|
183 | Ext.tree.TreeNode.superclass.setFirstChild.call(this, node); |
---|
184 | if(this.childrenRendered && of && node != of){ |
---|
185 | of.renderIndent(true, true); |
---|
186 | } |
---|
187 | if(this.rendered){ |
---|
188 | this.renderIndent(true, true); |
---|
189 | } |
---|
190 | }, |
---|
191 | |
---|
192 | // private override |
---|
193 | setLastChild : function(node){ |
---|
194 | var ol = this.lastChild; |
---|
195 | Ext.tree.TreeNode.superclass.setLastChild.call(this, node); |
---|
196 | if(this.childrenRendered && ol && node != ol){ |
---|
197 | ol.renderIndent(true, true); |
---|
198 | } |
---|
199 | if(this.rendered){ |
---|
200 | this.renderIndent(true, true); |
---|
201 | } |
---|
202 | }, |
---|
203 | |
---|
204 | // these methods are overridden to provide lazy rendering support |
---|
205 | // private override |
---|
206 | appendChild : function(n){ |
---|
207 | if(!n.render && !Ext.isArray(n)){ |
---|
208 | n = this.getLoader().createNode(n); |
---|
209 | } |
---|
210 | var node = Ext.tree.TreeNode.superclass.appendChild.call(this, n); |
---|
211 | if(node && this.childrenRendered){ |
---|
212 | node.render(); |
---|
213 | } |
---|
214 | this.ui.updateExpandIcon(); |
---|
215 | return node; |
---|
216 | }, |
---|
217 | |
---|
218 | // private override |
---|
219 | removeChild : function(node){ |
---|
220 | this.ownerTree.getSelectionModel().unselect(node); |
---|
221 | Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments); |
---|
222 | // if it's been rendered remove dom node |
---|
223 | if(this.childrenRendered){ |
---|
224 | node.ui.remove(); |
---|
225 | } |
---|
226 | if(this.childNodes.length < 1){ |
---|
227 | this.collapse(false, false); |
---|
228 | }else{ |
---|
229 | this.ui.updateExpandIcon(); |
---|
230 | } |
---|
231 | if(!this.firstChild && !this.isHiddenRoot()) { |
---|
232 | this.childrenRendered = false; |
---|
233 | } |
---|
234 | return node; |
---|
235 | }, |
---|
236 | |
---|
237 | // private override |
---|
238 | insertBefore : function(node, refNode){ |
---|
239 | if(!node.render){ |
---|
240 | node = this.getLoader().createNode(node); |
---|
241 | } |
---|
242 | var newNode = Ext.tree.TreeNode.superclass.insertBefore.apply(this, arguments); |
---|
243 | if(newNode && refNode && this.childrenRendered){ |
---|
244 | node.render(); |
---|
245 | } |
---|
246 | this.ui.updateExpandIcon(); |
---|
247 | return newNode; |
---|
248 | }, |
---|
249 | |
---|
250 | /** |
---|
251 | * Sets the text for this node |
---|
252 | * @param {String} text |
---|
253 | */ |
---|
254 | setText : function(text){ |
---|
255 | var oldText = this.text; |
---|
256 | this.text = text; |
---|
257 | this.attributes.text = text; |
---|
258 | if(this.rendered){ // event without subscribing |
---|
259 | this.ui.onTextChange(this, text, oldText); |
---|
260 | } |
---|
261 | this.fireEvent("textchange", this, text, oldText); |
---|
262 | }, |
---|
263 | |
---|
264 | /** |
---|
265 | * Triggers selection of this node |
---|
266 | */ |
---|
267 | select : function(){ |
---|
268 | this.getOwnerTree().getSelectionModel().select(this); |
---|
269 | }, |
---|
270 | |
---|
271 | /** |
---|
272 | * Triggers deselection of this node |
---|
273 | */ |
---|
274 | unselect : function(){ |
---|
275 | this.getOwnerTree().getSelectionModel().unselect(this); |
---|
276 | }, |
---|
277 | |
---|
278 | /** |
---|
279 | * Returns true if this node is selected |
---|
280 | * @return {Boolean} |
---|
281 | */ |
---|
282 | isSelected : function(){ |
---|
283 | return this.getOwnerTree().getSelectionModel().isSelected(this); |
---|
284 | }, |
---|
285 | |
---|
286 | /** |
---|
287 | * Expand this node. |
---|
288 | * @param {Boolean} deep (optional) True to expand all children as well |
---|
289 | * @param {Boolean} anim (optional) false to cancel the default animation |
---|
290 | * @param {Function} callback (optional) A callback to be called when |
---|
291 | * expanding this node completes (does not wait for deep expand to complete). |
---|
292 | * Called with 1 parameter, this node. |
---|
293 | */ |
---|
294 | expand : function(deep, anim, callback){ |
---|
295 | if(!this.expanded){ |
---|
296 | if(this.fireEvent("beforeexpand", this, deep, anim) === false){ |
---|
297 | return; |
---|
298 | } |
---|
299 | if(!this.childrenRendered){ |
---|
300 | this.renderChildren(); |
---|
301 | } |
---|
302 | this.expanded = true; |
---|
303 | if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){ |
---|
304 | this.ui.animExpand(function(){ |
---|
305 | this.fireEvent("expand", this); |
---|
306 | if(typeof callback == "function"){ |
---|
307 | callback(this); |
---|
308 | } |
---|
309 | if(deep === true){ |
---|
310 | this.expandChildNodes(true); |
---|
311 | } |
---|
312 | }.createDelegate(this)); |
---|
313 | return; |
---|
314 | }else{ |
---|
315 | this.ui.expand(); |
---|
316 | this.fireEvent("expand", this); |
---|
317 | if(typeof callback == "function"){ |
---|
318 | callback(this); |
---|
319 | } |
---|
320 | } |
---|
321 | }else{ |
---|
322 | if(typeof callback == "function"){ |
---|
323 | callback(this); |
---|
324 | } |
---|
325 | } |
---|
326 | if(deep === true){ |
---|
327 | this.expandChildNodes(true); |
---|
328 | } |
---|
329 | }, |
---|
330 | |
---|
331 | isHiddenRoot : function(){ |
---|
332 | return this.isRoot && !this.getOwnerTree().rootVisible; |
---|
333 | }, |
---|
334 | |
---|
335 | /** |
---|
336 | * Collapse this node. |
---|
337 | * @param {Boolean} deep (optional) True to collapse all children as well |
---|
338 | * @param {Boolean} anim (optional) false to cancel the default animation |
---|
339 | */ |
---|
340 | collapse : function(deep, anim){ |
---|
341 | if(this.expanded && !this.isHiddenRoot()){ |
---|
342 | if(this.fireEvent("beforecollapse", this, deep, anim) === false){ |
---|
343 | return; |
---|
344 | } |
---|
345 | this.expanded = false; |
---|
346 | if((this.getOwnerTree().animate && anim !== false) || anim){ |
---|
347 | this.ui.animCollapse(function(){ |
---|
348 | this.fireEvent("collapse", this); |
---|
349 | if(deep === true){ |
---|
350 | this.collapseChildNodes(true); |
---|
351 | } |
---|
352 | }.createDelegate(this)); |
---|
353 | return; |
---|
354 | }else{ |
---|
355 | this.ui.collapse(); |
---|
356 | this.fireEvent("collapse", this); |
---|
357 | } |
---|
358 | } |
---|
359 | if(deep === true){ |
---|
360 | var cs = this.childNodes; |
---|
361 | for(var i = 0, len = cs.length; i < len; i++) { |
---|
362 | cs[i].collapse(true, false); |
---|
363 | } |
---|
364 | } |
---|
365 | }, |
---|
366 | |
---|
367 | // private |
---|
368 | delayedExpand : function(delay){ |
---|
369 | if(!this.expandProcId){ |
---|
370 | this.expandProcId = this.expand.defer(delay, this); |
---|
371 | } |
---|
372 | }, |
---|
373 | |
---|
374 | // private |
---|
375 | cancelExpand : function(){ |
---|
376 | if(this.expandProcId){ |
---|
377 | clearTimeout(this.expandProcId); |
---|
378 | } |
---|
379 | this.expandProcId = false; |
---|
380 | }, |
---|
381 | |
---|
382 | /** |
---|
383 | * Toggles expanded/collapsed state of the node |
---|
384 | */ |
---|
385 | toggle : function(){ |
---|
386 | if(this.expanded){ |
---|
387 | this.collapse(); |
---|
388 | }else{ |
---|
389 | this.expand(); |
---|
390 | } |
---|
391 | }, |
---|
392 | |
---|
393 | /** |
---|
394 | * Ensures all parent nodes are expanded, and if necessary, scrolls |
---|
395 | * the node into view. |
---|
396 | * @param {Function} callback (optional) A function to call when the node has been made visible. |
---|
397 | */ |
---|
398 | ensureVisible : function(callback){ |
---|
399 | var tree = this.getOwnerTree(); |
---|
400 | tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){ |
---|
401 | var node = tree.getNodeById(this.id); // Somehow if we don't do this, we lose changes that happened to node in the meantime |
---|
402 | tree.getTreeEl().scrollChildIntoView(node.ui.anchor); |
---|
403 | Ext.callback(callback); |
---|
404 | }.createDelegate(this)); |
---|
405 | }, |
---|
406 | |
---|
407 | /** |
---|
408 | * Expand all child nodes |
---|
409 | * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes |
---|
410 | */ |
---|
411 | expandChildNodes : function(deep){ |
---|
412 | var cs = this.childNodes; |
---|
413 | for(var i = 0, len = cs.length; i < len; i++) { |
---|
414 | cs[i].expand(deep); |
---|
415 | } |
---|
416 | }, |
---|
417 | |
---|
418 | /** |
---|
419 | * Collapse all child nodes |
---|
420 | * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes |
---|
421 | */ |
---|
422 | collapseChildNodes : function(deep){ |
---|
423 | var cs = this.childNodes; |
---|
424 | for(var i = 0, len = cs.length; i < len; i++) { |
---|
425 | cs[i].collapse(deep); |
---|
426 | } |
---|
427 | }, |
---|
428 | |
---|
429 | /** |
---|
430 | * Disables this node |
---|
431 | */ |
---|
432 | disable : function(){ |
---|
433 | this.disabled = true; |
---|
434 | this.unselect(); |
---|
435 | if(this.rendered && this.ui.onDisableChange){ // event without subscribing |
---|
436 | this.ui.onDisableChange(this, true); |
---|
437 | } |
---|
438 | this.fireEvent("disabledchange", this, true); |
---|
439 | }, |
---|
440 | |
---|
441 | /** |
---|
442 | * Enables this node |
---|
443 | */ |
---|
444 | enable : function(){ |
---|
445 | this.disabled = false; |
---|
446 | if(this.rendered && this.ui.onDisableChange){ // event without subscribing |
---|
447 | this.ui.onDisableChange(this, false); |
---|
448 | } |
---|
449 | this.fireEvent("disabledchange", this, false); |
---|
450 | }, |
---|
451 | |
---|
452 | // private |
---|
453 | renderChildren : function(suppressEvent){ |
---|
454 | if(suppressEvent !== false){ |
---|
455 | this.fireEvent("beforechildrenrendered", this); |
---|
456 | } |
---|
457 | var cs = this.childNodes; |
---|
458 | for(var i = 0, len = cs.length; i < len; i++){ |
---|
459 | cs[i].render(true); |
---|
460 | } |
---|
461 | this.childrenRendered = true; |
---|
462 | }, |
---|
463 | |
---|
464 | // private |
---|
465 | sort : function(fn, scope){ |
---|
466 | Ext.tree.TreeNode.superclass.sort.apply(this, arguments); |
---|
467 | if(this.childrenRendered){ |
---|
468 | var cs = this.childNodes; |
---|
469 | for(var i = 0, len = cs.length; i < len; i++){ |
---|
470 | cs[i].render(true); |
---|
471 | } |
---|
472 | } |
---|
473 | }, |
---|
474 | |
---|
475 | // private |
---|
476 | render : function(bulkRender){ |
---|
477 | this.ui.render(bulkRender); |
---|
478 | if(!this.rendered){ |
---|
479 | // make sure it is registered |
---|
480 | this.getOwnerTree().registerNode(this); |
---|
481 | this.rendered = true; |
---|
482 | if(this.expanded){ |
---|
483 | this.expanded = false; |
---|
484 | this.expand(false, false); |
---|
485 | } |
---|
486 | } |
---|
487 | }, |
---|
488 | |
---|
489 | // private |
---|
490 | renderIndent : function(deep, refresh){ |
---|
491 | if(refresh){ |
---|
492 | this.ui.childIndent = null; |
---|
493 | } |
---|
494 | this.ui.renderIndent(); |
---|
495 | if(deep === true && this.childrenRendered){ |
---|
496 | var cs = this.childNodes; |
---|
497 | for(var i = 0, len = cs.length; i < len; i++){ |
---|
498 | cs[i].renderIndent(true, refresh); |
---|
499 | } |
---|
500 | } |
---|
501 | }, |
---|
502 | |
---|
503 | beginUpdate : function(){ |
---|
504 | this.childrenRendered = false; |
---|
505 | }, |
---|
506 | |
---|
507 | endUpdate : function(){ |
---|
508 | if(this.expanded && this.rendered){ |
---|
509 | this.renderChildren(); |
---|
510 | } |
---|
511 | }, |
---|
512 | |
---|
513 | destroy : function(){ |
---|
514 | if(this.childNodes){ |
---|
515 | for(var i = 0,l = this.childNodes.length; i < l; i++){ |
---|
516 | this.childNodes[i].destroy(); |
---|
517 | } |
---|
518 | this.childNodes = null; |
---|
519 | } |
---|
520 | if(this.ui.destroy){ |
---|
521 | this.ui.destroy(); |
---|
522 | } |
---|
523 | } |
---|
524 | }); |
---|
525 | |
---|
526 | Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode; |
---|