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 (data[prop] !== undefined && data[prop] !== node[prop]) {
  668. if (prop === this.setting.pid) {
  669. loadedData.push(this.getItems(node[this.setting.pid]));
  670. loadedData.push(this.getItems(data[this.setting.pid]));
  671. }
  672. if (prop === this.setting.order) {
  673. loadedData = loadedData.concat(this.getPosterity(node));
  674. }
  675. node[prop] = data[prop];
  676. }
  677. }
  678. loadedData.push(node);
  679. }
  680. }
  681. loadedData = _.uniq(loadedData);
  682. for (const node of loadedData) {
  683. node.children = this.getChildren(node);
  684. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  685. }
  686. this.sortTreeNode(true);
  687. return loadedData;
  688. };
  689. /**
  690. * 加载数据(动态),只加载不同部分
  691. * @param {Array} datas
  692. * @return {Array} 加载到树的数据
  693. * @privateA
  694. */
  695. _loadData (datas) {
  696. datas = datas instanceof Array ? datas : [datas];
  697. const loadedData = [], resortData = [];
  698. for (const data of datas) {
  699. let node = this.getItems(data[this.setting.id]);
  700. if (node) {
  701. const parent = this.getItems(node[this.setting.pid]);
  702. for (const prop in node) {
  703. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  704. node[prop] = data[prop];
  705. if (parent && resortData.indexOf(parent) === -1) {
  706. resortData.push(parent);
  707. }
  708. }
  709. }
  710. loadedData.push(node);
  711. } else {
  712. const keyName = itemsPre + data[this.setting.id];
  713. const node = JSON.parse(JSON.stringify(data));
  714. this.items[keyName] = node;
  715. this.datas.push(node);
  716. node.expanded = true;
  717. node.visible = true;
  718. loadedData.push(node);
  719. if (resortData.indexOf(node) === -1) {
  720. resortData.push(node);
  721. }
  722. const parent = this.getItems(node[this.setting.pid]);
  723. if (parent && resortData.indexOf(parent) === -1) {
  724. resortData.push(parent);
  725. }
  726. }
  727. }
  728. for (const node of resortData) {
  729. node.children = this.getChildren(node);
  730. }
  731. this.sortTreeNode(true);
  732. for (const node of loadedData) {
  733. if (!node.expanded) {
  734. this.setExpanded(node, true);
  735. }
  736. }
  737. return loadedData;
  738. };
  739. /**
  740. * 清理数据(动态)
  741. * @param datas
  742. * @private
  743. */
  744. _freeData (datas) {
  745. datas = datas instanceof Array ? datas : [datas];
  746. const freeDatas = [];
  747. const removeArrayData = function (array, data) {
  748. const index = array.indexOf(data);
  749. array.splice(index, 1);
  750. };
  751. for (const data of datas) {
  752. const node = this.getItems(data[this.setting.id]);
  753. if (node) {
  754. freeDatas.push(node);
  755. delete this.items[itemsPre + node[this.setting.id]];
  756. if (node[this.setting.pid] !== this.setting.rootId) {
  757. const parent = this.getItems(node[this.setting.pid]);
  758. if (parent) {
  759. removeArrayData(parent.children, node);
  760. }
  761. }
  762. removeArrayData(this.datas, node);
  763. removeArrayData(this.nodes, node);
  764. }
  765. }
  766. return freeDatas;
  767. };
  768. /**
  769. * 加载需展开的数据
  770. * @param {Array} datas
  771. * @returns {Array}
  772. * @private
  773. */
  774. _loadExpandData (datas) {
  775. datas = datas instanceof Array ? datas : [datas];
  776. const loadedData = [], existData = [], expandData = [], resortData = [];
  777. for (const data of datas) {
  778. let node = this.getItems(data[this.setting.id]);
  779. if (node) {
  780. existData.push(node);
  781. } else {
  782. const keyName = itemsPre + data[this.setting.id];
  783. const node = JSON.parse(JSON.stringify(data));
  784. this.items[keyName] = node;
  785. this.datas.push(node);
  786. node.expanded = false;
  787. node.visible = true;
  788. loadedData.push(node);
  789. if (resortData.indexOf(node) === -1) {
  790. resortData.push(node);
  791. }
  792. const parent = this.getItems(node[this.setting.pid]);
  793. if (parent && resortData.indexOf(parent) === -1) {
  794. resortData.push(parent);
  795. }
  796. }
  797. }
  798. for (const node of resortData) {
  799. node.children = this.getChildren(node);
  800. }
  801. this.sortTreeNode(true);
  802. for (const node of loadedData) {
  803. if (!node.expanded) {
  804. this.setExpanded(node, true);
  805. }
  806. }
  807. for (const node of existData) {
  808. const parent = this.getItems(node[this.setting.pid]);
  809. if (expandData.indexOf(parent) === -1) {
  810. expandData.push(parent);
  811. if (!parent.expanded) {
  812. this.setExpanded(parent, true);
  813. }
  814. }
  815. if (!node.expanded) {
  816. this.setExpanded(node, true);
  817. }
  818. }
  819. return [loadedData, expandData];
  820. };
  821. /**
  822. *
  823. * @param parent
  824. * @param node
  825. * @private
  826. */
  827. _getNodesParents(parents, nodes) {
  828. for (const node of nodes) {
  829. const parent = this.getParent(node);
  830. if (parent) {
  831. const paths = this.getFullPathNodes(parent.full_path);
  832. for (const p of paths) {
  833. if (parents.indexOf(p) === -1) {
  834. parents.push(p);
  835. }
  836. }
  837. }
  838. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  839. parents.push(node);
  840. }
  841. }
  842. }
  843. _getReCalcNodes(reCalcNodes, nodes) {
  844. for (const node of nodes) {
  845. const parent = this.getParent(node);
  846. if (parent) {
  847. const paths = this.getFullPathNodes(parent.full_path);
  848. for (const p of paths) {
  849. if (reCalcNodes.indexOf(p) === -1) {
  850. reCalcNodes.push(p);
  851. }
  852. }
  853. }
  854. // 最底层项目节,也需要计算
  855. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  856. reCalcNodes.push(node);
  857. //}
  858. }
  859. }
  860. /**
  861. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  862. *
  863. * @param data - 更新的数据 {update, create, delete}
  864. * @returns {{}}
  865. */
  866. loadPostData(data) {
  867. const result = {}, reCalcNodes = [];
  868. if (data.update) {
  869. result.update = this._updateData(data.update);
  870. this._getReCalcNodes(reCalcNodes, result.update);
  871. }
  872. if (data.create) {
  873. result.create = this._loadData(data.create);
  874. this._getReCalcNodes(reCalcNodes, result.create);
  875. }
  876. if (data.delete) {
  877. result.delete = this._freeData(data.delete);
  878. this._getReCalcNodes(reCalcNodes, result.delete);
  879. }
  880. reCalcNodes.sort((a, b) => {
  881. return b.level - a.level;
  882. });
  883. for (const node of reCalcNodes) {
  884. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  885. }
  886. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  887. return result;
  888. }
  889. /**
  890. * 以下方法需等待响应, 通过callback刷新界面
  891. */
  892. /**
  893. * 加载子节点
  894. * @param {Object} node
  895. * @param {function} callback
  896. */
  897. loadChildren (node, callback) {
  898. const self = this;
  899. const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
  900. postData(url, this.getNodeKeyData(node), function (data) {
  901. self._loadData(data);
  902. callback();
  903. });
  904. };
  905. /**
  906. * 树结构基本操作
  907. * @param {String} url - 请求地址
  908. * @param {Object} node - 操作节点
  909. * @param {String} type - 操作类型
  910. * @param {function} callback - 界面刷新
  911. */
  912. baseOperation (url, node, type, callback) {
  913. const self = this;
  914. const data = {
  915. id: node[this.setting.id],
  916. postType: type
  917. };
  918. postData(url, data, function (datas) {
  919. const refreshData = self.loadPostData(datas);
  920. callback(refreshData);
  921. });
  922. };
  923. /**
  924. * 节点数据编辑
  925. * @param {String} url - 请求地址
  926. * @param {Array|Object} updateData - 需更新的数据
  927. * @param {function} callback - 界面刷新
  928. */
  929. update (url, updateData, callback) {
  930. const self = this;
  931. postData(url, updateData, function (datas) {
  932. const refreshData = self.loadPostData(datas);
  933. callback(refreshData);
  934. }, function () {
  935. if (updateData instanceof Array) {
  936. const result = [];
  937. for (const data of updateData) {
  938. result.push(self.getItems(data[self.setting.id]));
  939. }
  940. callback(result)
  941. } else {
  942. callback([self.getItems(updateData[self.setting.id])]);
  943. }
  944. });
  945. };
  946. /**
  947. * 复制粘贴整块(目前仅可粘贴为后项)
  948. * @param {String} url - 请求地址
  949. * @param {Object} node - 操作节点
  950. * @param {Array} block - 被复制整块的节点列表
  951. * @param {function} callback - 界面刷新
  952. */
  953. pasteBlock (url, node, block, callback) {
  954. const self = this;
  955. const data = {
  956. id: node[self.setting.id],
  957. block: block
  958. };
  959. postData(url, data, function (datas) {
  960. const refreshData = self.loadPostData(datas);
  961. callback(refreshData);
  962. });
  963. };
  964. /**
  965. * 提交数据
  966. * @param {String} url - 请求地址
  967. * @param {Object} node - 当前选中节点
  968. * @param {Object} data - 提交的数据
  969. * @param {function} callback - 界面刷新
  970. */
  971. postData (url, node, data, callback) {
  972. const self = this;
  973. if (node) {
  974. data.id = node[self.setting.id];
  975. }
  976. postData(url, data, function (datas) {
  977. const refreshData = self.loadPostData(datas);
  978. callback(refreshData);
  979. // const result = {};
  980. // if (datas.update) {
  981. // result.update = self._updateData(datas.update);
  982. // }
  983. // if (datas.create) {
  984. // result.create = self._loadData(datas.create);
  985. // }
  986. // if (datas.delete) {
  987. // result.delete = self._freeData(datas.delete);
  988. // }
  989. // if (datas.expand) {
  990. // const [create, update] = self._loadExpandData(datas.expand);
  991. // result.create = result.create ? result.create.concat(create) : create;
  992. // result.expand = update;
  993. // }
  994. // callback(result);
  995. });
  996. };
  997. }
  998. class StageTree extends FxTree {
  999. /**
  1000. * 构造函数
  1001. */
  1002. constructor (setting) {
  1003. super(setting);
  1004. // stage关联索引
  1005. this.stageItems = {};
  1006. }
  1007. /**
  1008. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1009. * @param datas
  1010. */
  1011. loadDatas (datas) {
  1012. super.loadDatas(datas);
  1013. // 清空旧数据
  1014. this.stageItems = {};
  1015. // 加载全部数据
  1016. for (const data of this.datas) {
  1017. const keyName = itemsPre + data[this.setting.stageId];
  1018. this.stageItems[keyName] = data;
  1019. }
  1020. }
  1021. getStageItems(id) {
  1022. return this.stageItems[itemsPre + id];
  1023. }
  1024. loadStageData(datas, fieldPre, fields) {
  1025. datas = datas instanceof Array ? datas : [datas];
  1026. const loadedData = [];
  1027. for (const data of datas) {
  1028. let node = this.getStageItems(data.lid);
  1029. if (node) {
  1030. for (const prop of fields) {
  1031. if (data[prop] !== undefined) {
  1032. node[fieldPre + prop] = data[prop];
  1033. }
  1034. }
  1035. loadedData.push(node);
  1036. }
  1037. }
  1038. }
  1039. loadPreStageData(datas) {
  1040. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1041. }
  1042. loadCurStageData(datas) {
  1043. this.loadStageData(datas, '', this.setting.updateFields);
  1044. }
  1045. /**
  1046. * 加载数据(动态),只加载不同部分
  1047. * @param {Array} datas
  1048. * @return {Array} 加载到树的数据
  1049. * @privateA
  1050. */
  1051. _updateData (datas) {
  1052. datas = datas instanceof Array ? datas : [datas];
  1053. let loadedData = [];
  1054. for (const data of datas) {
  1055. let node = this.getItems(data[this.setting.id]);
  1056. if (node) {
  1057. for (const prop in node) {
  1058. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1059. }
  1060. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1061. if (prop === this.setting.pid) {
  1062. loadedData.push(this.getItems(node[this.setting.pid]));
  1063. loadedData.push(this.getItems(data[this.setting.pid]));
  1064. }
  1065. node[prop] = data[prop];
  1066. }
  1067. }
  1068. loadedData.push(node);
  1069. }
  1070. }
  1071. loadedData = _.uniq(loadedData);
  1072. for (const node of loadedData) {
  1073. node.children = this.getChildren(node);
  1074. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1075. }
  1076. this.sortTreeNode(true);
  1077. return loadedData;
  1078. };
  1079. /**
  1080. * 加载数据(动态),只加载不同部分
  1081. * @param {Array} datas
  1082. * @return {Array} 加载到树的数据
  1083. * @privateA
  1084. */
  1085. _updateStageData (datas) {
  1086. datas = datas instanceof Array ? datas : [datas];
  1087. const loadedData = [];
  1088. for (const data of datas) {
  1089. let node = this.getStageItems(data.lid);
  1090. if (node) {
  1091. for (const prop of this.setting.updateFields) {
  1092. if (data[prop] !== undefined) {
  1093. node[prop] = data[prop];
  1094. }
  1095. }
  1096. loadedData.push(node);
  1097. }
  1098. }
  1099. return loadedData;
  1100. };
  1101. /**
  1102. *
  1103. * @param parent
  1104. * @param node
  1105. * @private
  1106. */
  1107. _getNodesParents(parents, nodes) {
  1108. for (const node of nodes) {
  1109. const parent = this.getParent(node);
  1110. if (parent) {
  1111. const paths = this.getFullPathNodes(parent.full_path);
  1112. for (const p of paths) {
  1113. if (parents.indexOf(p) === -1) {
  1114. parents.push(p);
  1115. }
  1116. }
  1117. }
  1118. if (node.children && node.children.length > 0) {
  1119. parents.push(node);
  1120. }
  1121. }
  1122. }
  1123. /**
  1124. * 提交数据至后端,返回的前端树结构应刷新的部分
  1125. * StageTree仅有更新CurStage部分,不需要增删
  1126. *
  1127. * @param data - 需要更新的数据
  1128. * @returns {Array} - 界面需要刷新的数据
  1129. */
  1130. loadPostStageData(data) {
  1131. let result, parents = [];
  1132. if (data.bills) {
  1133. result = this._updateData(data.bills);
  1134. this._getNodesParents(parents, result);
  1135. }
  1136. if (data.curStageData) {
  1137. result = this._updateStageData(data.curStageData);
  1138. this._getNodesParents(parents, result);
  1139. }
  1140. result = result ? result.concat(parents) : parents;
  1141. result.sort((a, b) => {
  1142. return b.level - a.level;
  1143. });
  1144. for (const node of result) {
  1145. treeCalc.calculateNode(this, node);
  1146. }
  1147. return result;
  1148. }
  1149. }
  1150. class MasterTree extends FxTree {
  1151. /**
  1152. * 构造函数
  1153. */
  1154. constructor (setting) {
  1155. super(setting);
  1156. // 关联索引
  1157. this.masterItems = {};
  1158. }
  1159. /**
  1160. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1161. * @param datas
  1162. */
  1163. loadDatas (datas) {
  1164. super.loadDatas(datas);
  1165. // 清空旧数据
  1166. this.masterItems = {};
  1167. // minor数据缓存
  1168. this.minorData = {};
  1169. // 加载全部数据
  1170. for (const data of this.datas) {
  1171. const keyName = itemsPre + data[this.setting.masterId];
  1172. this.masterItems[keyName] = data;
  1173. }
  1174. }
  1175. /**
  1176. * 根据关联id,查找节点
  1177. * @param id
  1178. * @returns {*}
  1179. */
  1180. getMasterItems(id) {
  1181. return this.masterItems[itemsPre + id];
  1182. }
  1183. /**
  1184. * 加载关联数据
  1185. *
  1186. * @param {Array|Object}datas - 需要关联的数据
  1187. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1188. * @param {Array} fields - 关联字段
  1189. * @returns {Array}
  1190. */
  1191. loadMinorData(datas, fieldSuf, fields) {
  1192. if (!datas) { return; }
  1193. datas = datas instanceof Array ? datas : [datas];
  1194. this.minorData[fieldSuf] = datas;
  1195. const loadedData = [];
  1196. for (const data of datas) {
  1197. let node = this.getMasterItems(data[this.setting.minorId]);
  1198. if (node) {
  1199. for (const prop of fields) {
  1200. if (data[prop] !== undefined) {
  1201. node[prop + fieldSuf] = data[prop];
  1202. }
  1203. }
  1204. loadedData.push(node);
  1205. }
  1206. }
  1207. return loadedData;
  1208. }
  1209. }
  1210. if (type === 'base') {
  1211. return new BaseTree(setting);
  1212. } else if (type === 'fx') {
  1213. return new FxTree(setting);
  1214. } else if (type === 'stage') {
  1215. return new StageTree(setting);
  1216. } else if (type === 'ledger') {
  1217. return new LedgerTree(setting);
  1218. } else if (type === 'measure') {
  1219. return new MeasureTree(setting);
  1220. } else if (type === 'master') {
  1221. return new MasterTree(setting);
  1222. }
  1223. };
  1224. const treeCalc = {
  1225. mapTreeNode: function (tree) {
  1226. let map = {}, maxLevel = 0;
  1227. for (const node of tree.nodes) {
  1228. let levelArr = map[node.level];
  1229. if (!levelArr) {
  1230. levelArr = [];
  1231. map[node.level] = levelArr;
  1232. }
  1233. if (node.level > maxLevel) {
  1234. maxLevel = node.level;
  1235. }
  1236. levelArr.push(node);
  1237. }
  1238. return [maxLevel, map];
  1239. },
  1240. getMaxLevel: function (tree) {
  1241. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  1242. },
  1243. calculateNode: function (tree, node) {
  1244. if (node.children && node.children.length > 0) {
  1245. const gather = node.children.reduce(function (rst, x) {
  1246. const result = {};
  1247. for (const cf of tree.setting.calcFields) {
  1248. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1249. }
  1250. return result;
  1251. });
  1252. // 汇总子项
  1253. for (const cf of tree.setting.calcFields) {
  1254. if (gather[cf]) {
  1255. node[cf] = gather[cf];
  1256. } else {
  1257. node[cf] = null;
  1258. }
  1259. }
  1260. }
  1261. // 自身运算
  1262. if (tree.setting.calcFun) {
  1263. tree.setting.calcFun(node);
  1264. }
  1265. },
  1266. calculateLevelNode: function (tree, level) {
  1267. const nodes = tree.datas.filter((n) => { return n.level === level });
  1268. for (const node of nodes) {
  1269. this.calculateNode(tree, node);
  1270. }
  1271. },
  1272. calculateAll: function (tree) {
  1273. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1274. for (let i = maxLevel; i >= 0; i--) {
  1275. const levelNodes = levelMap[i];
  1276. if (levelNodes && levelNodes.length > 0) {
  1277. for (const node of levelNodes) {
  1278. this.calculateNode(tree, node);
  1279. }
  1280. }
  1281. }
  1282. },
  1283. calculateParent: function (tree, node) {
  1284. const nodes = tree.getFullPathNodes(node.full_path);
  1285. nodes.sort((a, b) => {
  1286. return b.level - a.level;
  1287. });
  1288. for (const n of nodes) {
  1289. this.calculateNode(tree, n);
  1290. }
  1291. return nodes;
  1292. }
  1293. };