path_tree.js 44 KB

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