node.js 12 KB

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