path_tree.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /**
  2. * (需使用 decimal.min.js, zh_calc.js)
  3. *
  4. * 构建pathTree
  5. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  6. * @param {Object} setting - 设置
  7. * @returns {PathTree}
  8. */
  9. 'use strict';
  10. class PosData {
  11. /**
  12. * 构造函数
  13. * @param {id|Number, masterId|Number} setting
  14. */
  15. constructor (setting) {
  16. // 无索引
  17. this.datas = null;
  18. // 以key为索引
  19. this.items = {};
  20. // 以分类id为索引的有序
  21. this.ledgerPos = {};
  22. // pos设置
  23. this.setting = setting;
  24. }
  25. /**
  26. * 加载部位明细数据
  27. * @param datas
  28. */
  29. loadDatas(datas) {
  30. this.datas = datas;
  31. this.items = {};
  32. this.ledgerPos = {};
  33. for (const data of this.datas) {
  34. const key = itemsPre + data[this.setting.id];
  35. this.items[key] = data;
  36. const masterKey = itemsPre + data[this.setting.ledgerId];
  37. if (!this.ledgerPos[masterKey]) {
  38. this.ledgerPos[masterKey] = [];
  39. }
  40. this.ledgerPos[masterKey].push(data);
  41. }
  42. }
  43. /**
  44. * 更新数据
  45. * @param datas
  46. */
  47. updateDatas(data) {
  48. const datas = data instanceof Array ? data : [data];
  49. const result = { create: [], update: [] };
  50. for (const d of datas) {
  51. const key = itemsPre + d[this.setting.id];
  52. if (!this.items[key]) {
  53. this.datas.push(d);
  54. this.items[key] = d;
  55. const masterKey = itemsPre + d[this.setting.ledgerId];
  56. if (!this.ledgerPos[masterKey]) {
  57. this.ledgerPos[masterKey] = [];
  58. }
  59. this.ledgerPos[masterKey].push(d);
  60. result.create.push(d);
  61. } else {
  62. const pos = this.items[key];
  63. for (const prop in d) {
  64. pos[prop] = d[prop];
  65. }
  66. result.update.push(pos);
  67. }
  68. }
  69. return result;
  70. }
  71. /**
  72. * 移除数据
  73. * @param datas
  74. */
  75. removeDatas(data) {
  76. if (!data) { return; }
  77. const datas = data instanceof Array ? data : [data];
  78. for (let i = datas.length - 1; i >= 0; i--) {
  79. const id = datas[i];
  80. const d = this.getPos(id);
  81. this.datas.splice(this.datas.indexOf(d), 1);
  82. const key = itemsPre + d[this.setting.id];
  83. delete this.items[key];
  84. const masterKey = itemsPre + d[this.setting.ledgerId];
  85. const range = this.ledgerPos[masterKey];
  86. range.splice(range.indexOf(d), 1);
  87. if (range.length === 0) {
  88. delete this.ledgerPos[masterKey];
  89. }
  90. }
  91. }
  92. /**
  93. * 移除数据 - 根据分类id
  94. * @param mid
  95. */
  96. removeDatasByMasterId (mid) {
  97. const masterKey = itemsPre + mid;
  98. const range = this.ledgerPos[masterKey];
  99. if (range) {
  100. delete this.ledgerPos[masterKey];
  101. for (const r of range) {
  102. this.datas.splice(this.datas.indexOf(r), 1);
  103. const key = itemsPre + r[this.setting.id];
  104. delete this.items[key];
  105. }
  106. }
  107. }
  108. getPos(id) {
  109. return this.items[itemsPre + id];
  110. }
  111. getLedgerPos(mid) {
  112. return this.ledgerPos[itemsPre + mid];
  113. }
  114. /**
  115. * 计算全部
  116. */
  117. calculateAll() {
  118. if (!this.setting.calcFun) { return; }
  119. for (const pos of this.datas) {
  120. this.setting.calcFun(pos);
  121. }
  122. }
  123. }
  124. class StagePosData extends PosData {
  125. loadStageData(datas, fieldPre, fields) {
  126. if (!datas) { return; }
  127. datas = datas instanceof Array ? datas : [datas];
  128. const loadedData = [];
  129. for (const data of datas) {
  130. let node = this.getPos(data.pid);
  131. if (node) {
  132. for (const prop of fields) {
  133. if (data[prop] !== undefined) {
  134. node[fieldPre + prop] = data[prop];
  135. }
  136. }
  137. if (this.setting.calcFun) {
  138. this.setting.calcFun(node);
  139. }
  140. loadedData.push(node);
  141. }
  142. }
  143. }
  144. loadPreStageData(datas) {
  145. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  146. }
  147. loadCurStageData(datas) {
  148. this.loadStageData(datas, '', this.setting.updateFields);
  149. }
  150. }
  151. class MasterPosData extends PosData {
  152. /**
  153. * 构造函数
  154. * @param {id|Number, masterId|Number} setting
  155. */
  156. constructor (setting) {
  157. super(setting);
  158. // 关联索引
  159. this.masterItems = {};
  160. }
  161. /**
  162. * 加载主数据
  163. * @param datas
  164. */
  165. loadDatas (datas) {
  166. super.loadDatas(datas);
  167. // 清空旧数据
  168. this.masterItems = {};
  169. // minor数据缓存
  170. this.minorData = {};
  171. // 加载全部数据
  172. for (const data of this.datas) {
  173. const keyName = itemsPre + data[this.setting.masterId];
  174. this.masterItems[keyName] = data;
  175. }
  176. }
  177. /**
  178. * 根据关联id,查找节点
  179. * @param id
  180. * @returns {*}
  181. */
  182. getMasterItems(id) {
  183. return this.masterItems[itemsPre + id];
  184. }
  185. /**
  186. * 加载关联数据
  187. *
  188. * @param {Array|Object}datas - 需要关联的数据
  189. * @param {String} fieldPre - 关联字段前缀(关联结果)
  190. * @param {Array} fields - 关联字段
  191. * @returns {Array}
  192. */
  193. loadMinorData(datas, fieldSuf, fields) {
  194. if (!datas) { return; }
  195. datas = datas instanceof Array ? datas : [datas];
  196. this.minorData[fieldSuf] = datas;
  197. const loadedData = [];
  198. for (const data of datas) {
  199. let node = this.getMasterItems(data[this.setting.minorId]);
  200. if (node) {
  201. for (const prop of fields) {
  202. if (data[prop] !== undefined) {
  203. node[prop + fieldSuf] = data[prop];
  204. }
  205. }
  206. loadedData.push(node);
  207. }
  208. }
  209. return loadedData;
  210. }
  211. }
  212. const itemsPre = 'id_';
  213. const createNewPathTree = function (type, setting) {
  214. class BaseTree {
  215. /**
  216. * 构造函数
  217. */
  218. constructor (setting) {
  219. // 无索引
  220. this.datas = [];
  221. // 以key为索引
  222. this.items = {};
  223. // 以排序为索引
  224. this.nodes = [];
  225. // 根节点
  226. this.children = [];
  227. // 树设置
  228. this.setting = setting;
  229. }
  230. /**
  231. * 树结构根据显示排序
  232. */
  233. sortTreeNode (isResort) {
  234. const self = this;
  235. const addSortNodes = function (nodes) {
  236. if (!nodes) { return }
  237. for (let i = 0; i < nodes.length; i++) {
  238. self.nodes.push(nodes[i]);
  239. nodes[i].index = self.nodes.length - 1;
  240. if (!isResort) {
  241. nodes[i].children = self.getChildren(nodes[i]);
  242. } else {
  243. nodes[i].children.sort(function (a, b) {
  244. return a.order - b.order;
  245. })
  246. }
  247. addSortNodes(nodes[i].children);
  248. }
  249. };
  250. this.nodes = [];
  251. addSortNodes(this.children);
  252. }
  253. /**
  254. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  255. * @param datas
  256. */
  257. loadDatas (datas) {
  258. // 清空旧数据
  259. this.items = {};
  260. this.nodes = [];
  261. this.datas = [];
  262. this.children = [];
  263. // 加载全部数据
  264. datas.sort(function (a, b) {
  265. return a.level - b.level;
  266. });
  267. for (const data of datas) {
  268. const keyName = itemsPre + data[this.setting.id];
  269. if (!this.items[keyName]) {
  270. const item = JSON.parse(JSON.stringify(data));
  271. item.children = [];
  272. item.expanded = true;
  273. item.visible = true;
  274. this.items[keyName] = item;
  275. this.datas.push(item);
  276. if (item[setting.pid] === setting.rootId) {
  277. this.children.push(item);
  278. } else {
  279. const parent = this.getParent(item);
  280. if (parent) {
  281. parent.children.push(item);
  282. }
  283. }
  284. }
  285. }
  286. this.children.sort(function (a, b) {
  287. return a.order - b.order;
  288. });
  289. this.sortTreeNode(true);
  290. }
  291. getItemsByIndex(index) {
  292. return this.nodes[index];
  293. }
  294. /**
  295. * 根据id获取树结构节点数据
  296. * @param {Number} id
  297. * @returns {Object}
  298. */
  299. getItems (id) {
  300. return this.items[itemsPre + id];
  301. };
  302. /**
  303. * 查找node的parent
  304. * @param {Object} node
  305. * @returns {Object}
  306. */
  307. getParent (node) {
  308. return this.getItems(node[this.setting.pid]);
  309. };
  310. /**
  311. * 查找node的前兄弟节点
  312. * @param node
  313. * @returns {*}
  314. */
  315. getPreSiblingNode(node) {
  316. const parent = this.getParent(node);
  317. const siblings = parent ? parent.children : this.children;
  318. const index = siblings.indexOf(node);
  319. if (index > 0) {
  320. return siblings[index - 1];
  321. } else {
  322. return null;
  323. }
  324. }
  325. /**
  326. * 查找node的后兄弟节点
  327. * @param node
  328. * @returns {*}
  329. */
  330. getNextSiblingNode(node) {
  331. const parent = this.getParent(node);
  332. const siblings = parent ? parent.children : this.children;
  333. const index = siblings.indexOf(node);
  334. if (index >= 0 && index < siblings.length - 1) {
  335. return siblings[index + 1];
  336. } else {
  337. return null;
  338. }
  339. }
  340. /**
  341. * 根据path查找完整节点
  342. * @param {Number} path
  343. */
  344. getFullPathNodes (path) {
  345. const self = this, ids = path.split('.');
  346. if (ids.length > 0) {
  347. return this.nodes.filter((x) => {
  348. return ids.indexOf('' + x[self.setting.id]) >= 0;
  349. });
  350. } else {
  351. return [];
  352. }
  353. };
  354. /**
  355. * 查询node的已下载子节点
  356. * @param {Object} node
  357. * @returns {Array}
  358. */
  359. getChildren (node) {
  360. const setting = this.setting;
  361. const pid = node ? node[setting.id] : setting.rootId;
  362. const children = this.datas.filter(function (x) {
  363. return x[setting.pid] === pid;
  364. });
  365. children.sort(function (a, b) {
  366. return a.order - b.order;
  367. });
  368. return children;
  369. };
  370. /**
  371. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  372. * @param node
  373. * @returns {*}
  374. * @private
  375. */
  376. _recursiveGetPosterity (node) {
  377. let posterity = node.children;
  378. for (const c of node.children) {
  379. posterity = posterity.concat(this._recursiveGetPosterity(c));
  380. }
  381. return posterity;
  382. };
  383. /**
  384. * 查询node的已下载的全部后代
  385. * @param {Object} node
  386. * @returns {Array}
  387. */
  388. getPosterity (node) {
  389. if (node.full_path !== '') {
  390. const reg = new RegExp('^' + node.full_path + '.');
  391. return this.datas.filter(function (x) {
  392. return reg.test(x.full_path);
  393. });
  394. } else {
  395. return this._recursiveGetPosterity(node);
  396. }
  397. };
  398. /**
  399. * 查询node是否是父节点的最后一个子节点
  400. * @param {Object} node
  401. * @returns {boolean}
  402. */
  403. isLastSibling (node) {
  404. const siblings = this.getChildren(this.getParent(node));
  405. return node.order === siblings[siblings.length - 1].order;
  406. };
  407. /**
  408. * 刷新子节点是否可见
  409. * @param {Object} node
  410. * @private
  411. */
  412. _refreshChildrenVisible (node) {
  413. if (!node.children) {
  414. node.children = this.getChildren(node);
  415. }
  416. if (node.children && node.children.length > 0) {
  417. for (const child of node.children) {
  418. child.visible = node.expanded && node.visible;
  419. this._refreshChildrenVisible(child);
  420. }
  421. }
  422. };
  423. /**
  424. * 设置节点是否展开, 并控制子节点可见
  425. * @param {Object} node
  426. * @param {Boolean} expanded
  427. */
  428. setExpanded (node, expanded) {
  429. node.expanded = expanded;
  430. this._refreshChildrenVisible(node);
  431. };
  432. /**
  433. * 提取节点key和索引数据
  434. * @param {Object} node - 节点
  435. * @returns {key}
  436. */
  437. getNodeKeyData (node) {
  438. const data = {};
  439. for (const key of this.setting.keys) {
  440. data[key] = node[key];
  441. }
  442. return data;
  443. };
  444. /**
  445. * 得到树结构构成id
  446. * @param node
  447. * @returns {*}
  448. */
  449. getNodeKey (node) {
  450. return node[this.setting.id];
  451. };
  452. /**
  453. * 递归 设置节点展开状态
  454. * @param {Array} nodes - 需要设置状态的节点
  455. * @param {Object} parent - nodes的父节点
  456. * @param {Function} checkFun - 判断节点展开状态的方法
  457. * @private
  458. */
  459. _recursiveExpand(nodes, parent, checkFun) {
  460. for (const node of nodes) {
  461. node.expanded = checkFun(node);
  462. node.visible = parent ? (parent.expanded && parent.visible) : true;
  463. this._recursiveExpand(node.children, node, checkFun);
  464. }
  465. }
  466. /**
  467. * 自定义展开规则
  468. * @param checkFun
  469. */
  470. expandByCustom(checkFun) {
  471. this._recursiveExpand(this.children, null, checkFun);
  472. }
  473. /**
  474. * 展开到第几层
  475. * @param {Number} level - 展开层数
  476. */
  477. expandByLevel(level) {
  478. // function recursiveExpand(nodes, parent) {
  479. // for (const node of nodes) {
  480. // node.expanded = node.level < level;
  481. // node.visible = parent ? (parent.expanded && parent.visible) : true;
  482. // recursiveExpand(node.children, node);
  483. // }
  484. // }
  485. // recursiveExpand(this.children);
  486. this.expandByCustom(function (n) {
  487. return n.level < level;
  488. });
  489. }
  490. }
  491. class MeasureTree extends BaseTree {
  492. addData (datas) {
  493. const loadedData = [];
  494. for (const data of datas) {
  495. let node = this.getItems(data[this.setting.id]);
  496. if (node) {
  497. for (const prop in node) {
  498. if (data[prop] !== undefined) {
  499. node[prop] = data[prop];
  500. }
  501. }
  502. loadedData.push(node);
  503. } else {
  504. const keyName = itemsPre + data[this.setting.id];
  505. const node = JSON.parse(JSON.stringify(data));
  506. this.items[keyName] = node;
  507. this.datas.push(node);
  508. node.expanded = false;
  509. node.visible = true;
  510. loadedData.push(node);
  511. }
  512. }
  513. this.sortTreeNode();
  514. for (const node of loadedData) {
  515. const children = node.children;
  516. if (!node.expanded && children.length > 0) {
  517. node.expanded = true;
  518. this._refreshChildrenVisible(node);
  519. }
  520. }
  521. return loadedData;
  522. }
  523. removeData (datas) {
  524. datas.sort(function (a, b) {
  525. return b.level - a.level;
  526. });
  527. const removeArrayData = function (array, data) {
  528. const index = array.indexOf(data);
  529. array.splice(index, 1);
  530. };
  531. for (const data of datas) {
  532. const node = this.getItems(data[this.setting.id]);
  533. if (node && this.getChildren(node).length === 0) {
  534. delete this.items[itemsPre + node[this.setting.id]];
  535. if (node[this.setting.pid] !== this.setting.rootId) {
  536. const parent = this.items[itemsPre + node[this.setting.pid]];
  537. removeArrayData(parent.children, node);
  538. }
  539. removeArrayData(this.datas, node);
  540. removeArrayData(this.nodes, node);
  541. }
  542. }
  543. };
  544. loadLeafData (data) {
  545. const datas = data instanceof Array ? data : [data];
  546. for (const d of datas) {
  547. let node = this.getItems(d[this.setting.id]);
  548. if (node && node.is_leaf) {
  549. for (const prop in node) {
  550. if (data[prop] !== undefined) {
  551. node[prop] = d[prop];
  552. }
  553. }
  554. }
  555. }
  556. };
  557. }
  558. class FxTree extends BaseTree {
  559. /**
  560. * 检查节点是否是最底层项目节
  561. * @param node
  562. * @returns {boolean}
  563. */
  564. isLeafXmj(node) {
  565. if (!node.code) {
  566. return false;
  567. }
  568. for (const child of node.children) {
  569. if (!child.b_code || child.b_code === '') {
  570. return false;
  571. }
  572. }
  573. return true;
  574. }
  575. /**
  576. * 查询最底层项目节(本身或父项)
  577. * @param {Object} node - 查询节点
  578. * @returns {Object}
  579. */
  580. getLeafXmjParent(node) {
  581. let parent = node;
  582. while (parent) {
  583. if (this.isLeafXmj(parent)) {
  584. return parent;
  585. } else {
  586. parent = this.getParent(parent);
  587. }
  588. }
  589. return null;
  590. }
  591. /**
  592. * 展开至最底层项目节
  593. */
  594. expandToLeafXmj() {
  595. const self = this;
  596. this.expandByCustom(function (node) {
  597. if (node.b_code && node.b_code !== '') {
  598. return false;
  599. } else {
  600. return !self.isLeafXmj(node);
  601. }
  602. })
  603. }
  604. /**
  605. * 展开至计算项
  606. */
  607. expandByCalcFields() {
  608. const self = this;
  609. this.expandByCustom(function (node) {
  610. for (const field of self.setting.calcFields) {
  611. if (node[field]) {
  612. return true;
  613. }
  614. }
  615. return false;
  616. })
  617. }
  618. }
  619. class LedgerTree extends FxTree {
  620. /**
  621. * 加载数据(动态),只加载不同部分
  622. * @param {Array} datas
  623. * @return {Array} 加载到树的数据
  624. * @privateA
  625. */
  626. _updateData (datas) {
  627. datas = datas instanceof Array ? datas : [datas];
  628. let loadedData = [];
  629. for (const data of datas) {
  630. let node = this.getItems(data[this.setting.id]);
  631. if (node) {
  632. for (const prop in node) {
  633. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  634. }
  635. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  636. if (prop === this.setting.pid) {
  637. loadedData.push(this.getItems(node[this.setting.pid]));
  638. loadedData.push(this.getItems(data[this.setting.pid]));
  639. }
  640. node[prop] = data[prop];
  641. }
  642. }
  643. loadedData.push(node);
  644. }
  645. }
  646. loadedData = _.uniq(loadedData);
  647. for (const node of loadedData) {
  648. node.children = this.getChildren(node);
  649. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  650. }
  651. this.sortTreeNode(true);
  652. return loadedData;
  653. };
  654. /**
  655. * 加载数据(动态),只加载不同部分
  656. * @param {Array} datas
  657. * @return {Array} 加载到树的数据
  658. * @privateA
  659. */
  660. _loadData (datas) {
  661. datas = datas instanceof Array ? datas : [datas];
  662. const loadedData = [], resortData = [];
  663. for (const data of datas) {
  664. let node = this.getItems(data[this.setting.id]);
  665. if (node) {
  666. const parent = this.getItems(node[this.setting.pid]);
  667. for (const prop in node) {
  668. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  669. node[prop] = data[prop];
  670. if (parent && resortData.indexOf(parent) === -1) {
  671. resortData.push(parent);
  672. }
  673. }
  674. }
  675. loadedData.push(node);
  676. } else {
  677. const keyName = itemsPre + data[this.setting.id];
  678. const node = JSON.parse(JSON.stringify(data));
  679. this.items[keyName] = node;
  680. this.datas.push(node);
  681. node.expanded = true;
  682. node.visible = true;
  683. loadedData.push(node);
  684. if (resortData.indexOf(node) === -1) {
  685. resortData.push(node);
  686. }
  687. const parent = this.getItems(node[this.setting.pid]);
  688. if (parent && resortData.indexOf(parent) === -1) {
  689. resortData.push(parent);
  690. }
  691. }
  692. }
  693. for (const node of resortData) {
  694. node.children = this.getChildren(node);
  695. }
  696. this.sortTreeNode(true);
  697. for (const node of loadedData) {
  698. if (!node.expanded) {
  699. this.setExpanded(node, true);
  700. }
  701. }
  702. return loadedData;
  703. };
  704. /**
  705. * 清理数据(动态)
  706. * @param datas
  707. * @private
  708. */
  709. _freeData (datas) {
  710. datas = datas instanceof Array ? datas : [datas];
  711. const freeDatas = [];
  712. const removeArrayData = function (array, data) {
  713. const index = array.indexOf(data);
  714. array.splice(index, 1);
  715. };
  716. for (const data of datas) {
  717. const node = this.getItems(data[this.setting.id]);
  718. if (node) {
  719. freeDatas.push(node);
  720. delete this.items[itemsPre + node[this.setting.id]];
  721. if (node[this.setting.pid] !== this.setting.rootId) {
  722. const parent = this.getItems(node[this.setting.pid]);
  723. if (parent) {
  724. removeArrayData(parent.children, node);
  725. }
  726. }
  727. removeArrayData(this.datas, node);
  728. removeArrayData(this.nodes, node);
  729. }
  730. }
  731. return freeDatas;
  732. };
  733. /**
  734. * 加载需展开的数据
  735. * @param {Array} datas
  736. * @returns {Array}
  737. * @private
  738. */
  739. _loadExpandData (datas) {
  740. datas = datas instanceof Array ? datas : [datas];
  741. const loadedData = [], existData = [], expandData = [], resortData = [];
  742. for (const data of datas) {
  743. let node = this.getItems(data[this.setting.id]);
  744. if (node) {
  745. existData.push(node);
  746. } else {
  747. const keyName = itemsPre + data[this.setting.id];
  748. const node = JSON.parse(JSON.stringify(data));
  749. this.items[keyName] = node;
  750. this.datas.push(node);
  751. node.expanded = false;
  752. node.visible = true;
  753. loadedData.push(node);
  754. if (resortData.indexOf(node) === -1) {
  755. resortData.push(node);
  756. }
  757. const parent = this.getItems(node[this.setting.pid]);
  758. if (parent && resortData.indexOf(parent) === -1) {
  759. resortData.push(parent);
  760. }
  761. }
  762. }
  763. for (const node of resortData) {
  764. node.children = this.getChildren(node);
  765. }
  766. this.sortTreeNode(true);
  767. for (const node of loadedData) {
  768. if (!node.expanded) {
  769. this.setExpanded(node, true);
  770. }
  771. }
  772. for (const node of existData) {
  773. const parent = this.getItems(node[this.setting.pid]);
  774. if (expandData.indexOf(parent) === -1) {
  775. expandData.push(parent);
  776. if (!parent.expanded) {
  777. this.setExpanded(parent, true);
  778. }
  779. }
  780. if (!node.expanded) {
  781. this.setExpanded(node, true);
  782. }
  783. }
  784. return [loadedData, expandData];
  785. };
  786. /**
  787. *
  788. * @param parent
  789. * @param node
  790. * @private
  791. */
  792. _getNodesParents(parents, nodes) {
  793. for (const node of nodes) {
  794. const parent = this.getParent(node);
  795. if (parent) {
  796. const paths = this.getFullPathNodes(parent.full_path);
  797. for (const p of paths) {
  798. if (parents.indexOf(p) === -1) {
  799. parents.push(p);
  800. }
  801. }
  802. }
  803. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  804. parents.push(node);
  805. }
  806. }
  807. }
  808. _getReCalcNodes(reCalcNodes, nodes) {
  809. for (const node of nodes) {
  810. const parent = this.getParent(node);
  811. if (parent) {
  812. const paths = this.getFullPathNodes(parent.full_path);
  813. for (const p of paths) {
  814. if (reCalcNodes.indexOf(p) === -1) {
  815. reCalcNodes.push(p);
  816. }
  817. }
  818. }
  819. // 最底层项目节,也需要计算
  820. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  821. reCalcNodes.push(node);
  822. //}
  823. }
  824. }
  825. /**
  826. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  827. *
  828. * @param data - 更新的数据 {update, create, delete}
  829. * @returns {{}}
  830. */
  831. loadPostData(data) {
  832. const result = {}, reCalcNodes = [];
  833. if (data.update) {
  834. result.update = this._updateData(data.update);
  835. this._getReCalcNodes(reCalcNodes, result.update);
  836. }
  837. if (data.create) {
  838. result.create = this._loadData(data.create);
  839. this._getReCalcNodes(reCalcNodes, result.create);
  840. }
  841. if (data.delete) {
  842. result.delete = this._freeData(data.delete);
  843. this._getReCalcNodes(reCalcNodes, result.delete);
  844. }
  845. reCalcNodes.sort((a, b) => {
  846. return b.level - a.level;
  847. });
  848. for (const node of reCalcNodes) {
  849. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  850. }
  851. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  852. return result;
  853. }
  854. /**
  855. * 以下方法需等待响应, 通过callback刷新界面
  856. */
  857. /**
  858. * 加载子节点
  859. * @param {Object} node
  860. * @param {function} callback
  861. */
  862. loadChildren (node, callback) {
  863. const self = this;
  864. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  865. postData(url, this.getNodeKeyData(node), function (data) {
  866. self._loadData(data);
  867. callback();
  868. });
  869. };
  870. /**
  871. * 树结构基本操作
  872. * @param {String} url - 请求地址
  873. * @param {Object} node - 操作节点
  874. * @param {String} type - 操作类型
  875. * @param {function} callback - 界面刷新
  876. */
  877. baseOperation (url, node, type, callback) {
  878. const self = this;
  879. const data = {
  880. id: node[this.setting.id],
  881. postType: type
  882. };
  883. postData(url, data, function (datas) {
  884. const refreshData = self.loadPostData(datas);
  885. callback(refreshData);
  886. });
  887. };
  888. /**
  889. * 节点数据编辑
  890. * @param {String} url - 请求地址
  891. * @param {Array|Object} updateData - 需更新的数据
  892. * @param {function} callback - 界面刷新
  893. */
  894. update (url, updateData, callback) {
  895. const self = this;
  896. postData(url, updateData, function (datas) {
  897. const refreshData = self.loadPostData(datas);
  898. callback(refreshData);
  899. }, function () {
  900. if (updateData instanceof Array) {
  901. const result = [];
  902. for (const data of updateData) {
  903. result.push(self.getItems(data[self.setting.id]));
  904. }
  905. callback(result)
  906. } else {
  907. callback([self.getItems(updateData[self.setting.id])]);
  908. }
  909. });
  910. };
  911. /**
  912. * 复制粘贴整块(目前仅可粘贴为后项)
  913. * @param {String} url - 请求地址
  914. * @param {Object} node - 操作节点
  915. * @param {Array} block - 被复制整块的节点列表
  916. * @param {function} callback - 界面刷新
  917. */
  918. pasteBlock (url, node, block, callback) {
  919. const self = this;
  920. const data = {
  921. id: node[self.setting.id],
  922. block: block
  923. };
  924. postData(url, data, function (datas) {
  925. const refreshData = self.loadPostData(datas);
  926. callback(refreshData);
  927. });
  928. };
  929. /**
  930. * 提交数据
  931. * @param {String} url - 请求地址
  932. * @param {Object} node - 当前选中节点
  933. * @param {Object} data - 提交的数据
  934. * @param {function} callback - 界面刷新
  935. */
  936. postData (url, node, data, callback) {
  937. const self = this;
  938. if (node) {
  939. data.id = node[self.setting.id];
  940. }
  941. postData(url, data, function (datas) {
  942. const refreshData = self.loadPostData(datas);
  943. callback(refreshData);
  944. // const result = {};
  945. // if (datas.update) {
  946. // result.update = self._updateData(datas.update);
  947. // }
  948. // if (datas.create) {
  949. // result.create = self._loadData(datas.create);
  950. // }
  951. // if (datas.delete) {
  952. // result.delete = self._freeData(datas.delete);
  953. // }
  954. // if (datas.expand) {
  955. // const [create, update] = self._loadExpandData(datas.expand);
  956. // result.create = result.create ? result.create.concat(create) : create;
  957. // result.expand = update;
  958. // }
  959. // callback(result);
  960. });
  961. };
  962. }
  963. class StageTree extends FxTree {
  964. /**
  965. * 构造函数
  966. */
  967. constructor (setting) {
  968. super(setting);
  969. // stage关联索引
  970. this.stageItems = {};
  971. }
  972. /**
  973. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  974. * @param datas
  975. */
  976. loadDatas (datas) {
  977. super.loadDatas(datas);
  978. // 清空旧数据
  979. this.stageItems = {};
  980. // 加载全部数据
  981. for (const data of this.datas) {
  982. const keyName = itemsPre + data[this.setting.stageId];
  983. this.stageItems[keyName] = data;
  984. }
  985. }
  986. getStageItems(id) {
  987. return this.stageItems[itemsPre + id];
  988. }
  989. loadStageData(datas, fieldPre, fields) {
  990. datas = datas instanceof Array ? datas : [datas];
  991. const loadedData = [];
  992. for (const data of datas) {
  993. let node = this.getStageItems(data.lid);
  994. if (node) {
  995. for (const prop of fields) {
  996. if (data[prop] !== undefined) {
  997. node[fieldPre + prop] = data[prop];
  998. }
  999. }
  1000. loadedData.push(node);
  1001. }
  1002. }
  1003. }
  1004. loadPreStageData(datas) {
  1005. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1006. }
  1007. loadCurStageData(datas) {
  1008. this.loadStageData(datas, '', this.setting.updateFields);
  1009. }
  1010. /**
  1011. * 加载数据(动态),只加载不同部分
  1012. * @param {Array} datas
  1013. * @return {Array} 加载到树的数据
  1014. * @privateA
  1015. */
  1016. _updateData (datas) {
  1017. datas = datas instanceof Array ? datas : [datas];
  1018. let loadedData = [];
  1019. for (const data of datas) {
  1020. let node = this.getItems(data[this.setting.id]);
  1021. if (node) {
  1022. for (const prop in node) {
  1023. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1024. }
  1025. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1026. if (prop === this.setting.pid) {
  1027. loadedData.push(this.getItems(node[this.setting.pid]));
  1028. loadedData.push(this.getItems(data[this.setting.pid]));
  1029. }
  1030. node[prop] = data[prop];
  1031. }
  1032. }
  1033. loadedData.push(node);
  1034. }
  1035. }
  1036. loadedData = _.uniq(loadedData);
  1037. for (const node of loadedData) {
  1038. node.children = this.getChildren(node);
  1039. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1040. }
  1041. this.sortTreeNode(true);
  1042. return loadedData;
  1043. };
  1044. /**
  1045. * 加载数据(动态),只加载不同部分
  1046. * @param {Array} datas
  1047. * @return {Array} 加载到树的数据
  1048. * @privateA
  1049. */
  1050. _updateStageData (datas) {
  1051. datas = datas instanceof Array ? datas : [datas];
  1052. const loadedData = [];
  1053. for (const data of datas) {
  1054. let node = this.getStageItems(data.lid);
  1055. if (node) {
  1056. for (const prop of this.setting.updateFields) {
  1057. if (data[prop] !== undefined) {
  1058. node[prop] = data[prop];
  1059. }
  1060. }
  1061. loadedData.push(node);
  1062. }
  1063. }
  1064. return loadedData;
  1065. };
  1066. /**
  1067. *
  1068. * @param parent
  1069. * @param node
  1070. * @private
  1071. */
  1072. _getNodesParents(parents, nodes) {
  1073. for (const node of nodes) {
  1074. const parent = this.getParent(node);
  1075. if (parent) {
  1076. const paths = this.getFullPathNodes(parent.full_path);
  1077. for (const p of paths) {
  1078. if (parents.indexOf(p) === -1) {
  1079. parents.push(p);
  1080. }
  1081. }
  1082. }
  1083. if (node.children && node.children.length > 0) {
  1084. parents.push(node);
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * 提交数据至后端,返回的前端树结构应刷新的部分
  1090. * StageTree仅有更新CurStage部分,不需要增删
  1091. *
  1092. * @param data - 需要更新的数据
  1093. * @returns {Array} - 界面需要刷新的数据
  1094. */
  1095. loadPostStageData(data) {
  1096. let result, parents = [];
  1097. if (data.bills) {
  1098. result = this._updateData(data.bills);
  1099. this._getNodesParents(parents, result);
  1100. }
  1101. if (data.curStageData) {
  1102. result = this._updateStageData(data.curStageData);
  1103. this._getNodesParents(parents, result);
  1104. }
  1105. result = result ? result.concat(parents) : parents;
  1106. result.sort((a, b) => {
  1107. return b.level - a.level;
  1108. });
  1109. for (const node of result) {
  1110. treeCalc.calculateNode(this, node);
  1111. }
  1112. return result;
  1113. }
  1114. }
  1115. class MasterTree extends FxTree {
  1116. /**
  1117. * 构造函数
  1118. */
  1119. constructor (setting) {
  1120. super(setting);
  1121. // 关联索引
  1122. this.masterItems = {};
  1123. }
  1124. /**
  1125. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1126. * @param datas
  1127. */
  1128. loadDatas (datas) {
  1129. super.loadDatas(datas);
  1130. // 清空旧数据
  1131. this.masterItems = {};
  1132. // minor数据缓存
  1133. this.minorData = {};
  1134. // 加载全部数据
  1135. for (const data of this.datas) {
  1136. const keyName = itemsPre + data[this.setting.masterId];
  1137. this.masterItems[keyName] = data;
  1138. }
  1139. }
  1140. /**
  1141. * 根据关联id,查找节点
  1142. * @param id
  1143. * @returns {*}
  1144. */
  1145. getMasterItems(id) {
  1146. return this.masterItems[itemsPre + id];
  1147. }
  1148. /**
  1149. * 加载关联数据
  1150. *
  1151. * @param {Array|Object}datas - 需要关联的数据
  1152. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1153. * @param {Array} fields - 关联字段
  1154. * @returns {Array}
  1155. */
  1156. loadMinorData(datas, fieldSuf, fields) {
  1157. if (!datas) { return; }
  1158. datas = datas instanceof Array ? datas : [datas];
  1159. this.minorData[fieldSuf] = datas;
  1160. const loadedData = [];
  1161. for (const data of datas) {
  1162. let node = this.getMasterItems(data[this.setting.minorId]);
  1163. if (node) {
  1164. for (const prop of fields) {
  1165. if (data[prop] !== undefined) {
  1166. node[prop + fieldSuf] = data[prop];
  1167. }
  1168. }
  1169. loadedData.push(node);
  1170. }
  1171. }
  1172. return loadedData;
  1173. }
  1174. }
  1175. if (type === 'base') {
  1176. return new BaseTree(setting);
  1177. } else if (type === 'fx') {
  1178. return new FxTree(setting);
  1179. } else if (type === 'stage') {
  1180. return new StageTree(setting);
  1181. } else if (type === 'ledger') {
  1182. return new LedgerTree(setting);
  1183. } else if (type === 'measure') {
  1184. return new MeasureTree(setting);
  1185. } else if (type === 'master') {
  1186. return new MasterTree(setting);
  1187. }
  1188. };
  1189. const treeCalc = {
  1190. mapTreeNode: function (tree) {
  1191. let map = {}, maxLevel = 0;
  1192. for (const node of tree.nodes) {
  1193. let levelArr = map[node.level];
  1194. if (!levelArr) {
  1195. levelArr = [];
  1196. map[node.level] = levelArr;
  1197. }
  1198. if (node.level > maxLevel) {
  1199. maxLevel = node.level;
  1200. }
  1201. levelArr.push(node);
  1202. }
  1203. return [maxLevel, map];
  1204. },
  1205. getMaxLevel: function (tree) {
  1206. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  1207. },
  1208. calculateNode: function (tree, node) {
  1209. if (node.children && node.children.length > 0) {
  1210. const gather = node.children.reduce(function (rst, x) {
  1211. const result = {};
  1212. for (const cf of tree.setting.calcFields) {
  1213. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1214. }
  1215. return result;
  1216. });
  1217. // 汇总子项
  1218. for (const cf of tree.setting.calcFields) {
  1219. if (gather[cf]) {
  1220. node[cf] = gather[cf];
  1221. } else {
  1222. node[cf] = null;
  1223. }
  1224. }
  1225. }
  1226. // 自身运算
  1227. if (tree.setting.calcFun) {
  1228. tree.setting.calcFun(node);
  1229. }
  1230. },
  1231. calculateLevelNode: function (tree, level) {
  1232. const nodes = tree.datas.filter((n) => { return n.level === level });
  1233. for (const node of nodes) {
  1234. this.calculateNode(tree, node);
  1235. }
  1236. },
  1237. calculateAll: function (tree) {
  1238. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1239. for (let i = maxLevel; i >= 0; i--) {
  1240. const levelNodes = levelMap[i];
  1241. if (levelNodes && levelNodes.length > 0) {
  1242. for (const node of levelNodes) {
  1243. this.calculateNode(tree, node);
  1244. }
  1245. }
  1246. }
  1247. },
  1248. calculateParent: function (tree, node) {
  1249. const nodes = tree.getFullPathNodes(node.full_path);
  1250. nodes.sort((a, b) => {
  1251. return b.level - a.level;
  1252. });
  1253. for (const n of nodes) {
  1254. this.calculateNode(tree, n);
  1255. }
  1256. return nodes;
  1257. }
  1258. };