f154c2a5e23fea878b2b060b32ba8676bc48ac88d6db94daa647ecea5640a0c45dfa49c55a3aa9fd7dbc48fb5ee97d8a1e9cb769f18f2f880adcd93352b9a8 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { reactive } from 'vue';
  2. import { isNil } from 'lodash-unified';
  3. import { markNodeData, NODE_KEY } from './util.mjs';
  4. import { hasOwn, isArray, isFunction, isString } from '@vue/shared';
  5. import { isBoolean, isUndefined } from '../../../../utils/types.mjs';
  6. const getChildState = (node) => {
  7. let all = true;
  8. let none = true;
  9. let allWithoutDisable = true;
  10. for (let i = 0, j = node.length; i < j; i++) {
  11. const n = node[i];
  12. if (n.checked !== true || n.indeterminate) {
  13. all = false;
  14. if (!n.disabled) {
  15. allWithoutDisable = false;
  16. }
  17. }
  18. if (n.checked !== false || n.indeterminate) {
  19. none = false;
  20. }
  21. }
  22. return { all, none, allWithoutDisable, half: !all && !none };
  23. };
  24. const reInitChecked = function(node) {
  25. if (node.childNodes.length === 0 || node.loading)
  26. return;
  27. const { all, none, half } = getChildState(node.childNodes);
  28. if (all) {
  29. node.checked = true;
  30. node.indeterminate = false;
  31. } else if (half) {
  32. node.checked = false;
  33. node.indeterminate = true;
  34. } else if (none) {
  35. node.checked = false;
  36. node.indeterminate = false;
  37. }
  38. const parent = node.parent;
  39. if (!parent || parent.level === 0)
  40. return;
  41. if (!node.store.checkStrictly) {
  42. reInitChecked(parent);
  43. }
  44. };
  45. const getPropertyFromData = function(node, prop) {
  46. const props = node.store.props;
  47. const data = node.data || {};
  48. const config = props[prop];
  49. if (isFunction(config)) {
  50. return config(data, node);
  51. } else if (isString(config)) {
  52. return data[config];
  53. } else if (isUndefined(config)) {
  54. const dataProp = data[prop];
  55. return isUndefined(dataProp) ? "" : dataProp;
  56. }
  57. };
  58. const setCanFocus = function(childNodes, focus) {
  59. childNodes.forEach((item) => {
  60. item.canFocus = focus;
  61. setCanFocus(item.childNodes, focus);
  62. });
  63. };
  64. let nodeIdSeed = 0;
  65. class Node {
  66. constructor(options) {
  67. this.isLeafByUser = void 0;
  68. this.isLeaf = void 0;
  69. this.id = nodeIdSeed++;
  70. this.text = null;
  71. this.checked = false;
  72. this.indeterminate = false;
  73. this.data = null;
  74. this.expanded = false;
  75. this.parent = null;
  76. this.visible = true;
  77. this.isCurrent = false;
  78. this.canFocus = false;
  79. for (const name in options) {
  80. if (hasOwn(options, name)) {
  81. this[name] = options[name];
  82. }
  83. }
  84. this.level = 0;
  85. this.loaded = false;
  86. this.childNodes = [];
  87. this.loading = false;
  88. if (this.parent) {
  89. this.level = this.parent.level + 1;
  90. }
  91. }
  92. initialize() {
  93. var _a;
  94. const store = this.store;
  95. if (!store) {
  96. throw new Error("[Node]store is required!");
  97. }
  98. store.registerNode(this);
  99. const props = store.props;
  100. if (props && typeof props.isLeaf !== "undefined") {
  101. const isLeaf = getPropertyFromData(this, "isLeaf");
  102. if (isBoolean(isLeaf)) {
  103. this.isLeafByUser = isLeaf;
  104. }
  105. }
  106. if (store.lazy !== true && this.data) {
  107. this.setData(this.data);
  108. if (store.defaultExpandAll) {
  109. this.expanded = true;
  110. this.canFocus = true;
  111. }
  112. } else if (this.level > 0 && store.lazy && store.defaultExpandAll && !this.isLeafByUser) {
  113. this.expand();
  114. }
  115. if (!isArray(this.data)) {
  116. markNodeData(this, this.data);
  117. }
  118. if (!this.data)
  119. return;
  120. const defaultExpandedKeys = store.defaultExpandedKeys;
  121. const key = store.key;
  122. if (key && !isNil(this.key) && defaultExpandedKeys && defaultExpandedKeys.includes(this.key)) {
  123. this.expand(null, store.autoExpandParent);
  124. }
  125. if (key && store.currentNodeKey !== void 0 && this.key === store.currentNodeKey) {
  126. store.currentNode = this;
  127. store.currentNode.isCurrent = true;
  128. }
  129. if (store.lazy) {
  130. store._initDefaultCheckedNode(this);
  131. }
  132. this.updateLeafState();
  133. if (this.level === 1 || ((_a = this.parent) == null ? void 0 : _a.expanded) === true)
  134. this.canFocus = true;
  135. }
  136. setData(data) {
  137. if (!isArray(data)) {
  138. markNodeData(this, data);
  139. }
  140. this.data = data;
  141. this.childNodes = [];
  142. let children;
  143. if (this.level === 0 && isArray(this.data)) {
  144. children = this.data;
  145. } else {
  146. children = getPropertyFromData(this, "children") || [];
  147. }
  148. for (let i = 0, j = children.length; i < j; i++) {
  149. this.insertChild({ data: children[i] });
  150. }
  151. }
  152. get label() {
  153. return getPropertyFromData(this, "label");
  154. }
  155. get key() {
  156. const nodeKey = this.store.key;
  157. if (this.data)
  158. return this.data[nodeKey];
  159. return null;
  160. }
  161. get disabled() {
  162. return getPropertyFromData(this, "disabled");
  163. }
  164. get nextSibling() {
  165. const parent = this.parent;
  166. if (parent) {
  167. const index = parent.childNodes.indexOf(this);
  168. if (index > -1) {
  169. return parent.childNodes[index + 1];
  170. }
  171. }
  172. return null;
  173. }
  174. get previousSibling() {
  175. const parent = this.parent;
  176. if (parent) {
  177. const index = parent.childNodes.indexOf(this);
  178. if (index > -1) {
  179. return index > 0 ? parent.childNodes[index - 1] : null;
  180. }
  181. }
  182. return null;
  183. }
  184. contains(target, deep = true) {
  185. return (this.childNodes || []).some((child) => child === target || deep && child.contains(target));
  186. }
  187. remove() {
  188. const parent = this.parent;
  189. if (parent) {
  190. parent.removeChild(this);
  191. }
  192. }
  193. insertChild(child, index, batch) {
  194. if (!child)
  195. throw new Error("InsertChild error: child is required.");
  196. if (!(child instanceof Node)) {
  197. if (!batch) {
  198. const children = this.getChildren(true);
  199. if (!(children == null ? void 0 : children.includes(child.data))) {
  200. if (isUndefined(index) || index < 0) {
  201. children == null ? void 0 : children.push(child.data);
  202. } else {
  203. children == null ? void 0 : children.splice(index, 0, child.data);
  204. }
  205. }
  206. }
  207. Object.assign(child, {
  208. parent: this,
  209. store: this.store
  210. });
  211. child = reactive(new Node(child));
  212. if (child instanceof Node) {
  213. child.initialize();
  214. }
  215. }
  216. child.level = this.level + 1;
  217. if (isUndefined(index) || index < 0) {
  218. this.childNodes.push(child);
  219. } else {
  220. this.childNodes.splice(index, 0, child);
  221. }
  222. this.updateLeafState();
  223. }
  224. insertBefore(child, ref) {
  225. let index;
  226. if (ref) {
  227. index = this.childNodes.indexOf(ref);
  228. }
  229. this.insertChild(child, index);
  230. }
  231. insertAfter(child, ref) {
  232. let index;
  233. if (ref) {
  234. index = this.childNodes.indexOf(ref);
  235. if (index !== -1)
  236. index += 1;
  237. }
  238. this.insertChild(child, index);
  239. }
  240. removeChild(child) {
  241. const children = this.getChildren() || [];
  242. const dataIndex = children.indexOf(child.data);
  243. if (dataIndex > -1) {
  244. children.splice(dataIndex, 1);
  245. }
  246. const index = this.childNodes.indexOf(child);
  247. if (index > -1) {
  248. this.store && this.store.deregisterNode(child);
  249. child.parent = null;
  250. this.childNodes.splice(index, 1);
  251. }
  252. this.updateLeafState();
  253. }
  254. removeChildByData(data) {
  255. let targetNode = null;
  256. for (let i = 0; i < this.childNodes.length; i++) {
  257. if (this.childNodes[i].data === data) {
  258. targetNode = this.childNodes[i];
  259. break;
  260. }
  261. }
  262. if (targetNode) {
  263. this.removeChild(targetNode);
  264. }
  265. }
  266. expand(callback, expandParent) {
  267. const done = () => {
  268. if (expandParent) {
  269. let parent = this.parent;
  270. while (parent && parent.level > 0) {
  271. parent.expanded = true;
  272. parent = parent.parent;
  273. }
  274. }
  275. this.expanded = true;
  276. if (callback)
  277. callback();
  278. setCanFocus(this.childNodes, true);
  279. };
  280. if (this.shouldLoadData()) {
  281. this.loadData((data) => {
  282. if (isArray(data)) {
  283. if (this.checked) {
  284. this.setChecked(true, true);
  285. } else if (!this.store.checkStrictly) {
  286. reInitChecked(this);
  287. }
  288. done();
  289. }
  290. });
  291. } else {
  292. done();
  293. }
  294. }
  295. doCreateChildren(array, defaultProps = {}) {
  296. array.forEach((item) => {
  297. this.insertChild(Object.assign({ data: item }, defaultProps), void 0, true);
  298. });
  299. }
  300. collapse() {
  301. this.expanded = false;
  302. setCanFocus(this.childNodes, false);
  303. }
  304. shouldLoadData() {
  305. return Boolean(this.store.lazy === true && this.store.load && !this.loaded);
  306. }
  307. updateLeafState() {
  308. if (this.store.lazy === true && this.loaded !== true && typeof this.isLeafByUser !== "undefined") {
  309. this.isLeaf = this.isLeafByUser;
  310. return;
  311. }
  312. const childNodes = this.childNodes;
  313. if (!this.store.lazy || this.store.lazy === true && this.loaded === true) {
  314. this.isLeaf = !childNodes || childNodes.length === 0;
  315. return;
  316. }
  317. this.isLeaf = false;
  318. }
  319. setChecked(value, deep, recursion, passValue) {
  320. this.indeterminate = value === "half";
  321. this.checked = value === true;
  322. if (this.store.checkStrictly)
  323. return;
  324. if (!(this.shouldLoadData() && !this.store.checkDescendants)) {
  325. const { all, allWithoutDisable } = getChildState(this.childNodes);
  326. if (!this.isLeaf && !all && allWithoutDisable) {
  327. this.checked = false;
  328. value = false;
  329. }
  330. const handleDescendants = () => {
  331. if (deep) {
  332. const childNodes = this.childNodes;
  333. for (let i = 0, j = childNodes.length; i < j; i++) {
  334. const child = childNodes[i];
  335. passValue = passValue || value !== false;
  336. const isCheck = child.disabled ? child.checked : passValue;
  337. child.setChecked(isCheck, deep, true, passValue);
  338. }
  339. const { half, all: all2 } = getChildState(childNodes);
  340. if (!all2) {
  341. this.checked = all2;
  342. this.indeterminate = half;
  343. }
  344. }
  345. };
  346. if (this.shouldLoadData()) {
  347. this.loadData(() => {
  348. handleDescendants();
  349. reInitChecked(this);
  350. }, {
  351. checked: value !== false
  352. });
  353. return;
  354. } else {
  355. handleDescendants();
  356. }
  357. }
  358. const parent = this.parent;
  359. if (!parent || parent.level === 0)
  360. return;
  361. if (!recursion) {
  362. reInitChecked(parent);
  363. }
  364. }
  365. getChildren(forceInit = false) {
  366. if (this.level === 0)
  367. return this.data;
  368. const data = this.data;
  369. if (!data)
  370. return null;
  371. const props = this.store.props;
  372. let children = "children";
  373. if (props) {
  374. children = props.children || "children";
  375. }
  376. if (isUndefined(data[children])) {
  377. data[children] = null;
  378. }
  379. if (forceInit && !data[children]) {
  380. data[children] = [];
  381. }
  382. return data[children];
  383. }
  384. updateChildren() {
  385. const newData = this.getChildren() || [];
  386. const oldData = this.childNodes.map((node) => node.data);
  387. const newDataMap = {};
  388. const newNodes = [];
  389. newData.forEach((item, index) => {
  390. const key = item[NODE_KEY];
  391. const isNodeExists = !!key && oldData.findIndex((data) => (data == null ? void 0 : data[NODE_KEY]) === key) >= 0;
  392. if (isNodeExists) {
  393. newDataMap[key] = { index, data: item };
  394. } else {
  395. newNodes.push({ index, data: item });
  396. }
  397. });
  398. if (!this.store.lazy) {
  399. oldData.forEach((item) => {
  400. if (!newDataMap[item == null ? void 0 : item[NODE_KEY]])
  401. this.removeChildByData(item);
  402. });
  403. }
  404. newNodes.forEach(({ index, data }) => {
  405. this.insertChild({ data }, index);
  406. });
  407. this.updateLeafState();
  408. }
  409. loadData(callback, defaultProps = {}) {
  410. if (this.store.lazy === true && this.store.load && !this.loaded && (!this.loading || Object.keys(defaultProps).length)) {
  411. this.loading = true;
  412. const resolve = (children) => {
  413. this.childNodes = [];
  414. this.doCreateChildren(children, defaultProps);
  415. this.loaded = true;
  416. this.loading = false;
  417. this.updateLeafState();
  418. if (callback) {
  419. callback.call(this, children);
  420. }
  421. };
  422. const reject = () => {
  423. this.loading = false;
  424. };
  425. this.store.load(this, resolve, reject);
  426. } else {
  427. if (callback) {
  428. callback.call(this);
  429. }
  430. }
  431. }
  432. eachNode(callback) {
  433. const arr = [this];
  434. while (arr.length) {
  435. const node = arr.shift();
  436. arr.unshift(...node.childNodes);
  437. callback(node);
  438. }
  439. }
  440. reInitChecked() {
  441. if (this.store.checkStrictly)
  442. return;
  443. reInitChecked(this);
  444. }
  445. }
  446. export { Node as default, getChildState };
  447. //# sourceMappingURL=node.mjs.map