4d7b9ebae9b8ccceb401dce81dbf0dd7de95e87e84b4d5456f9157aaf998e1a9d9da22dd067a6d2aa9a5ff5ed46b1a07df36c655e7ea9babfcea847eaebf71 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { nextTick } from 'vue';
  2. import { isNil } from 'lodash-unified';
  3. import Node from './node.mjs';
  4. import { getNodeKey } from './util.mjs';
  5. import { hasOwn, NOOP, isObject } from '@vue/shared';
  6. import { isPropAbsent } from '../../../../utils/types.mjs';
  7. class TreeStore {
  8. constructor(options) {
  9. this.lazy = false;
  10. this.checkStrictly = false;
  11. this.autoExpandParent = false;
  12. this.defaultExpandAll = false;
  13. this.checkDescendants = false;
  14. this.currentNode = null;
  15. this.currentNodeKey = null;
  16. for (const option in options) {
  17. if (hasOwn(options, option)) {
  18. this[option] = options[option];
  19. }
  20. }
  21. this.nodesMap = {};
  22. }
  23. initialize() {
  24. this.root = new Node({
  25. data: this.data,
  26. store: this
  27. });
  28. this.root.initialize();
  29. if (this.lazy && this.load) {
  30. const loadFn = this.load;
  31. loadFn(this.root, (data) => {
  32. this.root.doCreateChildren(data);
  33. this._initDefaultCheckedNodes();
  34. }, NOOP);
  35. } else {
  36. this._initDefaultCheckedNodes();
  37. }
  38. }
  39. filter(value) {
  40. const filterNodeMethod = this.filterNodeMethod;
  41. const lazy = this.lazy;
  42. const traverse = async function(node) {
  43. const childNodes = node.root ? node.root.childNodes : node.childNodes;
  44. for (const [index, child] of childNodes.entries()) {
  45. child.visible = !!(filterNodeMethod == null ? void 0 : filterNodeMethod.call(child, value, child.data, child));
  46. if (index % 80 === 0 && index > 0) {
  47. await nextTick();
  48. }
  49. await traverse(child);
  50. }
  51. if (!node.visible && childNodes.length) {
  52. let allHidden = true;
  53. allHidden = !childNodes.some((child) => child.visible);
  54. if (node.root) {
  55. node.root.visible = allHidden === false;
  56. } else {
  57. node.visible = allHidden === false;
  58. }
  59. }
  60. if (!value)
  61. return;
  62. if (node.visible && !node.isLeaf) {
  63. if (!lazy || node.loaded) {
  64. node.expand();
  65. }
  66. }
  67. };
  68. traverse(this);
  69. }
  70. setData(newVal) {
  71. const instanceChanged = newVal !== this.root.data;
  72. if (instanceChanged) {
  73. this.nodesMap = {};
  74. this.root.setData(newVal);
  75. this._initDefaultCheckedNodes();
  76. this.setCurrentNodeKey(this.currentNodeKey);
  77. } else {
  78. this.root.updateChildren();
  79. }
  80. }
  81. getNode(data) {
  82. if (data instanceof Node)
  83. return data;
  84. const key = isObject(data) ? getNodeKey(this.key, data) : data;
  85. return this.nodesMap[key] || null;
  86. }
  87. insertBefore(data, refData) {
  88. var _a;
  89. const refNode = this.getNode(refData);
  90. (_a = refNode.parent) == null ? void 0 : _a.insertBefore({ data }, refNode);
  91. }
  92. insertAfter(data, refData) {
  93. var _a;
  94. const refNode = this.getNode(refData);
  95. (_a = refNode.parent) == null ? void 0 : _a.insertAfter({ data }, refNode);
  96. }
  97. remove(data) {
  98. const node = this.getNode(data);
  99. if (node && node.parent) {
  100. if (node === this.currentNode) {
  101. this.currentNode = null;
  102. }
  103. node.parent.removeChild(node);
  104. }
  105. }
  106. append(data, parentData) {
  107. const parentNode = !isPropAbsent(parentData) ? this.getNode(parentData) : this.root;
  108. if (parentNode) {
  109. parentNode.insertChild({ data });
  110. }
  111. }
  112. _initDefaultCheckedNodes() {
  113. const defaultCheckedKeys = this.defaultCheckedKeys || [];
  114. const nodesMap = this.nodesMap;
  115. defaultCheckedKeys.forEach((checkedKey) => {
  116. const node = nodesMap[checkedKey];
  117. if (node) {
  118. node.setChecked(true, !this.checkStrictly);
  119. }
  120. });
  121. }
  122. _initDefaultCheckedNode(node) {
  123. const defaultCheckedKeys = this.defaultCheckedKeys || [];
  124. if (!isNil(node.key) && defaultCheckedKeys.includes(node.key)) {
  125. node.setChecked(true, !this.checkStrictly);
  126. }
  127. }
  128. setDefaultCheckedKey(newVal) {
  129. if (newVal !== this.defaultCheckedKeys) {
  130. this.defaultCheckedKeys = newVal;
  131. this._initDefaultCheckedNodes();
  132. }
  133. }
  134. registerNode(node) {
  135. const key = this.key;
  136. if (!node || !node.data)
  137. return;
  138. if (!key) {
  139. this.nodesMap[node.id] = node;
  140. } else {
  141. const nodeKey = node.key;
  142. if (!isNil(nodeKey))
  143. this.nodesMap[nodeKey] = node;
  144. }
  145. }
  146. deregisterNode(node) {
  147. const key = this.key;
  148. if (!key || !node || !node.data)
  149. return;
  150. node.childNodes.forEach((child) => {
  151. this.deregisterNode(child);
  152. });
  153. delete this.nodesMap[node.key];
  154. }
  155. getCheckedNodes(leafOnly = false, includeHalfChecked = false) {
  156. const checkedNodes = [];
  157. const traverse = function(node) {
  158. const childNodes = node.root ? node.root.childNodes : node.childNodes;
  159. childNodes.forEach((child) => {
  160. if ((child.checked || includeHalfChecked && child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) {
  161. checkedNodes.push(child.data);
  162. }
  163. traverse(child);
  164. });
  165. };
  166. traverse(this);
  167. return checkedNodes;
  168. }
  169. getCheckedKeys(leafOnly = false) {
  170. return this.getCheckedNodes(leafOnly).map((data) => (data || {})[this.key]);
  171. }
  172. getHalfCheckedNodes() {
  173. const nodes = [];
  174. const traverse = function(node) {
  175. const childNodes = node.root ? node.root.childNodes : node.childNodes;
  176. childNodes.forEach((child) => {
  177. if (child.indeterminate) {
  178. nodes.push(child.data);
  179. }
  180. traverse(child);
  181. });
  182. };
  183. traverse(this);
  184. return nodes;
  185. }
  186. getHalfCheckedKeys() {
  187. return this.getHalfCheckedNodes().map((data) => (data || {})[this.key]);
  188. }
  189. _getAllNodes() {
  190. const allNodes = [];
  191. const nodesMap = this.nodesMap;
  192. for (const nodeKey in nodesMap) {
  193. if (hasOwn(nodesMap, nodeKey)) {
  194. allNodes.push(nodesMap[nodeKey]);
  195. }
  196. }
  197. return allNodes;
  198. }
  199. updateChildren(key, data) {
  200. const node = this.nodesMap[key];
  201. if (!node)
  202. return;
  203. const childNodes = node.childNodes;
  204. for (let i = childNodes.length - 1; i >= 0; i--) {
  205. const child = childNodes[i];
  206. this.remove(child.data);
  207. }
  208. for (let i = 0, j = data.length; i < j; i++) {
  209. const child = data[i];
  210. this.append(child, node.data);
  211. }
  212. }
  213. _setCheckedKeys(key, leafOnly = false, checkedKeys) {
  214. const allNodes = this._getAllNodes().sort((a, b) => a.level - b.level);
  215. const cache = /* @__PURE__ */ Object.create(null);
  216. const keys = Object.keys(checkedKeys);
  217. allNodes.forEach((node) => node.setChecked(false, false));
  218. const cacheCheckedChild = (node) => {
  219. node.childNodes.forEach((child) => {
  220. var _a;
  221. cache[child.data[key]] = true;
  222. if ((_a = child.childNodes) == null ? void 0 : _a.length) {
  223. cacheCheckedChild(child);
  224. }
  225. });
  226. };
  227. for (let i = 0, j = allNodes.length; i < j; i++) {
  228. const node = allNodes[i];
  229. const nodeKey = node.data[key].toString();
  230. const checked = keys.includes(nodeKey);
  231. if (!checked) {
  232. if (node.checked && !cache[nodeKey]) {
  233. node.setChecked(false, false);
  234. }
  235. continue;
  236. }
  237. if (node.childNodes.length) {
  238. cacheCheckedChild(node);
  239. }
  240. if (node.isLeaf || this.checkStrictly) {
  241. node.setChecked(true, false);
  242. continue;
  243. }
  244. node.setChecked(true, true);
  245. if (leafOnly) {
  246. node.setChecked(false, false);
  247. const traverse = function(node2) {
  248. const childNodes = node2.childNodes;
  249. childNodes.forEach((child) => {
  250. if (!child.isLeaf) {
  251. child.setChecked(false, false);
  252. }
  253. traverse(child);
  254. });
  255. };
  256. traverse(node);
  257. }
  258. }
  259. }
  260. setCheckedNodes(array, leafOnly = false) {
  261. const key = this.key;
  262. const checkedKeys = {};
  263. array.forEach((item) => {
  264. checkedKeys[(item || {})[key]] = true;
  265. });
  266. this._setCheckedKeys(key, leafOnly, checkedKeys);
  267. }
  268. setCheckedKeys(keys, leafOnly = false) {
  269. this.defaultCheckedKeys = keys;
  270. const key = this.key;
  271. const checkedKeys = {};
  272. keys.forEach((key2) => {
  273. checkedKeys[key2] = true;
  274. });
  275. this._setCheckedKeys(key, leafOnly, checkedKeys);
  276. }
  277. setDefaultExpandedKeys(keys) {
  278. keys = keys || [];
  279. this.defaultExpandedKeys = keys;
  280. keys.forEach((key) => {
  281. const node = this.getNode(key);
  282. if (node)
  283. node.expand(null, this.autoExpandParent);
  284. });
  285. }
  286. setChecked(data, checked, deep) {
  287. const node = this.getNode(data);
  288. if (node) {
  289. node.setChecked(!!checked, deep);
  290. }
  291. }
  292. getCurrentNode() {
  293. return this.currentNode;
  294. }
  295. setCurrentNode(currentNode) {
  296. const prevCurrentNode = this.currentNode;
  297. if (prevCurrentNode) {
  298. prevCurrentNode.isCurrent = false;
  299. }
  300. this.currentNode = currentNode;
  301. this.currentNode.isCurrent = true;
  302. }
  303. setUserCurrentNode(node, shouldAutoExpandParent = true) {
  304. var _a;
  305. const key = node[this.key];
  306. const currNode = this.nodesMap[key];
  307. this.setCurrentNode(currNode);
  308. if (shouldAutoExpandParent && this.currentNode && this.currentNode.level > 1) {
  309. (_a = this.currentNode.parent) == null ? void 0 : _a.expand(null, true);
  310. }
  311. }
  312. setCurrentNodeKey(key, shouldAutoExpandParent = true) {
  313. var _a;
  314. this.currentNodeKey = key;
  315. if (isPropAbsent(key)) {
  316. this.currentNode && (this.currentNode.isCurrent = false);
  317. this.currentNode = null;
  318. return;
  319. }
  320. const node = this.getNode(key);
  321. if (node) {
  322. this.setCurrentNode(node);
  323. if (shouldAutoExpandParent && this.currentNode && this.currentNode.level > 1) {
  324. (_a = this.currentNode.parent) == null ? void 0 : _a.expand(null, true);
  325. }
  326. }
  327. }
  328. }
  329. export { TreeStore as default };
  330. //# sourceMappingURL=tree-store.mjs.map