path_tree.js 53 KB

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