cache_tree.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**
  2. * Created by Mai on 2017/4/5.
  3. */
  4. var cacheTree = {
  5. createNew: function (owner) {
  6. var tools = {
  7. findNode: function (nodes, check) {
  8. for (var i = 0; i < nodes.length; i++) {
  9. if (check(nodes[i])) {
  10. return nodes[i];
  11. }
  12. }
  13. return null;
  14. },
  15. reSortNodes: function (nodes, recursive) {
  16. var temp = [], first;
  17. var findFirstNode = function (nodes) {
  18. return tools.findNode(nodes, function (node) {
  19. return node.preSibling === null;
  20. });
  21. };
  22. var moveNode = function (node, orgArray, newArray, newIndex) {
  23. var next;
  24. orgArray.splice(orgArray.indexOf(node), 1);
  25. newArray.splice(newIndex, 0, node);
  26. if (node.getNextSiblingID() !== -1) {
  27. next = node.nextSibling;
  28. if (next && (orgArray.indexOf(next) >= 0)) {
  29. moveNode(next, orgArray, newArray, newIndex + 1);
  30. }
  31. }
  32. };
  33. if (nodes.length === 0) {
  34. return nodes;
  35. }
  36. if (recursive) {
  37. nodes.forEach(function (node) {
  38. node.children = tools.reSortNodes(node.children, recursive);
  39. });
  40. }
  41. while (nodes.length > 0) {
  42. first = findFirstNode(nodes);
  43. first = first ? first : nodes[0];
  44. moveNode(first, nodes, temp, temp.length);
  45. }
  46. nodes = null;
  47. tools.reSiblingNodes(temp);
  48. return temp;
  49. },
  50. reSiblingNodes: function (nodes) {
  51. var i;
  52. for (i = 0; i < nodes.length; i++) {
  53. nodes[i].preSibling = (i === 0) ? null : nodes[i - 1];
  54. nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
  55. }
  56. },
  57. // ��nodes�У���iIndex����������ʼȫ���Ƴ�
  58. removeNodes: function (tree, parent, iIndex, count) {
  59. var children = parent ? parent.children : tree.roots;
  60. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  61. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  62. if (pre) {
  63. pre.setNextSibling(next);
  64. } else if (next) {
  65. next.preSibling = null;
  66. }
  67. if (arguments.length === 4) {
  68. children.splice(iIndex, count);
  69. } else {
  70. children.splice(iIndex, children.length - iIndex);
  71. }
  72. },
  73. // 在parent.children/tree.roots中增加nodes, 位置从index开始
  74. addNodes: function (tree, parent, nodes, iIndex) {
  75. var children = parent ? parent.children : tree.roots;
  76. var pre, next, i;
  77. if (nodes.length === 0) { return; }
  78. if (arguments.length === 4) {
  79. pre = (iIndex <=0 || iIndex > children.length) ? null : children[iIndex-1];
  80. if(pre==null){
  81. next = iIndex==0?children[0]:null;
  82. }else {
  83. next = pre.nextSibling;
  84. }
  85. } else if (arguments.length === 3) {
  86. pre = children.length === 0 ? null : children[children.length - 1];
  87. next = null;
  88. }
  89. if (pre) {
  90. pre.setNextSibling(nodes[0]);
  91. } else {
  92. nodes[0].preSibling = null;
  93. }
  94. nodes[nodes.length - 1].setNextSibling(next);
  95. for (i = 0; i < nodes.length; i++) {
  96. if (arguments.length === 4) {
  97. children.splice(iIndex + i, 0, nodes[i]);
  98. } else if (arguments.length === 3) {
  99. children.push(nodes[i]);
  100. }
  101. nodes[i].parent = parent;
  102. }
  103. }
  104. };
  105. var Node = function (tree, id) {
  106. var ID = id;
  107. // ���µ����ԣ�����Ԫ��������ֱ���޸�
  108. this.tree = tree;
  109. this.children = [];
  110. this.updateData={};
  111. this.parent = null;
  112. this.nextSibling = null;
  113. this.preSibling = null;
  114. this.expanded = true;
  115. this.visible = true;
  116. this.sourceType = null;
  117. this.source = null;
  118. this.getID = function () {
  119. return ID;
  120. }
  121. };
  122. Node.prototype.getParentID = function () {
  123. return this.parent ? this.parent.getID() : -1;
  124. };
  125. Node.prototype.getNextSiblingID = function () {
  126. return this.nextSibling ? this.nextSibling.getID() : -1;
  127. };
  128. Node.prototype.setParent = function (parent) {
  129. this.parent = parent;
  130. };
  131. Node.prototype.setNextSibling = function (nextSibling) {
  132. this.nextSibling = nextSibling;
  133. if (nextSibling) {
  134. nextSibling.preSibling = this;
  135. }
  136. }
  137. Node.prototype.firstChild = function () {
  138. return this.children.length === 0 ? null : this.children[0];
  139. };
  140. Node.prototype.lastChild = function () {
  141. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  142. };
  143. Node.prototype.depth = function () {
  144. return this.parent ? this.parent.depth() + 1 : 0;
  145. };
  146. Node.prototype.isFirst = function () {
  147. if (this.parent) {
  148. return this.parent.children.indexOf(this) === 0 ? true : false;
  149. } else {
  150. return this.tree.roots.indexOf(this) === 0 ? true : false;
  151. }
  152. };
  153. Node.prototype.isLast = function () {
  154. if (this.parent) {
  155. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  156. } else {
  157. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  158. }
  159. };
  160. Node.prototype.siblingIndex = function () {
  161. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  162. }
  163. Node.prototype.posterityCount = function () {
  164. var iCount = 0;
  165. if (this.children.length !== 0) {
  166. iCount += this.children.length;
  167. this.children.forEach(function (child) {
  168. iCount += child.posterityCount();
  169. });
  170. }
  171. return iCount;
  172. };
  173. // 获取节点所有后代节点
  174. Node.prototype.getPosterity = function() {
  175. let posterity = [];
  176. getNodes(this.children);
  177. return posterity;
  178. function getNodes(nodes) {
  179. for (let node of nodes) {
  180. posterity.push(node);
  181. if (node.children.length > 0){
  182. getNodes(node.children);
  183. }
  184. }
  185. }
  186. };
  187. Node.prototype.setExpanded = function (expanded) {
  188. var setNodesVisible = function (nodes, visible) {
  189. nodes.forEach(function (node) {
  190. node.visible = visible;
  191. setNodesVisible(node.children, visible && node.expanded);
  192. })
  193. };
  194. this.expanded = expanded;
  195. setNodesVisible(this.children, expanded);
  196. };
  197. Node.prototype.serialNo = function () {
  198. return this.tree.items.indexOf(this);
  199. }
  200. Node.prototype.row = function () {
  201. return this.serialNo() + 1;
  202. }
  203. Node.prototype.addChild = function (node) {
  204. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  205. node.parent = this;
  206. if (preSibling) {
  207. preSibling.nextSibling = node;
  208. }
  209. node.preSibling = preSibling;
  210. this.children.push(node);
  211. };
  212. Node.prototype.removeChild = function (node) {
  213. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  214. if (preSibling) {
  215. preSibling.nextSibling = nextSibling;
  216. }
  217. if (nextSibling) {
  218. nextSibling.preSibling = preSibling;
  219. }
  220. this.children.splice(this.children.re)
  221. };
  222. Node.prototype.canUpLevel = function () {
  223. if (this.sourceType === this.tree.owner.Bills.getSourceType()) {
  224. return this.parent ? true : false;
  225. } else {
  226. return false;
  227. }
  228. };
  229. Node.prototype.canDownLevel = function () {
  230. return this.sourceType === this.tree.owner.Bills.getSourceType() ? !this.isFirst() : false;
  231. };
  232. Node.prototype.canUpMove = function () {
  233. return !this.isFirst();
  234. };
  235. Node.prototype.canDownMove = function () {
  236. return !this.isLast();
  237. }
  238. Node.prototype.upLevel = function () {
  239. var success = false,
  240. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  241. if (this.canUpLevel()) {
  242. // NextSiblings become child
  243. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  244. // Orginal Parent remove node and nextSiblings
  245. tools.removeNodes(this.tree, this.parent, iIndex);
  246. // New Parent add node
  247. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  248. if (!this.expanded) {
  249. this.setExpanded(true);
  250. }
  251. success = true;
  252. }
  253. return success;
  254. };
  255. Node.prototype.downLevel = function () {
  256. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  257. var newParent = this.preSibling;
  258. if (this.canDownLevel()) {
  259. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  260. tools.addNodes(this.tree, this.preSibling, [this]);
  261. if (!newParent.expanded) {
  262. newParent.setExpanded(true);
  263. }
  264. success = true;
  265. }
  266. return success;
  267. };
  268. Node.prototype.upMove = function () {
  269. var success = false;
  270. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  271. if (this.canUpMove()) {
  272. if (orgPre.preSibling) {
  273. orgPre.preSibling.setNextSibling(this);
  274. } else {
  275. this.preSibling = null;
  276. }
  277. orgPre.setNextSibling(this.nextSibling);
  278. this.setNextSibling(orgPre);
  279. belongArray.splice(iIndex, 1);
  280. belongArray.splice(iIndex - 1, 0, this);
  281. this.tree.sortTreeItems();
  282. success = true;
  283. }
  284. return success;
  285. };
  286. Node.prototype.downMove = function () {
  287. var success = false;
  288. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  289. if (this.canDownMove()) {
  290. if (this.preSibling) {
  291. this.preSibling.setNextSibling(orgNext);
  292. } else if (orgNext) {
  293. orgNext.preSibling = null;
  294. }
  295. this.setNextSibling(orgNext.nextSibling);
  296. orgNext.setNextSibling(this);
  297. belongArray.splice(iIndex, 1);
  298. belongArray.splice(iIndex + 1, 0, this);
  299. this.tree.sortTreeItems();
  300. success = true;
  301. }
  302. return success;
  303. };
  304. //获取节点的祖先节点(最顶父节点)
  305. Node.prototype.getAncestor = function () {
  306. let node = this;
  307. while (node.parent) {
  308. node = node.parent;
  309. }
  310. return node;
  311. };
  312. // 节点所属固定ID
  313. Node.prototype.getFlag = function () {
  314. if (!this.data || !this.data.flags || !this.data.flags[0] || !this.data.flags[0].flag) {
  315. return 0;
  316. }
  317. return this.data.flags[0].flag;
  318. };
  319. // 节点所属固定ID
  320. Node.prototype.belongToFlag = function () {
  321. let node = this;
  322. while (node) {
  323. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  324. return node.data.flags[0].flag;
  325. }
  326. node = node.parent;
  327. }
  328. return null;
  329. };
  330. // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层
  331. Node.prototype.isBelongToFlags = function (flags) {
  332. let node = this;
  333. while (node) {
  334. const flag = node.getFlag();
  335. if (flags.includes(flag)) {
  336. return true;
  337. }
  338. node = node.parent;
  339. }
  340. return false;
  341. }
  342. var Tree = function (owner) {
  343. this.owner = owner;
  344. this.nodes = {};
  345. this.roots = [];
  346. this.items = [];
  347. this.prefix = 'id_';
  348. this.selected = null;
  349. var _MaxID = 0;
  350. this.newNodeID = function (id) {
  351. if (arguments.length === 0) {
  352. _MaxID += 1;
  353. return _MaxID;
  354. } else {
  355. _MaxID = Math.max(_MaxID, id);
  356. }
  357. };
  358. var rootId = -1;
  359. this.rootID = function () {
  360. return rootId;
  361. }
  362. };
  363. Tree.prototype.addNode = function (parent, nextSibling, id) {
  364. if(!isDef(id) || id == -1){
  365. return null;
  366. }
  367. var newNode = new Node(this, id);
  368. this.nodes[this.prefix + newNode.getID()] = newNode;
  369. if (nextSibling) {
  370. tools.addNodes(this, parent, [newNode], nextSibling.siblingIndex());
  371. } else {
  372. tools.addNodes(this, parent, [newNode]);
  373. }
  374. return newNode;
  375. };
  376. Tree.prototype.sortTreeItems = function () {
  377. var that = this;
  378. var addItems = function (nodes) {
  379. var i;
  380. for (i = 0; i < nodes.length; i++) {
  381. that.items.push(nodes[i]);
  382. addItems(nodes[i].children);
  383. }
  384. };
  385. this.items.splice(0, this.items.length);
  386. addItems(this.roots);
  387. };
  388. Tree.prototype.firstNode = function () {
  389. return this.roots.length === 0 ? null : this.roots[0];
  390. };
  391. Tree.prototype.findNode = function (id) {
  392. return this.nodes[this.prefix + id];
  393. };
  394. Tree.prototype.count = function () {
  395. var iCount = 0;
  396. if (this.roots.length !== 0) {
  397. iCount += this.roots.length;
  398. this.roots.forEach(function (node) {
  399. iCount += node.posterityCount();
  400. });
  401. }
  402. return iCount;
  403. };
  404. Tree.prototype.insert = function (parentID, nextSiblingID, id) {
  405. var parent = !parentID || parentID === -1 ? null : this.nodes[this.prefix + parentID];
  406. var nextSibling = !nextSiblingID || nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  407. var newNode = this.addNode(parent, nextSibling, id);
  408. this.sortTreeItems();
  409. return newNode;
  410. };
  411. // 一次性插入多个连续的同层节点
  412. Tree.prototype.multiInsert = function (datas, preID) {
  413. const newNodes = [];
  414. for (const data of datas) {
  415. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  416. newNodes.push(this.nodes[this.prefix + data.ID]);
  417. }
  418. const fisrtNode = this.nodes[this.prefix + datas[0].ID];
  419. const firstPre = this.nodes[this.prefix + preID];
  420. if (firstPre) {
  421. firstPre.nextSibling = fisrtNode;
  422. firstPre.data.NextSiblingID = fisrtNode.getID();
  423. }
  424. const parent = this.nodes[this.prefix + datas[0].ParentID] || null;
  425. const parentChildren = parent ? parent.children : this.roots;
  426. let baseIndex = firstPre ? parentChildren.indexOf(firstPre) + 1 : 0;
  427. datas.forEach(data => {
  428. const node = this.nodes[this.prefix + data.ID];
  429. node.parent = parent;
  430. parentChildren.splice(baseIndex++, 0, node);
  431. const next = this.nodes[this.prefix + data.NextSiblingID] || null;
  432. node.nextSibling = next;
  433. if (next) {
  434. next.preSibling = node;
  435. }
  436. });
  437. this.roots = tools.reSortNodes(this.roots, true);
  438. this.sortTreeItems();
  439. return newNodes;
  440. }
  441. // 插入某一完整片段到某节点的子项中
  442. Tree.prototype.insertByDatas = function (datas) {
  443. let rst = [];
  444. for(let data of datas){
  445. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  446. rst.push(this.nodes[this.prefix + data.ID]);
  447. }
  448. for(let data of datas){
  449. let node = this.nodes[this.prefix + data.ID];
  450. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  451. node.parent = parent;
  452. if(!parent){
  453. this.roots.push(node);
  454. }
  455. else {
  456. parent.children.push(node);
  457. }
  458. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  459. node.nextSibling = next;
  460. if(next){
  461. //将原本nextSibing的前节点的设置为node的上兄弟节点
  462. if (next.preSibling && next.preSibling.nextSibling !== node) {
  463. next.preSibling.nextSibling = node;
  464. next.preSibling.data.NextSiblingID = data.ID;
  465. node.preSibling = next.preSibling;
  466. }
  467. next.preSibling = node;
  468. } else {
  469. let lastChild = parent ? parent.children[parent.children.length - 2] : this.roots[this.roots.length - 2];
  470. if (lastChild && lastChild.nextSibling !== node) {
  471. lastChild.nextSibling = node;
  472. lastChild.data.NextSiblingID = data.ID;
  473. }
  474. }
  475. }
  476. //resort
  477. this.roots = tools.reSortNodes(this.roots, true);
  478. this.sortTreeItems();
  479. return rst;
  480. };
  481. Tree.prototype.delete = function (node,resetPre=true) {
  482. var success = false;
  483. success=this.cascadeRemove(node);
  484. this.sortTreeItems();
  485. if(resetPre) this.preSelected = null;
  486. return success;
  487. };
  488. Tree.prototype.cascadeRemove = function (node) {
  489. var success = false;
  490. var me = this;
  491. var removeNodes = function (node) {
  492. delete me.nodes[me.prefix + node.getID()];
  493. for(let ch of node.children){
  494. removeNodes(ch);
  495. }
  496. };
  497. if (node) {
  498. removeNodes(node);
  499. if (node.preSibling) {
  500. node.preSibling.setNextSibling(node.nextSibling);
  501. } else if (node.nextSibling) {
  502. node.nextSibling.preSibling = null;
  503. }
  504. if (node.parent) {
  505. node.parent.children.splice(node.siblingIndex(), 1);
  506. } else {
  507. this.roots.splice(node.siblingIndex(), 1);
  508. }
  509. success = true;
  510. }
  511. return success;
  512. };
  513. Tree.prototype.m_delete = function (nodes) {//删除多个节点,级联删除
  514. for(let node of nodes){
  515. this.cascadeRemove(node);
  516. }
  517. this.sortTreeItems();
  518. return true;
  519. };
  520. Tree.prototype.singleDelete = function (node) {//只删除当前节点,不删除子节点
  521. var success = false;
  522. var me = this;
  523. if(node){
  524. delete me.nodes[me.prefix + node.getID()];
  525. if (node.parent) {//从父项的子节点中移除
  526. node.parent.children.splice(node.siblingIndex(), 1);
  527. } else {
  528. this.roots.splice(node.siblingIndex(), 1);
  529. }
  530. if(node.children.length>0){
  531. if(node.preSibling){//子项变成前兄弟的子项
  532. for(let c of node.children){
  533. node.preSibling.addChild(c);
  534. }
  535. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  536. let oldChild = node.parent.children;
  537. node.parent.children = [];
  538. for(let c of node.children){
  539. node.parent.addChild(c);
  540. }
  541. for(let oc of oldChild){
  542. node.parent.addChild(oc);
  543. }
  544. }else {//都没有的情况
  545. node.parent.children = [];//删除本身节点
  546. for(let c of node.children){
  547. node.parent.addChild(c);
  548. }
  549. }
  550. this.sortTreeItems();
  551. success = true;
  552. }
  553. }
  554. return success
  555. };
  556. Tree.prototype.getAllSubNode = function (node,nodeArray) {
  557. for(let c of node.children){
  558. nodeArray.push(c);
  559. this.getAllSubNode(c,nodeArray);
  560. }
  561. };
  562. Tree.prototype.getLeavesNodes = function (node) {//取该节点下的所有叶子节点
  563. let leaves = [];
  564. getLeaves(node,leaves);
  565. return leaves;
  566. function getLeaves(node,nodeArr) {
  567. if(node.children.length>0){
  568. for(let ch of node.children){
  569. getLeaves(ch,nodeArr);
  570. }
  571. }else {
  572. nodeArr.push(node);
  573. }
  574. }
  575. };
  576. Tree.prototype.getNodeByID = function (ID) {
  577. let node = this.nodes[this.prefix+ID];
  578. return node;
  579. };
  580. return new Tree(owner);
  581. }
  582. };