path_tree.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. }
  773. class MeasureTree extends BaseTree {
  774. addData(datas) {
  775. const loadedData = [];
  776. for (const data of datas) {
  777. let node = this.getItems(data[this.setting.id]);
  778. if (node) {
  779. for (const prop in data) {
  780. if (data[prop] !== undefined) {
  781. node[prop] = data[prop];
  782. }
  783. }
  784. loadedData.push(node);
  785. } else {
  786. const keyName = itemsPre + data[this.setting.id];
  787. const node = JSON.parse(JSON.stringify(data));
  788. this.items[keyName] = node;
  789. this.datas.push(node);
  790. node.expanded = false;
  791. node.visible = true;
  792. loadedData.push(node);
  793. }
  794. }
  795. this.sortTreeNode();
  796. for (const node of loadedData) {
  797. const children = node.children;
  798. if (!node.expanded && children.length > 0) {
  799. node.expanded = true;
  800. this._refreshChildrenVisible(node);
  801. }
  802. }
  803. return loadedData;
  804. }
  805. removeData(datas) {
  806. datas.sort(function (a, b) {
  807. return b.level - a.level;
  808. });
  809. const removeArrayData = function (array, data) {
  810. const index = array.indexOf(data);
  811. array.splice(index, 1);
  812. };
  813. for (const data of datas) {
  814. const node = this.getItems(data[this.setting.id]);
  815. if (node && this.getChildren(node).length === 0) {
  816. delete this.items[itemsPre + node[this.setting.id]];
  817. if (node[this.setting.pid] !== this.setting.rootId) {
  818. const parent = this.items[itemsPre + node[this.setting.pid]];
  819. removeArrayData(parent.children, node);
  820. }
  821. removeArrayData(this.datas, node);
  822. removeArrayData(this.nodes, node);
  823. }
  824. }
  825. };
  826. loadLeafData(data) {
  827. const datas = data instanceof Array ? data : [data];
  828. for (const d of datas) {
  829. let node = this.getItems(d[this.setting.id]);
  830. if (node && node.is_leaf) {
  831. for (const prop in d) {
  832. if (data[prop] !== undefined) {
  833. node[prop] = d[prop];
  834. }
  835. }
  836. }
  837. }
  838. };
  839. }
  840. class FxTree extends BaseTree {
  841. /**
  842. * 检查节点是否是最底层项目节
  843. * @param node
  844. * @returns {boolean}
  845. */
  846. isLeafXmj(node) {
  847. if (node.b_code && node.b_code !== '') {
  848. return false;
  849. }
  850. for (const child of node.children) {
  851. if (!child.b_code || child.b_code === '') {
  852. return false;
  853. }
  854. }
  855. return true;
  856. }
  857. /**
  858. * 查询最底层项目节(本身或父项)
  859. * @param {Object} node - 查询节点
  860. * @returns {Object}
  861. */
  862. getLeafXmjParent(node) {
  863. let parent = node;
  864. while (parent) {
  865. if (this.isLeafXmj(parent)) {
  866. return parent;
  867. } else {
  868. parent = this.getParent(parent);
  869. }
  870. }
  871. return null;
  872. }
  873. /**
  874. * 展开至最底层项目节
  875. */
  876. expandToLeafXmj() {
  877. const self = this;
  878. this.expandByCustom(function (node) {
  879. if (node.b_code && node.b_code !== '') {
  880. return false;
  881. } else {
  882. return !self.isLeafXmj(node);
  883. }
  884. })
  885. }
  886. /**
  887. * 展开至计算项
  888. */
  889. expandByCalcFields() {
  890. const self = this;
  891. this.expandByCustom(function (node) {
  892. for (const field of self.setting.calcFields) {
  893. if (node[field]) {
  894. return true;
  895. }
  896. }
  897. return false;
  898. })
  899. }
  900. }
  901. class LedgerTree extends FxTree {
  902. /**
  903. *
  904. * @param parent
  905. * @param node
  906. * @private
  907. */
  908. _getNodesParents(parents, nodes) {
  909. for (const node of nodes) {
  910. const parent = this.getParent(node);
  911. if (parent) {
  912. const paths = this.getFullPathNodes(parent.full_path);
  913. for (const p of paths) {
  914. if (parents.indexOf(p) === -1) {
  915. parents.push(p);
  916. }
  917. }
  918. }
  919. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  920. parents.push(node);
  921. }
  922. }
  923. }
  924. _getReCalcNodes(reCalcNodes, nodes) {
  925. for (const node of nodes) {
  926. const parent = this.getParent(node);
  927. if (parent) {
  928. const paths = this.getFullPathNodes(parent.full_path);
  929. for (const p of paths) {
  930. if (reCalcNodes.indexOf(p) === -1) {
  931. reCalcNodes.push(p);
  932. }
  933. }
  934. }
  935. // 最底层项目节,也需要计算
  936. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  937. reCalcNodes.push(node);
  938. //}
  939. }
  940. }
  941. /**
  942. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  943. *
  944. * @param data - 更新的数据 {update, create, delete}
  945. * @returns {{}}
  946. */
  947. loadPostData(data) {
  948. const result = {}, reCalcNodes = [];
  949. if (!data) return result;
  950. if (data.delete) {
  951. result.delete = this._freeData(data.delete);
  952. this._getReCalcNodes(reCalcNodes, result.delete);
  953. }
  954. if (data.create) {
  955. result.create = this._loadData(data.create);
  956. this._getReCalcNodes(reCalcNodes, result.create);
  957. }
  958. if (data.update) {
  959. result.update = this._updateData(data.update);
  960. this._getReCalcNodes(reCalcNodes, result.update);
  961. }
  962. reCalcNodes.sort((a, b) => {
  963. return b.level - a.level;
  964. });
  965. for (const node of reCalcNodes) {
  966. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  967. }
  968. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  969. return result;
  970. }
  971. }
  972. class ReviseTree extends LedgerTree {
  973. checkNodeUsed(node, pos) {
  974. if (node.children && node.children.length > 0) {
  975. for (const child of node.children) {
  976. const used = this.checkNodeUsed(child, pos);
  977. if (used) return used;
  978. }
  979. } else {
  980. if (node.used) return node.used;
  981. const posRange = pos.getLedgerPos(node.id);
  982. if (posRange && posRange.length > 0) {
  983. for (const p of posRange) {
  984. if (p.used) return p.used;
  985. }
  986. }
  987. }
  988. return false;
  989. }
  990. }
  991. class StageTree extends FxTree {
  992. /**
  993. * 构造函数
  994. */
  995. constructor(setting) {
  996. super(setting);
  997. // stage关联索引
  998. this.stageItems = {};
  999. }
  1000. /**
  1001. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1002. * @param datas
  1003. */
  1004. loadDatas(datas) {
  1005. super.loadDatas(datas);
  1006. // 清空旧数据
  1007. this.stageItems = {};
  1008. // 加载全部数据
  1009. for (const data of this.datas) {
  1010. const keyName = itemsPre + data[this.setting.stageId];
  1011. this.stageItems[keyName] = data;
  1012. }
  1013. }
  1014. getStageItems(id) {
  1015. return this.stageItems[itemsPre + id];
  1016. }
  1017. loadStageData(datas, fieldPre, fields) {
  1018. datas = datas instanceof Array ? datas : [datas];
  1019. const loadedData = [];
  1020. for (const data of datas) {
  1021. let node = this.getStageItems(data.lid);
  1022. if (node) {
  1023. for (const prop of fields) {
  1024. if (data[prop] !== undefined) {
  1025. node[fieldPre + prop] = data[prop];
  1026. }
  1027. }
  1028. loadedData.push(node);
  1029. }
  1030. }
  1031. }
  1032. loadPreStageData(datas) {
  1033. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1034. }
  1035. loadCurStageData(datas) {
  1036. this.loadStageData(datas, '', this.setting.updateFields);
  1037. }
  1038. /**
  1039. * 加载数据(动态),只加载不同部分
  1040. * @param {Array} datas
  1041. * @return {Array} 加载到树的数据
  1042. * @privateA
  1043. */
  1044. _updateData(datas) {
  1045. datas = datas instanceof Array ? datas : [datas];
  1046. let loadedData = [];
  1047. for (const data of datas) {
  1048. let node = this.getItems(data[this.setting.id]);
  1049. if (node) {
  1050. for (const prop in data) {
  1051. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1052. }
  1053. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1054. if (prop === this.setting.pid) {
  1055. loadedData.push(this.getItems(node[this.setting.pid]));
  1056. loadedData.push(this.getItems(data[this.setting.pid]));
  1057. }
  1058. node[prop] = data[prop];
  1059. }
  1060. }
  1061. loadedData.push(node);
  1062. }
  1063. }
  1064. loadedData = _.uniq(loadedData);
  1065. for (const node of loadedData) {
  1066. node.children = this.getChildren(node);
  1067. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1068. }
  1069. this.sortTreeNode(true);
  1070. return loadedData;
  1071. };
  1072. /**
  1073. * 加载数据(动态),只加载不同部分
  1074. * @param {Array} datas
  1075. * @return {Array} 加载到树的数据
  1076. * @privateA
  1077. */
  1078. _updateStageData(datas) {
  1079. datas = datas instanceof Array ? datas : [datas];
  1080. const loadedData = [];
  1081. for (const data of datas) {
  1082. let node = this.getStageItems(data.lid);
  1083. if (node) {
  1084. for (const prop of this.setting.updateFields) {
  1085. if (data[prop] !== undefined) {
  1086. node[prop] = data[prop];
  1087. }
  1088. }
  1089. loadedData.push(node);
  1090. }
  1091. }
  1092. return loadedData;
  1093. };
  1094. /**
  1095. *
  1096. * @param parent
  1097. * @param node
  1098. * @private
  1099. */
  1100. _getNodesParents(parents, nodes) {
  1101. for (const node of nodes) {
  1102. const parent = this.getParent(node);
  1103. if (parent) {
  1104. const paths = this.getFullPathNodes(parent.full_path);
  1105. for (const p of paths) {
  1106. if (parents.indexOf(p) === -1) {
  1107. parents.push(p);
  1108. }
  1109. }
  1110. }
  1111. if (node.children && node.children.length > 0) {
  1112. parents.push(node);
  1113. }
  1114. }
  1115. }
  1116. _updateDgnData(datas) {
  1117. datas = datas instanceof Array ? datas : [datas];
  1118. let loadedData = [];
  1119. for (const data of datas) {
  1120. let node = this.getStageItems(data.id);
  1121. if (node) {
  1122. for (const prop in data) {
  1123. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1124. node[prop] = data[prop];
  1125. }
  1126. }
  1127. loadedData.push(node);
  1128. }
  1129. }
  1130. return loadedData;
  1131. }
  1132. /**
  1133. * 提交数据至后端,返回的前端树结构应刷新的部分
  1134. * StageTree仅有更新CurStage部分,不需要增删
  1135. *
  1136. * @param data - 需要更新的数据
  1137. * @returns {Array} - 界面需要刷新的数据
  1138. */
  1139. loadPostStageData(data) {
  1140. let result, parents = [];
  1141. if (data.bills) {
  1142. result = this._updateData(data.bills);
  1143. this._getNodesParents(parents, result);
  1144. }
  1145. if (data.curStageData) {
  1146. result = this._updateStageData(data.curStageData);
  1147. this._getNodesParents(parents, result);
  1148. }
  1149. if (data.dgn) {
  1150. const dgnResult = this._updateDgnData(data.dgn);
  1151. result = result ? result.concat(dgnResult) : dgnResult;
  1152. }
  1153. result = result ? result.concat(parents) : parents;
  1154. result.sort((a, b) => {
  1155. return b.level - a.level;
  1156. });
  1157. for (const node of result) {
  1158. treeCalc.calculateNode(this, node);
  1159. }
  1160. return result;
  1161. }
  1162. }
  1163. class MasterTree extends FxTree {
  1164. /**
  1165. * 构造函数
  1166. */
  1167. constructor(setting) {
  1168. super(setting);
  1169. // 关联索引
  1170. this.masterItems = {};
  1171. }
  1172. /**
  1173. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1174. * @param datas
  1175. */
  1176. loadDatas(datas) {
  1177. super.loadDatas(datas);
  1178. // 清空旧数据
  1179. this.masterItems = {};
  1180. // minor数据缓存
  1181. this.minorData = {};
  1182. // 加载全部数据
  1183. for (const data of this.datas) {
  1184. const keyName = itemsPre + data[this.setting.masterId];
  1185. this.masterItems[keyName] = data;
  1186. }
  1187. }
  1188. /**
  1189. * 根据关联id,查找节点
  1190. * @param id
  1191. * @returns {*}
  1192. */
  1193. getMasterItems(id) {
  1194. return this.masterItems[itemsPre + id];
  1195. }
  1196. /**
  1197. * 加载关联数据
  1198. *
  1199. * @param {Array|Object}datas - 需要关联的数据
  1200. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1201. * @param {Array} fields - 关联字段
  1202. * @returns {Array}
  1203. */
  1204. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1205. for (const cf of calcFields) {
  1206. this.setting.calcFields.push(cf + fieldSuf);
  1207. }
  1208. if (!datas) return;
  1209. datas = datas instanceof Array ? datas : [datas];
  1210. this.minorData[fieldSuf] = datas;
  1211. const loadedData = [];
  1212. for (const data of datas) {
  1213. let node = this.getMasterItems(data[this.setting.minorId]);
  1214. if (node) {
  1215. for (const prop of fields) {
  1216. if (data[prop] !== undefined) {
  1217. node[prop + fieldSuf] = data[prop];
  1218. }
  1219. }
  1220. loadedData.push(node);
  1221. }
  1222. }
  1223. return loadedData;
  1224. }
  1225. }
  1226. class FilterTree extends BaseTree {
  1227. addData(data, fields) {
  1228. const item = {};
  1229. for (const prop in data) {
  1230. if (fields.indexOf(prop) >= 0) {
  1231. item[prop] = data[prop];
  1232. }
  1233. }
  1234. const keyName = itemsPre + item[this.setting.id];
  1235. if (!this.items[keyName]) {
  1236. item.children = [];
  1237. item.is_leaf = true;
  1238. item.expanded = true;
  1239. item.visible = true;
  1240. this.items[keyName] = item;
  1241. this.datas.push(item);
  1242. if (item[setting.pid] === setting.rootId) {
  1243. this.children.push(item);
  1244. } else {
  1245. const parent = this.getParent(item);
  1246. if (parent) {
  1247. parent.is_leaf = false;
  1248. parent.children.push(item);
  1249. }
  1250. }
  1251. } else {
  1252. return this.items[keyName];
  1253. }
  1254. return item;
  1255. }
  1256. }
  1257. class GatherTree extends BaseTree {
  1258. clearDatas() {
  1259. this.items = {};
  1260. this.nodes = [];
  1261. this.datas = [];
  1262. this.children = [];
  1263. }
  1264. get newId() {
  1265. if (!this._maxId) {
  1266. this._maxId = 0;
  1267. }
  1268. this._maxId++;
  1269. return this._maxId;
  1270. }
  1271. addNode(data, parent) {
  1272. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1273. let item = _.find(this.items, data);
  1274. if (item) return item;
  1275. item = data;
  1276. item[this.setting.id] = this.newId;
  1277. const keyName = itemsPre + item[this.setting.id];
  1278. item.children = [];
  1279. item.is_leaf = true;
  1280. item.expanded = true;
  1281. item.visible = true;
  1282. this.items[keyName] = item;
  1283. this.datas.push(item);
  1284. if (parent) {
  1285. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1286. item[this.setting.level] = parent[this.setting.level] + 1;
  1287. item[this.setting.order] = parent.children.length + 1;
  1288. parent.is_leaf = false;
  1289. parent.children.push(item);
  1290. } else {
  1291. item[this.setting.fullPath] = '' + item[this.setting.id];
  1292. item[this.setting.level] = 1;
  1293. item[this.setting.order] = this.children.length + 1;
  1294. this.children.push(item);
  1295. }
  1296. return item;
  1297. }
  1298. sortTreeNodeCustom(field, fun, isResort) {
  1299. const self = this;
  1300. const sortNodes = function (nodes) {
  1301. nodes.sort(function (a, b) {
  1302. return fun(a[field], b[field]);
  1303. });
  1304. for (const [i, node] of nodes.entries()) {
  1305. node.order = i + 1;
  1306. }
  1307. };
  1308. const addSortNodes = function (nodes) {
  1309. if (!nodes) { return }
  1310. for (let i = 0; i < nodes.length; i++) {
  1311. self.nodes.push(nodes[i]);
  1312. nodes[i].index = self.nodes.length - 1;
  1313. if (!isResort) {
  1314. nodes[i].children = self.getChildren(nodes[i]);
  1315. }
  1316. sortNodes(nodes[i].children);
  1317. addSortNodes(nodes[i].children);
  1318. }
  1319. };
  1320. this.nodes = [];
  1321. if (!isResort) {
  1322. this.children = this.getChildren();
  1323. }
  1324. sortNodes(this.children);
  1325. addSortNodes(this.children);
  1326. }
  1327. }
  1328. class CompareTree extends FxTree {
  1329. constructor(setting) {
  1330. super(setting);
  1331. this._newId = 1;
  1332. }
  1333. get newId() {
  1334. return this._newId++;
  1335. }
  1336. loadCompareNode(node, parent, loadFun) {
  1337. const siblings = parent ? parent.children : this.children;
  1338. let cur = siblings.find(function (x) {
  1339. return node.b_code
  1340. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1341. : x.code === node.code && x.name === node.name;
  1342. });
  1343. if (!cur) {
  1344. const id = this.newId;
  1345. cur = {
  1346. id: id,
  1347. pid: parent ? parent.id : this.setting.rootId,
  1348. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1349. level: parent ? parent.level + 1 : 1,
  1350. order: siblings.length + 1,
  1351. children: [],
  1352. code: node.code, b_code: node.b_code, name: node.name,
  1353. unit: node.unit, unit_price: node.unit_price,
  1354. };
  1355. siblings.push(cur);
  1356. this.datas.push(cur);
  1357. }
  1358. loadFun(cur, node);
  1359. for (const c of node.children) {
  1360. this.loadCompareNode(c, cur, loadFun);
  1361. }
  1362. }
  1363. generateSortNodes() {
  1364. const self = this;
  1365. const addSortNode = function (node) {
  1366. self.nodes.push(node);
  1367. for (const c of node.children) {
  1368. addSortNode(c);
  1369. }
  1370. }
  1371. this.nodes = [];
  1372. for (const n of this.children) {
  1373. addSortNode(n);
  1374. }
  1375. }
  1376. loadCompareTree(data, loadFun) {
  1377. for (const c of data.billsTree.children) {
  1378. this.loadCompareNode(c, null, loadFun);
  1379. }
  1380. // todo load Pos Data;
  1381. }
  1382. calculateDiffer() {
  1383. if (this.setting.calcDiffer) {
  1384. for (const d of this.datas) {
  1385. this.setting.calcDiffer(d);
  1386. }
  1387. }
  1388. }
  1389. loadCompareData(data1, data2) {
  1390. this.loadCompareTree(data1, this.setting.loadInfo1);
  1391. this.loadCompareTree(data2, this.setting.loadInfo2);
  1392. for (const d of this.datas) {
  1393. d.is_leaf = d.children.length === 0;
  1394. d.expanded = true;
  1395. d.visible = true;
  1396. this.items[itemsPre + d[this.setting.id]] = d;
  1397. }
  1398. this.generateSortNodes();
  1399. this.calculateDiffer();
  1400. }
  1401. }
  1402. class TreeGatherTree extends FxTree {
  1403. constructor(setting) {
  1404. super(setting);
  1405. this._newId = 1;
  1406. }
  1407. get newId() {
  1408. return this._newId++;
  1409. }
  1410. loadGatherNode(node, parent, index, loadFun) {
  1411. const siblings = parent ? parent.children : this.children;
  1412. let cur = siblings.find(function (x) {
  1413. return node.b_code
  1414. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1415. : x.code === node.code && x.name === node.name;
  1416. });
  1417. if (!cur) {
  1418. const id = this.newId;
  1419. cur = {
  1420. id: id,
  1421. pid: parent ? parent.id : this.setting.rootId,
  1422. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1423. level: parent ? parent.level + 1 : 1,
  1424. order: siblings.length + 1,
  1425. children: [],
  1426. code: node.code, b_code: node.b_code, name: node.name,
  1427. unit: node.unit, unit_price: node.unit_price,
  1428. };
  1429. siblings.push(cur);
  1430. this.datas.push(cur);
  1431. }
  1432. loadFun(cur, node, index);
  1433. for (const c of node.children) {
  1434. this.loadGatherNode(c, cur, index, loadFun);
  1435. }
  1436. }
  1437. generateSortNodes() {
  1438. const self = this;
  1439. const addSortNode = function (node) {
  1440. self.nodes.push(node);
  1441. for (const c of node.children) {
  1442. addSortNode(c);
  1443. }
  1444. }
  1445. this.nodes = [];
  1446. for (const n of this.children) {
  1447. addSortNode(n);
  1448. }
  1449. }
  1450. loadGatherTree(data, index, loadFun) {
  1451. for (const c of data.billsTree.children) {
  1452. this.loadGatherNode(c, null, index, loadFun);
  1453. }
  1454. // todo load Pos Data;
  1455. }
  1456. calculateSum() {
  1457. if (this.setting.calcSum) {
  1458. for (const d of this.datas) {
  1459. this.setting.calcSum(d, this.count);
  1460. }
  1461. }
  1462. }
  1463. loadGatherData(datas) {
  1464. this.count = datas.length;
  1465. for (const [i, data] of datas.entries()) {
  1466. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1467. }
  1468. for (const d of this.datas) {
  1469. d.is_leaf = d.children.length === 0;
  1470. d.expanded = true;
  1471. d.visible = true;
  1472. this.items[itemsPre + d[this.setting.id]] = d;
  1473. }
  1474. this.generateSortNodes();
  1475. this.calculateSum();
  1476. }
  1477. }
  1478. if (type === 'base') {
  1479. return new BaseTree(setting);
  1480. } else if (type === 'fx') {
  1481. return new FxTree(setting);
  1482. } else if (type === 'stage') {
  1483. return new StageTree(setting);
  1484. } else if (type === 'ledger') {
  1485. return new LedgerTree(setting);
  1486. } else if (type === 'revise') {
  1487. return new ReviseTree(setting);
  1488. } else if (type === 'measure') {
  1489. return new MeasureTree(setting);
  1490. } else if (type === 'master') {
  1491. return new MasterTree(setting);
  1492. } else if (type === 'filter') {
  1493. return new FilterTree(setting);
  1494. } else if (type === 'gather') {
  1495. return new GatherTree(setting);
  1496. } else if (type === 'compare') {
  1497. return new CompareTree(setting);
  1498. } else if (type === 'tree-gather') {
  1499. return new TreeGatherTree(setting);
  1500. }
  1501. };
  1502. const treeCalc = {
  1503. mapTreeNode: function (tree) {
  1504. let map = {}, maxLevel = 0;
  1505. for (const node of tree.nodes) {
  1506. let levelArr = map[node.level];
  1507. if (!levelArr) {
  1508. levelArr = [];
  1509. map[node.level] = levelArr;
  1510. }
  1511. if (node.level > maxLevel) {
  1512. maxLevel = node.level;
  1513. }
  1514. levelArr.push(node);
  1515. }
  1516. return [maxLevel, map];
  1517. },
  1518. getMaxLevel: function (tree) {
  1519. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1520. },
  1521. calculateNode: function (tree, node) {
  1522. if (node.children && node.children.length > 0) {
  1523. const gather = node.children.reduce(function (rst, x) {
  1524. const result = {};
  1525. for (const cf of tree.setting.calcFields) {
  1526. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1527. }
  1528. return result;
  1529. });
  1530. // 汇总子项
  1531. for (const cf of tree.setting.calcFields) {
  1532. if (gather[cf]) {
  1533. node[cf] = gather[cf];
  1534. } else {
  1535. node[cf] = null;
  1536. }
  1537. }
  1538. }
  1539. // 自身运算
  1540. if (tree.setting.calcFun) {
  1541. tree.setting.calcFun(node);
  1542. }
  1543. },
  1544. calculateLevelNode: function (tree, level) {
  1545. const nodes = tree.datas.filter((n) => { return n.level === level });
  1546. for (const node of nodes) {
  1547. this.calculateNode(tree, node);
  1548. }
  1549. },
  1550. calculateAll: function (tree) {
  1551. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1552. for (let i = maxLevel; i >= 0; i--) {
  1553. const levelNodes = levelMap[i];
  1554. if (levelNodes && levelNodes.length > 0) {
  1555. for (const node of levelNodes) {
  1556. this.calculateNode(tree, node);
  1557. }
  1558. }
  1559. }
  1560. },
  1561. calculateParent: function (tree, node) {
  1562. const nodes = tree.getFullPathNodes(node.full_path);
  1563. nodes.sort((a, b) => {
  1564. return b.level - a.level;
  1565. });
  1566. for (const n of nodes) {
  1567. this.calculateNode(tree, n);
  1568. }
  1569. return nodes;
  1570. }
  1571. };