path_tree.js 39 KB

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