path_tree.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /**
  2. * 构建pathTree
  3. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  4. * @param {Object} setting - 设置
  5. * @returns {PathTree}
  6. */
  7. 'use strict';
  8. class PosData {
  9. /**
  10. * 构造函数
  11. * @param {id|Number, masterId|Number} setting
  12. */
  13. constructor (setting) {
  14. // 无索引
  15. this.datas = null;
  16. // 以key为索引
  17. this.items = {};
  18. // 以分类id为索引的有序
  19. this.masterRange = {};
  20. // pos设置
  21. this.setting = JSON.parse(JSON.stringify(setting));
  22. }
  23. /**
  24. * 加载部位明细数据
  25. * @param datas
  26. */
  27. loadDatas(datas) {
  28. this.datas = datas;
  29. for (const data of this.datas) {
  30. const key = itemsPre + data[this.setting.id];
  31. this.items[key] = data;
  32. const masterKey = itemsPre + data[this.setting.masterId];
  33. if (!this.masterRange[masterKey]) {
  34. this.masterRange[masterKey] = [];
  35. }
  36. this.masterRange[masterKey].push(data);
  37. }
  38. }
  39. /**
  40. * 更新数据
  41. * @param datas
  42. */
  43. updateDatas(data) {
  44. const datas = data instanceof Array ? data : [data];
  45. for (const d of datas) {
  46. const key = itemsPre + d[this.setting.id];
  47. if (!this.items[key]) {
  48. this.datas.push(d);
  49. this.items[key] = d;
  50. const masterKey = itemsPre + d[this.setting.masterId];
  51. if (!this.masterRange[masterKey]) {
  52. this.masterRange[masterKey] = [];
  53. }
  54. this.masterRange[masterKey].push(d);
  55. } else {
  56. const pos = this.items[key];
  57. for (const prop in d) {
  58. pos[prop] = d[prop];
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * 移除数据
  65. * @param datas
  66. */
  67. removeDatas(data) {
  68. const datas = data instanceof Array ? data : [data];
  69. for (let i = datas.length - 1; i >= 0; i--) {
  70. const d = datas[i];
  71. this.datas.splice(this.datas.indexOf(d), 1);
  72. const key = itemsPre + d[this.setting.id];
  73. delete this.items[key];
  74. const masterKey = itemsPre + d[this.setting.masterId];
  75. const range = this.masterRange[masterKey];
  76. range.splice(range.indexOf(d), 1);
  77. if (range.length === 0) {
  78. delete this.masterRange[masterKey];
  79. }
  80. }
  81. }
  82. /**
  83. * 移除数据 - 根据分类id
  84. * @param mid
  85. */
  86. removeDatasByMasterId (mid) {
  87. const masterKey = itemsPre + mid;
  88. const range = this.masterRange(mid);
  89. delete this.masterRange[masterKey];
  90. for (const r of range) {
  91. this.datas.splice(this.datas.indexOf(r), 1);
  92. const key = itemsPre + r[this.setting.id];
  93. delete this.items[key];
  94. }
  95. }
  96. getPos(id) {
  97. return this.items[itemsPre + id];
  98. }
  99. getMasterRange(mid) {
  100. return this.masterRange[itemsPre + mid];
  101. }
  102. }
  103. const itemsPre = 'id_';
  104. const createNewPathTree = function (type, setting) {
  105. class BaseTree {
  106. /**
  107. * 构造函数
  108. */
  109. constructor (setting) {
  110. // 无索引
  111. this.datas = [];
  112. // 以key为索引
  113. this.items = {};
  114. // 以排序为索引
  115. this.nodes = [];
  116. // 索引
  117. this.children = {};
  118. // 树设置
  119. this.setting = JSON.parse(JSON.stringify(setting));
  120. }
  121. /**
  122. * 树结构根据显示排序
  123. */
  124. sortTreeNode (isResort) {
  125. const self = this;
  126. const addSortNodes = function (nodes) {
  127. if (!nodes) { return }
  128. for (let i = 0; i < nodes.length; i++) {
  129. self.nodes.push(nodes[i]);
  130. nodes[i].index = self.nodes.length - 1;
  131. if (!isResort) {
  132. nodes[i].children = self.getChildren(nodes[i]);
  133. }
  134. addSortNodes(nodes[i].children);
  135. }
  136. };
  137. self.nodes = [];
  138. addSortNodes(this.getChildren(null));
  139. }
  140. /**
  141. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  142. * @param datas
  143. */
  144. loadDatas (datas) {
  145. // 清空旧数据
  146. this.items = {};
  147. this.nodes = [];
  148. // 加载全部数据
  149. for (const data of datas) {
  150. const keyName = itemsPre + data[this.setting.id];
  151. this.items[keyName] = JSON.parse(JSON.stringify(data));
  152. this.datas.push(this.items[keyName]);
  153. }
  154. this.sortTreeNode();
  155. for (const node of this.nodes) {
  156. node.expanded = node.children.length > 0;
  157. node.visible = true;
  158. }
  159. }
  160. /**
  161. * 根据id获取树结构节点数据
  162. * @param {Number} id
  163. * @returns {Object}
  164. */
  165. getItems (id) {
  166. return this.items[itemsPre + id];
  167. };
  168. /**
  169. * 查找node的parent
  170. * @param {Object} node
  171. * @returns {Object}
  172. */
  173. getParent (node) {
  174. return this.getItems(node[this.setting.pid]);
  175. };
  176. /**
  177. * 根据path查找完整节点
  178. * @param {Number} path
  179. */
  180. getFullPathNodes (path) {
  181. const self = this, ids = path.split('.');
  182. if (ids.length > 0) {
  183. return this.nodes.filter((x) => {
  184. return ids.indexOf('' + x[self.setting.id]) >= 0;
  185. });
  186. } else {
  187. return [];
  188. }
  189. };
  190. /**
  191. * 查询node的已下载子节点
  192. * @param {Object} node
  193. * @returns {Array}
  194. */
  195. getChildren (node) {
  196. const setting = this.setting;
  197. const pid = node ? node[setting.id] : setting.rootId;
  198. const children = this.datas.filter(function (x) {
  199. return x[setting.pid] === pid;
  200. });
  201. children.sort(function (a, b) {
  202. return a.order - b.order;
  203. });
  204. return children;
  205. };
  206. /**
  207. * 查询node的已下载的全部后代
  208. * @param {Object} node
  209. * @returns {Array}
  210. */
  211. getPosterity (node) {
  212. const reg = new RegExp('^' + node.full_path + '.');
  213. return this.datas.filter(function (x) {
  214. return reg.test(x.full_path);
  215. })
  216. };
  217. /**
  218. * 查询node是否是父节点的最后一个子节点
  219. * @param {Object} node
  220. * @returns {boolean}
  221. */
  222. isLastSibling (node) {
  223. const siblings = this.getChildren(this.getParent(node));
  224. return node.order === siblings[siblings.length - 1].order;
  225. };
  226. /**
  227. * 刷新子节点是否可见
  228. * @param {Object} node
  229. * @private
  230. */
  231. _refreshChildrenVisible (node) {
  232. if (!node.children) {
  233. node.children = this.getChildren(node);
  234. }
  235. if (node.children && node.children.length > 0) {
  236. for (const child of node.children) {
  237. child.visible = node.expanded && node.visible;
  238. this._refreshChildrenVisible(child);
  239. }
  240. }
  241. };
  242. /**
  243. * 设置节点是否展开, 并控制子节点可见
  244. * @param {Object} node
  245. * @param {Boolean} expanded
  246. */
  247. setExpanded (node, expanded) {
  248. node.expanded = expanded;
  249. this._refreshChildrenVisible(node);
  250. };
  251. /**
  252. * 提取节点key和索引数据
  253. * @param {Object} node - 节点
  254. * @returns {key}
  255. */
  256. getNodeKeyData (node) {
  257. const data = {};
  258. for (const key of this.setting.keys) {
  259. data[key] = node[key];
  260. }
  261. return data;
  262. };
  263. /**
  264. * 得到树结构构成id
  265. * @param node
  266. * @returns {*}
  267. */
  268. getNodeKey (node) {
  269. return node[this.setting.id];
  270. };
  271. }
  272. class MeasureTree extends BaseTree {
  273. addData (datas) {
  274. const loadedData = [];
  275. for (const data of datas) {
  276. let node = this.getItems(data[this.setting.id]);
  277. if (node) {
  278. for (const prop in node) {
  279. if (data[prop] !== undefined) {
  280. node[prop] = data[prop];
  281. }
  282. }
  283. loadedData.push(node);
  284. } else {
  285. const keyName = itemsPre + data[this.setting.id];
  286. const node = JSON.parse(JSON.stringify(data));
  287. this.items[keyName] = node;
  288. this.datas.push(node);
  289. node.expanded = false;
  290. node.visible = true;
  291. loadedData.push(node);
  292. }
  293. }
  294. this.sortTreeNode();
  295. for (const node of loadedData) {
  296. const children = node.children;
  297. if (!node.expanded && children.length > 0) {
  298. node.expanded = true;
  299. this._refreshChildrenVisible(node);
  300. }
  301. }
  302. return loadedData;
  303. }
  304. removeData (datas) {
  305. datas.sort(function (a, b) {
  306. return b.level - a.level;
  307. });
  308. console.log(datas);
  309. const removeArrayData = function (array, data) {
  310. const index = array.indexOf(data);
  311. array.splice(index, 1);
  312. };
  313. for (const data of datas) {
  314. const node = this.getItems(data[this.setting.id]);
  315. if (node && this.getChildren(node).length === 0) {
  316. delete this.items[itemsPre + node[this.setting.id]];
  317. if (node[this.setting.pid] !== this.setting.rootId) {
  318. const parent = this.items[itemsPre + node[this.setting.pid]];
  319. removeArrayData(parent.children, node);
  320. }
  321. removeArrayData(this.datas, node);
  322. removeArrayData(this.nodes, node);
  323. }
  324. }
  325. };
  326. loadLeafData (data) {
  327. const datas = data instanceof Array ? data : [data];
  328. for (const d of datas) {
  329. let node = this.getItems(d[this.setting.id]);
  330. if (node && node.is_leaf) {
  331. for (const prop in node) {
  332. if (data[prop] !== undefined) {
  333. node[prop] = d[prop];
  334. }
  335. }
  336. }
  337. }
  338. };
  339. }
  340. class ActiveTree extends BaseTree {
  341. /**
  342. * 加载数据(动态),只加载不同部分
  343. * @param {Array} datas
  344. * @return {Array} 加载到树的数据
  345. * @privateA
  346. */
  347. _loadData (datas) {
  348. const loadedData = [];
  349. for (const data of datas) {
  350. let node = this.getItems(data[this.setting.id]);
  351. if (node) {
  352. for (const prop in node) {
  353. if (data[prop] !== undefined) {
  354. node[prop] = data[prop];
  355. }
  356. }
  357. loadedData.push(node);
  358. } else {
  359. const keyName = itemsPre + data[this.setting.id];
  360. const node = JSON.parse(JSON.stringify(data));
  361. this.items[keyName] = node;
  362. this.datas.push(node);
  363. node.expanded = false;
  364. node.visible = true;
  365. loadedData.push(node);
  366. }
  367. }
  368. this.sortTreeNode();
  369. for (const node of loadedData) {
  370. const children = node.children;
  371. if (!node.expanded && children.length > 0) {
  372. node.expanded = true;
  373. this._refreshChildrenVisible(node);
  374. }
  375. }
  376. return loadedData;
  377. };
  378. /**
  379. * 以下方法需等待响应, 通过callback刷新界面
  380. */
  381. /**
  382. * 加载子节点
  383. * @param {Object} node
  384. * @param {function} callback
  385. */
  386. loadChildren (node, callback) {
  387. const self = this;
  388. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  389. console.log(url);
  390. postData(url, this.getNodeKeyData(node), function (data) {
  391. self._loadData(data);
  392. callback();
  393. });
  394. };
  395. }
  396. class LedgerTree extends BaseTree {
  397. /**
  398. * 加载数据(动态),只加载不同部分
  399. * @param {Array} datas
  400. * @return {Array} 加载到树的数据
  401. * @privateA
  402. */
  403. _updateData (datas) {
  404. datas = datas instanceof Array ? datas : [datas];
  405. const loadedData = [];
  406. for (const data of datas) {
  407. let node = this.getItems(data[this.setting.id]);
  408. if (node) {
  409. for (const prop in node) {
  410. if (data[prop] !== undefined) {
  411. node[prop] = data[prop];
  412. }
  413. }
  414. loadedData.push(node);
  415. }
  416. }
  417. for (const node of loadedData) {
  418. const children = this.getChildren(node);
  419. node.expanded = children.length > 0 && children[0].visible;
  420. }
  421. this.sortTreeNode(true);
  422. return loadedData;
  423. };
  424. /**
  425. * 加载数据(动态),只加载不同部分
  426. * @param {Array} datas
  427. * @return {Array} 加载到树的数据
  428. * @privateA
  429. */
  430. _loadData (datas) {
  431. datas = datas instanceof Array ? datas : [datas];
  432. const loadedData = [], resortData = [];
  433. for (const data of datas) {
  434. let node = this.getItems(data[this.setting.id]);
  435. if (node) {
  436. const parent = this.getItems(node[this.setting.pid]);
  437. for (const prop in node) {
  438. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  439. node[prop] = data[prop];
  440. if (parent && resortData.indexOf(parent) === -1) {
  441. resortData.push(parent);
  442. }
  443. }
  444. }
  445. loadedData.push(node);
  446. } else {
  447. const keyName = itemsPre + data[this.setting.id];
  448. const node = JSON.parse(JSON.stringify(data));
  449. this.items[keyName] = node;
  450. this.datas.push(node);
  451. node.expanded = false;
  452. node.visible = true;
  453. loadedData.push(node);
  454. if (resortData.indexOf(node) === -1) {
  455. resortData.push(node);
  456. }
  457. const parent = this.getItems(node[this.setting.pid]);
  458. if (parent && resortData.indexOf(parent) === -1) {
  459. resortData.push(parent);
  460. }
  461. }
  462. }
  463. for (const node of resortData) {
  464. node.children = this.getChildren(node);
  465. }
  466. this.sortTreeNode(true);
  467. for (const node of loadedData) {
  468. if (!node.expanded) {
  469. this.setExpanded(node, true);
  470. }
  471. }
  472. return loadedData;
  473. };
  474. /**
  475. * 清理数据(动态)
  476. * @param datas
  477. * @private
  478. */
  479. _freeData (datas) {
  480. datas = datas instanceof Array ? datas : [datas];
  481. const freeDatas = [];
  482. const removeArrayData = function (array, data) {
  483. const index = array.indexOf(data);
  484. array.splice(index, 1);
  485. };
  486. for (const data of datas) {
  487. const node = this.getItems(data[this.setting.id]);
  488. if (node) {
  489. freeDatas.push(node);
  490. delete this.items[itemsPre + node[this.setting.id]];
  491. if (node[this.setting.pid] !== this.setting.rootId) {
  492. const parent = this.getItems(node[this.setting.pid]);
  493. if (parent) {
  494. removeArrayData(parent.children, node);
  495. }
  496. }
  497. removeArrayData(this.datas, node);
  498. removeArrayData(this.nodes, node);
  499. }
  500. }
  501. return freeDatas;
  502. };
  503. /**
  504. * 加载需展开的数据
  505. * @param {Array} datas
  506. * @returns {Array}
  507. * @private
  508. */
  509. _loadExpandData (datas) {
  510. datas = datas instanceof Array ? datas : [datas];
  511. const loadedData = [], existData = [], expandData = [], resortData = [];
  512. for (const data of datas) {
  513. let node = this.getItems(data[this.setting.id]);
  514. if (node) {
  515. existData.push(node);
  516. } else {
  517. const keyName = itemsPre + data[this.setting.id];
  518. const node = JSON.parse(JSON.stringify(data));
  519. this.items[keyName] = node;
  520. this.datas.push(node);
  521. node.expanded = false;
  522. node.visible = true;
  523. loadedData.push(node);
  524. if (resortData.indexOf(node) === -1) {
  525. resortData.push(node);
  526. }
  527. const parent = this.getItems(node[this.setting.pid]);
  528. if (parent && resortData.indexOf(parent) === -1) {
  529. resortData.push(parent);
  530. }
  531. }
  532. }
  533. for (const node of resortData) {
  534. node.children = this.getChildren(node);
  535. }
  536. this.sortTreeNode(true);
  537. for (const node of loadedData) {
  538. if (!node.expanded) {
  539. this.setExpanded(node, true);
  540. }
  541. }
  542. for (const node of existData) {
  543. const parent = this.getItems(node[this.setting.pid]);
  544. if (expandData.indexOf(parent) === -1) {
  545. expandData.push(parent);
  546. if (!parent.expanded) {
  547. this.setExpanded(parent, true);
  548. }
  549. }
  550. if (!node.expanded) {
  551. this.setExpanded(node, true);
  552. }
  553. }
  554. return [loadedData, expandData];
  555. };
  556. /**
  557. *
  558. * @param parent
  559. * @param node
  560. * @private
  561. */
  562. _getNodesParents(parents, nodes) {
  563. for (const node of nodes) {
  564. const parent = this.getParent(node);
  565. if (parent) {
  566. const paths = this.getFullPathNodes(parent.full_path);
  567. for (const p of paths) {
  568. if (parents.indexOf(p) === -1) {
  569. parents.push(p);
  570. }
  571. }
  572. }
  573. }
  574. }
  575. isLeafXmj(node) {
  576. for (const child of node.children) {
  577. if (child.code !== '') {
  578. return false;
  579. }
  580. }
  581. return true;
  582. }
  583. /**
  584. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  585. *
  586. * @param data - 更新的数据 {update, create, delete}
  587. * @param {function} callback - 界面刷新
  588. */
  589. loadPostData(data, callback) {
  590. const result = {}, parents = [];
  591. if (data.update) {
  592. result.update = this._updateData(data.update);
  593. this._getNodesParents(parents, result.update);
  594. }
  595. if (data.create) {
  596. result.create = this._loadData(data.create);
  597. this._getNodesParents(parents, result.create);
  598. }
  599. if (data.delete) {
  600. result.delete = this._freeData(data.delete);
  601. this._getNodesParents(parents, result.delete);
  602. }
  603. parents.sort((a, b) => {
  604. return b.level - a.level;
  605. });
  606. for (const parent of parents) {
  607. treeCalc.calculateNode(this, parent, ['total_price']);
  608. }
  609. result.update = result.update ? result.update.concat(parents) : parents;
  610. callback(result);
  611. }
  612. /**
  613. * 以下方法需等待响应, 通过callback刷新界面
  614. */
  615. /**
  616. * 加载子节点
  617. * @param {Object} node
  618. * @param {function} callback
  619. */
  620. loadChildren (node, callback) {
  621. const self = this;
  622. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  623. console.log(url);
  624. postData(url, this.getNodeKeyData(node), function (data) {
  625. self._loadData(data);
  626. callback();
  627. });
  628. };
  629. /**
  630. * 树结构基本操作
  631. * @param {String} url - 请求地址
  632. * @param {Object} node - 操作节点
  633. * @param {String} type - 操作类型
  634. * @param {function} callback - 界面刷新
  635. */
  636. baseOperation (url, node, type, callback) {
  637. const self = this;
  638. const data = {
  639. id: node[this.setting.id],
  640. postType: type
  641. };
  642. postData(url, data, function (datas) {
  643. self.loadPostData(datas, callback);
  644. });
  645. };
  646. /**
  647. * 节点数据编辑
  648. * @param {String} url - 请求地址
  649. * @param {Array|Object} updateData - 需更新的数据
  650. * @param {function} callback - 界面刷新
  651. */
  652. update (url, updateData, callback) {
  653. const self = this;
  654. postData(url, updateData, function (datas) {
  655. self.loadPostData(datas, callback);
  656. }, function () {
  657. if (updateData instanceof Array) {
  658. const result = [];
  659. for (const data of updateData) {
  660. result.push(self.getItems(data[self.setting.id]));
  661. }
  662. callback(result)
  663. } else {
  664. callback([self.getItems(updateData[self.setting.id])]);
  665. }
  666. });
  667. };
  668. /**
  669. * 复制粘贴整块(目前仅可粘贴为后项)
  670. * @param {String} url - 请求地址
  671. * @param {Object} node - 操作节点
  672. * @param {Array} block - 被复制整块的节点列表
  673. * @param {function} callback - 界面刷新
  674. */
  675. pasteBlock (url, node, block, callback) {
  676. const self = this;
  677. const data = {
  678. id: node[self.setting.id],
  679. block: block
  680. };
  681. postData(url, data, function (datas) {
  682. self.loadPostData(datas, callback);
  683. });
  684. };
  685. /**
  686. * 提交数据
  687. * @param {String} url - 请求地址
  688. * @param {Object} node - 当前选中节点
  689. * @param {Object} data - 提交的数据
  690. * @param {function} callback - 界面刷新
  691. */
  692. postData (url, node, data, callback) {
  693. const self = this;
  694. if (node) {
  695. data.id = node[self.setting.id];
  696. }
  697. postData(url, data, function (datas) {
  698. const result = {};
  699. if (datas.update) {
  700. result.update = self._updateData(datas.update);
  701. }
  702. if (datas.create) {
  703. result.create = self._loadData(datas.create);
  704. }
  705. if (datas.delete) {
  706. result.delete = self._freeData(datas.delete);
  707. }
  708. if (datas.expand) {
  709. const [create, update] = self._loadExpandData(datas.expand);
  710. result.create = result.create ? result.create.concat(create) : create;
  711. result.expand = update;
  712. }
  713. callback(result);
  714. });
  715. };
  716. }
  717. if (type === 'base') {
  718. return new BaseTree(setting);
  719. } else if (type === 'active') {
  720. return new ActiveTree(setting);
  721. } else if (type === 'ledger') {
  722. return new LedgerTree(setting);
  723. } else if (type === 'measure') {
  724. return new MeasureTree(setting);
  725. }
  726. };
  727. const treeCalc = {
  728. getMaxLevel: function (tree) {
  729. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  730. },
  731. calculateNode: function (tree, node, calcFields) {
  732. const children = tree.getChildren(node);
  733. if (children.length > 0) {
  734. const gather = children.reduce(function (rst, x) {
  735. const result = {};
  736. const fieldCalc = function (field) {
  737. if (rst[field]) {
  738. result[field] = x[field] ? rst[field] + x[field] : rst[field];
  739. } else {
  740. result[field] = x[field] ? x[field] : undefined;
  741. }
  742. }
  743. for (const cf of calcFields) {
  744. fieldCalc(cf);
  745. }
  746. return result;
  747. });
  748. for (const cf of calcFields) {
  749. if (gather[cf]) {
  750. node[cf] = gather[cf];
  751. }
  752. }
  753. }
  754. },
  755. calculateLevelNode: function (tree, level, calcFields) {
  756. const nodes = tree.datas.filter((n) => { return n.level === level });
  757. for (const node of nodes) {
  758. this.calculateNode(tree, node, calcFields);
  759. }
  760. },
  761. calculateAll: function (tree, calcFields) {
  762. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  763. this.calculateLevelNode(tree, i, calcFields);
  764. }
  765. },
  766. calculateParent: function (tree, node, calcFields) {
  767. const nodes = tree.getFullPathNodes(node.full_path);
  768. nodes.sort((a, b) => {
  769. return b.level - a.level;
  770. });
  771. for (const n of nodes) {
  772. this.calculateNode(tree, n, calcFields);
  773. }
  774. return nodes;
  775. }
  776. };