Tree-min.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.data.Tree = function(A) {
  7. this.nodeHash = {};
  8. this.root = null;
  9. if (A) {
  10. this.setRootNode(A)
  11. }
  12. this.addEvents("append", "remove", "move", "insert", "beforeappend",
  13. "beforeremove", "beforemove", "beforeinsert");
  14. Ext.data.Tree.superclass.constructor.call(this)
  15. };
  16. Ext.extend(Ext.data.Tree, Ext.util.Observable, {
  17. pathSeparator : "/",
  18. proxyNodeEvent : function() {
  19. return this.fireEvent.apply(this, arguments)
  20. },
  21. getRootNode : function() {
  22. return this.root
  23. },
  24. setRootNode : function(A) {
  25. this.root = A;
  26. A.ownerTree = this;
  27. A.isRoot = true;
  28. this.registerNode(A);
  29. return A
  30. },
  31. getNodeById : function(A) {
  32. return this.nodeHash[A]
  33. },
  34. registerNode : function(A) {
  35. this.nodeHash[A.id] = A
  36. },
  37. unregisterNode : function(A) {
  38. delete this.nodeHash[A.id]
  39. },
  40. toString : function() {
  41. return "[Tree" + (this.id ? " " + this.id : "") + "]"
  42. }
  43. });
  44. Ext.data.Node = function(A) {
  45. this.attributes = A || {};
  46. this.leaf = this.attributes.leaf;
  47. this.id = this.attributes.id;
  48. if (!this.id) {
  49. this.id = Ext.id(null, "ynode-");
  50. this.attributes.id = this.id
  51. }
  52. this.childNodes = [];
  53. if (!this.childNodes.indexOf) {
  54. this.childNodes.indexOf = function(D) {
  55. for (var C = 0, B = this.length; C < B; C++) {
  56. if (this[C] == D) {
  57. return C
  58. }
  59. }
  60. return -1
  61. }
  62. }
  63. this.parentNode = null;
  64. this.firstChild = null;
  65. this.lastChild = null;
  66. this.previousSibling = null;
  67. this.nextSibling = null;
  68. this.addEvents({
  69. "append" : true,
  70. "remove" : true,
  71. "move" : true,
  72. "insert" : true,
  73. "beforeappend" : true,
  74. "beforeremove" : true,
  75. "beforemove" : true,
  76. "beforeinsert" : true
  77. });
  78. this.listeners = this.attributes.listeners;
  79. Ext.data.Node.superclass.constructor.call(this)
  80. };
  81. Ext.extend(Ext.data.Node, Ext.util.Observable, {
  82. fireEvent : function(B) {
  83. if (Ext.data.Node.superclass.fireEvent.apply(this, arguments) === false) {
  84. return false
  85. }
  86. var A = this.getOwnerTree();
  87. if (A) {
  88. if (A.proxyNodeEvent.apply(A, arguments) === false) {
  89. return false
  90. }
  91. }
  92. return true
  93. },
  94. isLeaf : function() {
  95. return this.leaf === true
  96. },
  97. setFirstChild : function(A) {
  98. this.firstChild = A
  99. },
  100. setLastChild : function(A) {
  101. this.lastChild = A
  102. },
  103. isLast : function() {
  104. return (!this.parentNode ? true : this.parentNode.lastChild == this)
  105. },
  106. isFirst : function() {
  107. return (!this.parentNode ? true : this.parentNode.firstChild == this)
  108. },
  109. hasChildNodes : function() {
  110. return !this.isLeaf() && this.childNodes.length > 0
  111. },
  112. appendChild : function(E) {
  113. var F = false;
  114. if (E instanceof Array) {
  115. F = E
  116. } else {
  117. if (arguments.length > 1) {
  118. F = arguments
  119. }
  120. }
  121. if (F) {
  122. for (var D = 0, A = F.length; D < A; D++) {
  123. this.appendChild(F[D])
  124. }
  125. } else {
  126. if (this.fireEvent("beforeappend", this.ownerTree, this, E) === false) {
  127. return false
  128. }
  129. var B = this.childNodes.length;
  130. var C = E.parentNode;
  131. if (C) {
  132. if (E.fireEvent("beforemove", E.getOwnerTree(), E, C, this, B) === false) {
  133. return false
  134. }
  135. C.removeChild(E)
  136. }
  137. B = this.childNodes.length;
  138. if (B == 0) {
  139. this.setFirstChild(E)
  140. }
  141. this.childNodes.push(E);
  142. E.parentNode = this;
  143. var G = this.childNodes[B - 1];
  144. if (G) {
  145. E.previousSibling = G;
  146. G.nextSibling = E
  147. } else {
  148. E.previousSibling = null
  149. }
  150. E.nextSibling = null;
  151. this.setLastChild(E);
  152. E.setOwnerTree(this.getOwnerTree());
  153. this.fireEvent("append", this.ownerTree, this, E, B);
  154. if (C) {
  155. E.fireEvent("move", this.ownerTree, E, C, this, B)
  156. }
  157. return E
  158. }
  159. },
  160. removeChild : function(B) {
  161. var A = this.childNodes.indexOf(B);
  162. if (A == -1) {
  163. return false
  164. }
  165. if (this.fireEvent("beforeremove", this.ownerTree, this, B) === false) {
  166. return false
  167. }
  168. this.childNodes.splice(A, 1);
  169. if (B.previousSibling) {
  170. B.previousSibling.nextSibling = B.nextSibling
  171. }
  172. if (B.nextSibling) {
  173. B.nextSibling.previousSibling = B.previousSibling
  174. }
  175. if (this.firstChild == B) {
  176. this.setFirstChild(B.nextSibling)
  177. }
  178. if (this.lastChild == B) {
  179. this.setLastChild(B.previousSibling)
  180. }
  181. B.setOwnerTree(null);
  182. B.parentNode = null;
  183. B.previousSibling = null;
  184. B.nextSibling = null;
  185. this.fireEvent("remove", this.ownerTree, this, B);
  186. return B
  187. },
  188. insertBefore : function(D, A) {
  189. if (!A) {
  190. return this.appendChild(D)
  191. }
  192. if (D == A) {
  193. return false
  194. }
  195. if (this.fireEvent("beforeinsert", this.ownerTree, this, D, A) === false) {
  196. return false
  197. }
  198. var B = this.childNodes.indexOf(A);
  199. var C = D.parentNode;
  200. var E = B;
  201. if (C == this && this.childNodes.indexOf(D) < B) {
  202. E--
  203. }
  204. if (C) {
  205. if (D.fireEvent("beforemove", D.getOwnerTree(), D, C, this, B, A) === false) {
  206. return false
  207. }
  208. C.removeChild(D)
  209. }
  210. if (E == 0) {
  211. this.setFirstChild(D)
  212. }
  213. this.childNodes.splice(E, 0, D);
  214. D.parentNode = this;
  215. var F = this.childNodes[E - 1];
  216. if (F) {
  217. D.previousSibling = F;
  218. F.nextSibling = D
  219. } else {
  220. D.previousSibling = null
  221. }
  222. D.nextSibling = A;
  223. A.previousSibling = D;
  224. D.setOwnerTree(this.getOwnerTree());
  225. this.fireEvent("insert", this.ownerTree, this, D, A);
  226. if (C) {
  227. D.fireEvent("move", this.ownerTree, D, C, this, E, A)
  228. }
  229. return D
  230. },
  231. remove : function() {
  232. this.parentNode.removeChild(this);
  233. return this
  234. },
  235. item : function(A) {
  236. return this.childNodes[A]
  237. },
  238. replaceChild : function(A, B) {
  239. this.insertBefore(A, B);
  240. this.removeChild(B);
  241. return B
  242. },
  243. indexOf : function(A) {
  244. return this.childNodes.indexOf(A)
  245. },
  246. getOwnerTree : function() {
  247. if (!this.ownerTree) {
  248. var A = this;
  249. while (A) {
  250. if (A.ownerTree) {
  251. this.ownerTree = A.ownerTree;
  252. break
  253. }
  254. A = A.parentNode
  255. }
  256. }
  257. return this.ownerTree
  258. },
  259. getDepth : function() {
  260. var B = 0;
  261. var A = this;
  262. while (A.parentNode) {
  263. ++B;
  264. A = A.parentNode
  265. }
  266. return B
  267. },
  268. setOwnerTree : function(B) {
  269. if (B != this.ownerTree) {
  270. if (this.ownerTree) {
  271. this.ownerTree.unregisterNode(this)
  272. }
  273. this.ownerTree = B;
  274. var D = this.childNodes;
  275. for (var C = 0, A = D.length; C < A; C++) {
  276. D[C].setOwnerTree(B)
  277. }
  278. if (B) {
  279. B.registerNode(this)
  280. }
  281. }
  282. },
  283. getPath : function(B) {
  284. B = B || "id";
  285. var D = this.parentNode;
  286. var A = [this.attributes[B]];
  287. while (D) {
  288. A.unshift(D.attributes[B]);
  289. D = D.parentNode
  290. }
  291. var C = this.getOwnerTree().pathSeparator;
  292. return C + A.join(C)
  293. },
  294. bubble : function(C, B, A) {
  295. var D = this;
  296. while (D) {
  297. if (C.apply(B || D, A || [D]) === false) {
  298. break
  299. }
  300. D = D.parentNode
  301. }
  302. },
  303. cascade : function(F, E, B) {
  304. if (F.apply(E || this, B || [this]) !== false) {
  305. var D = this.childNodes;
  306. for (var C = 0, A = D.length; C < A; C++) {
  307. D[C].cascade(F, E, B)
  308. }
  309. }
  310. },
  311. eachChild : function(F, E, B) {
  312. var D = this.childNodes;
  313. for (var C = 0, A = D.length; C < A; C++) {
  314. if (F.apply(E || this, B || [D[C]]) === false) {
  315. break
  316. }
  317. }
  318. },
  319. findChild : function(D, E) {
  320. var C = this.childNodes;
  321. for (var B = 0, A = C.length; B < A; B++) {
  322. if (C[B].attributes[D] == E) {
  323. return C[B]
  324. }
  325. }
  326. return null
  327. },
  328. findChildBy : function(E, D) {
  329. var C = this.childNodes;
  330. for (var B = 0, A = C.length; B < A; B++) {
  331. if (E.call(D || C[B], C[B]) === true) {
  332. return C[B]
  333. }
  334. }
  335. return null
  336. },
  337. sort : function(E, D) {
  338. var C = this.childNodes;
  339. var A = C.length;
  340. if (A > 0) {
  341. var F = D ? function() {
  342. E.apply(D, arguments)
  343. } : E;
  344. C.sort(F);
  345. for (var B = 0; B < A; B++) {
  346. var G = C[B];
  347. G.previousSibling = C[B - 1];
  348. G.nextSibling = C[B + 1];
  349. if (B == 0) {
  350. this.setFirstChild(G)
  351. }
  352. if (B == A - 1) {
  353. this.setLastChild(G)
  354. }
  355. }
  356. }
  357. },
  358. contains : function(A) {
  359. return A.isAncestor(this)
  360. },
  361. isAncestor : function(A) {
  362. var B = this.parentNode;
  363. while (B) {
  364. if (B == A) {
  365. return true
  366. }
  367. B = B.parentNode
  368. }
  369. return false
  370. },
  371. toString : function() {
  372. return "[Node" + (this.id ? " " + this.id : "") + "]"
  373. }
  374. });