fc62078594264422698ff152cf4ffc7d6be7d824b77162366fb8feebdb05786f10805647e4ce8528ebf6900c95856aa71a86b70ccdfc6a2c761be7e5e109d6 12 KB

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