path_tree.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 = 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. if (node.children.length > 0) {
  574. parents.push(node);
  575. }
  576. }
  577. }
  578. isLeafXmj(node) {
  579. for (const child of node.children) {
  580. if (child.code !== '') {
  581. return false;
  582. }
  583. }
  584. return true;
  585. }
  586. /**
  587. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  588. *
  589. * @param data - 更新的数据 {update, create, delete}
  590. * @param {function} callback - 界面刷新
  591. */
  592. loadPostData(data, callback) {
  593. const result = {}, parents = [];
  594. if (data.update) {
  595. result.update = this._updateData(data.update);
  596. this._getNodesParents(parents, result.update);
  597. }
  598. if (data.create) {
  599. result.create = this._loadData(data.create);
  600. this._getNodesParents(parents, result.create);
  601. }
  602. if (data.delete) {
  603. result.delete = this._freeData(data.delete);
  604. this._getNodesParents(parents, result.delete);
  605. }
  606. parents.sort((a, b) => {
  607. return b.level - a.level;
  608. });
  609. for (const parent of parents) {
  610. treeCalc.calculateNode(this, parent, this.setting.calcFields, this.setting.calcFun);
  611. }
  612. result.update = result.update ? result.update.concat(parents) : parents;
  613. callback(result);
  614. }
  615. /**
  616. * 以下方法需等待响应, 通过callback刷新界面
  617. */
  618. /**
  619. * 加载子节点
  620. * @param {Object} node
  621. * @param {function} callback
  622. */
  623. loadChildren (node, callback) {
  624. const self = this;
  625. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  626. console.log(url);
  627. postData(url, this.getNodeKeyData(node), function (data) {
  628. self._loadData(data);
  629. callback();
  630. });
  631. };
  632. /**
  633. * 树结构基本操作
  634. * @param {String} url - 请求地址
  635. * @param {Object} node - 操作节点
  636. * @param {String} type - 操作类型
  637. * @param {function} callback - 界面刷新
  638. */
  639. baseOperation (url, node, type, callback) {
  640. const self = this;
  641. const data = {
  642. id: node[this.setting.id],
  643. postType: type
  644. };
  645. postData(url, data, function (datas) {
  646. self.loadPostData(datas, callback);
  647. });
  648. };
  649. /**
  650. * 节点数据编辑
  651. * @param {String} url - 请求地址
  652. * @param {Array|Object} updateData - 需更新的数据
  653. * @param {function} callback - 界面刷新
  654. */
  655. update (url, updateData, callback) {
  656. const self = this;
  657. postData(url, updateData, function (datas) {
  658. self.loadPostData(datas, callback);
  659. }, function () {
  660. if (updateData instanceof Array) {
  661. const result = [];
  662. for (const data of updateData) {
  663. result.push(self.getItems(data[self.setting.id]));
  664. }
  665. callback(result)
  666. } else {
  667. callback([self.getItems(updateData[self.setting.id])]);
  668. }
  669. });
  670. };
  671. /**
  672. * 复制粘贴整块(目前仅可粘贴为后项)
  673. * @param {String} url - 请求地址
  674. * @param {Object} node - 操作节点
  675. * @param {Array} block - 被复制整块的节点列表
  676. * @param {function} callback - 界面刷新
  677. */
  678. pasteBlock (url, node, block, callback) {
  679. const self = this;
  680. const data = {
  681. id: node[self.setting.id],
  682. block: block
  683. };
  684. postData(url, data, function (datas) {
  685. self.loadPostData(datas, callback);
  686. });
  687. };
  688. /**
  689. * 提交数据
  690. * @param {String} url - 请求地址
  691. * @param {Object} node - 当前选中节点
  692. * @param {Object} data - 提交的数据
  693. * @param {function} callback - 界面刷新
  694. */
  695. postData (url, node, data, callback) {
  696. const self = this;
  697. if (node) {
  698. data.id = node[self.setting.id];
  699. }
  700. postData(url, data, function (datas) {
  701. const result = {};
  702. if (datas.update) {
  703. result.update = self._updateData(datas.update);
  704. }
  705. if (datas.create) {
  706. result.create = self._loadData(datas.create);
  707. }
  708. if (datas.delete) {
  709. result.delete = self._freeData(datas.delete);
  710. }
  711. if (datas.expand) {
  712. const [create, update] = self._loadExpandData(datas.expand);
  713. result.create = result.create ? result.create.concat(create) : create;
  714. result.expand = update;
  715. }
  716. callback(result);
  717. });
  718. };
  719. }
  720. if (type === 'base') {
  721. return new BaseTree(setting);
  722. } else if (type === 'active') {
  723. return new ActiveTree(setting);
  724. } else if (type === 'ledger') {
  725. return new LedgerTree(setting);
  726. } else if (type === 'measure') {
  727. return new MeasureTree(setting);
  728. }
  729. };
  730. const treeCalc = {
  731. getMaxLevel: function (tree) {
  732. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  733. },
  734. calculateNode: function (tree, node, calcFields, calcFun) {
  735. const children = tree.getChildren(node);
  736. if (children.length > 0) {
  737. const gather = children.reduce(function (rst, x) {
  738. const result = {};
  739. const fieldCalc = function (field) {
  740. if (rst[field]) {
  741. result[field] = x[field] ? rst[field] + x[field] : rst[field];
  742. } else {
  743. result[field] = x[field] ? x[field] : undefined;
  744. }
  745. }
  746. for (const cf of calcFields) {
  747. fieldCalc(cf);
  748. }
  749. return result;
  750. });
  751. for (const cf of calcFields) {
  752. if (gather[cf]) {
  753. node[cf] = gather[cf];
  754. }
  755. }
  756. }
  757. if (calcFun) {
  758. calcFun(node);
  759. }
  760. },
  761. calculateLevelNode: function (tree, level, calcFields, calcFun) {
  762. const nodes = tree.datas.filter((n) => { return n.level === level });
  763. for (const node of nodes) {
  764. this.calculateNode(tree, node, calcFields, calcFun);
  765. }
  766. },
  767. calculateAll: function (tree, calcFields, calcFun) {
  768. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  769. this.calculateLevelNode(tree, i, calcFields, calcFun);
  770. }
  771. },
  772. calculateParent: function (tree, node, calcFields, calcFun) {
  773. const nodes = tree.getFullPathNodes(node.full_path);
  774. nodes.sort((a, b) => {
  775. return b.level - a.level;
  776. });
  777. for (const n of nodes) {
  778. this.calculateNode(tree, n, calcFields, calcFun);
  779. }
  780. return nodes;
  781. }
  782. };