tree-store.js 9.2 KB

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