path_tree.js 32 KB

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