path_tree.js 54 KB

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