path_tree.js 37 KB

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