path_tree.js 57 KB

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