path_tree.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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[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. * 查找node的前兄弟节点
  296. * @param node
  297. * @returns {*}
  298. */
  299. getPreSiblingNode(node) {
  300. const parent = this.getParent(node);
  301. const siblings = parent ? parent.children : this.children;
  302. const index = siblings.indexOf(node);
  303. if (index > 0) {
  304. return siblings[index - 1];
  305. } else {
  306. return null;
  307. }
  308. }
  309. /**
  310. * 查找node的后兄弟节点
  311. * @param node
  312. * @returns {*}
  313. */
  314. getNextSiblingNode(node) {
  315. const parent = this.getParent(node);
  316. const siblings = parent ? parent.children : this.children;
  317. const index = siblings.indexOf(node);
  318. if (index >= 0 && index < siblings.length - 1) {
  319. return siblings[index + 1];
  320. } else {
  321. return null;
  322. }
  323. }
  324. /**
  325. * 根据path查找完整节点
  326. * @param {Number} path
  327. */
  328. getFullPathNodes (path) {
  329. const self = this, ids = path.split('.');
  330. if (ids.length > 0) {
  331. return this.nodes.filter((x) => {
  332. return ids.indexOf('' + x[self.setting.id]) >= 0;
  333. });
  334. } else {
  335. return [];
  336. }
  337. };
  338. /**
  339. * 查询node的已下载子节点
  340. * @param {Object} node
  341. * @returns {Array}
  342. */
  343. getChildren (node) {
  344. const setting = this.setting;
  345. const pid = node ? node[setting.id] : setting.rootId;
  346. const children = this.datas.filter(function (x) {
  347. return x[setting.pid] === pid;
  348. });
  349. children.sort(function (a, b) {
  350. return a.order - b.order;
  351. });
  352. return children;
  353. };
  354. /**
  355. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  356. * @param node
  357. * @returns {*}
  358. * @private
  359. */
  360. _recursiveGetPosterity (node) {
  361. let posterity = node.children;
  362. for (const c of node.children) {
  363. posterity = posterity.concat(this._recursiveGetPosterity(c));
  364. }
  365. return posterity;
  366. };
  367. /**
  368. * 查询node的已下载的全部后代
  369. * @param {Object} node
  370. * @returns {Array}
  371. */
  372. getPosterity (node) {
  373. if (node.full_path !== '') {
  374. const reg = new RegExp('^' + node.full_path + '.');
  375. return this.datas.filter(function (x) {
  376. return reg.test(x.full_path);
  377. });
  378. } else {
  379. return this._recursiveGetPosterity(node);
  380. }
  381. };
  382. /**
  383. * 查询node是否是父节点的最后一个子节点
  384. * @param {Object} node
  385. * @returns {boolean}
  386. */
  387. isLastSibling (node) {
  388. const siblings = this.getChildren(this.getParent(node));
  389. return node.order === siblings[siblings.length - 1].order;
  390. };
  391. /**
  392. * 刷新子节点是否可见
  393. * @param {Object} node
  394. * @private
  395. */
  396. _refreshChildrenVisible (node) {
  397. if (!node.children) {
  398. node.children = this.getChildren(node);
  399. }
  400. if (node.children && node.children.length > 0) {
  401. for (const child of node.children) {
  402. child.visible = node.expanded && node.visible;
  403. this._refreshChildrenVisible(child);
  404. }
  405. }
  406. };
  407. /**
  408. * 设置节点是否展开, 并控制子节点可见
  409. * @param {Object} node
  410. * @param {Boolean} expanded
  411. */
  412. setExpanded (node, expanded) {
  413. node.expanded = expanded;
  414. this._refreshChildrenVisible(node);
  415. };
  416. /**
  417. * 提取节点key和索引数据
  418. * @param {Object} node - 节点
  419. * @returns {key}
  420. */
  421. getNodeKeyData (node) {
  422. const data = {};
  423. for (const key of this.setting.keys) {
  424. data[key] = node[key];
  425. }
  426. return data;
  427. };
  428. /**
  429. * 得到树结构构成id
  430. * @param node
  431. * @returns {*}
  432. */
  433. getNodeKey (node) {
  434. return node[this.setting.id];
  435. };
  436. /**
  437. * 递归 设置节点展开状态
  438. * @param {Array} nodes - 需要设置状态的节点
  439. * @param {Object} parent - nodes的父节点
  440. * @param {Function} checkFun - 判断节点展开状态的方法
  441. * @private
  442. */
  443. _recursiveExpand(nodes, parent, checkFun) {
  444. for (const node of nodes) {
  445. node.expanded = checkFun(node);
  446. node.visible = parent ? (parent.expanded && parent.visible) : true;
  447. this._recursiveExpand(node.children, node, checkFun);
  448. }
  449. }
  450. /**
  451. * 自定义展开规则
  452. * @param checkFun
  453. */
  454. expandByCustom(checkFun) {
  455. this._recursiveExpand(this.children, null, checkFun);
  456. }
  457. /**
  458. * 展开到第几层
  459. * @param {Number} level - 展开层数
  460. */
  461. expandByLevel(level) {
  462. // function recursiveExpand(nodes, parent) {
  463. // for (const node of nodes) {
  464. // node.expanded = node.level < level;
  465. // node.visible = parent ? (parent.expanded && parent.visible) : true;
  466. // recursiveExpand(node.children, node);
  467. // }
  468. // }
  469. // recursiveExpand(this.children);
  470. this.expandByCustom(function (n) {
  471. return n.level < level;
  472. });
  473. }
  474. }
  475. class MeasureTree extends BaseTree {
  476. addData (datas) {
  477. const loadedData = [];
  478. for (const data of datas) {
  479. let node = this.getItems(data[this.setting.id]);
  480. if (node) {
  481. for (const prop in node) {
  482. if (data[prop] !== undefined) {
  483. node[prop] = data[prop];
  484. }
  485. }
  486. loadedData.push(node);
  487. } else {
  488. const keyName = itemsPre + data[this.setting.id];
  489. const node = JSON.parse(JSON.stringify(data));
  490. this.items[keyName] = node;
  491. this.datas.push(node);
  492. node.expanded = false;
  493. node.visible = true;
  494. loadedData.push(node);
  495. }
  496. }
  497. this.sortTreeNode();
  498. for (const node of loadedData) {
  499. const children = node.children;
  500. if (!node.expanded && children.length > 0) {
  501. node.expanded = true;
  502. this._refreshChildrenVisible(node);
  503. }
  504. }
  505. return loadedData;
  506. }
  507. removeData (datas) {
  508. datas.sort(function (a, b) {
  509. return b.level - a.level;
  510. });
  511. const removeArrayData = function (array, data) {
  512. const index = array.indexOf(data);
  513. array.splice(index, 1);
  514. };
  515. for (const data of datas) {
  516. const node = this.getItems(data[this.setting.id]);
  517. if (node && this.getChildren(node).length === 0) {
  518. delete this.items[itemsPre + node[this.setting.id]];
  519. if (node[this.setting.pid] !== this.setting.rootId) {
  520. const parent = this.items[itemsPre + node[this.setting.pid]];
  521. removeArrayData(parent.children, node);
  522. }
  523. removeArrayData(this.datas, node);
  524. removeArrayData(this.nodes, node);
  525. }
  526. }
  527. };
  528. loadLeafData (data) {
  529. const datas = data instanceof Array ? data : [data];
  530. for (const d of datas) {
  531. let node = this.getItems(d[this.setting.id]);
  532. if (node && node.is_leaf) {
  533. for (const prop in node) {
  534. if (data[prop] !== undefined) {
  535. node[prop] = d[prop];
  536. }
  537. }
  538. }
  539. }
  540. };
  541. }
  542. class FxTree extends BaseTree {
  543. /**
  544. * 检查节点是否是最底层项目节
  545. * @param node
  546. * @returns {boolean}
  547. */
  548. isLeafXmj(node) {
  549. if (!node.code) {
  550. return false;
  551. }
  552. for (const child of node.children) {
  553. if (!child.b_code || child.b_code === '') {
  554. return false;
  555. }
  556. }
  557. return true;
  558. }
  559. /**
  560. * 查询最底层项目节(本身或父项)
  561. * @param {Object} node - 查询节点
  562. * @returns {Object}
  563. */
  564. getLeafXmjParent(node) {
  565. let parent = node;
  566. while (parent) {
  567. if (this.isLeafXmj(parent)) {
  568. return parent;
  569. } else {
  570. parent = this.getParent(parent);
  571. }
  572. }
  573. return null;
  574. }
  575. /**
  576. * 展开至最底层项目节
  577. */
  578. expandToLeafXmj() {
  579. const self = this;
  580. this.expandByCustom(function (node) {
  581. if (node.b_code && node.b_code !== '') {
  582. return false;
  583. } else {
  584. return !self.isLeafXmj(node);
  585. }
  586. })
  587. }
  588. }
  589. class LedgerTree extends FxTree {
  590. /**
  591. * 加载数据(动态),只加载不同部分
  592. * @param {Array} datas
  593. * @return {Array} 加载到树的数据
  594. * @privateA
  595. */
  596. _updateData (datas) {
  597. datas = datas instanceof Array ? datas : [datas];
  598. let loadedData = [];
  599. for (const data of datas) {
  600. let node = this.getItems(data[this.setting.id]);
  601. if (node) {
  602. for (const prop in node) {
  603. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  604. }
  605. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  606. if (prop === this.setting.pid) {
  607. loadedData.push(this.getItems(node[this.setting.pid]));
  608. loadedData.push(this.getItems(data[this.setting.pid]));
  609. }
  610. node[prop] = data[prop];
  611. }
  612. }
  613. loadedData.push(node);
  614. }
  615. }
  616. loadedData = _.uniq(loadedData);
  617. for (const node of loadedData) {
  618. node.children = this.getChildren(node);
  619. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  620. }
  621. this.sortTreeNode(true);
  622. return loadedData;
  623. };
  624. /**
  625. * 加载数据(动态),只加载不同部分
  626. * @param {Array} datas
  627. * @return {Array} 加载到树的数据
  628. * @privateA
  629. */
  630. _loadData (datas) {
  631. datas = datas instanceof Array ? datas : [datas];
  632. const loadedData = [], resortData = [];
  633. for (const data of datas) {
  634. let node = this.getItems(data[this.setting.id]);
  635. if (node) {
  636. const parent = this.getItems(node[this.setting.pid]);
  637. for (const prop in node) {
  638. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  639. node[prop] = data[prop];
  640. if (parent && resortData.indexOf(parent) === -1) {
  641. resortData.push(parent);
  642. }
  643. }
  644. }
  645. loadedData.push(node);
  646. } else {
  647. const keyName = itemsPre + data[this.setting.id];
  648. const node = JSON.parse(JSON.stringify(data));
  649. this.items[keyName] = node;
  650. this.datas.push(node);
  651. node.expanded = true;
  652. node.visible = true;
  653. loadedData.push(node);
  654. if (resortData.indexOf(node) === -1) {
  655. resortData.push(node);
  656. }
  657. const parent = this.getItems(node[this.setting.pid]);
  658. if (parent && resortData.indexOf(parent) === -1) {
  659. resortData.push(parent);
  660. }
  661. }
  662. }
  663. for (const node of resortData) {
  664. node.children = this.getChildren(node);
  665. }
  666. this.sortTreeNode(true);
  667. for (const node of loadedData) {
  668. if (!node.expanded) {
  669. this.setExpanded(node, true);
  670. }
  671. }
  672. return loadedData;
  673. };
  674. /**
  675. * 清理数据(动态)
  676. * @param datas
  677. * @private
  678. */
  679. _freeData (datas) {
  680. datas = datas instanceof Array ? datas : [datas];
  681. const freeDatas = [];
  682. const removeArrayData = function (array, data) {
  683. const index = array.indexOf(data);
  684. array.splice(index, 1);
  685. };
  686. for (const data of datas) {
  687. const node = this.getItems(data[this.setting.id]);
  688. if (node) {
  689. freeDatas.push(node);
  690. delete this.items[itemsPre + node[this.setting.id]];
  691. if (node[this.setting.pid] !== this.setting.rootId) {
  692. const parent = this.getItems(node[this.setting.pid]);
  693. if (parent) {
  694. removeArrayData(parent.children, node);
  695. }
  696. }
  697. removeArrayData(this.datas, node);
  698. removeArrayData(this.nodes, node);
  699. }
  700. }
  701. return freeDatas;
  702. };
  703. /**
  704. * 加载需展开的数据
  705. * @param {Array} datas
  706. * @returns {Array}
  707. * @private
  708. */
  709. _loadExpandData (datas) {
  710. datas = datas instanceof Array ? datas : [datas];
  711. const loadedData = [], existData = [], expandData = [], resortData = [];
  712. for (const data of datas) {
  713. let node = this.getItems(data[this.setting.id]);
  714. if (node) {
  715. existData.push(node);
  716. } else {
  717. const keyName = itemsPre + data[this.setting.id];
  718. const node = JSON.parse(JSON.stringify(data));
  719. this.items[keyName] = node;
  720. this.datas.push(node);
  721. node.expanded = false;
  722. node.visible = true;
  723. loadedData.push(node);
  724. if (resortData.indexOf(node) === -1) {
  725. resortData.push(node);
  726. }
  727. const parent = this.getItems(node[this.setting.pid]);
  728. if (parent && resortData.indexOf(parent) === -1) {
  729. resortData.push(parent);
  730. }
  731. }
  732. }
  733. for (const node of resortData) {
  734. node.children = this.getChildren(node);
  735. }
  736. this.sortTreeNode(true);
  737. for (const node of loadedData) {
  738. if (!node.expanded) {
  739. this.setExpanded(node, true);
  740. }
  741. }
  742. for (const node of existData) {
  743. const parent = this.getItems(node[this.setting.pid]);
  744. if (expandData.indexOf(parent) === -1) {
  745. expandData.push(parent);
  746. if (!parent.expanded) {
  747. this.setExpanded(parent, true);
  748. }
  749. }
  750. if (!node.expanded) {
  751. this.setExpanded(node, true);
  752. }
  753. }
  754. return [loadedData, expandData];
  755. };
  756. /**
  757. *
  758. * @param parent
  759. * @param node
  760. * @private
  761. */
  762. _getNodesParents(parents, nodes) {
  763. for (const node of nodes) {
  764. const parent = this.getParent(node);
  765. if (parent) {
  766. const paths = this.getFullPathNodes(parent.full_path);
  767. for (const p of paths) {
  768. if (parents.indexOf(p) === -1) {
  769. parents.push(p);
  770. }
  771. }
  772. }
  773. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  774. parents.push(node);
  775. }
  776. }
  777. }
  778. _getReCalcNodes(reCalcNodes, nodes) {
  779. for (const node of nodes) {
  780. const parent = this.getParent(node);
  781. if (parent) {
  782. const paths = this.getFullPathNodes(parent.full_path);
  783. for (const p of paths) {
  784. if (reCalcNodes.indexOf(p) === -1) {
  785. reCalcNodes.push(p);
  786. }
  787. }
  788. }
  789. // 最底层项目节,也需要计算
  790. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  791. reCalcNodes.push(node);
  792. //}
  793. }
  794. }
  795. /**
  796. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  797. *
  798. * @param data - 更新的数据 {update, create, delete}
  799. * @returns {{}}
  800. */
  801. loadPostData(data) {
  802. const result = {}, reCalcNodes = [];
  803. if (data.update) {
  804. result.update = this._updateData(data.update);
  805. this._getReCalcNodes(reCalcNodes, result.update);
  806. }
  807. if (data.create) {
  808. result.create = this._loadData(data.create);
  809. this._getReCalcNodes(reCalcNodes, result.create);
  810. }
  811. if (data.delete) {
  812. result.delete = this._freeData(data.delete);
  813. this._getReCalcNodes(reCalcNodes, result.delete);
  814. }
  815. reCalcNodes.sort((a, b) => {
  816. return b.level - a.level;
  817. });
  818. for (const node of reCalcNodes) {
  819. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  820. }
  821. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  822. return result;
  823. }
  824. /**
  825. * 以下方法需等待响应, 通过callback刷新界面
  826. */
  827. /**
  828. * 加载子节点
  829. * @param {Object} node
  830. * @param {function} callback
  831. */
  832. loadChildren (node, callback) {
  833. const self = this;
  834. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  835. postData(url, this.getNodeKeyData(node), function (data) {
  836. self._loadData(data);
  837. callback();
  838. });
  839. };
  840. /**
  841. * 树结构基本操作
  842. * @param {String} url - 请求地址
  843. * @param {Object} node - 操作节点
  844. * @param {String} type - 操作类型
  845. * @param {function} callback - 界面刷新
  846. */
  847. baseOperation (url, node, type, callback) {
  848. const self = this;
  849. const data = {
  850. id: node[this.setting.id],
  851. postType: type
  852. };
  853. postData(url, data, function (datas) {
  854. const refreshData = self.loadPostData(datas);
  855. callback(refreshData);
  856. });
  857. };
  858. /**
  859. * 节点数据编辑
  860. * @param {String} url - 请求地址
  861. * @param {Array|Object} updateData - 需更新的数据
  862. * @param {function} callback - 界面刷新
  863. */
  864. update (url, updateData, callback) {
  865. const self = this;
  866. postData(url, updateData, function (datas) {
  867. const refreshData = self.loadPostData(datas);
  868. callback(refreshData);
  869. }, function () {
  870. if (updateData instanceof Array) {
  871. const result = [];
  872. for (const data of updateData) {
  873. result.push(self.getItems(data[self.setting.id]));
  874. }
  875. callback(result)
  876. } else {
  877. callback([self.getItems(updateData[self.setting.id])]);
  878. }
  879. });
  880. };
  881. /**
  882. * 复制粘贴整块(目前仅可粘贴为后项)
  883. * @param {String} url - 请求地址
  884. * @param {Object} node - 操作节点
  885. * @param {Array} block - 被复制整块的节点列表
  886. * @param {function} callback - 界面刷新
  887. */
  888. pasteBlock (url, node, block, callback) {
  889. const self = this;
  890. const data = {
  891. id: node[self.setting.id],
  892. block: block
  893. };
  894. postData(url, data, function (datas) {
  895. const refreshData = self.loadPostData(datas);
  896. callback(refreshData);
  897. });
  898. };
  899. /**
  900. * 提交数据
  901. * @param {String} url - 请求地址
  902. * @param {Object} node - 当前选中节点
  903. * @param {Object} data - 提交的数据
  904. * @param {function} callback - 界面刷新
  905. */
  906. postData (url, node, data, callback) {
  907. const self = this;
  908. if (node) {
  909. data.id = node[self.setting.id];
  910. }
  911. postData(url, data, function (datas) {
  912. const refreshData = self.loadPostData(datas);
  913. callback(refreshData);
  914. // const result = {};
  915. // if (datas.update) {
  916. // result.update = self._updateData(datas.update);
  917. // }
  918. // if (datas.create) {
  919. // result.create = self._loadData(datas.create);
  920. // }
  921. // if (datas.delete) {
  922. // result.delete = self._freeData(datas.delete);
  923. // }
  924. // if (datas.expand) {
  925. // const [create, update] = self._loadExpandData(datas.expand);
  926. // result.create = result.create ? result.create.concat(create) : create;
  927. // result.expand = update;
  928. // }
  929. // callback(result);
  930. });
  931. };
  932. }
  933. class StageTree extends FxTree {
  934. /**
  935. * 构造函数
  936. */
  937. constructor (setting) {
  938. super(setting);
  939. // stage关联索引
  940. this.stageItems = {};
  941. }
  942. /**
  943. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  944. * @param datas
  945. */
  946. loadDatas (datas) {
  947. super.loadDatas(datas);
  948. // 清空旧数据
  949. this.stageItems = {};
  950. // 加载全部数据
  951. for (const data of this.datas) {
  952. const keyName = itemsPre + data[this.setting.stageId];
  953. this.stageItems[keyName] = data;
  954. }
  955. }
  956. getStageItems(id) {
  957. return this.stageItems[itemsPre + id];
  958. }
  959. loadStageData(datas, fieldPre, fields) {
  960. datas = datas instanceof Array ? datas : [datas];
  961. const loadedData = [];
  962. for (const data of datas) {
  963. let node = this.getStageItems(data.lid);
  964. if (node) {
  965. for (const prop of fields) {
  966. if (data[prop] !== undefined) {
  967. node[fieldPre + prop] = data[prop];
  968. }
  969. }
  970. loadedData.push(node);
  971. }
  972. }
  973. }
  974. loadPreStageData(datas) {
  975. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  976. }
  977. loadCurStageData(datas) {
  978. this.loadStageData(datas, '', this.setting.updateFields);
  979. }
  980. /**
  981. * 加载数据(动态),只加载不同部分
  982. * @param {Array} datas
  983. * @return {Array} 加载到树的数据
  984. * @privateA
  985. */
  986. _updateData (datas) {
  987. datas = datas instanceof Array ? datas : [datas];
  988. let loadedData = [];
  989. for (const data of datas) {
  990. let node = this.getItems(data[this.setting.id]);
  991. if (node) {
  992. for (const prop in node) {
  993. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  994. }
  995. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  996. if (prop === this.setting.pid) {
  997. loadedData.push(this.getItems(node[this.setting.pid]));
  998. loadedData.push(this.getItems(data[this.setting.pid]));
  999. }
  1000. node[prop] = data[prop];
  1001. }
  1002. }
  1003. loadedData.push(node);
  1004. }
  1005. }
  1006. loadedData = _.uniq(loadedData);
  1007. for (const node of loadedData) {
  1008. node.children = this.getChildren(node);
  1009. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1010. }
  1011. this.sortTreeNode(true);
  1012. return loadedData;
  1013. };
  1014. /**
  1015. * 加载数据(动态),只加载不同部分
  1016. * @param {Array} datas
  1017. * @return {Array} 加载到树的数据
  1018. * @privateA
  1019. */
  1020. _updateStageData (datas) {
  1021. datas = datas instanceof Array ? datas : [datas];
  1022. const loadedData = [];
  1023. for (const data of datas) {
  1024. let node = this.getStageItems(data.lid);
  1025. if (node) {
  1026. for (const prop of this.setting.updateFields) {
  1027. if (data[prop] !== undefined) {
  1028. node[prop] = data[prop];
  1029. }
  1030. }
  1031. loadedData.push(node);
  1032. }
  1033. }
  1034. return loadedData;
  1035. };
  1036. /**
  1037. *
  1038. * @param parent
  1039. * @param node
  1040. * @private
  1041. */
  1042. _getNodesParents(parents, nodes) {
  1043. for (const node of nodes) {
  1044. const parent = this.getParent(node);
  1045. if (parent) {
  1046. const paths = this.getFullPathNodes(parent.full_path);
  1047. for (const p of paths) {
  1048. if (parents.indexOf(p) === -1) {
  1049. parents.push(p);
  1050. }
  1051. }
  1052. }
  1053. if (node.children && node.children.length > 0) {
  1054. parents.push(node);
  1055. }
  1056. }
  1057. }
  1058. /**
  1059. * 提交数据至后端,返回的前端树结构应刷新的部分
  1060. * StageTree仅有更新CurStage部分,不需要增删
  1061. *
  1062. * @param data - 需要更新的数据
  1063. * @returns {Array} - 界面需要刷新的数据
  1064. */
  1065. loadPostStageData(data) {
  1066. let result, parents = [];
  1067. if (data.bills) {
  1068. result = this._updateData(data.bills);
  1069. this._getNodesParents(parents, result);
  1070. }
  1071. if (data.curStageData) {
  1072. result = this._updateStageData(data.curStageData);
  1073. this._getNodesParents(parents, result);
  1074. }
  1075. result = result ? result.concat(parents) : parents;
  1076. result.sort((a, b) => {
  1077. return b.level - a.level;
  1078. });
  1079. for (const node of result) {
  1080. treeCalc.calculateNode(this, node);
  1081. }
  1082. return result;
  1083. }
  1084. }
  1085. class MasterTree extends FxTree {
  1086. /**
  1087. * 构造函数
  1088. */
  1089. constructor (setting) {
  1090. super(setting);
  1091. // 关联索引
  1092. this.masterItems = {};
  1093. }
  1094. /**
  1095. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1096. * @param datas
  1097. */
  1098. loadDatas (datas) {
  1099. super.loadDatas(datas);
  1100. // 清空旧数据
  1101. this.masterItems = {};
  1102. // minor数据缓存
  1103. this.minorData = {};
  1104. // 加载全部数据
  1105. for (const data of this.datas) {
  1106. const keyName = itemsPre + data[this.setting.masterId];
  1107. this.masterItems[keyName] = data;
  1108. }
  1109. }
  1110. /**
  1111. * 根据关联id,查找节点
  1112. * @param id
  1113. * @returns {*}
  1114. */
  1115. getMasterItems(id) {
  1116. return this.masterItems[itemsPre + id];
  1117. }
  1118. /**
  1119. * 加载关联数据
  1120. *
  1121. * @param {Array|Object}datas - 需要关联的数据
  1122. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1123. * @param {Array} fields - 关联字段
  1124. * @returns {Array}
  1125. */
  1126. loadMinorData(datas, fieldSuf, fields) {
  1127. if (!datas) { return; }
  1128. datas = datas instanceof Array ? datas : [datas];
  1129. this.minorData[fieldSuf] = datas;
  1130. const loadedData = [];
  1131. for (const data of datas) {
  1132. let node = this.getMasterItems(data[this.setting.minorId]);
  1133. if (node) {
  1134. for (const prop of fields) {
  1135. if (data[prop] !== undefined) {
  1136. node[prop + fieldSuf] = data[prop];
  1137. }
  1138. }
  1139. loadedData.push(node);
  1140. }
  1141. }
  1142. return loadedData;
  1143. }
  1144. /**
  1145. * 展开至最底层项目节
  1146. */
  1147. expandByCalcFields() {
  1148. const self = this;
  1149. this.expandByCustom(function (node) {
  1150. for (const field of self.setting.calcFields) {
  1151. if (node[field]) {
  1152. return true;
  1153. }
  1154. }
  1155. return false;
  1156. })
  1157. }
  1158. }
  1159. if (type === 'base') {
  1160. return new BaseTree(setting);
  1161. } else if (type === 'stage') {
  1162. return new StageTree(setting);
  1163. } else if (type === 'ledger') {
  1164. return new LedgerTree(setting);
  1165. } else if (type === 'measure') {
  1166. return new MeasureTree(setting);
  1167. } else if (type === 'master') {
  1168. return new MasterTree(setting);
  1169. }
  1170. };
  1171. const treeCalc = {
  1172. getMaxLevel: function (tree) {
  1173. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  1174. },
  1175. calculateNode: function (tree, node) {
  1176. if (node.children && node.children.length > 0) {
  1177. const gather = node.children.reduce(function (rst, x) {
  1178. const result = {};
  1179. for (const cf of tree.setting.calcFields) {
  1180. result[cf] = ZhCalc.plus(rst[cf], x[cf]);
  1181. }
  1182. return result;
  1183. });
  1184. // 汇总子项
  1185. for (const cf of tree.setting.calcFields) {
  1186. if (gather[cf]) {
  1187. node[cf] = gather[cf];
  1188. } else {
  1189. node[cf] = null;
  1190. }
  1191. }
  1192. }
  1193. // 自身运算
  1194. if (tree.setting.calcFun) {
  1195. tree.setting.calcFun(node);
  1196. }
  1197. },
  1198. calculateLevelNode: function (tree, level) {
  1199. const nodes = tree.datas.filter((n) => { return n.level === level });
  1200. for (const node of nodes) {
  1201. this.calculateNode(tree, node);
  1202. }
  1203. },
  1204. calculateAll: function (tree) {
  1205. for (let i = this.getMaxLevel(tree); i >= 0; i--) {
  1206. this.calculateLevelNode(tree, i);
  1207. }
  1208. },
  1209. calculateParent: function (tree, node) {
  1210. const nodes = tree.getFullPathNodes(node.full_path);
  1211. nodes.sort((a, b) => {
  1212. return b.level - a.level;
  1213. });
  1214. for (const n of nodes) {
  1215. this.calculateNode(tree, n);
  1216. }
  1217. return nodes;
  1218. }
  1219. };