path_tree.js 37 KB

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