path_tree.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /**
  2. * (需使用 decimal.min.js, zh_calc.js)
  3. *
  4. * 构建pathTree
  5. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  6. * @param {Object} setting - 设置
  7. * @returns {PathTree}
  8. */
  9. 'use strict';
  10. class PosData {
  11. /**
  12. * 构造函数
  13. * @param {id|Number, masterId|Number} setting
  14. */
  15. constructor (setting) {
  16. // 无索引
  17. this.datas = null;
  18. // 以key为索引
  19. this.items = {};
  20. // 以分类id为索引的有序
  21. this.ledgerPos = {};
  22. // pos设置
  23. this.setting = setting;
  24. }
  25. /**
  26. * 加载部位明细数据
  27. * @param datas
  28. */
  29. loadDatas(datas) {
  30. this.datas = datas;
  31. this.items = {};
  32. this.ledgerPos = {};
  33. for (const data of this.datas) {
  34. const key = itemsPre + data[this.setting.id];
  35. this.items[key] = data;
  36. const masterKey = itemsPre + data[this.setting.ledgerId];
  37. if (!this.ledgerPos[masterKey]) {
  38. this.ledgerPos[masterKey] = [];
  39. }
  40. this.ledgerPos[masterKey].push(data);
  41. }
  42. }
  43. /**
  44. * 更新数据
  45. * @param datas
  46. */
  47. updateDatas(data) {
  48. const datas = data instanceof Array ? data : [data];
  49. const result = { create: [], update: [] };
  50. for (const d of datas) {
  51. const key = itemsPre + d[this.setting.id];
  52. if (!this.items[key]) {
  53. this.datas.push(d);
  54. this.items[key] = d;
  55. const masterKey = itemsPre + d[this.setting.ledgerId];
  56. if (!this.ledgerPos[masterKey]) {
  57. this.ledgerPos[masterKey] = [];
  58. }
  59. this.ledgerPos[masterKey].push(d);
  60. result.create.push(d);
  61. } else {
  62. const pos = this.items[key];
  63. for (const prop in d) {
  64. pos[prop] = d[prop];
  65. }
  66. result.update.push(pos);
  67. }
  68. }
  69. return result;
  70. }
  71. /**
  72. * 移除数据
  73. * @param datas
  74. */
  75. removeDatas(data) {
  76. if (!data) { return; }
  77. const datas = data instanceof Array ? data : [data];
  78. for (let i = datas.length - 1; i >= 0; i--) {
  79. const id = datas[i];
  80. const d = this.getPos(id);
  81. this.datas.splice(this.datas.indexOf(d), 1);
  82. const key = itemsPre + d[this.setting.id];
  83. delete this.items[key];
  84. const masterKey = itemsPre + d[this.setting.ledgerId];
  85. const range = this.ledgerPos[masterKey];
  86. range.splice(range.indexOf(d), 1);
  87. if (range.length === 0) {
  88. delete this.ledgerPos[masterKey];
  89. }
  90. }
  91. }
  92. /**
  93. * 移除数据 - 根据分类id
  94. * @param mid
  95. */
  96. removeDatasByMasterId (mid) {
  97. const masterKey = itemsPre + mid;
  98. const range = this.ledgerPos[masterKey];
  99. if (range) {
  100. delete this.ledgerPos[masterKey];
  101. for (const r of range) {
  102. this.datas.splice(this.datas.indexOf(r), 1);
  103. const key = itemsPre + r[this.setting.id];
  104. delete this.items[key];
  105. }
  106. }
  107. }
  108. getPos(id) {
  109. return this.items[itemsPre + id];
  110. }
  111. getLedgerPos(mid) {
  112. return this.ledgerPos[itemsPre + mid];
  113. }
  114. /**
  115. * 计算全部
  116. */
  117. calculateAll() {
  118. if (!this.setting.calcFun) { return; }
  119. for (const pos of this.datas) {
  120. this.setting.calcFun(pos);
  121. }
  122. }
  123. }
  124. class StagePosData extends PosData {
  125. loadStageData(datas, fieldPre, fields) {
  126. if (!datas) { return; }
  127. datas = datas instanceof Array ? datas : [datas];
  128. const loadedData = [];
  129. for (const data of datas) {
  130. let node = this.getPos(data.pid);
  131. if (node) {
  132. for (const prop of fields) {
  133. if (data[prop] !== undefined) {
  134. node[fieldPre + prop] = data[prop];
  135. }
  136. }
  137. if (this.setting.calcFun) {
  138. this.setting.calcFun(node);
  139. }
  140. loadedData.push(node);
  141. }
  142. }
  143. }
  144. loadPreStageData(datas) {
  145. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  146. }
  147. loadCurStageData(datas) {
  148. this.loadStageData(datas, '', this.setting.updateFields);
  149. }
  150. }
  151. class MasterPosData extends PosData {
  152. /**
  153. * 构造函数
  154. * @param {id|Number, masterId|Number} setting
  155. */
  156. constructor (setting) {
  157. super(setting);
  158. // 关联索引
  159. this.masterItems = {};
  160. }
  161. /**
  162. * 加载主数据
  163. * @param datas
  164. */
  165. loadDatas (datas) {
  166. super.loadDatas(datas);
  167. // 清空旧数据
  168. this.masterItems = {};
  169. // minor数据缓存
  170. this.minorData = {};
  171. // 加载全部数据
  172. for (const data of this.datas) {
  173. const keyName = itemsPre + data[this.setting.masterId];
  174. this.masterItems[keyName] = data;
  175. }
  176. }
  177. /**
  178. * 根据关联id,查找节点
  179. * @param id
  180. * @returns {*}
  181. */
  182. getMasterItems(id) {
  183. return this.masterItems[itemsPre + id];
  184. }
  185. /**
  186. * 加载关联数据
  187. *
  188. * @param {Array|Object}datas - 需要关联的数据
  189. * @param {String} fieldPre - 关联字段前缀(关联结果)
  190. * @param {Array} fields - 关联字段
  191. * @returns {Array}
  192. */
  193. loadMinorData(datas, fieldSuf, fields) {
  194. if (!datas) { return; }
  195. datas = datas instanceof Array ? datas : [datas];
  196. this.minorData[fieldSuf] = datas;
  197. const loadedData = [];
  198. for (const data of datas) {
  199. let node = this.getMasterItems(data[this.setting.minorId]);
  200. if (node) {
  201. for (const prop of fields) {
  202. if (data[prop] !== undefined) {
  203. node[prop + fieldSuf] = data[prop];
  204. }
  205. }
  206. loadedData.push(node);
  207. }
  208. }
  209. return loadedData;
  210. }
  211. }
  212. const itemsPre = 'id_';
  213. const createNewPathTree = function (type, setting) {
  214. class BaseTree {
  215. /**
  216. * 构造函数
  217. */
  218. constructor (setting) {
  219. // 无索引
  220. this.datas = [];
  221. // 以key为索引
  222. this.items = {};
  223. // 以排序为索引
  224. this.nodes = [];
  225. // 根节点
  226. this.children = [];
  227. // 树设置
  228. this.setting = setting;
  229. }
  230. /**
  231. * 树结构根据显示排序
  232. */
  233. sortTreeNode (isResort) {
  234. const self = this;
  235. const addSortNodes = function (nodes) {
  236. if (!nodes) { return }
  237. for (let i = 0; i < nodes.length; i++) {
  238. self.nodes.push(nodes[i]);
  239. nodes[i].index = self.nodes.length - 1;
  240. if (!isResort) {
  241. nodes[i].children = self.getChildren(nodes[i]);
  242. } else {
  243. nodes[i].children.sort(function (a, b) {
  244. return a.order - b.order;
  245. })
  246. }
  247. addSortNodes(nodes[i].children);
  248. }
  249. };
  250. self.nodes = [];
  251. this.children = this.getChildren(null);
  252. addSortNodes(this.getChildren(null));
  253. }
  254. /**
  255. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  256. * @param datas
  257. */
  258. loadDatas (datas) {
  259. // 清空旧数据
  260. this.items = {};
  261. this.nodes = [];
  262. this.datas = [];
  263. this.children = [];
  264. // 加载全部数据
  265. for (const data of datas) {
  266. const keyName = itemsPre + data[this.setting.id];
  267. if (!this.items[keyName]) {
  268. this.items[keyName] = JSON.parse(JSON.stringify(data));
  269. this.datas.push(this.items[keyName]);
  270. }
  271. }
  272. this.sortTreeNode();
  273. for (const node of this.nodes) {
  274. //node.expanded = node.children.length > 0;
  275. node.expanded = true;
  276. node.visible = true;
  277. }
  278. }
  279. getItemsByIndex(index) {
  280. return this.nodes[index];
  281. }
  282. /**
  283. * 根据id获取树结构节点数据
  284. * @param {Number} id
  285. * @returns {Object}
  286. */
  287. getItems (id) {
  288. return this.items[itemsPre + id];
  289. };
  290. /**
  291. * 查找node的parent
  292. * @param {Object} node
  293. * @returns {Object}
  294. */
  295. getParent (node) {
  296. return this.getItems(node[this.setting.pid]);
  297. };
  298. /**
  299. * 查找node的前兄弟节点
  300. * @param node
  301. * @returns {*}
  302. */
  303. getPreSiblingNode(node) {
  304. const parent = this.getParent(node);
  305. const siblings = parent ? parent.children : this.children;
  306. const index = siblings.indexOf(node);
  307. if (index > 0) {
  308. return siblings[index - 1];
  309. } else {
  310. return null;
  311. }
  312. }
  313. /**
  314. * 查找node的后兄弟节点
  315. * @param node
  316. * @returns {*}
  317. */
  318. getNextSiblingNode(node) {
  319. const parent = this.getParent(node);
  320. const siblings = parent ? parent.children : this.children;
  321. const index = siblings.indexOf(node);
  322. if (index >= 0 && index < siblings.length - 1) {
  323. return siblings[index + 1];
  324. } else {
  325. return null;
  326. }
  327. }
  328. /**
  329. * 根据path查找完整节点
  330. * @param {Number} path
  331. */
  332. getFullPathNodes (path) {
  333. const self = this, ids = path.split('.');
  334. if (ids.length > 0) {
  335. return this.nodes.filter((x) => {
  336. return ids.indexOf('' + x[self.setting.id]) >= 0;
  337. });
  338. } else {
  339. return [];
  340. }
  341. };
  342. /**
  343. * 查询node的已下载子节点
  344. * @param {Object} node
  345. * @returns {Array}
  346. */
  347. getChildren (node) {
  348. const setting = this.setting;
  349. const pid = node ? node[setting.id] : setting.rootId;
  350. const children = this.datas.filter(function (x) {
  351. return x[setting.pid] === pid;
  352. });
  353. children.sort(function (a, b) {
  354. return a.order - b.order;
  355. });
  356. return children;
  357. };
  358. /**
  359. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  360. * @param node
  361. * @returns {*}
  362. * @private
  363. */
  364. _recursiveGetPosterity (node) {
  365. let posterity = node.children;
  366. for (const c of node.children) {
  367. posterity = posterity.concat(this._recursiveGetPosterity(c));
  368. }
  369. return posterity;
  370. };
  371. /**
  372. * 查询node的已下载的全部后代
  373. * @param {Object} node
  374. * @returns {Array}
  375. */
  376. getPosterity (node) {
  377. if (node.full_path !== '') {
  378. const reg = new RegExp('^' + node.full_path + '.');
  379. return this.datas.filter(function (x) {
  380. return reg.test(x.full_path);
  381. });
  382. } else {
  383. return this._recursiveGetPosterity(node);
  384. }
  385. };
  386. /**
  387. * 查询node是否是父节点的最后一个子节点
  388. * @param {Object} node
  389. * @returns {boolean}
  390. */
  391. isLastSibling (node) {
  392. const siblings = this.getChildren(this.getParent(node));
  393. return node.order === siblings[siblings.length - 1].order;
  394. };
  395. /**
  396. * 刷新子节点是否可见
  397. * @param {Object} node
  398. * @private
  399. */
  400. _refreshChildrenVisible (node) {
  401. if (!node.children) {
  402. node.children = this.getChildren(node);
  403. }
  404. if (node.children && node.children.length > 0) {
  405. for (const child of node.children) {
  406. child.visible = node.expanded && node.visible;
  407. this._refreshChildrenVisible(child);
  408. }
  409. }
  410. };
  411. /**
  412. * 设置节点是否展开, 并控制子节点可见
  413. * @param {Object} node
  414. * @param {Boolean} expanded
  415. */
  416. setExpanded (node, expanded) {
  417. node.expanded = expanded;
  418. this._refreshChildrenVisible(node);
  419. };
  420. /**
  421. * 提取节点key和索引数据
  422. * @param {Object} node - 节点
  423. * @returns {key}
  424. */
  425. getNodeKeyData (node) {
  426. const data = {};
  427. for (const key of this.setting.keys) {
  428. data[key] = node[key];
  429. }
  430. return data;
  431. };
  432. /**
  433. * 得到树结构构成id
  434. * @param node
  435. * @returns {*}
  436. */
  437. getNodeKey (node) {
  438. return node[this.setting.id];
  439. };
  440. /**
  441. * 递归 设置节点展开状态
  442. * @param {Array} nodes - 需要设置状态的节点
  443. * @param {Object} parent - nodes的父节点
  444. * @param {Function} checkFun - 判断节点展开状态的方法
  445. * @private
  446. */
  447. _recursiveExpand(nodes, parent, checkFun) {
  448. for (const node of nodes) {
  449. node.expanded = checkFun(node);
  450. node.visible = parent ? (parent.expanded && parent.visible) : true;
  451. this._recursiveExpand(node.children, node, checkFun);
  452. }
  453. }
  454. /**
  455. * 自定义展开规则
  456. * @param checkFun
  457. */
  458. expandByCustom(checkFun) {
  459. this._recursiveExpand(this.children, null, checkFun);
  460. }
  461. /**
  462. * 展开到第几层
  463. * @param {Number} level - 展开层数
  464. */
  465. expandByLevel(level) {
  466. // function recursiveExpand(nodes, parent) {
  467. // for (const node of nodes) {
  468. // node.expanded = node.level < level;
  469. // node.visible = parent ? (parent.expanded && parent.visible) : true;
  470. // recursiveExpand(node.children, node);
  471. // }
  472. // }
  473. // recursiveExpand(this.children);
  474. this.expandByCustom(function (n) {
  475. return n.level < level;
  476. });
  477. }
  478. }
  479. class MeasureTree extends BaseTree {
  480. addData (datas) {
  481. const loadedData = [];
  482. for (const data of datas) {
  483. let node = this.getItems(data[this.setting.id]);
  484. if (node) {
  485. for (const prop in node) {
  486. if (data[prop] !== undefined) {
  487. node[prop] = data[prop];
  488. }
  489. }
  490. loadedData.push(node);
  491. } else {
  492. const keyName = itemsPre + data[this.setting.id];
  493. const node = JSON.parse(JSON.stringify(data));
  494. this.items[keyName] = node;
  495. this.datas.push(node);
  496. node.expanded = false;
  497. node.visible = true;
  498. loadedData.push(node);
  499. }
  500. }
  501. this.sortTreeNode();
  502. for (const node of loadedData) {
  503. const children = node.children;
  504. if (!node.expanded && children.length > 0) {
  505. node.expanded = true;
  506. this._refreshChildrenVisible(node);
  507. }
  508. }
  509. return loadedData;
  510. }
  511. removeData (datas) {
  512. datas.sort(function (a, b) {
  513. return b.level - a.level;
  514. });
  515. const removeArrayData = function (array, data) {
  516. const index = array.indexOf(data);
  517. array.splice(index, 1);
  518. };
  519. for (const data of datas) {
  520. const node = this.getItems(data[this.setting.id]);
  521. if (node && this.getChildren(node).length === 0) {
  522. delete this.items[itemsPre + node[this.setting.id]];
  523. if (node[this.setting.pid] !== this.setting.rootId) {
  524. const parent = this.items[itemsPre + node[this.setting.pid]];
  525. removeArrayData(parent.children, node);
  526. }
  527. removeArrayData(this.datas, node);
  528. removeArrayData(this.nodes, node);
  529. }
  530. }
  531. };
  532. loadLeafData (data) {
  533. const datas = data instanceof Array ? data : [data];
  534. for (const d of datas) {
  535. let node = this.getItems(d[this.setting.id]);
  536. if (node && node.is_leaf) {
  537. for (const prop in node) {
  538. if (data[prop] !== undefined) {
  539. node[prop] = d[prop];
  540. }
  541. }
  542. }
  543. }
  544. };
  545. }
  546. class FxTree extends BaseTree {
  547. /**
  548. * 检查节点是否是最底层项目节
  549. * @param node
  550. * @returns {boolean}
  551. */
  552. isLeafXmj(node) {
  553. if (!node.code) {
  554. return false;
  555. }
  556. for (const child of node.children) {
  557. if (!child.b_code || child.b_code === '') {
  558. return false;
  559. }
  560. }
  561. return true;
  562. }
  563. /**
  564. * 查询最底层项目节(本身或父项)
  565. * @param {Object} node - 查询节点
  566. * @returns {Object}
  567. */
  568. getLeafXmjParent(node) {
  569. let parent = node;
  570. while (parent) {
  571. if (this.isLeafXmj(parent)) {
  572. return parent;
  573. } else {
  574. parent = this.getParent(parent);
  575. }
  576. }
  577. return null;
  578. }
  579. /**
  580. * 展开至最底层项目节
  581. */
  582. expandToLeafXmj() {
  583. const self = this;
  584. this.expandByCustom(function (node) {
  585. if (node.b_code && node.b_code !== '') {
  586. return false;
  587. } else {
  588. return !self.isLeafXmj(node);
  589. }
  590. })
  591. }
  592. }
  593. class LedgerTree extends FxTree {
  594. /**
  595. * 加载数据(动态),只加载不同部分
  596. * @param {Array} datas
  597. * @return {Array} 加载到树的数据
  598. * @privateA
  599. */
  600. _updateData (datas) {
  601. datas = datas instanceof Array ? datas : [datas];
  602. let loadedData = [];
  603. for (const data of datas) {
  604. let node = this.getItems(data[this.setting.id]);
  605. if (node) {
  606. for (const prop in node) {
  607. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  608. }
  609. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  610. if (prop === this.setting.pid) {
  611. loadedData.push(this.getItems(node[this.setting.pid]));
  612. loadedData.push(this.getItems(data[this.setting.pid]));
  613. }
  614. node[prop] = data[prop];
  615. }
  616. }
  617. loadedData.push(node);
  618. }
  619. }
  620. loadedData = _.uniq(loadedData);
  621. for (const node of loadedData) {
  622. node.children = this.getChildren(node);
  623. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  624. }
  625. this.sortTreeNode(true);
  626. return loadedData;
  627. };
  628. /**
  629. * 加载数据(动态),只加载不同部分
  630. * @param {Array} datas
  631. * @return {Array} 加载到树的数据
  632. * @privateA
  633. */
  634. _loadData (datas) {
  635. datas = datas instanceof Array ? datas : [datas];
  636. const loadedData = [], resortData = [];
  637. for (const data of datas) {
  638. let node = this.getItems(data[this.setting.id]);
  639. if (node) {
  640. const parent = this.getItems(node[this.setting.pid]);
  641. for (const prop in node) {
  642. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  643. node[prop] = data[prop];
  644. if (parent && resortData.indexOf(parent) === -1) {
  645. resortData.push(parent);
  646. }
  647. }
  648. }
  649. loadedData.push(node);
  650. } else {
  651. const keyName = itemsPre + data[this.setting.id];
  652. const node = JSON.parse(JSON.stringify(data));
  653. this.items[keyName] = node;
  654. this.datas.push(node);
  655. node.expanded = true;
  656. node.visible = true;
  657. loadedData.push(node);
  658. if (resortData.indexOf(node) === -1) {
  659. resortData.push(node);
  660. }
  661. const parent = this.getItems(node[this.setting.pid]);
  662. if (parent && resortData.indexOf(parent) === -1) {
  663. resortData.push(parent);
  664. }
  665. }
  666. }
  667. for (const node of resortData) {
  668. node.children = this.getChildren(node);
  669. }
  670. this.sortTreeNode(true);
  671. for (const node of loadedData) {
  672. if (!node.expanded) {
  673. this.setExpanded(node, true);
  674. }
  675. }
  676. return loadedData;
  677. };
  678. /**
  679. * 清理数据(动态)
  680. * @param datas
  681. * @private
  682. */
  683. _freeData (datas) {
  684. datas = datas instanceof Array ? datas : [datas];
  685. const freeDatas = [];
  686. const removeArrayData = function (array, data) {
  687. const index = array.indexOf(data);
  688. array.splice(index, 1);
  689. };
  690. for (const data of datas) {
  691. const node = this.getItems(data[this.setting.id]);
  692. if (node) {
  693. freeDatas.push(node);
  694. delete this.items[itemsPre + node[this.setting.id]];
  695. if (node[this.setting.pid] !== this.setting.rootId) {
  696. const parent = this.getItems(node[this.setting.pid]);
  697. if (parent) {
  698. removeArrayData(parent.children, node);
  699. }
  700. }
  701. removeArrayData(this.datas, node);
  702. removeArrayData(this.nodes, node);
  703. }
  704. }
  705. return freeDatas;
  706. };
  707. /**
  708. * 加载需展开的数据
  709. * @param {Array} datas
  710. * @returns {Array}
  711. * @private
  712. */
  713. _loadExpandData (datas) {
  714. datas = datas instanceof Array ? datas : [datas];
  715. const loadedData = [], existData = [], expandData = [], resortData = [];
  716. for (const data of datas) {
  717. let node = this.getItems(data[this.setting.id]);
  718. if (node) {
  719. existData.push(node);
  720. } else {
  721. const keyName = itemsPre + data[this.setting.id];
  722. const node = JSON.parse(JSON.stringify(data));
  723. this.items[keyName] = node;
  724. this.datas.push(node);
  725. node.expanded = false;
  726. node.visible = true;
  727. loadedData.push(node);
  728. if (resortData.indexOf(node) === -1) {
  729. resortData.push(node);
  730. }
  731. const parent = this.getItems(node[this.setting.pid]);
  732. if (parent && resortData.indexOf(parent) === -1) {
  733. resortData.push(parent);
  734. }
  735. }
  736. }
  737. for (const node of resortData) {
  738. node.children = this.getChildren(node);
  739. }
  740. this.sortTreeNode(true);
  741. for (const node of loadedData) {
  742. if (!node.expanded) {
  743. this.setExpanded(node, true);
  744. }
  745. }
  746. for (const node of existData) {
  747. const parent = this.getItems(node[this.setting.pid]);
  748. if (expandData.indexOf(parent) === -1) {
  749. expandData.push(parent);
  750. if (!parent.expanded) {
  751. this.setExpanded(parent, true);
  752. }
  753. }
  754. if (!node.expanded) {
  755. this.setExpanded(node, true);
  756. }
  757. }
  758. return [loadedData, expandData];
  759. };
  760. /**
  761. *
  762. * @param parent
  763. * @param node
  764. * @private
  765. */
  766. _getNodesParents(parents, nodes) {
  767. for (const node of nodes) {
  768. const parent = this.getParent(node);
  769. if (parent) {
  770. const paths = this.getFullPathNodes(parent.full_path);
  771. for (const p of paths) {
  772. if (parents.indexOf(p) === -1) {
  773. parents.push(p);
  774. }
  775. }
  776. }
  777. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  778. parents.push(node);
  779. }
  780. }
  781. }
  782. _getReCalcNodes(reCalcNodes, nodes) {
  783. for (const node of nodes) {
  784. const parent = this.getParent(node);
  785. if (parent) {
  786. const paths = this.getFullPathNodes(parent.full_path);
  787. for (const p of paths) {
  788. if (reCalcNodes.indexOf(p) === -1) {
  789. reCalcNodes.push(p);
  790. }
  791. }
  792. }
  793. // 最底层项目节,也需要计算
  794. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  795. reCalcNodes.push(node);
  796. //}
  797. }
  798. }
  799. /**
  800. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  801. *
  802. * @param data - 更新的数据 {update, create, delete}
  803. * @returns {{}}
  804. */
  805. loadPostData(data) {
  806. const result = {}, reCalcNodes = [];
  807. if (data.update) {
  808. result.update = this._updateData(data.update);
  809. this._getReCalcNodes(reCalcNodes, result.update);
  810. }
  811. if (data.create) {
  812. result.create = this._loadData(data.create);
  813. this._getReCalcNodes(reCalcNodes, result.create);
  814. }
  815. if (data.delete) {
  816. result.delete = this._freeData(data.delete);
  817. this._getReCalcNodes(reCalcNodes, result.delete);
  818. }
  819. reCalcNodes.sort((a, b) => {
  820. return b.level - a.level;
  821. });
  822. for (const node of reCalcNodes) {
  823. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  824. }
  825. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  826. return result;
  827. }
  828. /**
  829. * 以下方法需等待响应, 通过callback刷新界面
  830. */
  831. /**
  832. * 加载子节点
  833. * @param {Object} node
  834. * @param {function} callback
  835. */
  836. loadChildren (node, callback) {
  837. const self = this;
  838. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  839. postData(url, this.getNodeKeyData(node), function (data) {
  840. self._loadData(data);
  841. callback();
  842. });
  843. };
  844. /**
  845. * 树结构基本操作
  846. * @param {String} url - 请求地址
  847. * @param {Object} node - 操作节点
  848. * @param {String} type - 操作类型
  849. * @param {function} callback - 界面刷新
  850. */
  851. baseOperation (url, node, type, callback) {
  852. const self = this;
  853. const data = {
  854. id: node[this.setting.id],
  855. postType: type
  856. };
  857. postData(url, data, function (datas) {
  858. const refreshData = self.loadPostData(datas);
  859. callback(refreshData);
  860. });
  861. };
  862. /**
  863. * 节点数据编辑
  864. * @param {String} url - 请求地址
  865. * @param {Array|Object} updateData - 需更新的数据
  866. * @param {function} callback - 界面刷新
  867. */
  868. update (url, updateData, callback) {
  869. const self = this;
  870. postData(url, updateData, function (datas) {
  871. const refreshData = self.loadPostData(datas);
  872. callback(refreshData);
  873. }, function () {
  874. if (updateData instanceof Array) {
  875. const result = [];
  876. for (const data of updateData) {
  877. result.push(self.getItems(data[self.setting.id]));
  878. }
  879. callback(result)
  880. } else {
  881. callback([self.getItems(updateData[self.setting.id])]);
  882. }
  883. });
  884. };
  885. /**
  886. * 复制粘贴整块(目前仅可粘贴为后项)
  887. * @param {String} url - 请求地址
  888. * @param {Object} node - 操作节点
  889. * @param {Array} block - 被复制整块的节点列表
  890. * @param {function} callback - 界面刷新
  891. */
  892. pasteBlock (url, node, block, callback) {
  893. const self = this;
  894. const data = {
  895. id: node[self.setting.id],
  896. block: block
  897. };
  898. postData(url, data, function (datas) {
  899. const refreshData = self.loadPostData(datas);
  900. callback(refreshData);
  901. });
  902. };
  903. /**
  904. * 提交数据
  905. * @param {String} url - 请求地址
  906. * @param {Object} node - 当前选中节点
  907. * @param {Object} data - 提交的数据
  908. * @param {function} callback - 界面刷新
  909. */
  910. postData (url, node, data, callback) {
  911. const self = this;
  912. if (node) {
  913. data.id = node[self.setting.id];
  914. }
  915. postData(url, data, function (datas) {
  916. const refreshData = self.loadPostData(datas);
  917. callback(refreshData);
  918. // const result = {};
  919. // if (datas.update) {
  920. // result.update = self._updateData(datas.update);
  921. // }
  922. // if (datas.create) {
  923. // result.create = self._loadData(datas.create);
  924. // }
  925. // if (datas.delete) {
  926. // result.delete = self._freeData(datas.delete);
  927. // }
  928. // if (datas.expand) {
  929. // const [create, update] = self._loadExpandData(datas.expand);
  930. // result.create = result.create ? result.create.concat(create) : create;
  931. // result.expand = update;
  932. // }
  933. // callback(result);
  934. });
  935. };
  936. }
  937. class StageTree extends FxTree {
  938. /**
  939. * 构造函数
  940. */
  941. constructor (setting) {
  942. super(setting);
  943. // stage关联索引
  944. this.stageItems = {};
  945. }
  946. /**
  947. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  948. * @param datas
  949. */
  950. loadDatas (datas) {
  951. super.loadDatas(datas);
  952. // 清空旧数据
  953. this.stageItems = {};
  954. // 加载全部数据
  955. for (const data of this.datas) {
  956. const keyName = itemsPre + data[this.setting.stageId];
  957. this.stageItems[keyName] = data;
  958. }
  959. }
  960. getStageItems(id) {
  961. return this.stageItems[itemsPre + id];
  962. }
  963. loadStageData(datas, fieldPre, fields) {
  964. datas = datas instanceof Array ? datas : [datas];
  965. const loadedData = [];
  966. for (const data of datas) {
  967. let node = this.getStageItems(data.lid);
  968. if (node) {
  969. for (const prop of fields) {
  970. if (data[prop] !== undefined) {
  971. node[fieldPre + prop] = data[prop];
  972. }
  973. }
  974. loadedData.push(node);
  975. }
  976. }
  977. }
  978. loadPreStageData(datas) {
  979. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  980. }
  981. loadCurStageData(datas) {
  982. this.loadStageData(datas, '', this.setting.updateFields);
  983. }
  984. /**
  985. * 加载数据(动态),只加载不同部分
  986. * @param {Array} datas
  987. * @return {Array} 加载到树的数据
  988. * @privateA
  989. */
  990. _updateData (datas) {
  991. datas = datas instanceof Array ? datas : [datas];
  992. let loadedData = [];
  993. for (const data of datas) {
  994. let node = this.getItems(data[this.setting.id]);
  995. if (node) {
  996. for (const prop in node) {
  997. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  998. }
  999. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1000. if (prop === this.setting.pid) {
  1001. loadedData.push(this.getItems(node[this.setting.pid]));
  1002. loadedData.push(this.getItems(data[this.setting.pid]));
  1003. }
  1004. node[prop] = data[prop];
  1005. }
  1006. }
  1007. loadedData.push(node);
  1008. }
  1009. }
  1010. loadedData = _.uniq(loadedData);
  1011. for (const node of loadedData) {
  1012. node.children = this.getChildren(node);
  1013. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1014. }
  1015. this.sortTreeNode(true);
  1016. return loadedData;
  1017. };
  1018. /**
  1019. * 加载数据(动态),只加载不同部分
  1020. * @param {Array} datas
  1021. * @return {Array} 加载到树的数据
  1022. * @privateA
  1023. */
  1024. _updateStageData (datas) {
  1025. datas = datas instanceof Array ? datas : [datas];
  1026. const loadedData = [];
  1027. for (const data of datas) {
  1028. let node = this.getStageItems(data.lid);
  1029. if (node) {
  1030. for (const prop of this.setting.updateFields) {
  1031. if (data[prop] !== undefined) {
  1032. node[prop] = data[prop];
  1033. }
  1034. }
  1035. loadedData.push(node);
  1036. }
  1037. }
  1038. return loadedData;
  1039. };
  1040. /**
  1041. *
  1042. * @param parent
  1043. * @param node
  1044. * @private
  1045. */
  1046. _getNodesParents(parents, nodes) {
  1047. for (const node of nodes) {
  1048. const parent = this.getParent(node);
  1049. if (parent) {
  1050. const paths = this.getFullPathNodes(parent.full_path);
  1051. for (const p of paths) {
  1052. if (parents.indexOf(p) === -1) {
  1053. parents.push(p);
  1054. }
  1055. }
  1056. }
  1057. if (node.children && node.children.length > 0) {
  1058. parents.push(node);
  1059. }
  1060. }
  1061. }
  1062. /**
  1063. * 提交数据至后端,返回的前端树结构应刷新的部分
  1064. * StageTree仅有更新CurStage部分,不需要增删
  1065. *
  1066. * @param data - 需要更新的数据
  1067. * @returns {Array} - 界面需要刷新的数据
  1068. */
  1069. loadPostStageData(data) {
  1070. let result, parents = [];
  1071. if (data.bills) {
  1072. result = this._updateData(data.bills);
  1073. this._getNodesParents(parents, result);
  1074. }
  1075. if (data.curStageData) {
  1076. result = this._updateStageData(data.curStageData);
  1077. this._getNodesParents(parents, result);
  1078. }
  1079. result = result ? result.concat(parents) : parents;
  1080. result.sort((a, b) => {
  1081. return b.level - a.level;
  1082. });
  1083. for (const node of result) {
  1084. treeCalc.calculateNode(this, node);
  1085. }
  1086. return result;
  1087. }
  1088. }
  1089. class MasterTree extends FxTree {
  1090. /**
  1091. * 构造函数
  1092. */
  1093. constructor (setting) {
  1094. super(setting);
  1095. // 关联索引
  1096. this.masterItems = {};
  1097. }
  1098. /**
  1099. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1100. * @param datas
  1101. */
  1102. loadDatas (datas) {
  1103. super.loadDatas(datas);
  1104. // 清空旧数据
  1105. this.masterItems = {};
  1106. // minor数据缓存
  1107. this.minorData = {};
  1108. // 加载全部数据
  1109. for (const data of this.datas) {
  1110. const keyName = itemsPre + data[this.setting.masterId];
  1111. this.masterItems[keyName] = data;
  1112. }
  1113. }
  1114. /**
  1115. * 根据关联id,查找节点
  1116. * @param id
  1117. * @returns {*}
  1118. */
  1119. getMasterItems(id) {
  1120. return this.masterItems[itemsPre + id];
  1121. }
  1122. /**
  1123. * 加载关联数据
  1124. *
  1125. * @param {Array|Object}datas - 需要关联的数据
  1126. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1127. * @param {Array} fields - 关联字段
  1128. * @returns {Array}
  1129. */
  1130. loadMinorData(datas, fieldSuf, fields) {
  1131. if (!datas) { return; }
  1132. datas = datas instanceof Array ? datas : [datas];
  1133. this.minorData[fieldSuf] = datas;
  1134. const loadedData = [];
  1135. for (const data of datas) {
  1136. let node = this.getMasterItems(data[this.setting.minorId]);
  1137. if (node) {
  1138. for (const prop of fields) {
  1139. if (data[prop] !== undefined) {
  1140. node[prop + fieldSuf] = data[prop];
  1141. }
  1142. }
  1143. loadedData.push(node);
  1144. }
  1145. }
  1146. return loadedData;
  1147. }
  1148. /**
  1149. * 展开至最底层项目节
  1150. */
  1151. expandByCalcFields() {
  1152. const self = this;
  1153. this.expandByCustom(function (node) {
  1154. for (const field of self.setting.calcFields) {
  1155. if (node[field]) {
  1156. return true;
  1157. }
  1158. }
  1159. return false;
  1160. })
  1161. }
  1162. }
  1163. if (type === 'base') {
  1164. return new BaseTree(setting);
  1165. } else if (type === 'stage') {
  1166. return new StageTree(setting);
  1167. } else if (type === 'ledger') {
  1168. return new LedgerTree(setting);
  1169. } else if (type === 'measure') {
  1170. return new MeasureTree(setting);
  1171. } else if (type === 'master') {
  1172. return new MasterTree(setting);
  1173. }
  1174. };
  1175. const treeCalc = {
  1176. getMaxLevel: function (tree) {
  1177. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  1178. },
  1179. calculateNode: function (tree, node) {
  1180. if (node.children && node.children.length > 0) {
  1181. const gather = node.children.reduce(function (rst, x) {
  1182. const result = {};
  1183. for (const cf of tree.setting.calcFields) {
  1184. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1185. }
  1186. return result;
  1187. });
  1188. // 汇总子项
  1189. for (const cf of tree.setting.calcFields) {
  1190. if (gather[cf]) {
  1191. node[cf] = gather[cf];
  1192. } else {
  1193. node[cf] = null;
  1194. }
  1195. }
  1196. }
  1197. // 自身运算
  1198. if (tree.setting.calcFun) {
  1199. tree.setting.calcFun(node);
  1200. }
  1201. },
  1202. calculateLevelNode: function (tree, level) {
  1203. const nodes = tree.datas.filter((n) => { return n.level === level });
  1204. for (const node of nodes) {
  1205. this.calculateNode(tree, node);
  1206. }
  1207. },
  1208. calculateAll: function (tree) {
  1209. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  1210. this.calculateLevelNode(tree, i);
  1211. }
  1212. },
  1213. calculateParent: function (tree, node) {
  1214. const nodes = tree.getFullPathNodes(node.full_path);
  1215. nodes.sort((a, b) => {
  1216. return b.level - a.level;
  1217. });
  1218. for (const n of nodes) {
  1219. this.calculateNode(tree, n);
  1220. }
  1221. return nodes;
  1222. }
  1223. };