nodeCtx.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { TreeNode, Tree, None } from './tree';
  2. export class NodeContext {
  3. // 对树节点数据的引用
  4. ref: TreeNode;
  5. tree: Tree;
  6. // 展开收起
  7. expanded = false;
  8. constructor(node: TreeNode, tree: Tree) {
  9. this.ref = node;
  10. this.tree = tree;
  11. }
  12. ID(): string {
  13. return this.ref.ID;
  14. }
  15. parentID(): string {
  16. return this.ref.parentID;
  17. }
  18. // 节点在完整、排好序的树数据中的行号
  19. row(): number {
  20. return this.tree.data.indexOf(this.ref);
  21. }
  22. // 获取树结构展开显示的行号
  23. visualRow(): number {
  24. let row = 0;
  25. for (const n of this.tree.data) {
  26. if (this.ref === n) return row;
  27. if (n.getCtx().visible()) {
  28. row += 1;
  29. }
  30. }
  31. return row;
  32. }
  33. // 节点在相同父节点下的行号
  34. rowInParent(): number {
  35. return this.brothers().indexOf(this.ref);
  36. }
  37. // 节点的原始seq数据
  38. seq(): number {
  39. return this.ref.seq;
  40. }
  41. // 节点深度,根节点深度为0
  42. depth(): number {
  43. const parent = this.parent();
  44. return parent ? parent.getCtx().depth() + 1 : 0;
  45. }
  46. // 节点是否可见,根据先代节点的expanded就可以计算出来,不需要维护visible属性
  47. visible(): boolean {
  48. let parent = this.parent();
  49. while (parent) {
  50. if (!parent.getCtx().expanded) {
  51. return false;
  52. }
  53. parent = parent.getCtx().parent();
  54. }
  55. return true;
  56. }
  57. parent(): TreeNode | None {
  58. return this.tree.findParent(this.ID());
  59. }
  60. next(): TreeNode | None {
  61. return this.tree.findNext(this.ID());
  62. }
  63. prev(): TreeNode | None {
  64. return this.tree.findPrev(this.ID());
  65. }
  66. // 获取节点子项
  67. children(): TreeNode[] {
  68. return this.tree.parentMap[this.ID()] || [];
  69. }
  70. firstChild(): TreeNode | None {
  71. return this.children()[0] || null;
  72. }
  73. lastChild(): TreeNode | None {
  74. const children = this.children();
  75. return children[children.length - 1] || null;
  76. }
  77. // 获取节点后代(包含嵌套子项)
  78. posterity(): TreeNode[] {
  79. const posterity: TreeNode[] = [];
  80. const getChild = (nodes: TreeNode[]): void => {
  81. nodes.forEach(node => {
  82. posterity.push(node);
  83. const children = node.getCtx().children();
  84. if (children.length) {
  85. getChild(children);
  86. }
  87. });
  88. };
  89. getChild(this.children());
  90. return posterity;
  91. }
  92. posterityCount(): number {
  93. return this.posterity().length;
  94. }
  95. // 获取节点最上层的父项(起源)
  96. progenitor(): TreeNode | None {
  97. let parent = this.parent();
  98. while (parent && parent.getCtx().parent()) {
  99. parent = parent.getCtx().parent();
  100. }
  101. return parent;
  102. }
  103. // 获取节点所有先代(包含嵌套父项)
  104. ancestor(): TreeNode[] {
  105. const ancestor: TreeNode[] = [];
  106. let parent = this.parent();
  107. while (parent) {
  108. ancestor.push(parent);
  109. parent = parent.getCtx().parent();
  110. }
  111. return ancestor;
  112. }
  113. ancestorCount(): number {
  114. return this.ancestor().length;
  115. }
  116. // 获取同层节点
  117. brothers(includeSelf = true): TreeNode[] {
  118. let nodes = this.tree.parentMap[this.parentID()] || [];
  119. if (!includeSelf) {
  120. nodes = nodes.filter(node => node !== this.ref);
  121. }
  122. return nodes;
  123. }
  124. brothersCount(includeSelf = true): number {
  125. return this.brothers(includeSelf).length;
  126. }
  127. // 获取后兄弟节点们
  128. nextBrothers(): TreeNode[] {
  129. const nodes = this.tree.parentMap[this.parentID()] || [];
  130. return nodes.filter(node => node.seq >= this.seq() && node !== this.ref);
  131. }
  132. // 获取前兄弟节点们
  133. prevBrothers(): TreeNode[] {
  134. const nodes = this.tree.parentMap[this.parentID()] || [];
  135. return nodes.filter(node => node.seq <= this.seq() && node !== this.ref);
  136. }
  137. // 只有前节点存在才可上移
  138. canUpMove(): boolean {
  139. return !!this.prev();
  140. }
  141. // 只有后节点存在才可下移
  142. canDownMove(): boolean {
  143. return !!this.next();
  144. }
  145. // 只有父节点存在才可升级
  146. canUpLevel(): boolean {
  147. return !!this.parent();
  148. }
  149. // 只有前节点存在才可降级
  150. canDownLevel(): boolean {
  151. return !!this.prev();
  152. }
  153. }