path_tree.js 42 KB

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