path_tree.js 44 KB

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