cache_tree.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.parent = null;
  111. this.nextSibling = null;
  112. this.preSibling = null;
  113. this.expanded = true;
  114. this.visible = true;
  115. this.sourceType = null;
  116. this.source = null;
  117. this.getID = function () {
  118. return ID;
  119. }
  120. };
  121. Node.prototype.getParentID = function () {
  122. return this.parent ? this.parent.getID() : -1;
  123. };
  124. Node.prototype.getNextSiblingID = function () {
  125. return this.nextSibling ? this.nextSibling.getID() : -1;
  126. };
  127. Node.prototype.setParent = function (parent) {
  128. this.parent = parent;
  129. };
  130. Node.prototype.setNextSibling = function (nextSibling) {
  131. this.nextSibling = nextSibling;
  132. if (nextSibling) {
  133. nextSibling.preSibling = this;
  134. }
  135. }
  136. Node.prototype.firstChild = function () {
  137. return this.children.length === 0 ? null : this.children[0];
  138. };
  139. Node.prototype.lastChild = function () {
  140. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  141. };
  142. Node.prototype.depth = function () {
  143. return this.parent ? this.parent.depth() + 1 : 0;
  144. };
  145. Node.prototype.isFirst = function () {
  146. if (this.parent) {
  147. return this.parent.children.indexOf(this) === 0 ? true : false;
  148. } else {
  149. return this.tree.roots.indexOf(this) === 0 ? true : false;
  150. }
  151. };
  152. Node.prototype.isLast = function () {
  153. if (this.parent) {
  154. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  155. } else {
  156. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  157. }
  158. };
  159. Node.prototype.siblingIndex = function () {
  160. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  161. }
  162. Node.prototype.posterityCount = function () {
  163. var iCount = 0;
  164. if (this.children.length !== 0) {
  165. iCount += this.children.length;
  166. this.children.forEach(function (child) {
  167. iCount += child.posterityCount();
  168. });
  169. }
  170. return iCount;
  171. };
  172. Node.prototype.setExpanded = function (expanded) {
  173. var setNodesVisible = function (nodes, visible) {
  174. nodes.forEach(function (node) {
  175. node.visible = visible;
  176. setNodesVisible(node.children, visible && node.expanded);
  177. })
  178. };
  179. this.expanded = expanded;
  180. setNodesVisible(this.children, expanded);
  181. };
  182. Node.prototype.serialNo = function () {
  183. return this.tree.items.indexOf(this);
  184. }
  185. Node.prototype.addChild = function (node) {
  186. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  187. node.parent = this;
  188. if (preSibling) {
  189. preSibling.nextSibling = node;
  190. }
  191. node.preSibling = preSibling;
  192. this.children.push(node);
  193. };
  194. Node.prototype.removeChild = function (node) {
  195. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  196. if (preSibling) {
  197. preSibling.nextSibling = nextSibling;
  198. }
  199. if (nextSibling) {
  200. nextSibling.preSibling = preSibling;
  201. }
  202. this.children.splice(this.children.re)
  203. };
  204. Node.prototype.canUpLevel = function () {
  205. if (this.sourceType === this.tree.owner.Bills.getSourceType()) {
  206. return this.parent ? true : false;
  207. } else {
  208. return false;
  209. }
  210. };
  211. Node.prototype.canDownLevel = function () {
  212. return this.sourceType === this.tree.owner.Bills.getSourceType() ? !this.isFirst() : false;
  213. };
  214. Node.prototype.canUpMove = function () {
  215. return !this.isFirst();
  216. };
  217. Node.prototype.canDownMove = function () {
  218. return !this.isLast();
  219. }
  220. Node.prototype.upLevel = function () {
  221. var success = false,
  222. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  223. if (this.canUpLevel) {
  224. // NextSiblings become child
  225. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  226. // Orginal Parent remove node and nextSiblings
  227. tools.removeNodes(this.tree, this.parent, iIndex);
  228. // New Parent add node
  229. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  230. if (!this.expanded) {
  231. this.setExpanded(true);
  232. }
  233. success = true;
  234. }
  235. return success;
  236. };
  237. Node.prototype.downLevel = function () {
  238. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  239. var newParent = this.preSibling;
  240. if (this.canDownLevel()) {
  241. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  242. tools.addNodes(this.tree, this.preSibling, [this]);
  243. if (!newParent.expanded) {
  244. newParent.setExpanded(true);
  245. }
  246. success = true;
  247. }
  248. return success;
  249. };
  250. Node.prototype.upMove = function () {
  251. var success = false;
  252. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  253. if (this.canUpMove()) {
  254. if (orgPre.preSibling) {
  255. orgPre.preSibling.setNextSibling(this);
  256. } else {
  257. this.preSibling = null;
  258. }
  259. orgPre.setNextSibling(this.nextSibling);
  260. this.setNextSibling(orgPre);
  261. belongArray.splice(iIndex, 1);
  262. belongArray.splice(iIndex - 1, 0, this);
  263. this.tree.sortTreeItems();
  264. success = true;
  265. }
  266. return success;
  267. };
  268. Node.prototype.downMove = function () {
  269. var success = false;
  270. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  271. if (this.canDownMove()) {
  272. if (this.preSibling) {
  273. this.preSibling.setNextSibling(orgNext);
  274. } else if (orgNext) {
  275. orgNext.preSibling = null;
  276. }
  277. this.setNextSibling(orgNext.nextSibling);
  278. orgNext.setNextSibling(this);
  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. // 节点所属固定ID
  287. Node.prototype.getFlag = function() {
  288. if (!this.data || !this.data.flags || !this.data.flags[0] || ! this.data.flags[0].flag) {
  289. return 0;
  290. }
  291. return this.data.flags[0].flag;
  292. };
  293. // 节点所属固定ID
  294. Node.prototype.belongToFlag = function () {
  295. let node = this;
  296. while (node) {
  297. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  298. return node.data.flags[0].flag;
  299. }
  300. node = node.parent;
  301. }
  302. return null;
  303. };
  304. // 获取节点所有后代节点
  305. Node.prototype.getPosterity = function() {
  306. let posterity = [];
  307. getNodes(this.children);
  308. return posterity;
  309. function getNodes(nodes) {
  310. for (let node of nodes) {
  311. posterity.push(node);
  312. if (node.children.length > 0){
  313. getNodes(node.children);
  314. }
  315. }
  316. }
  317. };
  318. var Tree = function (owner) {
  319. this.owner = owner;
  320. this.nodes = {};
  321. this.roots = [];
  322. this.items = [];
  323. this.prefix = 'id_';
  324. this.selected = null;
  325. var _MaxID = 0;
  326. this.newNodeID = function (id) {
  327. if (arguments.length === 0) {
  328. _MaxID += 1;
  329. return _MaxID;
  330. } else {
  331. _MaxID = Math.max(_MaxID, id);
  332. }
  333. };
  334. var rootId = -1;
  335. this.rootID = function () {
  336. return rootId;
  337. }
  338. };
  339. Tree.prototype.addNode = function (parent, nextSibling, id) {
  340. if(!isDef(id) || id == -1){
  341. return null;
  342. }
  343. var newNode = new Node(this, id);
  344. this.nodes[this.prefix + newNode.getID()] = newNode;
  345. if (nextSibling) {
  346. tools.addNodes(this, parent, [newNode], nextSibling.siblingIndex());
  347. } else {
  348. tools.addNodes(this, parent, [newNode]);
  349. }
  350. return newNode;
  351. };
  352. Tree.prototype.sortTreeItems = function () {
  353. var that = this;
  354. var addItems = function (nodes) {
  355. var i;
  356. for (i = 0; i < nodes.length; i++) {
  357. that.items.push(nodes[i]);
  358. addItems(nodes[i].children);
  359. }
  360. };
  361. this.items.splice(0, this.items.length);
  362. addItems(this.roots);
  363. };
  364. Tree.prototype.firstNode = function () {
  365. return this.roots.length === 0 ? null : this.roots[0];
  366. };
  367. Tree.prototype.findNode = function (id) {
  368. return this.nodes[this.prefix + id];
  369. };
  370. Tree.prototype.count = function () {
  371. var iCount = 0;
  372. if (this.roots.length !== 0) {
  373. iCount += this.roots.length;
  374. this.roots.forEach(function (node) {
  375. iCount += node.posterityCount();
  376. });
  377. }
  378. return iCount;
  379. };
  380. Tree.prototype.insert = function (parentID, nextSiblingID, id) {
  381. var parent = !parentID || parentID === -1 ? null : this.nodes[this.prefix + parentID];
  382. var nextSibling = !nextSiblingID || nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  383. var newNode = this.addNode(parent, nextSibling, id);
  384. this.sortTreeItems();
  385. return newNode;
  386. };
  387. Tree.prototype.insertByDatas = function (datas) {
  388. let rst = [];
  389. for(let data of datas){
  390. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  391. rst.push(this.nodes[this.prefix + data.ID]);
  392. }
  393. for(let data of datas){
  394. let node = this.nodes[this.prefix + data.ID];
  395. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  396. node.parent = parent;
  397. if(!parent){
  398. this.roots.push(node);
  399. }
  400. else {
  401. parent.children.push(node);
  402. }
  403. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  404. node.nextSibling = next;
  405. if(next){
  406. next.preSibling = node;
  407. }
  408. }
  409. //resort
  410. this.roots = tools.reSortNodes(this.roots, true);
  411. this.sortTreeItems();
  412. return rst;
  413. };
  414. Tree.prototype.delete = function (node) {
  415. var success = false;
  416. success=this.cascadeRemove(node);
  417. this.sortTreeItems();
  418. this.preSelected = null;
  419. return success;
  420. };
  421. Tree.prototype.cascadeRemove = function (node) {
  422. var success = false;
  423. var me = this;
  424. var removeNodes = function (node) {
  425. delete me.nodes[me.prefix + node.getID()];
  426. for(let ch of node.children){
  427. removeNodes(ch);
  428. }
  429. };
  430. if (node) {
  431. removeNodes(node);
  432. if (node.preSibling) {
  433. node.preSibling.setNextSibling(node.nextSibling);
  434. } else if (node.nextSibling) {
  435. node.nextSibling.preSibling = null;
  436. }
  437. if (node.parent) {
  438. node.parent.children.splice(node.siblingIndex(), 1);
  439. } else {
  440. this.roots.splice(node.siblingIndex(), 1);
  441. }
  442. success = true;
  443. }
  444. return success;
  445. };
  446. Tree.prototype.m_delete = function (nodes) {//删除多个节点,级联删除
  447. for(let node of nodes){
  448. this.cascadeRemove(node);
  449. }
  450. this.sortTreeItems();
  451. return true;
  452. };
  453. Tree.prototype.singleDelete = function (node) {//只删除当前节点,不删除子节点
  454. var success = false;
  455. var me = this;
  456. if(node){
  457. delete me.nodes[me.prefix + node.getID()];
  458. if (node.parent) {//从父项的子节点中移除
  459. node.parent.children.splice(node.siblingIndex(), 1);
  460. } else {
  461. this.roots.splice(node.siblingIndex(), 1);
  462. }
  463. if(node.children.length>0){
  464. if(node.preSibling){//子项变成前兄弟的子项
  465. for(let c of node.children){
  466. node.preSibling.addChild(c);
  467. }
  468. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  469. let oldChild = node.parent.children;
  470. node.parent.children = [];
  471. for(let c of node.children){
  472. node.parent.addChild(c);
  473. }
  474. for(let oc of oldChild){
  475. node.parent.addChild(oc);
  476. }
  477. }else {//都没有的情况
  478. node.parent.children = [];//删除本身节点
  479. for(let c of node.children){
  480. node.parent.addChild(c);
  481. }
  482. }
  483. this.sortTreeItems();
  484. success = true;
  485. }
  486. }
  487. return success
  488. };
  489. Tree.prototype.getAllSubNode = function (node,nodeArray,ignoreID) {
  490. for(let c of node.children){
  491. if(ignoreID && c.data.ID == ignoreID) continue;
  492. nodeArray.push(c);
  493. this.getAllSubNode(c,nodeArray);
  494. }
  495. };
  496. Tree.prototype.getLeavesNodes = function (node) {//取该节点下的所有叶子节点
  497. let leaves = [];
  498. getLeaves(node,leaves);
  499. return leaves;
  500. function getLeaves(node,nodeArr) {
  501. if(node.children.length>0){
  502. for(let ch of node.children){
  503. getLeaves(ch,nodeArr);
  504. }
  505. }else {
  506. nodeArr.push(node);
  507. }
  508. }
  509. };
  510. Tree.prototype.getNodeByID = function (ID) {
  511. let node = this.nodes[this.prefix+ID];
  512. return node;
  513. };
  514. return new Tree(owner);
  515. }
  516. };