d6ed120ee0b6bbfa505ce086872dc3ade50338e6bcd03c30eb33cebb797afb064a097394923ea016df1c1a2bfa33ad876b48c21b30a39c195f7d79075aff1c 9.7 KB

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