path_tree.js 43 KB

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