path_tree.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 = 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. class StagePosData extends PosData {
  104. loadStageData(datas, fieldPre, fields) {
  105. datas = datas instanceof Array ? datas : [datas];
  106. const loadedData = [];
  107. for (const data of datas) {
  108. let node = this.getPos(data.pid);
  109. if (node) {
  110. for (const prop of fields) {
  111. if (data[fieldPre + prop] !== undefined) {
  112. node[fieldPre + prop] = data[prop];
  113. }
  114. }
  115. if (this.setting.calcFun) {
  116. this.setting.calcFun(node);
  117. }
  118. loadedData.push(node);
  119. }
  120. }
  121. }
  122. loadPreStageData(datas) {
  123. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  124. }
  125. loadCurStageData(datas) {
  126. this.loadStageData(datas, '', this.setting.updateFields);
  127. }
  128. calculateAll() {
  129. if (!this.setting.calcFun) { return; }
  130. for (const pos of this.datas) {
  131. this.setting.calcFun(pos);
  132. }
  133. }
  134. }
  135. const itemsPre = 'id_';
  136. const createNewPathTree = function (type, setting) {
  137. class BaseTree {
  138. /**
  139. * 构造函数
  140. */
  141. constructor (setting) {
  142. // 无索引
  143. this.datas = [];
  144. // 以key为索引
  145. this.items = {};
  146. // 以排序为索引
  147. this.nodes = [];
  148. // 根节点
  149. this.children = [];
  150. // 树设置
  151. this.setting = setting;
  152. }
  153. /**
  154. * 树结构根据显示排序
  155. */
  156. sortTreeNode (isResort) {
  157. const self = this;
  158. const addSortNodes = function (nodes) {
  159. if (!nodes) { return }
  160. for (let i = 0; i < nodes.length; i++) {
  161. self.nodes.push(nodes[i]);
  162. nodes[i].index = self.nodes.length - 1;
  163. if (!isResort) {
  164. nodes[i].children = self.getChildren(nodes[i]);
  165. }
  166. addSortNodes(nodes[i].children);
  167. }
  168. };
  169. self.nodes = [];
  170. this.children = this.getChildren(null);
  171. addSortNodes(this.getChildren(null));
  172. }
  173. /**
  174. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  175. * @param datas
  176. */
  177. loadDatas (datas) {
  178. // 清空旧数据
  179. this.items = {};
  180. this.nodes = [];
  181. this.datas = [];
  182. this.children = [];
  183. // 加载全部数据
  184. for (const data of datas) {
  185. const keyName = itemsPre + data[this.setting.id];
  186. if (!this.items[keyName]) {
  187. this.items[keyName] = JSON.parse(JSON.stringify(data));
  188. this.datas.push(this.items[keyName]);
  189. }
  190. }
  191. this.sortTreeNode();
  192. for (const node of this.nodes) {
  193. node.expanded = node.children.length > 0;
  194. node.visible = true;
  195. }
  196. }
  197. getItemsByIndex(index) {
  198. return this.nodes[index];
  199. }
  200. /**
  201. * 根据id获取树结构节点数据
  202. * @param {Number} id
  203. * @returns {Object}
  204. */
  205. getItems (id) {
  206. return this.items[itemsPre + id];
  207. };
  208. /**
  209. * 查找node的parent
  210. * @param {Object} node
  211. * @returns {Object}
  212. */
  213. getParent (node) {
  214. return this.getItems(node[this.setting.pid]);
  215. };
  216. /**
  217. * 根据path查找完整节点
  218. * @param {Number} path
  219. */
  220. getFullPathNodes (path) {
  221. const self = this, ids = path.split('.');
  222. if (ids.length > 0) {
  223. return this.nodes.filter((x) => {
  224. return ids.indexOf('' + x[self.setting.id]) >= 0;
  225. });
  226. } else {
  227. return [];
  228. }
  229. };
  230. /**
  231. * 查询node的已下载子节点
  232. * @param {Object} node
  233. * @returns {Array}
  234. */
  235. getChildren (node) {
  236. const setting = this.setting;
  237. const pid = node ? node[setting.id] : setting.rootId;
  238. const children = this.datas.filter(function (x) {
  239. return x[setting.pid] === pid;
  240. });
  241. children.sort(function (a, b) {
  242. return a.order - b.order;
  243. });
  244. return children;
  245. };
  246. /**
  247. * 查询node的已下载的全部后代
  248. * @param {Object} node
  249. * @returns {Array}
  250. */
  251. getPosterity (node) {
  252. const reg = new RegExp('^' + node.full_path + '.');
  253. return this.datas.filter(function (x) {
  254. return reg.test(x.full_path);
  255. })
  256. };
  257. /**
  258. * 查询node是否是父节点的最后一个子节点
  259. * @param {Object} node
  260. * @returns {boolean}
  261. */
  262. isLastSibling (node) {
  263. const siblings = this.getChildren(this.getParent(node));
  264. return node.order === siblings[siblings.length - 1].order;
  265. };
  266. /**
  267. * 刷新子节点是否可见
  268. * @param {Object} node
  269. * @private
  270. */
  271. _refreshChildrenVisible (node) {
  272. if (!node.children) {
  273. node.children = this.getChildren(node);
  274. }
  275. if (node.children && node.children.length > 0) {
  276. for (const child of node.children) {
  277. child.visible = node.expanded && node.visible;
  278. this._refreshChildrenVisible(child);
  279. }
  280. }
  281. };
  282. /**
  283. * 设置节点是否展开, 并控制子节点可见
  284. * @param {Object} node
  285. * @param {Boolean} expanded
  286. */
  287. setExpanded (node, expanded) {
  288. node.expanded = expanded;
  289. this._refreshChildrenVisible(node);
  290. };
  291. /**
  292. * 提取节点key和索引数据
  293. * @param {Object} node - 节点
  294. * @returns {key}
  295. */
  296. getNodeKeyData (node) {
  297. const data = {};
  298. for (const key of this.setting.keys) {
  299. data[key] = node[key];
  300. }
  301. return data;
  302. };
  303. /**
  304. * 得到树结构构成id
  305. * @param node
  306. * @returns {*}
  307. */
  308. getNodeKey (node) {
  309. return node[this.setting.id];
  310. };
  311. }
  312. class MeasureTree extends BaseTree {
  313. addData (datas) {
  314. const loadedData = [];
  315. for (const data of datas) {
  316. let node = this.getItems(data[this.setting.id]);
  317. if (node) {
  318. for (const prop in node) {
  319. if (data[prop] !== undefined) {
  320. node[prop] = data[prop];
  321. }
  322. }
  323. loadedData.push(node);
  324. } else {
  325. const keyName = itemsPre + data[this.setting.id];
  326. const node = JSON.parse(JSON.stringify(data));
  327. this.items[keyName] = node;
  328. this.datas.push(node);
  329. node.expanded = false;
  330. node.visible = true;
  331. loadedData.push(node);
  332. }
  333. }
  334. this.sortTreeNode();
  335. for (const node of loadedData) {
  336. const children = node.children;
  337. if (!node.expanded && children.length > 0) {
  338. node.expanded = true;
  339. this._refreshChildrenVisible(node);
  340. }
  341. }
  342. return loadedData;
  343. }
  344. removeData (datas) {
  345. datas.sort(function (a, b) {
  346. return b.level - a.level;
  347. });
  348. console.log(datas);
  349. const removeArrayData = function (array, data) {
  350. const index = array.indexOf(data);
  351. array.splice(index, 1);
  352. };
  353. for (const data of datas) {
  354. const node = this.getItems(data[this.setting.id]);
  355. if (node && this.getChildren(node).length === 0) {
  356. delete this.items[itemsPre + node[this.setting.id]];
  357. if (node[this.setting.pid] !== this.setting.rootId) {
  358. const parent = this.items[itemsPre + node[this.setting.pid]];
  359. removeArrayData(parent.children, node);
  360. }
  361. removeArrayData(this.datas, node);
  362. removeArrayData(this.nodes, node);
  363. }
  364. }
  365. };
  366. loadLeafData (data) {
  367. const datas = data instanceof Array ? data : [data];
  368. for (const d of datas) {
  369. let node = this.getItems(d[this.setting.id]);
  370. if (node && node.is_leaf) {
  371. for (const prop in node) {
  372. if (data[prop] !== undefined) {
  373. node[prop] = d[prop];
  374. }
  375. }
  376. }
  377. }
  378. };
  379. }
  380. class LedgerTree extends BaseTree {
  381. /**
  382. * 加载数据(动态),只加载不同部分
  383. * @param {Array} datas
  384. * @return {Array} 加载到树的数据
  385. * @privateA
  386. */
  387. _updateData (datas) {
  388. datas = datas instanceof Array ? datas : [datas];
  389. const loadedData = [];
  390. for (const data of datas) {
  391. let node = this.getItems(data[this.setting.id]);
  392. if (node) {
  393. for (const prop in node) {
  394. if (data[prop] !== undefined) {
  395. node[prop] = data[prop];
  396. }
  397. }
  398. loadedData.push(node);
  399. }
  400. }
  401. for (const node of loadedData) {
  402. const children = this.getChildren(node);
  403. node.expanded = children.length > 0 && children[0].visible;
  404. }
  405. this.sortTreeNode(true);
  406. return loadedData;
  407. };
  408. /**
  409. * 加载数据(动态),只加载不同部分
  410. * @param {Array} datas
  411. * @return {Array} 加载到树的数据
  412. * @privateA
  413. */
  414. _loadData (datas) {
  415. datas = datas instanceof Array ? datas : [datas];
  416. const loadedData = [], resortData = [];
  417. for (const data of datas) {
  418. let node = this.getItems(data[this.setting.id]);
  419. if (node) {
  420. const parent = this.getItems(node[this.setting.pid]);
  421. for (const prop in node) {
  422. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  423. node[prop] = data[prop];
  424. if (parent && resortData.indexOf(parent) === -1) {
  425. resortData.push(parent);
  426. }
  427. }
  428. }
  429. loadedData.push(node);
  430. } else {
  431. const keyName = itemsPre + data[this.setting.id];
  432. const node = JSON.parse(JSON.stringify(data));
  433. this.items[keyName] = node;
  434. this.datas.push(node);
  435. node.expanded = false;
  436. node.visible = true;
  437. loadedData.push(node);
  438. if (resortData.indexOf(node) === -1) {
  439. resortData.push(node);
  440. }
  441. const parent = this.getItems(node[this.setting.pid]);
  442. if (parent && resortData.indexOf(parent) === -1) {
  443. resortData.push(parent);
  444. }
  445. }
  446. }
  447. for (const node of resortData) {
  448. node.children = this.getChildren(node);
  449. }
  450. this.sortTreeNode(true);
  451. for (const node of loadedData) {
  452. if (!node.expanded) {
  453. this.setExpanded(node, true);
  454. }
  455. }
  456. return loadedData;
  457. };
  458. /**
  459. * 清理数据(动态)
  460. * @param datas
  461. * @private
  462. */
  463. _freeData (datas) {
  464. datas = datas instanceof Array ? datas : [datas];
  465. const freeDatas = [];
  466. const removeArrayData = function (array, data) {
  467. const index = array.indexOf(data);
  468. array.splice(index, 1);
  469. };
  470. for (const data of datas) {
  471. const node = this.getItems(data[this.setting.id]);
  472. if (node) {
  473. freeDatas.push(node);
  474. delete this.items[itemsPre + node[this.setting.id]];
  475. if (node[this.setting.pid] !== this.setting.rootId) {
  476. const parent = this.getItems(node[this.setting.pid]);
  477. if (parent) {
  478. removeArrayData(parent.children, node);
  479. }
  480. }
  481. removeArrayData(this.datas, node);
  482. removeArrayData(this.nodes, node);
  483. }
  484. }
  485. return freeDatas;
  486. };
  487. /**
  488. * 加载需展开的数据
  489. * @param {Array} datas
  490. * @returns {Array}
  491. * @private
  492. */
  493. _loadExpandData (datas) {
  494. datas = datas instanceof Array ? datas : [datas];
  495. const loadedData = [], existData = [], expandData = [], resortData = [];
  496. for (const data of datas) {
  497. let node = this.getItems(data[this.setting.id]);
  498. if (node) {
  499. existData.push(node);
  500. } else {
  501. const keyName = itemsPre + data[this.setting.id];
  502. const node = JSON.parse(JSON.stringify(data));
  503. this.items[keyName] = node;
  504. this.datas.push(node);
  505. node.expanded = false;
  506. node.visible = true;
  507. loadedData.push(node);
  508. if (resortData.indexOf(node) === -1) {
  509. resortData.push(node);
  510. }
  511. const parent = this.getItems(node[this.setting.pid]);
  512. if (parent && resortData.indexOf(parent) === -1) {
  513. resortData.push(parent);
  514. }
  515. }
  516. }
  517. for (const node of resortData) {
  518. node.children = this.getChildren(node);
  519. }
  520. this.sortTreeNode(true);
  521. for (const node of loadedData) {
  522. if (!node.expanded) {
  523. this.setExpanded(node, true);
  524. }
  525. }
  526. for (const node of existData) {
  527. const parent = this.getItems(node[this.setting.pid]);
  528. if (expandData.indexOf(parent) === -1) {
  529. expandData.push(parent);
  530. if (!parent.expanded) {
  531. this.setExpanded(parent, true);
  532. }
  533. }
  534. if (!node.expanded) {
  535. this.setExpanded(node, true);
  536. }
  537. }
  538. return [loadedData, expandData];
  539. };
  540. /**
  541. *
  542. * @param parent
  543. * @param node
  544. * @private
  545. */
  546. _getNodesParents(parents, nodes) {
  547. for (const node of nodes) {
  548. const parent = this.getParent(node);
  549. if (parent) {
  550. const paths = this.getFullPathNodes(parent.full_path);
  551. for (const p of paths) {
  552. if (parents.indexOf(p) === -1) {
  553. parents.push(p);
  554. }
  555. }
  556. }
  557. if (node.children.length > 0) {
  558. parents.push(node);
  559. }
  560. }
  561. }
  562. isLeafXmj(node) {
  563. for (const child of node.children) {
  564. if (child.code !== '') {
  565. return false;
  566. }
  567. }
  568. return true;
  569. }
  570. /**
  571. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  572. *
  573. * @param data - 更新的数据 {update, create, delete}
  574. * @param {function} callback - 界面刷新
  575. */
  576. loadPostData(data, callback) {
  577. const result = {}, parents = [];
  578. if (data.update) {
  579. result.update = this._updateData(data.update);
  580. this._getNodesParents(parents, result.update);
  581. }
  582. if (data.create) {
  583. result.create = this._loadData(data.create);
  584. this._getNodesParents(parents, result.create);
  585. }
  586. if (data.delete) {
  587. result.delete = this._freeData(data.delete);
  588. this._getNodesParents(parents, result.delete);
  589. }
  590. parents.sort((a, b) => {
  591. return b.level - a.level;
  592. });
  593. for (const parent of parents) {
  594. treeCalc.calculateNode(this, parent, this.setting.calcFields, this.setting.calcFun);
  595. }
  596. result.update = result.update ? result.update.concat(parents) : parents;
  597. callback(result);
  598. }
  599. /**
  600. * 以下方法需等待响应, 通过callback刷新界面
  601. */
  602. /**
  603. * 加载子节点
  604. * @param {Object} node
  605. * @param {function} callback
  606. */
  607. loadChildren (node, callback) {
  608. const self = this;
  609. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  610. console.log(url);
  611. postData(url, this.getNodeKeyData(node), function (data) {
  612. self._loadData(data);
  613. callback();
  614. });
  615. };
  616. /**
  617. * 树结构基本操作
  618. * @param {String} url - 请求地址
  619. * @param {Object} node - 操作节点
  620. * @param {String} type - 操作类型
  621. * @param {function} callback - 界面刷新
  622. */
  623. baseOperation (url, node, type, callback) {
  624. const self = this;
  625. const data = {
  626. id: node[this.setting.id],
  627. postType: type
  628. };
  629. postData(url, data, function (datas) {
  630. self.loadPostData(datas, callback);
  631. });
  632. };
  633. /**
  634. * 节点数据编辑
  635. * @param {String} url - 请求地址
  636. * @param {Array|Object} updateData - 需更新的数据
  637. * @param {function} callback - 界面刷新
  638. */
  639. update (url, updateData, callback) {
  640. const self = this;
  641. postData(url, updateData, function (datas) {
  642. self.loadPostData(datas, callback);
  643. }, function () {
  644. if (updateData instanceof Array) {
  645. const result = [];
  646. for (const data of updateData) {
  647. result.push(self.getItems(data[self.setting.id]));
  648. }
  649. callback(result)
  650. } else {
  651. callback([self.getItems(updateData[self.setting.id])]);
  652. }
  653. });
  654. };
  655. /**
  656. * 复制粘贴整块(目前仅可粘贴为后项)
  657. * @param {String} url - 请求地址
  658. * @param {Object} node - 操作节点
  659. * @param {Array} block - 被复制整块的节点列表
  660. * @param {function} callback - 界面刷新
  661. */
  662. pasteBlock (url, node, block, callback) {
  663. const self = this;
  664. const data = {
  665. id: node[self.setting.id],
  666. block: block
  667. };
  668. postData(url, data, function (datas) {
  669. self.loadPostData(datas, callback);
  670. });
  671. };
  672. /**
  673. * 提交数据
  674. * @param {String} url - 请求地址
  675. * @param {Object} node - 当前选中节点
  676. * @param {Object} data - 提交的数据
  677. * @param {function} callback - 界面刷新
  678. */
  679. postData (url, node, data, callback) {
  680. const self = this;
  681. if (node) {
  682. data.id = node[self.setting.id];
  683. }
  684. postData(url, data, function (datas) {
  685. const result = {};
  686. if (datas.update) {
  687. result.update = self._updateData(datas.update);
  688. }
  689. if (datas.create) {
  690. result.create = self._loadData(datas.create);
  691. }
  692. if (datas.delete) {
  693. result.delete = self._freeData(datas.delete);
  694. }
  695. if (datas.expand) {
  696. const [create, update] = self._loadExpandData(datas.expand);
  697. result.create = result.create ? result.create.concat(create) : create;
  698. result.expand = update;
  699. }
  700. callback(result);
  701. });
  702. };
  703. }
  704. class StageTree extends BaseTree {
  705. /**
  706. * 构造函数
  707. */
  708. constructor (setting) {
  709. super(setting);
  710. // stage关联索引
  711. this.stageItems = {};
  712. }
  713. /**
  714. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  715. * @param datas
  716. */
  717. loadDatas (datas) {
  718. super.loadDatas(datas);
  719. // 清空旧数据
  720. this.stageItems = {};
  721. // 加载全部数据
  722. for (const data of this.datas) {
  723. const keyName = itemsPre + data[this.setting.stageId];
  724. this.stageItems[keyName] = data;
  725. }
  726. }
  727. getStageItems(id) {
  728. return this.stageItems[itemsPre + id];
  729. }
  730. loadStageData(datas, fieldPre, fields) {
  731. datas = datas instanceof Array ? datas : [datas];
  732. const loadedData = [];
  733. for (const data of datas) {
  734. let node = this.getStageItems(data.lid);
  735. if (node) {
  736. for (const prop of fields) {
  737. if (data[fieldPre + prop] !== undefined) {
  738. node[fieldPre + prop] = data[prop];
  739. }
  740. }
  741. loadedData.push(node);
  742. }
  743. }
  744. }
  745. loadPreStageData(preStageData) {
  746. this.loadStageData(curStageData, 'pre_', this.setting.updateFields);
  747. }
  748. loadCurStageData(curStageData) {
  749. this.loadStageData(curStageData, '', this.setting.updateFields);
  750. }
  751. /**
  752. * 加载数据(动态),只加载不同部分
  753. * @param {Array} datas
  754. * @return {Array} 加载到树的数据
  755. * @privateA
  756. */
  757. _updateStageData (datas) {
  758. datas = datas instanceof Array ? datas : [datas];
  759. const loadedData = [];
  760. for (const data of datas) {
  761. let node = this.getStageItems(data.lid);
  762. if (node) {
  763. for (const prop of this.setting.updateFields) {
  764. if (data[prop] !== undefined) {
  765. node[prop] = data[prop];
  766. }
  767. }
  768. loadedData.push(node);
  769. }
  770. }
  771. return loadedData;
  772. };
  773. /**
  774. *
  775. * @param parent
  776. * @param node
  777. * @private
  778. */
  779. _getNodesParents(parents, nodes) {
  780. for (const node of nodes) {
  781. const parent = this.getParent(node);
  782. if (parent) {
  783. const paths = this.getFullPathNodes(parent.full_path);
  784. for (const p of paths) {
  785. if (parents.indexOf(p) === -1) {
  786. parents.push(p);
  787. }
  788. }
  789. }
  790. if (node.children && node.children.length > 0) {
  791. parents.push(node);
  792. }
  793. }
  794. }
  795. isLeafXmj(node) {
  796. for (const child of node.children) {
  797. if (child.code !== '') {
  798. return false;
  799. }
  800. }
  801. return true;
  802. }
  803. /**
  804. * 提交数据至后端,返回的前端树结构应刷新的部分
  805. * StageTree仅有更新CurStage部分,不需要增删
  806. *
  807. * @param data - 需要更新的数据
  808. * @returns {Array} - 界面需要刷新的数据
  809. */
  810. loadPostStageData(data) {
  811. let result, parents = [];
  812. if (data) {
  813. result = this._updateStageData(data);
  814. this._getNodesParents(parents, result);
  815. }
  816. result = result ? result.concat(parents) : parents;
  817. result.sort((a, b) => {
  818. return b.level - a.level;
  819. });
  820. for (const node of result) {
  821. treeCalc.calculateNode(this, node);
  822. }
  823. return result;
  824. }
  825. }
  826. if (type === 'base') {
  827. return new BaseTree(setting);
  828. } else if (type === 'stage') {
  829. return new StageTree(setting);
  830. } else if (type === 'ledger') {
  831. return new LedgerTree(setting);
  832. } else if (type === 'measure') {
  833. return new MeasureTree(setting);
  834. }
  835. };
  836. const treeCalc = {
  837. getMaxLevel: function (tree) {
  838. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  839. },
  840. calculateNode: function (tree, node) {
  841. if (node.children && node.children.length > 0) {
  842. const gather = node.children.reduce(function (rst, x) {
  843. const result = {};
  844. const fieldCalc = function (field) {
  845. if (rst[field]) {
  846. result[field] = x[field] ? _.round(rst[field] + x[field], 6) : rst[field];
  847. } else {
  848. result[field] = x[field] ? x[field] : undefined;
  849. }
  850. }
  851. for (const cf of tree.setting.calcFields) {
  852. fieldCalc(cf);
  853. }
  854. return result;
  855. });
  856. // 汇总子项
  857. for (const cf of tree.setting.calcFields) {
  858. if (gather[cf]) {
  859. node[cf] = gather[cf];
  860. } else {
  861. node[cf] = null;
  862. }
  863. }
  864. }
  865. // 自身运算
  866. if (tree.setting.calcFun) {
  867. tree.setting.calcFun(node);
  868. }
  869. },
  870. calculateLevelNode: function (tree, level) {
  871. const nodes = tree.datas.filter((n) => { return n.level === level });
  872. for (const node of nodes) {
  873. this.calculateNode(tree, node);
  874. }
  875. },
  876. calculateAll: function (tree) {
  877. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  878. this.calculateLevelNode(tree, i);
  879. }
  880. },
  881. calculateParent: function (tree, node) {
  882. const nodes = tree.getFullPathNodes(node.full_path);
  883. nodes.sort((a, b) => {
  884. return b.level - a.level;
  885. });
  886. for (const n of nodes) {
  887. this.calculateNode(tree, n);
  888. }
  889. return nodes;
  890. }
  891. };