0c03b48ef473b792cdcec60e29b0b7d567e2e92a2d280c700a21bd77d49c7765ab69a360fecdca898ba9f5a2433e8d8bfb5d80dc545a1495a3a35fd7c7c4f9 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { IndexTreeModel } from './indexTreeModel.js';
  6. import { TreeError } from './tree.js';
  7. import { Iterable } from '../../../common/iterator.js';
  8. export class ObjectTreeModel {
  9. constructor(user, list, options = {}) {
  10. this.user = user;
  11. this.rootRef = null;
  12. this.nodes = new Map();
  13. this.nodesByIdentity = new Map();
  14. this.model = new IndexTreeModel(user, list, null, options);
  15. this.onDidSplice = this.model.onDidSplice;
  16. this.onDidChangeCollapseState = this.model.onDidChangeCollapseState;
  17. this.onDidChangeRenderNodeCount = this.model.onDidChangeRenderNodeCount;
  18. if (options.sorter) {
  19. this.sorter = {
  20. compare(a, b) {
  21. return options.sorter.compare(a.element, b.element);
  22. }
  23. };
  24. }
  25. this.identityProvider = options.identityProvider;
  26. }
  27. setChildren(element, children = Iterable.empty(), options = {}) {
  28. const location = this.getElementLocation(element);
  29. this._setChildren(location, this.preserveCollapseState(children), options);
  30. }
  31. _setChildren(location, children = Iterable.empty(), options) {
  32. const insertedElements = new Set();
  33. const insertedElementIds = new Set();
  34. const onDidCreateNode = (node) => {
  35. var _a;
  36. if (node.element === null) {
  37. return;
  38. }
  39. const tnode = node;
  40. insertedElements.add(tnode.element);
  41. this.nodes.set(tnode.element, tnode);
  42. if (this.identityProvider) {
  43. const id = this.identityProvider.getId(tnode.element).toString();
  44. insertedElementIds.add(id);
  45. this.nodesByIdentity.set(id, tnode);
  46. }
  47. (_a = options.onDidCreateNode) === null || _a === void 0 ? void 0 : _a.call(options, tnode);
  48. };
  49. const onDidDeleteNode = (node) => {
  50. var _a;
  51. if (node.element === null) {
  52. return;
  53. }
  54. const tnode = node;
  55. if (!insertedElements.has(tnode.element)) {
  56. this.nodes.delete(tnode.element);
  57. }
  58. if (this.identityProvider) {
  59. const id = this.identityProvider.getId(tnode.element).toString();
  60. if (!insertedElementIds.has(id)) {
  61. this.nodesByIdentity.delete(id);
  62. }
  63. }
  64. (_a = options.onDidDeleteNode) === null || _a === void 0 ? void 0 : _a.call(options, tnode);
  65. };
  66. this.model.splice([...location, 0], Number.MAX_VALUE, children, Object.assign(Object.assign({}, options), { onDidCreateNode, onDidDeleteNode }));
  67. }
  68. preserveCollapseState(elements = Iterable.empty()) {
  69. if (this.sorter) {
  70. elements = [...elements].sort(this.sorter.compare.bind(this.sorter));
  71. }
  72. return Iterable.map(elements, treeElement => {
  73. let node = this.nodes.get(treeElement.element);
  74. if (!node && this.identityProvider) {
  75. const id = this.identityProvider.getId(treeElement.element).toString();
  76. node = this.nodesByIdentity.get(id);
  77. }
  78. if (!node) {
  79. return Object.assign(Object.assign({}, treeElement), { children: this.preserveCollapseState(treeElement.children) });
  80. }
  81. const collapsible = typeof treeElement.collapsible === 'boolean' ? treeElement.collapsible : node.collapsible;
  82. const collapsed = typeof treeElement.collapsed !== 'undefined' ? treeElement.collapsed : node.collapsed;
  83. return Object.assign(Object.assign({}, treeElement), { collapsible,
  84. collapsed, children: this.preserveCollapseState(treeElement.children) });
  85. });
  86. }
  87. rerender(element) {
  88. const location = this.getElementLocation(element);
  89. this.model.rerender(location);
  90. }
  91. getFirstElementChild(ref = null) {
  92. const location = this.getElementLocation(ref);
  93. return this.model.getFirstElementChild(location);
  94. }
  95. has(element) {
  96. return this.nodes.has(element);
  97. }
  98. getListIndex(element) {
  99. const location = this.getElementLocation(element);
  100. return this.model.getListIndex(location);
  101. }
  102. getListRenderCount(element) {
  103. const location = this.getElementLocation(element);
  104. return this.model.getListRenderCount(location);
  105. }
  106. isCollapsible(element) {
  107. const location = this.getElementLocation(element);
  108. return this.model.isCollapsible(location);
  109. }
  110. setCollapsible(element, collapsible) {
  111. const location = this.getElementLocation(element);
  112. return this.model.setCollapsible(location, collapsible);
  113. }
  114. isCollapsed(element) {
  115. const location = this.getElementLocation(element);
  116. return this.model.isCollapsed(location);
  117. }
  118. setCollapsed(element, collapsed, recursive) {
  119. const location = this.getElementLocation(element);
  120. return this.model.setCollapsed(location, collapsed, recursive);
  121. }
  122. expandTo(element) {
  123. const location = this.getElementLocation(element);
  124. this.model.expandTo(location);
  125. }
  126. refilter() {
  127. this.model.refilter();
  128. }
  129. getNode(element = null) {
  130. if (element === null) {
  131. return this.model.getNode(this.model.rootRef);
  132. }
  133. const node = this.nodes.get(element);
  134. if (!node) {
  135. throw new TreeError(this.user, `Tree element not found: ${element}`);
  136. }
  137. return node;
  138. }
  139. getNodeLocation(node) {
  140. return node.element;
  141. }
  142. getParentNodeLocation(element) {
  143. if (element === null) {
  144. throw new TreeError(this.user, `Invalid getParentNodeLocation call`);
  145. }
  146. const node = this.nodes.get(element);
  147. if (!node) {
  148. throw new TreeError(this.user, `Tree element not found: ${element}`);
  149. }
  150. const location = this.model.getNodeLocation(node);
  151. const parentLocation = this.model.getParentNodeLocation(location);
  152. const parent = this.model.getNode(parentLocation);
  153. return parent.element;
  154. }
  155. getElementLocation(element) {
  156. if (element === null) {
  157. return [];
  158. }
  159. const node = this.nodes.get(element);
  160. if (!node) {
  161. throw new TreeError(this.user, `Tree element not found: ${element}`);
  162. }
  163. return this.model.getNodeLocation(node);
  164. }
  165. }