path_tree.js 45 KB

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