path_tree.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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. const itemsPre = 'id_';
  11. class PosData {
  12. /**
  13. * 构造函数
  14. * @param {id|Number, masterId|Number} setting
  15. */
  16. constructor(setting) {
  17. // 无索引
  18. this.datas = [];
  19. // 以key为索引
  20. this.items = {};
  21. // 以分类id为索引的有序
  22. this.ledgerPos = {};
  23. // pos设置
  24. this.setting = setting;
  25. }
  26. /**
  27. * 加载部位明细数据
  28. * @param datas
  29. */
  30. loadDatas(datas) {
  31. this.datas = datas;
  32. this.items = {};
  33. this.ledgerPos = {};
  34. for (const data of this.datas) {
  35. const key = itemsPre + data[this.setting.id];
  36. this.items[key] = data;
  37. const masterKey = itemsPre + data[this.setting.ledgerId];
  38. if (!this.ledgerPos[masterKey]) {
  39. this.ledgerPos[masterKey] = [];
  40. }
  41. this.ledgerPos[masterKey].push(data);
  42. }
  43. for (const prop in this.ledgerPos) {
  44. this.resortLedgerPos(this.ledgerPos[prop]);
  45. }
  46. }
  47. /**
  48. * 更新数据
  49. * @param datas
  50. */
  51. updateDatas(data) {
  52. const datas = data instanceof Array ? data : [data];
  53. const result = { create: [], update: [] }, resort = [];
  54. for (const d of datas) {
  55. const key = itemsPre + d[this.setting.id];
  56. if (!this.items[key]) {
  57. this.datas.push(d);
  58. this.items[key] = d;
  59. const masterKey = itemsPre + d[this.setting.ledgerId];
  60. if (!this.ledgerPos[masterKey]) {
  61. this.ledgerPos[masterKey] = [];
  62. }
  63. this.ledgerPos[masterKey].push(d);
  64. result.create.push(d);
  65. } else {
  66. const pos = this.items[key];
  67. for (const prop in d) {
  68. pos[prop] = d[prop];
  69. }
  70. result.update.push(pos);
  71. }
  72. const masterKey = itemsPre + d[this.setting.ledgerId];
  73. if (resort.indexOf(masterKey) === -1) {
  74. resort.push(masterKey);
  75. }
  76. }
  77. for (const s of resort) {
  78. this.resortLedgerPos(this.ledgerPos[s]);
  79. }
  80. return result;
  81. }
  82. /**
  83. * 移除数据
  84. * @param datas
  85. */
  86. removeDatas(data) {
  87. if (!data) { return; }
  88. const datas = data instanceof Array ? data : [data];
  89. for (let i = datas.length - 1; i >= 0; i--) {
  90. const id = datas[i];
  91. const d = this.getPos(id);
  92. this.datas.splice(this.datas.indexOf(d), 1);
  93. const key = itemsPre + d[this.setting.id];
  94. delete this.items[key];
  95. const masterKey = itemsPre + d[this.setting.ledgerId];
  96. const range = this.ledgerPos[masterKey];
  97. range.splice(range.indexOf(d), 1);
  98. if (range.length === 0) {
  99. delete this.ledgerPos[masterKey];
  100. }
  101. }
  102. }
  103. /**
  104. * 移除数据 - 根据分类id
  105. * @param mid
  106. */
  107. removeDatasByMasterId(mid) {
  108. const masterKey = itemsPre + mid;
  109. const range = this.ledgerPos[masterKey];
  110. if (range) {
  111. delete this.ledgerPos[masterKey];
  112. for (const r of range) {
  113. this.datas.splice(this.datas.indexOf(r), 1);
  114. const key = itemsPre + r[this.setting.id];
  115. delete this.items[key];
  116. }
  117. }
  118. }
  119. getPos(id) {
  120. return this.items[itemsPre + id];
  121. }
  122. getLedgerPos(mid) {
  123. return this.ledgerPos[itemsPre + mid];
  124. }
  125. resortLedgerPos(ledgerPos) {
  126. if (ledgerPos instanceof Array) {
  127. ledgerPos.sort(function (a, b) {
  128. return a.porder - b.porder;
  129. })
  130. }
  131. }
  132. /**
  133. * 计算全部
  134. */
  135. calculateAll() {
  136. if (!this.setting.calcFun) { return; }
  137. for (const pos of this.datas) {
  138. this.setting.calcFun(pos);
  139. }
  140. }
  141. }
  142. class StagePosData extends PosData {
  143. loadStageData(datas, fieldPre, fields) {
  144. if (!datas) { return; }
  145. datas = datas instanceof Array ? datas : [datas];
  146. const loadedData = [];
  147. for (const data of datas) {
  148. let node = this.getPos(data.pid);
  149. if (node) {
  150. for (const prop of fields) {
  151. if (data[prop] !== undefined) {
  152. node[fieldPre + prop] = data[prop];
  153. }
  154. }
  155. if (this.setting.calcFun) {
  156. this.setting.calcFun(node);
  157. }
  158. loadedData.push(node);
  159. }
  160. }
  161. }
  162. loadPreStageData(datas) {
  163. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  164. }
  165. loadCurStageData(datas) {
  166. this.loadStageData(datas, '', this.setting.updateFields);
  167. }
  168. }
  169. class MasterPosData extends PosData {
  170. /**
  171. * 构造函数
  172. * @param {id|Number, masterId|Number} setting
  173. */
  174. constructor(setting) {
  175. super(setting);
  176. // 关联索引
  177. this.masterItems = {};
  178. }
  179. /**
  180. * 加载主数据
  181. * @param datas
  182. */
  183. loadDatas(datas) {
  184. super.loadDatas(datas);
  185. // 清空旧数据
  186. this.masterItems = {};
  187. // minor数据缓存
  188. this.minorData = {};
  189. // 加载全部数据
  190. for (const data of this.datas) {
  191. const keyName = itemsPre + data[this.setting.masterId];
  192. this.masterItems[keyName] = data;
  193. }
  194. }
  195. /**
  196. * 根据关联id,查找节点
  197. * @param id
  198. * @returns {*}
  199. */
  200. getMasterItems(id) {
  201. return this.masterItems[itemsPre + id];
  202. }
  203. /**
  204. * 加载关联数据
  205. *
  206. * @param {Array|Object}datas - 需要关联的数据
  207. * @param {String} fieldPre - 关联字段前缀(关联结果)
  208. * @param {Array} fields - 关联字段
  209. * @returns {Array}
  210. */
  211. loadMinorData(datas, fieldSuf, fields) {
  212. if (!datas) return;
  213. datas = datas instanceof Array ? datas : [datas];
  214. this.minorData[fieldSuf] = datas;
  215. const loadedData = [];
  216. for (const data of datas) {
  217. let node = this.getMasterItems(data[this.setting.minorId]);
  218. if (node) {
  219. for (const prop of fields) {
  220. if (data[prop] !== undefined) {
  221. node[prop + fieldSuf] = data[prop];
  222. }
  223. }
  224. loadedData.push(node);
  225. }
  226. }
  227. return loadedData;
  228. }
  229. }
  230. const createNewPathTree = function (type, setting) {
  231. class BaseTree {
  232. /**
  233. * 构造函数
  234. */
  235. constructor(setting) {
  236. // 无索引
  237. this.datas = [];
  238. // 以key为索引
  239. this.items = {};
  240. // 以排序为索引
  241. this.nodes = [];
  242. // 根节点
  243. this.children = [];
  244. // 树设置
  245. this.setting = setting;
  246. }
  247. /**
  248. * 树结构根据显示排序
  249. */
  250. sortTreeNode(isResort) {
  251. const self = this;
  252. const addSortNodes = function (nodes) {
  253. if (!nodes) { return }
  254. for (let i = 0; i < nodes.length; i++) {
  255. self.nodes.push(nodes[i]);
  256. nodes[i].index = self.nodes.length - 1;
  257. if (!isResort) {
  258. nodes[i].children = self.getChildren(nodes[i]);
  259. } else {
  260. nodes[i].children.sort(function (a, b) {
  261. return a.order - b.order;
  262. })
  263. }
  264. addSortNodes(nodes[i].children);
  265. }
  266. };
  267. this.nodes = [];
  268. if (!isResort) {
  269. this.children = this.getChildren();
  270. } else {
  271. this.children.sort(function (a, b) {
  272. return a.order - b.order;
  273. })
  274. }
  275. addSortNodes(this.children);
  276. }
  277. /**
  278. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  279. * @param datas
  280. */
  281. loadDatas(datas) {
  282. // 清空旧数据
  283. this.items = {};
  284. this.nodes = [];
  285. this.datas = [];
  286. this.children = [];
  287. // 加载全部数据
  288. datas.sort(function (a, b) {
  289. return a.level - b.level;
  290. });
  291. for (const data of datas) {
  292. const keyName = itemsPre + data[this.setting.id];
  293. if (!this.items[keyName]) {
  294. const item = JSON.parse(JSON.stringify(data));
  295. item.children = [];
  296. item.expanded = true;
  297. item.visible = true;
  298. this.items[keyName] = item;
  299. this.datas.push(item);
  300. if (item[setting.pid] === setting.rootId) {
  301. this.children.push(item);
  302. } else {
  303. const parent = this.getParent(item);
  304. if (parent) {
  305. parent.children.push(item);
  306. }
  307. }
  308. }
  309. }
  310. this.children.sort(function (a, b) {
  311. return a.order - b.order;
  312. });
  313. this.sortTreeNode(true);
  314. if (this.setting.autoExpand >= 0) this.expandByLevel(this.setting.autoExpand);
  315. }
  316. getItemsByIndex(index) {
  317. return this.nodes[index];
  318. }
  319. /**
  320. * 根据id获取树结构节点数据
  321. * @param {Number} id
  322. * @returns {Object}
  323. */
  324. getItems(id) {
  325. return this.items[itemsPre + id];
  326. };
  327. /**
  328. * 查找node的parent
  329. * @param {Object} node
  330. * @returns {Object}
  331. */
  332. getParent(node) {
  333. return this.getItems(node[this.setting.pid]);
  334. };
  335. getAllParents(node) {
  336. const parents = [];
  337. if (node.full_path && node.full_path !== '') {
  338. const parentIds = node.full_path.split('-');
  339. for (const id of parentIds) {
  340. if (id !== node[this.setting.id]) {
  341. parents.push(this.getItems(id));
  342. }
  343. }
  344. } else {
  345. let vP = this.getParent(node);
  346. while (vP) {
  347. parents.push(vP);
  348. vP = this.getParent(vP);
  349. }
  350. }
  351. return parents;
  352. }
  353. /**
  354. * 查找node的前兄弟节点
  355. * @param node
  356. * @returns {*}
  357. */
  358. getPreSiblingNode(node) {
  359. if (!node) return null;
  360. const parent = this.getParent(node);
  361. const siblings = parent ? parent.children : this.children;
  362. const index = siblings.indexOf(node);
  363. return (index > 0) ? siblings[index - 1] : null;
  364. }
  365. /**
  366. * 查找node的后兄弟节点
  367. * @param node
  368. * @returns {*}
  369. */
  370. getNextSiblingNode(node) {
  371. const parent = this.getParent(node);
  372. const siblings = parent ? parent.children : this.children;
  373. const index = siblings.indexOf(node);
  374. if (index >= 0 && index < siblings.length - 1) {
  375. return siblings[index + 1];
  376. } else {
  377. return null;
  378. }
  379. }
  380. /**
  381. * 根据path查找完整节点
  382. * @param {Number} path
  383. */
  384. getFullPathNodes(path) {
  385. const self = this, ids = path.split('-');
  386. if (ids.length > 0) {
  387. return this.nodes.filter((x) => {
  388. return ids.indexOf('' + x[self.setting.id]) >= 0;
  389. });
  390. } else {
  391. return [];
  392. }
  393. };
  394. /**
  395. * 查询node的已下载子节点
  396. * @param {Object} node
  397. * @returns {Array}
  398. */
  399. getChildren(node) {
  400. const setting = this.setting;
  401. const pid = node ? node[setting.id] : setting.rootId;
  402. const children = this.datas.filter(function (x) {
  403. return x[setting.pid] === pid;
  404. });
  405. children.sort(function (a, b) {
  406. return a.order - b.order;
  407. });
  408. return children;
  409. };
  410. /**
  411. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  412. * @param node
  413. * @returns {*}
  414. * @private
  415. */
  416. _recursiveGetPosterity(node) {
  417. let posterity = node.children;
  418. for (const c of node.children) {
  419. posterity = posterity.concat(this._recursiveGetPosterity(c));
  420. }
  421. return posterity;
  422. };
  423. /**
  424. * 查询node的已下载的全部后代
  425. * @param {Object} node
  426. * @returns {Array}
  427. */
  428. getPosterity(node) {
  429. if (node.full_path !== '') {
  430. const reg = new RegExp('^' + node.full_path + '-');
  431. return this.datas.filter(function (x) {
  432. return reg.test(x.full_path);
  433. });
  434. } else {
  435. return this._recursiveGetPosterity(node);
  436. }
  437. };
  438. /**
  439. * 查询node是否是父节点的最后一个子节点
  440. * @param {Object} node
  441. * @returns {boolean}
  442. */
  443. isLastSibling(node) {
  444. const siblings = this.getChildren(this.getParent(node));
  445. return (siblings && siblings.length > 0) ? node.order === siblings[siblings.length - 1].order : false;
  446. };
  447. /**
  448. * 刷新子节点是否可见
  449. * @param {Object} node
  450. * @private
  451. */
  452. _refreshChildrenVisible(node) {
  453. if (!node.children) {
  454. node.children = this.getChildren(node);
  455. }
  456. if (node.children && node.children.length > 0) {
  457. for (const child of node.children) {
  458. child.visible = node.expanded && node.visible;
  459. this._refreshChildrenVisible(child);
  460. }
  461. }
  462. };
  463. /**
  464. * 设置节点是否展开, 并控制子节点可见
  465. * @param {Object} node
  466. * @param {Boolean} expanded
  467. */
  468. setExpanded(node, expanded) {
  469. node.expanded = expanded;
  470. this._refreshChildrenVisible(node);
  471. };
  472. /**
  473. * 提取节点key和索引数据
  474. * @param {Object} node - 节点
  475. * @returns {key}
  476. */
  477. getNodeKeyData(node) {
  478. const data = {};
  479. for (const key of this.setting.keys) {
  480. data[key] = node[key];
  481. }
  482. return data;
  483. };
  484. /**
  485. * 得到树结构构成id
  486. * @param node
  487. * @returns {*}
  488. */
  489. getNodeKey(node) {
  490. return node[this.setting.id];
  491. };
  492. /**
  493. * 递归 设置节点展开状态
  494. * @param {Array} nodes - 需要设置状态的节点
  495. * @param {Object} parent - nodes的父节点
  496. * @param {Function} checkFun - 判断节点展开状态的方法
  497. * @private
  498. */
  499. _recursiveExpand(nodes, parent, checkFun) {
  500. for (const node of nodes) {
  501. node.expanded = checkFun(node);
  502. node.visible = parent ? (parent.expanded && parent.visible) : true;
  503. this._recursiveExpand(node.children, node, checkFun);
  504. }
  505. }
  506. /**
  507. * 自定义展开规则
  508. * @param checkFun
  509. */
  510. expandByCustom(checkFun) {
  511. this._recursiveExpand(this.children, null, checkFun);
  512. }
  513. /**
  514. * 展开到第几层
  515. * @param {Number} level - 展开层数
  516. */
  517. expandByLevel(level) {
  518. this.expandByCustom(function (n) {
  519. return n.level < level;
  520. });
  521. }
  522. /**
  523. * 自动展开节点node
  524. * @param node
  525. * @returns {*}
  526. */
  527. autoExpandNode(node) {
  528. const parents = this.getAllParents(node);
  529. const reload = [];
  530. for (const p of parents) {
  531. if (!p.expanded) {
  532. reload.push(p);
  533. this.setExpanded(p, true);
  534. }
  535. }
  536. return reload;
  537. }
  538. /**
  539. * 加载数据(动态),只加载不同部分
  540. * @param {Array} datas
  541. * @return {Array} 加载到树的数据
  542. * @privateA
  543. */
  544. _updateData(datas) {
  545. datas = datas instanceof Array ? datas : [datas];
  546. let loadedData = [];
  547. for (const data of datas) {
  548. let node = this.getItems(data[this.setting.id]);
  549. if (node) {
  550. for (const prop in data) {
  551. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  552. if (prop === this.setting.pid) {
  553. loadedData.push(this.getItems(node[this.setting.pid]));
  554. loadedData.push(this.getItems(data[this.setting.pid]));
  555. }
  556. if (prop === this.setting.order) {
  557. loadedData = loadedData.concat(this.getPosterity(node));
  558. }
  559. node[prop] = data[prop];
  560. }
  561. }
  562. loadedData.push(node);
  563. }
  564. }
  565. loadedData = _.uniq(loadedData);
  566. for (const node of loadedData) {
  567. if (node) {
  568. node.children = this.getChildren(node);
  569. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  570. } else {
  571. this.children = this.getChildren(null);
  572. }
  573. }
  574. this.sortTreeNode(true);
  575. return loadedData;
  576. };
  577. /**
  578. * 加载数据(动态),只加载不同部分
  579. * @param {Array} datas
  580. * @return {Array} 加载到树的数据
  581. * @privateA
  582. */
  583. _loadData(datas) {
  584. datas = datas instanceof Array ? datas : [datas];
  585. const loadedData = [], resortData = [];
  586. for (const data of datas) {
  587. let node = this.getItems(data[this.setting.id]);
  588. if (node) {
  589. const parent = this.getItems(node[this.setting.pid]);
  590. for (const prop in data) {
  591. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  592. node[prop] = data[prop];
  593. if (parent && resortData.indexOf(parent) === -1) {
  594. resortData.push(parent);
  595. }
  596. }
  597. }
  598. loadedData.push(node);
  599. } else {
  600. const keyName = itemsPre + data[this.setting.id];
  601. const node = JSON.parse(JSON.stringify(data));
  602. this.items[keyName] = node;
  603. this.datas.push(node);
  604. node.expanded = true;
  605. node.visible = true;
  606. loadedData.push(node);
  607. if (resortData.indexOf(node) === -1) {
  608. resortData.push(node);
  609. }
  610. const parent = this.getItems(node[this.setting.pid]);
  611. if (parent && resortData.indexOf(parent) === -1) {
  612. resortData.push(parent);
  613. } else {
  614. resortData.push(this.setting.rootId);
  615. }
  616. }
  617. }
  618. for (const node of resortData) {
  619. if (node && node !== this.setting.rootId) {
  620. node.children = this.getChildren(node);
  621. } else {
  622. this.children = this.getChildren(null);
  623. }
  624. }
  625. this.sortTreeNode(true);
  626. for (const node of loadedData) {
  627. if (!node.expanded) {
  628. this.setExpanded(node, true);
  629. }
  630. }
  631. return loadedData;
  632. };
  633. /**
  634. * 清理数据(动态)
  635. * @param datas
  636. * @private
  637. */
  638. _freeData(datas) {
  639. datas = datas instanceof Array ? datas : [datas];
  640. const freeDatas = [];
  641. const removeArrayData = function (array, data) {
  642. const index = array.indexOf(data);
  643. array.splice(index, 1);
  644. };
  645. for (const data of datas) {
  646. const node = this.getItems(data[this.setting.id]);
  647. if (node) {
  648. freeDatas.push(node);
  649. node.deleteIndex = this.nodes.indexOf(node);
  650. delete this.items[itemsPre + node[this.setting.id]];
  651. if (node[this.setting.pid] !== this.setting.rootId) {
  652. const parent = this.getItems(node[this.setting.pid]);
  653. if (parent) {
  654. removeArrayData(parent.children, node);
  655. }
  656. } else {
  657. removeArrayData(this.children, node);
  658. }
  659. removeArrayData(this.datas, node);
  660. }
  661. }
  662. for (const node of freeDatas) {
  663. removeArrayData(this.nodes, node);
  664. }
  665. return freeDatas;
  666. };
  667. /**
  668. * 加载需展开的数据
  669. * @param {Array} datas
  670. * @returns {Array}
  671. * @private
  672. */
  673. _loadExpandData(datas) {
  674. datas = datas instanceof Array ? datas : [datas];
  675. const loadedData = [], existData = [], expandData = [], resortData = [];
  676. for (const data of datas) {
  677. let node = this.getItems(data[this.setting.id]);
  678. if (node) {
  679. existData.push(node);
  680. } else {
  681. const keyName = itemsPre + data[this.setting.id];
  682. const node = JSON.parse(JSON.stringify(data));
  683. this.items[keyName] = node;
  684. this.datas.push(node);
  685. node.expanded = false;
  686. node.visible = true;
  687. loadedData.push(node);
  688. if (resortData.indexOf(node) === -1) {
  689. resortData.push(node);
  690. }
  691. const parent = this.getItems(node[this.setting.pid]);
  692. if (parent && resortData.indexOf(parent) === -1) {
  693. resortData.push(parent);
  694. }
  695. }
  696. }
  697. for (const node of resortData) {
  698. node.children = this.getChildren(node);
  699. }
  700. this.sortTreeNode(true);
  701. for (const node of loadedData) {
  702. if (!node.expanded) {
  703. this.setExpanded(node, true);
  704. }
  705. }
  706. for (const node of existData) {
  707. const parent = this.getItems(node[this.setting.pid]);
  708. if (expandData.indexOf(parent) === -1) {
  709. expandData.push(parent);
  710. if (!parent.expanded) {
  711. this.setExpanded(parent, true);
  712. }
  713. }
  714. if (!node.expanded) {
  715. this.setExpanded(node, true);
  716. }
  717. }
  718. return [loadedData, expandData];
  719. };
  720. /**
  721. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  722. *
  723. * @param data - 更新的数据 {update, create, delete}
  724. * @returns {{}}
  725. */
  726. loadPostData(data) {
  727. const result = {};
  728. if (data.delete) {
  729. result.delete = this._freeData(data.delete);
  730. }
  731. if (data.create) {
  732. result.create = this._loadData(data.create);
  733. }
  734. if (data.update) {
  735. result.update = this._updateData(data.update);
  736. }
  737. return result;
  738. }
  739. /**
  740. * 加载子节点
  741. * @param {Object} node
  742. * @param {function} callback
  743. */
  744. loadChildren(node, callback) {
  745. if (this.setting.url !== '') {
  746. const self = this;
  747. postData(this.setting.url, { postType: 'load-child', id: this.getNodeKeyData(node) }, function (data) {
  748. self._loadData(data);
  749. callback();
  750. });
  751. }
  752. };
  753. }
  754. class MeasureTree extends BaseTree {
  755. addData(datas) {
  756. const loadedData = [];
  757. for (const data of datas) {
  758. let node = this.getItems(data[this.setting.id]);
  759. if (node) {
  760. for (const prop in data) {
  761. if (data[prop] !== undefined) {
  762. node[prop] = data[prop];
  763. }
  764. }
  765. loadedData.push(node);
  766. } else {
  767. const keyName = itemsPre + data[this.setting.id];
  768. const node = JSON.parse(JSON.stringify(data));
  769. this.items[keyName] = node;
  770. this.datas.push(node);
  771. node.expanded = false;
  772. node.visible = true;
  773. loadedData.push(node);
  774. }
  775. }
  776. this.sortTreeNode();
  777. for (const node of loadedData) {
  778. const children = node.children;
  779. if (!node.expanded && children.length > 0) {
  780. node.expanded = true;
  781. this._refreshChildrenVisible(node);
  782. }
  783. }
  784. return loadedData;
  785. }
  786. removeData(datas) {
  787. datas.sort(function (a, b) {
  788. return b.level - a.level;
  789. });
  790. const removeArrayData = function (array, data) {
  791. const index = array.indexOf(data);
  792. array.splice(index, 1);
  793. };
  794. for (const data of datas) {
  795. const node = this.getItems(data[this.setting.id]);
  796. if (node && this.getChildren(node).length === 0) {
  797. delete this.items[itemsPre + node[this.setting.id]];
  798. if (node[this.setting.pid] !== this.setting.rootId) {
  799. const parent = this.items[itemsPre + node[this.setting.pid]];
  800. removeArrayData(parent.children, node);
  801. }
  802. removeArrayData(this.datas, node);
  803. removeArrayData(this.nodes, node);
  804. }
  805. }
  806. };
  807. loadLeafData(data) {
  808. const datas = data instanceof Array ? data : [data];
  809. for (const d of datas) {
  810. let node = this.getItems(d[this.setting.id]);
  811. if (node && node.is_leaf) {
  812. for (const prop in d) {
  813. if (data[prop] !== undefined) {
  814. node[prop] = d[prop];
  815. }
  816. }
  817. }
  818. }
  819. };
  820. }
  821. class FxTree extends BaseTree {
  822. /**
  823. * 检查节点是否是最底层项目节
  824. * @param node
  825. * @returns {boolean}
  826. */
  827. isLeafXmj(node) {
  828. if (node.b_code && node.b_code !== '') {
  829. return false;
  830. }
  831. for (const child of node.children) {
  832. if (!child.b_code || child.b_code === '') {
  833. return false;
  834. }
  835. }
  836. return true;
  837. }
  838. /**
  839. * 查询最底层项目节(本身或父项)
  840. * @param {Object} node - 查询节点
  841. * @returns {Object}
  842. */
  843. getLeafXmjParent(node) {
  844. let parent = node;
  845. while (parent) {
  846. if (this.isLeafXmj(parent)) {
  847. return parent;
  848. } else {
  849. parent = this.getParent(parent);
  850. }
  851. }
  852. return null;
  853. }
  854. /**
  855. * 展开至最底层项目节
  856. */
  857. expandToLeafXmj() {
  858. const self = this;
  859. this.expandByCustom(function (node) {
  860. if (node.b_code && node.b_code !== '') {
  861. return false;
  862. } else {
  863. return !self.isLeafXmj(node);
  864. }
  865. })
  866. }
  867. /**
  868. * 展开至计算项
  869. */
  870. expandByCalcFields() {
  871. const self = this;
  872. this.expandByCustom(function (node) {
  873. for (const field of self.setting.calcFields) {
  874. if (node[field]) {
  875. return true;
  876. }
  877. }
  878. return false;
  879. })
  880. }
  881. }
  882. class LedgerTree extends FxTree {
  883. /**
  884. *
  885. * @param parent
  886. * @param node
  887. * @private
  888. */
  889. _getNodesParents(parents, nodes) {
  890. for (const node of nodes) {
  891. const parent = this.getParent(node);
  892. if (parent) {
  893. const paths = this.getFullPathNodes(parent.full_path);
  894. for (const p of paths) {
  895. if (parents.indexOf(p) === -1) {
  896. parents.push(p);
  897. }
  898. }
  899. }
  900. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  901. parents.push(node);
  902. }
  903. }
  904. }
  905. _getReCalcNodes(reCalcNodes, nodes) {
  906. for (const node of nodes) {
  907. const parent = this.getParent(node);
  908. if (parent) {
  909. const paths = this.getFullPathNodes(parent.full_path);
  910. for (const p of paths) {
  911. if (reCalcNodes.indexOf(p) === -1) {
  912. reCalcNodes.push(p);
  913. }
  914. }
  915. }
  916. // 最底层项目节,也需要计算
  917. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  918. reCalcNodes.push(node);
  919. //}
  920. }
  921. }
  922. /**
  923. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  924. *
  925. * @param data - 更新的数据 {update, create, delete}
  926. * @returns {{}}
  927. */
  928. loadPostData(data) {
  929. const result = {}, reCalcNodes = [];
  930. if (!data) return result;
  931. if (data.delete) {
  932. result.delete = this._freeData(data.delete);
  933. this._getReCalcNodes(reCalcNodes, result.delete);
  934. }
  935. if (data.create) {
  936. result.create = this._loadData(data.create);
  937. this._getReCalcNodes(reCalcNodes, result.create);
  938. }
  939. if (data.update) {
  940. result.update = this._updateData(data.update);
  941. this._getReCalcNodes(reCalcNodes, result.update);
  942. }
  943. reCalcNodes.sort((a, b) => {
  944. return b.level - a.level;
  945. });
  946. for (const node of reCalcNodes) {
  947. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  948. }
  949. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  950. return result;
  951. }
  952. }
  953. class ReviseTree extends LedgerTree {
  954. checkNodeUsed(node, pos) {
  955. if (node.children && node.children.length > 0) {
  956. for (const child of node.children) {
  957. const used = this.checkNodeUsed(child, pos);
  958. if (used) return used;
  959. }
  960. } else {
  961. if (node.used) return node.used;
  962. const posRange = pos.getLedgerPos(node.id);
  963. if (posRange && posRange.length > 0) {
  964. for (const p of posRange) {
  965. if (p.used) return p.used;
  966. }
  967. }
  968. }
  969. return false;
  970. }
  971. }
  972. class StageTree extends FxTree {
  973. /**
  974. * 构造函数
  975. */
  976. constructor(setting) {
  977. super(setting);
  978. // stage关联索引
  979. this.stageItems = {};
  980. }
  981. /**
  982. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  983. * @param datas
  984. */
  985. loadDatas(datas) {
  986. super.loadDatas(datas);
  987. // 清空旧数据
  988. this.stageItems = {};
  989. // 加载全部数据
  990. for (const data of this.datas) {
  991. const keyName = itemsPre + data[this.setting.stageId];
  992. this.stageItems[keyName] = data;
  993. }
  994. }
  995. getStageItems(id) {
  996. return this.stageItems[itemsPre + id];
  997. }
  998. loadStageData(datas, fieldPre, fields) {
  999. datas = datas instanceof Array ? datas : [datas];
  1000. const loadedData = [];
  1001. for (const data of datas) {
  1002. let node = this.getStageItems(data.lid);
  1003. if (node) {
  1004. for (const prop of fields) {
  1005. if (data[prop] !== undefined) {
  1006. node[fieldPre + prop] = data[prop];
  1007. }
  1008. }
  1009. loadedData.push(node);
  1010. }
  1011. }
  1012. }
  1013. loadPreStageData(datas) {
  1014. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1015. }
  1016. loadCurStageData(datas) {
  1017. this.loadStageData(datas, '', this.setting.updateFields);
  1018. }
  1019. /**
  1020. * 加载数据(动态),只加载不同部分
  1021. * @param {Array} datas
  1022. * @return {Array} 加载到树的数据
  1023. * @privateA
  1024. */
  1025. _updateData(datas) {
  1026. datas = datas instanceof Array ? datas : [datas];
  1027. let loadedData = [];
  1028. for (const data of datas) {
  1029. let node = this.getItems(data[this.setting.id]);
  1030. if (node) {
  1031. for (const prop in data) {
  1032. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1033. }
  1034. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1035. if (prop === this.setting.pid) {
  1036. loadedData.push(this.getItems(node[this.setting.pid]));
  1037. loadedData.push(this.getItems(data[this.setting.pid]));
  1038. }
  1039. node[prop] = data[prop];
  1040. }
  1041. }
  1042. loadedData.push(node);
  1043. }
  1044. }
  1045. loadedData = _.uniq(loadedData);
  1046. for (const node of loadedData) {
  1047. node.children = this.getChildren(node);
  1048. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1049. }
  1050. this.sortTreeNode(true);
  1051. return loadedData;
  1052. };
  1053. /**
  1054. * 加载数据(动态),只加载不同部分
  1055. * @param {Array} datas
  1056. * @return {Array} 加载到树的数据
  1057. * @privateA
  1058. */
  1059. _updateStageData(datas) {
  1060. datas = datas instanceof Array ? datas : [datas];
  1061. const loadedData = [];
  1062. for (const data of datas) {
  1063. let node = this.getStageItems(data.lid);
  1064. if (node) {
  1065. for (const prop of this.setting.updateFields) {
  1066. if (data[prop] !== undefined) {
  1067. node[prop] = data[prop];
  1068. }
  1069. }
  1070. loadedData.push(node);
  1071. }
  1072. }
  1073. return loadedData;
  1074. };
  1075. /**
  1076. *
  1077. * @param parent
  1078. * @param node
  1079. * @private
  1080. */
  1081. _getNodesParents(parents, nodes) {
  1082. for (const node of nodes) {
  1083. const parent = this.getParent(node);
  1084. if (parent) {
  1085. const paths = this.getFullPathNodes(parent.full_path);
  1086. for (const p of paths) {
  1087. if (parents.indexOf(p) === -1) {
  1088. parents.push(p);
  1089. }
  1090. }
  1091. }
  1092. if (node.children && node.children.length > 0) {
  1093. parents.push(node);
  1094. }
  1095. }
  1096. }
  1097. _updateDgnData(datas) {
  1098. datas = datas instanceof Array ? datas : [datas];
  1099. let loadedData = [];
  1100. for (const data of datas) {
  1101. let node = this.getStageItems(data.id);
  1102. if (node) {
  1103. for (const prop in data) {
  1104. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1105. node[prop] = data[prop];
  1106. }
  1107. }
  1108. loadedData.push(node);
  1109. }
  1110. }
  1111. return loadedData;
  1112. }
  1113. /**
  1114. * 提交数据至后端,返回的前端树结构应刷新的部分
  1115. * StageTree仅有更新CurStage部分,不需要增删
  1116. *
  1117. * @param data - 需要更新的数据
  1118. * @returns {Array} - 界面需要刷新的数据
  1119. */
  1120. loadPostStageData(data) {
  1121. let result, parents = [];
  1122. if (data.bills) {
  1123. result = this._updateData(data.bills);
  1124. this._getNodesParents(parents, result);
  1125. }
  1126. if (data.curStageData) {
  1127. result = this._updateStageData(data.curStageData);
  1128. this._getNodesParents(parents, result);
  1129. }
  1130. if (data.dgn) {
  1131. const dgnResult = this._updateDgnData(data.dgn);
  1132. result = result ? result.concat(dgnResult) : dgnResult;
  1133. }
  1134. result = result ? result.concat(parents) : parents;
  1135. result.sort((a, b) => {
  1136. return b.level - a.level;
  1137. });
  1138. for (const node of result) {
  1139. treeCalc.calculateNode(this, node);
  1140. }
  1141. return result;
  1142. }
  1143. }
  1144. class MasterTree extends FxTree {
  1145. /**
  1146. * 构造函数
  1147. */
  1148. constructor(setting) {
  1149. super(setting);
  1150. // 关联索引
  1151. this.masterItems = {};
  1152. }
  1153. /**
  1154. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1155. * @param datas
  1156. */
  1157. loadDatas(datas) {
  1158. super.loadDatas(datas);
  1159. // 清空旧数据
  1160. this.masterItems = {};
  1161. // minor数据缓存
  1162. this.minorData = {};
  1163. // 加载全部数据
  1164. for (const data of this.datas) {
  1165. const keyName = itemsPre + data[this.setting.masterId];
  1166. this.masterItems[keyName] = data;
  1167. }
  1168. }
  1169. /**
  1170. * 根据关联id,查找节点
  1171. * @param id
  1172. * @returns {*}
  1173. */
  1174. getMasterItems(id) {
  1175. return this.masterItems[itemsPre + id];
  1176. }
  1177. /**
  1178. * 加载关联数据
  1179. *
  1180. * @param {Array|Object}datas - 需要关联的数据
  1181. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1182. * @param {Array} fields - 关联字段
  1183. * @returns {Array}
  1184. */
  1185. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1186. for (const cf of calcFields) {
  1187. this.setting.calcFields.push(cf + fieldSuf);
  1188. }
  1189. if (!datas) return;
  1190. datas = datas instanceof Array ? datas : [datas];
  1191. this.minorData[fieldSuf] = datas;
  1192. const loadedData = [];
  1193. for (const data of datas) {
  1194. let node = this.getMasterItems(data[this.setting.minorId]);
  1195. if (node) {
  1196. for (const prop of fields) {
  1197. if (data[prop] !== undefined) {
  1198. node[prop + fieldSuf] = data[prop];
  1199. }
  1200. }
  1201. loadedData.push(node);
  1202. }
  1203. }
  1204. return loadedData;
  1205. }
  1206. }
  1207. class FilterTree extends BaseTree {
  1208. addData(data, fields) {
  1209. const item = {};
  1210. for (const prop in data) {
  1211. if (fields.indexOf(prop) >= 0) {
  1212. item[prop] = data[prop];
  1213. }
  1214. }
  1215. const keyName = itemsPre + item[this.setting.id];
  1216. if (!this.items[keyName]) {
  1217. item.children = [];
  1218. item.is_leaf = true;
  1219. item.expanded = true;
  1220. item.visible = true;
  1221. this.items[keyName] = item;
  1222. this.datas.push(item);
  1223. if (item[setting.pid] === setting.rootId) {
  1224. this.children.push(item);
  1225. } else {
  1226. const parent = this.getParent(item);
  1227. if (parent) {
  1228. parent.is_leaf = false;
  1229. parent.children.push(item);
  1230. }
  1231. }
  1232. } else {
  1233. return this.items[keyName];
  1234. }
  1235. return item;
  1236. }
  1237. }
  1238. class GatherTree extends BaseTree {
  1239. clearDatas() {
  1240. this.items = {};
  1241. this.nodes = [];
  1242. this.datas = [];
  1243. this.children = [];
  1244. }
  1245. get newId() {
  1246. if (!this._maxId) {
  1247. this._maxId = 0;
  1248. }
  1249. this._maxId++;
  1250. return this._maxId;
  1251. }
  1252. addNode(data, parent) {
  1253. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1254. let item = _.find(this.items, data);
  1255. if (item) return item;
  1256. item = data;
  1257. item[this.setting.id] = this.newId;
  1258. const keyName = itemsPre + item[this.setting.id];
  1259. item.children = [];
  1260. item.is_leaf = true;
  1261. item.expanded = true;
  1262. item.visible = true;
  1263. this.items[keyName] = item;
  1264. this.datas.push(item);
  1265. if (parent) {
  1266. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1267. item[this.setting.level] = parent[this.setting.level] + 1;
  1268. item[this.setting.order] = parent.children.length + 1;
  1269. parent.is_leaf = false;
  1270. parent.children.push(item);
  1271. } else {
  1272. item[this.setting.fullPath] = '' + item[this.setting.id];
  1273. item[this.setting.level] = 1;
  1274. item[this.setting.order] = this.children.length + 1;
  1275. this.children.push(item);
  1276. }
  1277. return item;
  1278. }
  1279. sortTreeNodeCustom(field, fun, isResort) {
  1280. const self = this;
  1281. const sortNodes = function (nodes) {
  1282. nodes.sort(function (a, b) {
  1283. return fun(a[field], b[field]);
  1284. });
  1285. for (const [i, node] of nodes.entries()) {
  1286. node.order = i + 1;
  1287. }
  1288. };
  1289. const addSortNodes = function (nodes) {
  1290. if (!nodes) { return }
  1291. for (let i = 0; i < nodes.length; i++) {
  1292. self.nodes.push(nodes[i]);
  1293. nodes[i].index = self.nodes.length - 1;
  1294. if (!isResort) {
  1295. nodes[i].children = self.getChildren(nodes[i]);
  1296. }
  1297. sortNodes(nodes[i].children);
  1298. addSortNodes(nodes[i].children);
  1299. }
  1300. };
  1301. this.nodes = [];
  1302. if (!isResort) {
  1303. this.children = this.getChildren();
  1304. }
  1305. sortNodes(this.children);
  1306. addSortNodes(this.children);
  1307. }
  1308. }
  1309. class CompareTree extends FxTree {
  1310. constructor(setting) {
  1311. super(setting);
  1312. this._newId = 1;
  1313. }
  1314. get newId() {
  1315. return this._newId++;
  1316. }
  1317. loadCompareNode(node, parent, loadFun) {
  1318. const siblings = parent ? parent.children : this.children;
  1319. let cur = siblings.find(function (x) {
  1320. return node.b_code
  1321. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1322. : x.code === node.code && x.name === node.name;
  1323. });
  1324. if (!cur) {
  1325. const id = this.newId;
  1326. cur = {
  1327. id: id,
  1328. pid: parent ? parent.id : this.setting.rootId,
  1329. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1330. level: parent ? parent.level + 1 : 1,
  1331. order: siblings.length + 1,
  1332. children: [],
  1333. code: node.code, b_code: node.b_code, name: node.name,
  1334. unit: node.unit, unit_price: node.unit_price,
  1335. };
  1336. siblings.push(cur);
  1337. this.datas.push(cur);
  1338. }
  1339. loadFun(cur, node);
  1340. for (const c of node.children) {
  1341. this.loadCompareNode(c, cur, loadFun);
  1342. }
  1343. }
  1344. generateSortNodes() {
  1345. const self = this;
  1346. const addSortNode = function (node) {
  1347. self.nodes.push(node);
  1348. for (const c of node.children) {
  1349. addSortNode(c);
  1350. }
  1351. }
  1352. this.nodes = [];
  1353. for (const n of this.children) {
  1354. addSortNode(n);
  1355. }
  1356. }
  1357. loadCompareTree(data, loadFun) {
  1358. for (const c of data.billsTree.children) {
  1359. this.loadCompareNode(c, null, loadFun);
  1360. }
  1361. // todo load Pos Data;
  1362. }
  1363. calculateDiffer() {
  1364. if (this.setting.calcDiffer) {
  1365. for (const d of this.datas) {
  1366. this.setting.calcDiffer(d);
  1367. }
  1368. }
  1369. }
  1370. loadCompareData(data1, data2) {
  1371. this.loadCompareTree(data1, this.setting.loadInfo1);
  1372. this.loadCompareTree(data2, this.setting.loadInfo2);
  1373. for (const d of this.datas) {
  1374. d.is_leaf = d.children.length === 0;
  1375. d.expanded = true;
  1376. d.visible = true;
  1377. this.items[itemsPre + d[this.setting.id]] = d;
  1378. }
  1379. this.generateSortNodes();
  1380. this.calculateDiffer();
  1381. }
  1382. }
  1383. class TreeGatherTree extends FxTree {
  1384. constructor(setting) {
  1385. super(setting);
  1386. this._newId = 1;
  1387. }
  1388. get newId() {
  1389. return this._newId++;
  1390. }
  1391. loadGatherNode(node, parent, index, loadFun) {
  1392. const siblings = parent ? parent.children : this.children;
  1393. let cur = siblings.find(function (x) {
  1394. return node.b_code
  1395. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1396. : x.code === node.code && x.name === node.name;
  1397. });
  1398. if (!cur) {
  1399. const id = this.newId;
  1400. cur = {
  1401. id: id,
  1402. pid: parent ? parent.id : this.setting.rootId,
  1403. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1404. level: parent ? parent.level + 1 : 1,
  1405. order: siblings.length + 1,
  1406. children: [],
  1407. code: node.code, b_code: node.b_code, name: node.name,
  1408. unit: node.unit, unit_price: node.unit_price,
  1409. };
  1410. siblings.push(cur);
  1411. this.datas.push(cur);
  1412. }
  1413. loadFun(cur, node, index);
  1414. for (const c of node.children) {
  1415. this.loadGatherNode(c, cur, index, loadFun);
  1416. }
  1417. }
  1418. generateSortNodes() {
  1419. const self = this;
  1420. const addSortNode = function (node) {
  1421. self.nodes.push(node);
  1422. for (const c of node.children) {
  1423. addSortNode(c);
  1424. }
  1425. }
  1426. this.nodes = [];
  1427. for (const n of this.children) {
  1428. addSortNode(n);
  1429. }
  1430. }
  1431. loadGatherTree(data, index, loadFun) {
  1432. for (const c of data.billsTree.children) {
  1433. this.loadGatherNode(c, null, index, loadFun);
  1434. }
  1435. // todo load Pos Data;
  1436. }
  1437. calculateSum() {
  1438. if (this.setting.calcSum) {
  1439. for (const d of this.datas) {
  1440. this.setting.calcSum(d, this.count);
  1441. }
  1442. }
  1443. }
  1444. loadGatherData(datas) {
  1445. this.count = datas.length;
  1446. for (const [i, data] of datas.entries()) {
  1447. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1448. }
  1449. for (const d of this.datas) {
  1450. d.is_leaf = d.children.length === 0;
  1451. d.expanded = true;
  1452. d.visible = true;
  1453. this.items[itemsPre + d[this.setting.id]] = d;
  1454. }
  1455. this.generateSortNodes();
  1456. this.calculateSum();
  1457. }
  1458. }
  1459. if (type === 'base') {
  1460. return new BaseTree(setting);
  1461. } else if (type === 'fx') {
  1462. return new FxTree(setting);
  1463. } else if (type === 'stage') {
  1464. return new StageTree(setting);
  1465. } else if (type === 'ledger') {
  1466. return new LedgerTree(setting);
  1467. } else if (type === 'revise') {
  1468. return new ReviseTree(setting);
  1469. } else if (type === 'measure') {
  1470. return new MeasureTree(setting);
  1471. } else if (type === 'master') {
  1472. return new MasterTree(setting);
  1473. } else if (type === 'filter') {
  1474. return new FilterTree(setting);
  1475. } else if (type === 'gather') {
  1476. return new GatherTree(setting);
  1477. } else if (type === 'compare') {
  1478. return new CompareTree(setting);
  1479. } else if (type === 'tree-gather') {
  1480. return new TreeGatherTree(setting);
  1481. }
  1482. };
  1483. const treeCalc = {
  1484. mapTreeNode: function (tree) {
  1485. let map = {}, maxLevel = 0;
  1486. for (const node of tree.nodes) {
  1487. let levelArr = map[node.level];
  1488. if (!levelArr) {
  1489. levelArr = [];
  1490. map[node.level] = levelArr;
  1491. }
  1492. if (node.level > maxLevel) {
  1493. maxLevel = node.level;
  1494. }
  1495. levelArr.push(node);
  1496. }
  1497. return [maxLevel, map];
  1498. },
  1499. getMaxLevel: function (tree) {
  1500. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1501. },
  1502. calculateNode: function (tree, node) {
  1503. if (node.children && node.children.length > 0) {
  1504. const gather = node.children.reduce(function (rst, x) {
  1505. const result = {};
  1506. for (const cf of tree.setting.calcFields) {
  1507. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1508. }
  1509. return result;
  1510. });
  1511. // 汇总子项
  1512. for (const cf of tree.setting.calcFields) {
  1513. if (gather[cf]) {
  1514. node[cf] = gather[cf];
  1515. } else {
  1516. node[cf] = null;
  1517. }
  1518. }
  1519. }
  1520. // 自身运算
  1521. if (tree.setting.calcFun) {
  1522. tree.setting.calcFun(node);
  1523. }
  1524. },
  1525. calculateLevelNode: function (tree, level) {
  1526. const nodes = tree.datas.filter((n) => { return n.level === level });
  1527. for (const node of nodes) {
  1528. this.calculateNode(tree, node);
  1529. }
  1530. },
  1531. calculateAll: function (tree) {
  1532. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1533. for (let i = maxLevel; i >= 0; i--) {
  1534. const levelNodes = levelMap[i];
  1535. if (levelNodes && levelNodes.length > 0) {
  1536. for (const node of levelNodes) {
  1537. this.calculateNode(tree, node);
  1538. }
  1539. }
  1540. }
  1541. },
  1542. calculateParent: function (tree, node) {
  1543. const nodes = tree.getFullPathNodes(node.full_path);
  1544. nodes.sort((a, b) => {
  1545. return b.level - a.level;
  1546. });
  1547. for (const n of nodes) {
  1548. this.calculateNode(tree, n);
  1549. }
  1550. return nodes;
  1551. }
  1552. };