path_tree.js 53 KB

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