path_tree.js 53 KB

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