path_tree.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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. * 递归 设置节点展开状态
  313. * @param {Array} nodes - 需要设置状态的节点
  314. * @param {Object} parent - nodes的父节点
  315. * @param {Function} checkFun - 判断节点展开状态的方法
  316. * @private
  317. */
  318. _recursiveExpand(nodes, parent, checkFun) {
  319. for (const node of nodes) {
  320. node.expanded = checkFun(node);
  321. node.visible = parent ? (parent.expanded && parent.visible) : true;
  322. this._recursiveExpand(node.children, node, checkFun);
  323. }
  324. }
  325. /**
  326. * 自定义展开规则
  327. * @param checkFun
  328. */
  329. expandByCustom(checkFun) {
  330. this._recursiveExpand(this.children, null, checkFun);
  331. }
  332. /**
  333. * 展开到第几层
  334. * @param {Number} level - 展开层数
  335. */
  336. expandByLevel(level) {
  337. // function recursiveExpand(nodes, parent) {
  338. // for (const node of nodes) {
  339. // node.expanded = node.level < level;
  340. // node.visible = parent ? (parent.expanded && parent.visible) : true;
  341. // recursiveExpand(node.children, node);
  342. // }
  343. // }
  344. // recursiveExpand(this.children);
  345. this.expandByCustom(function (n) {
  346. return n.level < level;
  347. });
  348. }
  349. }
  350. class MeasureTree extends BaseTree {
  351. addData (datas) {
  352. const loadedData = [];
  353. for (const data of datas) {
  354. let node = this.getItems(data[this.setting.id]);
  355. if (node) {
  356. for (const prop in node) {
  357. if (data[prop] !== undefined) {
  358. node[prop] = data[prop];
  359. }
  360. }
  361. loadedData.push(node);
  362. } else {
  363. const keyName = itemsPre + data[this.setting.id];
  364. const node = JSON.parse(JSON.stringify(data));
  365. this.items[keyName] = node;
  366. this.datas.push(node);
  367. node.expanded = false;
  368. node.visible = true;
  369. loadedData.push(node);
  370. }
  371. }
  372. this.sortTreeNode();
  373. for (const node of loadedData) {
  374. const children = node.children;
  375. if (!node.expanded && children.length > 0) {
  376. node.expanded = true;
  377. this._refreshChildrenVisible(node);
  378. }
  379. }
  380. return loadedData;
  381. }
  382. removeData (datas) {
  383. datas.sort(function (a, b) {
  384. return b.level - a.level;
  385. });
  386. const removeArrayData = function (array, data) {
  387. const index = array.indexOf(data);
  388. array.splice(index, 1);
  389. };
  390. for (const data of datas) {
  391. const node = this.getItems(data[this.setting.id]);
  392. if (node && this.getChildren(node).length === 0) {
  393. delete this.items[itemsPre + node[this.setting.id]];
  394. if (node[this.setting.pid] !== this.setting.rootId) {
  395. const parent = this.items[itemsPre + node[this.setting.pid]];
  396. removeArrayData(parent.children, node);
  397. }
  398. removeArrayData(this.datas, node);
  399. removeArrayData(this.nodes, node);
  400. }
  401. }
  402. };
  403. loadLeafData (data) {
  404. const datas = data instanceof Array ? data : [data];
  405. for (const d of datas) {
  406. let node = this.getItems(d[this.setting.id]);
  407. if (node && node.is_leaf) {
  408. for (const prop in node) {
  409. if (data[prop] !== undefined) {
  410. node[prop] = d[prop];
  411. }
  412. }
  413. }
  414. }
  415. };
  416. }
  417. class FxTree extends BaseTree {
  418. /**
  419. * 检查节点是否是最底层项目节
  420. * @param node
  421. * @returns {boolean}
  422. */
  423. isLeafXmj(node) {
  424. for (const child of node.children) {
  425. if (!child.b_code || child.b_code === '') {
  426. return false;
  427. }
  428. }
  429. return true;
  430. }
  431. /**
  432. * 展开至最底层项目节
  433. */
  434. expandToLeafXmj() {
  435. const self = this;
  436. this.expandByCustom(function (node) {
  437. if (node.b_code && node.b_code !== '') {
  438. return false;
  439. } else {
  440. return !self.isLeafXmj(node);
  441. }
  442. })
  443. }
  444. }
  445. class LedgerTree extends FxTree {
  446. /**
  447. * 加载数据(动态),只加载不同部分
  448. * @param {Array} datas
  449. * @return {Array} 加载到树的数据
  450. * @privateA
  451. */
  452. _updateData (datas) {
  453. datas = datas instanceof Array ? datas : [datas];
  454. const loadedData = [];
  455. for (const data of datas) {
  456. let node = this.getItems(data[this.setting.id]);
  457. if (node) {
  458. for (const prop in node) {
  459. if (data[prop] !== undefined) {
  460. node[prop] = data[prop];
  461. }
  462. }
  463. loadedData.push(node);
  464. }
  465. }
  466. for (const node of loadedData) {
  467. const children = this.getChildren(node);
  468. node.expanded = children.length > 0 && children[0].visible;
  469. }
  470. this.sortTreeNode(true);
  471. return loadedData;
  472. };
  473. /**
  474. * 加载数据(动态),只加载不同部分
  475. * @param {Array} datas
  476. * @return {Array} 加载到树的数据
  477. * @privateA
  478. */
  479. _loadData (datas) {
  480. datas = datas instanceof Array ? datas : [datas];
  481. const loadedData = [], resortData = [];
  482. for (const data of datas) {
  483. let node = this.getItems(data[this.setting.id]);
  484. if (node) {
  485. const parent = this.getItems(node[this.setting.pid]);
  486. for (const prop in node) {
  487. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  488. node[prop] = data[prop];
  489. if (parent && resortData.indexOf(parent) === -1) {
  490. resortData.push(parent);
  491. }
  492. }
  493. }
  494. loadedData.push(node);
  495. } else {
  496. const keyName = itemsPre + data[this.setting.id];
  497. const node = JSON.parse(JSON.stringify(data));
  498. this.items[keyName] = node;
  499. this.datas.push(node);
  500. node.expanded = false;
  501. node.visible = true;
  502. loadedData.push(node);
  503. if (resortData.indexOf(node) === -1) {
  504. resortData.push(node);
  505. }
  506. const parent = this.getItems(node[this.setting.pid]);
  507. if (parent && resortData.indexOf(parent) === -1) {
  508. resortData.push(parent);
  509. }
  510. }
  511. }
  512. for (const node of resortData) {
  513. node.children = this.getChildren(node);
  514. }
  515. this.sortTreeNode(true);
  516. for (const node of loadedData) {
  517. if (!node.expanded) {
  518. this.setExpanded(node, true);
  519. }
  520. }
  521. return loadedData;
  522. };
  523. /**
  524. * 清理数据(动态)
  525. * @param datas
  526. * @private
  527. */
  528. _freeData (datas) {
  529. datas = datas instanceof Array ? datas : [datas];
  530. const freeDatas = [];
  531. const removeArrayData = function (array, data) {
  532. const index = array.indexOf(data);
  533. array.splice(index, 1);
  534. };
  535. for (const data of datas) {
  536. const node = this.getItems(data[this.setting.id]);
  537. if (node) {
  538. freeDatas.push(node);
  539. delete this.items[itemsPre + node[this.setting.id]];
  540. if (node[this.setting.pid] !== this.setting.rootId) {
  541. const parent = this.getItems(node[this.setting.pid]);
  542. if (parent) {
  543. removeArrayData(parent.children, node);
  544. }
  545. }
  546. removeArrayData(this.datas, node);
  547. removeArrayData(this.nodes, node);
  548. }
  549. }
  550. return freeDatas;
  551. };
  552. /**
  553. * 加载需展开的数据
  554. * @param {Array} datas
  555. * @returns {Array}
  556. * @private
  557. */
  558. _loadExpandData (datas) {
  559. datas = datas instanceof Array ? datas : [datas];
  560. const loadedData = [], existData = [], expandData = [], resortData = [];
  561. for (const data of datas) {
  562. let node = this.getItems(data[this.setting.id]);
  563. if (node) {
  564. existData.push(node);
  565. } else {
  566. const keyName = itemsPre + data[this.setting.id];
  567. const node = JSON.parse(JSON.stringify(data));
  568. this.items[keyName] = node;
  569. this.datas.push(node);
  570. node.expanded = false;
  571. node.visible = true;
  572. loadedData.push(node);
  573. if (resortData.indexOf(node) === -1) {
  574. resortData.push(node);
  575. }
  576. const parent = this.getItems(node[this.setting.pid]);
  577. if (parent && resortData.indexOf(parent) === -1) {
  578. resortData.push(parent);
  579. }
  580. }
  581. }
  582. for (const node of resortData) {
  583. node.children = this.getChildren(node);
  584. }
  585. this.sortTreeNode(true);
  586. for (const node of loadedData) {
  587. if (!node.expanded) {
  588. this.setExpanded(node, true);
  589. }
  590. }
  591. for (const node of existData) {
  592. const parent = this.getItems(node[this.setting.pid]);
  593. if (expandData.indexOf(parent) === -1) {
  594. expandData.push(parent);
  595. if (!parent.expanded) {
  596. this.setExpanded(parent, true);
  597. }
  598. }
  599. if (!node.expanded) {
  600. this.setExpanded(node, true);
  601. }
  602. }
  603. return [loadedData, expandData];
  604. };
  605. /**
  606. *
  607. * @param parent
  608. * @param node
  609. * @private
  610. */
  611. _getNodesParents(parents, nodes) {
  612. for (const node of nodes) {
  613. const parent = this.getParent(node);
  614. if (parent) {
  615. const paths = this.getFullPathNodes(parent.full_path);
  616. for (const p of paths) {
  617. if (parents.indexOf(p) === -1) {
  618. parents.push(p);
  619. }
  620. }
  621. }
  622. if (node.children.length > 0) {
  623. parents.push(node);
  624. }
  625. }
  626. }
  627. /**
  628. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  629. *
  630. * @param data - 更新的数据 {update, create, delete}
  631. * @param {function} callback - 界面刷新
  632. */
  633. loadPostData(data, callback) {
  634. const result = {}, parents = [];
  635. if (data.update) {
  636. result.update = this._updateData(data.update);
  637. this._getNodesParents(parents, result.update);
  638. }
  639. if (data.create) {
  640. result.create = this._loadData(data.create);
  641. this._getNodesParents(parents, result.create);
  642. }
  643. if (data.delete) {
  644. result.delete = this._freeData(data.delete);
  645. this._getNodesParents(parents, result.delete);
  646. }
  647. parents.sort((a, b) => {
  648. return b.level - a.level;
  649. });
  650. for (const parent of parents) {
  651. treeCalc.calculateNode(this, parent, this.setting.calcFields, this.setting.calcFun);
  652. }
  653. result.update = result.update ? result.update.concat(parents) : parents;
  654. callback(result);
  655. }
  656. /**
  657. * 以下方法需等待响应, 通过callback刷新界面
  658. */
  659. /**
  660. * 加载子节点
  661. * @param {Object} node
  662. * @param {function} callback
  663. */
  664. loadChildren (node, callback) {
  665. const self = this;
  666. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  667. postData(url, this.getNodeKeyData(node), function (data) {
  668. self._loadData(data);
  669. callback();
  670. });
  671. };
  672. /**
  673. * 树结构基本操作
  674. * @param {String} url - 请求地址
  675. * @param {Object} node - 操作节点
  676. * @param {String} type - 操作类型
  677. * @param {function} callback - 界面刷新
  678. */
  679. baseOperation (url, node, type, callback) {
  680. const self = this;
  681. const data = {
  682. id: node[this.setting.id],
  683. postType: type
  684. };
  685. postData(url, data, function (datas) {
  686. self.loadPostData(datas, callback);
  687. });
  688. };
  689. /**
  690. * 节点数据编辑
  691. * @param {String} url - 请求地址
  692. * @param {Array|Object} updateData - 需更新的数据
  693. * @param {function} callback - 界面刷新
  694. */
  695. update (url, updateData, callback) {
  696. const self = this;
  697. postData(url, updateData, function (datas) {
  698. self.loadPostData(datas, callback);
  699. }, function () {
  700. if (updateData instanceof Array) {
  701. const result = [];
  702. for (const data of updateData) {
  703. result.push(self.getItems(data[self.setting.id]));
  704. }
  705. callback(result)
  706. } else {
  707. callback([self.getItems(updateData[self.setting.id])]);
  708. }
  709. });
  710. };
  711. /**
  712. * 复制粘贴整块(目前仅可粘贴为后项)
  713. * @param {String} url - 请求地址
  714. * @param {Object} node - 操作节点
  715. * @param {Array} block - 被复制整块的节点列表
  716. * @param {function} callback - 界面刷新
  717. */
  718. pasteBlock (url, node, block, callback) {
  719. const self = this;
  720. const data = {
  721. id: node[self.setting.id],
  722. block: block
  723. };
  724. postData(url, data, function (datas) {
  725. self.loadPostData(datas, callback);
  726. });
  727. };
  728. /**
  729. * 提交数据
  730. * @param {String} url - 请求地址
  731. * @param {Object} node - 当前选中节点
  732. * @param {Object} data - 提交的数据
  733. * @param {function} callback - 界面刷新
  734. */
  735. postData (url, node, data, callback) {
  736. const self = this;
  737. if (node) {
  738. data.id = node[self.setting.id];
  739. }
  740. postData(url, data, function (datas) {
  741. const result = {};
  742. if (datas.update) {
  743. result.update = self._updateData(datas.update);
  744. }
  745. if (datas.create) {
  746. result.create = self._loadData(datas.create);
  747. }
  748. if (datas.delete) {
  749. result.delete = self._freeData(datas.delete);
  750. }
  751. if (datas.expand) {
  752. const [create, update] = self._loadExpandData(datas.expand);
  753. result.create = result.create ? result.create.concat(create) : create;
  754. result.expand = update;
  755. }
  756. callback(result);
  757. });
  758. };
  759. }
  760. class StageTree extends FxTree {
  761. /**
  762. * 构造函数
  763. */
  764. constructor (setting) {
  765. super(setting);
  766. // stage关联索引
  767. this.stageItems = {};
  768. }
  769. /**
  770. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  771. * @param datas
  772. */
  773. loadDatas (datas) {
  774. super.loadDatas(datas);
  775. // 清空旧数据
  776. this.stageItems = {};
  777. // 加载全部数据
  778. for (const data of this.datas) {
  779. const keyName = itemsPre + data[this.setting.stageId];
  780. this.stageItems[keyName] = data;
  781. }
  782. }
  783. getStageItems(id) {
  784. return this.stageItems[itemsPre + id];
  785. }
  786. loadStageData(datas, fieldPre, fields) {
  787. datas = datas instanceof Array ? datas : [datas];
  788. const loadedData = [];
  789. for (const data of datas) {
  790. let node = this.getStageItems(data.lid);
  791. if (node) {
  792. for (const prop of fields) {
  793. if (data[fieldPre + prop] !== undefined) {
  794. node[fieldPre + prop] = data[prop];
  795. }
  796. }
  797. loadedData.push(node);
  798. }
  799. }
  800. }
  801. loadPreStageData(preStageData) {
  802. this.loadStageData(curStageData, 'pre_', this.setting.updateFields);
  803. }
  804. loadCurStageData(curStageData) {
  805. this.loadStageData(curStageData, '', this.setting.updateFields);
  806. }
  807. /**
  808. * 加载数据(动态),只加载不同部分
  809. * @param {Array} datas
  810. * @return {Array} 加载到树的数据
  811. * @privateA
  812. */
  813. _updateStageData (datas) {
  814. datas = datas instanceof Array ? datas : [datas];
  815. const loadedData = [];
  816. for (const data of datas) {
  817. let node = this.getStageItems(data.lid);
  818. if (node) {
  819. for (const prop of this.setting.updateFields) {
  820. if (data[prop] !== undefined) {
  821. node[prop] = data[prop];
  822. }
  823. }
  824. loadedData.push(node);
  825. }
  826. }
  827. return loadedData;
  828. };
  829. /**
  830. *
  831. * @param parent
  832. * @param node
  833. * @private
  834. */
  835. _getNodesParents(parents, nodes) {
  836. for (const node of nodes) {
  837. const parent = this.getParent(node);
  838. if (parent) {
  839. const paths = this.getFullPathNodes(parent.full_path);
  840. for (const p of paths) {
  841. if (parents.indexOf(p) === -1) {
  842. parents.push(p);
  843. }
  844. }
  845. }
  846. if (node.children && node.children.length > 0) {
  847. parents.push(node);
  848. }
  849. }
  850. }
  851. /**
  852. * 提交数据至后端,返回的前端树结构应刷新的部分
  853. * StageTree仅有更新CurStage部分,不需要增删
  854. *
  855. * @param data - 需要更新的数据
  856. * @returns {Array} - 界面需要刷新的数据
  857. */
  858. loadPostStageData(data) {
  859. let result, parents = [];
  860. if (data) {
  861. result = this._updateStageData(data);
  862. this._getNodesParents(parents, result);
  863. }
  864. result = result ? result.concat(parents) : parents;
  865. result.sort((a, b) => {
  866. return b.level - a.level;
  867. });
  868. for (const node of result) {
  869. treeCalc.calculateNode(this, node);
  870. }
  871. return result;
  872. }
  873. }
  874. if (type === 'base') {
  875. return new BaseTree(setting);
  876. } else if (type === 'stage') {
  877. return new StageTree(setting);
  878. } else if (type === 'ledger') {
  879. return new LedgerTree(setting);
  880. } else if (type === 'measure') {
  881. return new MeasureTree(setting);
  882. }
  883. };
  884. const treeCalc = {
  885. getMaxLevel: function (tree) {
  886. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  887. },
  888. calculateNode: function (tree, node) {
  889. if (node.children && node.children.length > 0) {
  890. const gather = node.children.reduce(function (rst, x) {
  891. const result = {};
  892. const fieldCalc = function (field) {
  893. if (rst[field]) {
  894. result[field] = x[field] ? _.round(rst[field] + x[field], 6) : rst[field];
  895. } else {
  896. result[field] = x[field] ? x[field] : undefined;
  897. }
  898. }
  899. for (const cf of tree.setting.calcFields) {
  900. fieldCalc(cf);
  901. }
  902. return result;
  903. });
  904. // 汇总子项
  905. for (const cf of tree.setting.calcFields) {
  906. if (gather[cf]) {
  907. node[cf] = gather[cf];
  908. } else {
  909. node[cf] = null;
  910. }
  911. }
  912. }
  913. // 自身运算
  914. if (tree.setting.calcFun) {
  915. tree.setting.calcFun(node);
  916. }
  917. },
  918. calculateLevelNode: function (tree, level) {
  919. const nodes = tree.datas.filter((n) => { return n.level === level });
  920. for (const node of nodes) {
  921. this.calculateNode(tree, node);
  922. }
  923. },
  924. calculateAll: function (tree) {
  925. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  926. this.calculateLevelNode(tree, i);
  927. }
  928. },
  929. calculateParent: function (tree, node) {
  930. const nodes = tree.getFullPathNodes(node.full_path);
  931. nodes.sort((a, b) => {
  932. return b.level - a.level;
  933. });
  934. for (const n of nodes) {
  935. this.calculateNode(tree, n);
  936. }
  937. return nodes;
  938. }
  939. };