/hippo/src/main/webapp/ext/src/widgets/tree/TreeNode.js

http://hdbc.googlecode.com/ · JavaScript · 546 lines · 307 code · 36 blank · 203 comment · 84 complexity · f6c708b07b0af0161ad9ca39667196fa MD5 · raw file

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