path_tree.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /**
  2. * (需使用 decimal.min.js, zh_calc.js)
  3. *
  4. * 构建pathTree
  5. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  6. *
  7. * 所台账结构数据均用到该文件,请勿随意修改。
  8. * @param {Object} setting - 设置
  9. * @returns {PathTree}
  10. */
  11. 'use strict';
  12. const itemsPre = 'id_';
  13. class PosData {
  14. /**
  15. * 构造函数
  16. * @param {id|Number, masterId|Number} setting
  17. */
  18. constructor(setting) {
  19. // 无索引
  20. this.datas = [];
  21. // 以key为索引
  22. this.items = {};
  23. // 以分类id为索引的有序
  24. this.ledgerPos = {};
  25. // pos设置
  26. this.setting = setting;
  27. }
  28. /**
  29. * 加载部位明细数据
  30. * @param datas
  31. */
  32. loadDatas(datas) {
  33. this.datas = datas;
  34. this.items = {};
  35. this.ledgerPos = {};
  36. for (const data of this.datas) {
  37. const key = itemsPre + data[this.setting.id];
  38. this.items[key] = data;
  39. const masterKey = itemsPre + data[this.setting.ledgerId];
  40. if (!this.ledgerPos[masterKey]) {
  41. this.ledgerPos[masterKey] = [];
  42. }
  43. this.ledgerPos[masterKey].push(data);
  44. }
  45. for (const prop in this.ledgerPos) {
  46. this.resortLedgerPos(this.ledgerPos[prop]);
  47. }
  48. }
  49. /**
  50. * 更新数据
  51. * @param datas
  52. */
  53. updateDatas(data) {
  54. const datas = data instanceof Array ? data : [data];
  55. const result = { create: [], update: [] }, resort = [];
  56. for (const d of datas) {
  57. const key = itemsPre + d[this.setting.id];
  58. if (!this.items[key]) {
  59. this.datas.push(d);
  60. this.items[key] = d;
  61. const masterKey = itemsPre + d[this.setting.ledgerId];
  62. if (!this.ledgerPos[masterKey]) {
  63. this.ledgerPos[masterKey] = [];
  64. }
  65. this.ledgerPos[masterKey].push(d);
  66. result.create.push(d);
  67. } else {
  68. const pos = this.items[key];
  69. for (const prop in d) {
  70. pos[prop] = d[prop];
  71. }
  72. result.update.push(pos);
  73. }
  74. const masterKey = itemsPre + d[this.setting.ledgerId];
  75. if (resort.indexOf(masterKey) === -1) {
  76. resort.push(masterKey);
  77. }
  78. }
  79. if (this.setting.calcFun) {
  80. for (const u of result.update) {
  81. this.setting.calcFun(u);
  82. }
  83. for (const c of result.create) {
  84. this.setting.calcFun(c);
  85. }
  86. }
  87. for (const s of resort) {
  88. this.resortLedgerPos(this.ledgerPos[s]);
  89. }
  90. return result;
  91. }
  92. /**
  93. * 移除数据
  94. * @param datas
  95. */
  96. removeDatas(data) {
  97. if (!data) { return; }
  98. const datas = data instanceof Array ? data : [data];
  99. for (let i = datas.length - 1; i >= 0; i--) {
  100. const id = datas[i];
  101. const d = this.getPos(id);
  102. this.datas.splice(this.datas.indexOf(d), 1);
  103. const key = itemsPre + d[this.setting.id];
  104. delete this.items[key];
  105. const masterKey = itemsPre + d[this.setting.ledgerId];
  106. const range = this.ledgerPos[masterKey];
  107. range.splice(range.indexOf(d), 1);
  108. if (range.length === 0) {
  109. delete this.ledgerPos[masterKey];
  110. }
  111. }
  112. }
  113. /**
  114. * 移除数据 - 根据分类id
  115. * @param mid
  116. */
  117. removeDatasByMasterId(mid) {
  118. const masterKey = itemsPre + mid;
  119. const range = this.ledgerPos[masterKey];
  120. if (range) {
  121. delete this.ledgerPos[masterKey];
  122. for (const r of range) {
  123. this.datas.splice(this.datas.indexOf(r), 1);
  124. const key = itemsPre + r[this.setting.id];
  125. delete this.items[key];
  126. }
  127. }
  128. }
  129. getPos(id) {
  130. return this.items[itemsPre + id];
  131. }
  132. getLedgerPos(mid) {
  133. return this.ledgerPos[itemsPre + mid];
  134. }
  135. resortLedgerPos(ledgerPos) {
  136. if (ledgerPos instanceof Array) {
  137. ledgerPos.sort(function (a, b) {
  138. return a.porder - b.porder;
  139. })
  140. }
  141. }
  142. /**
  143. * 计算全部
  144. */
  145. calculateAll() {
  146. if (!this.setting.calcFun) { return; }
  147. for (const pos of this.datas) {
  148. this.setting.calcFun(pos);
  149. }
  150. }
  151. }
  152. class StagePosData extends PosData {
  153. loadStageData(datas, fieldPre, fields) {
  154. if (!datas) { return; }
  155. datas = datas instanceof Array ? datas : [datas];
  156. const loadedData = [];
  157. for (const data of datas) {
  158. let node = this.getPos(data.pid);
  159. if (node) {
  160. for (const prop of fields) {
  161. if (data[prop] !== undefined) {
  162. node[fieldPre + prop] = data[prop];
  163. }
  164. }
  165. if (this.setting.calcFun) {
  166. this.setting.calcFun(node);
  167. }
  168. loadedData.push(node);
  169. }
  170. }
  171. }
  172. loadPreStageData(datas) {
  173. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  174. }
  175. loadCurStageData(datas) {
  176. this.loadStageData(datas, '', this.setting.updateFields);
  177. }
  178. }
  179. class MasterPosData extends PosData {
  180. /**
  181. * 构造函数
  182. * @param {id|Number, masterId|Number} setting
  183. */
  184. constructor(setting) {
  185. super(setting);
  186. // 关联索引
  187. this.masterItems = {};
  188. }
  189. /**
  190. * 加载主数据
  191. * @param datas
  192. */
  193. loadDatas(datas) {
  194. super.loadDatas(datas);
  195. // 清空旧数据
  196. this.masterItems = {};
  197. // minor数据缓存
  198. this.minorData = {};
  199. // 加载全部数据
  200. for (const data of this.datas) {
  201. const keyName = itemsPre + data[this.setting.masterId];
  202. this.masterItems[keyName] = data;
  203. }
  204. }
  205. /**
  206. * 根据关联id,查找节点
  207. * @param id
  208. * @returns {*}
  209. */
  210. getMasterItems(id) {
  211. return this.masterItems[itemsPre + id];
  212. }
  213. /**
  214. * 加载关联数据
  215. *
  216. * @param {Array|Object}datas - 需要关联的数据
  217. * @param {String} fieldPre - 关联字段前缀(关联结果)
  218. * @param {Array} fields - 关联字段
  219. * @returns {Array}
  220. */
  221. loadMinorData(datas, fieldSuf, fields) {
  222. if (!datas) return;
  223. datas = datas instanceof Array ? datas : [datas];
  224. this.minorData[fieldSuf] = datas;
  225. const loadedData = [];
  226. for (const data of datas) {
  227. let node = this.getMasterItems(data[this.setting.minorId]);
  228. if (node) {
  229. for (const prop of fields) {
  230. if (data[prop] !== undefined) {
  231. node[prop + fieldSuf] = data[prop];
  232. }
  233. }
  234. loadedData.push(node);
  235. }
  236. }
  237. return loadedData;
  238. }
  239. }
  240. const createNewPathTree = function (type, setting) {
  241. class BaseTree {
  242. /**
  243. * 构造函数
  244. */
  245. constructor(setting) {
  246. const self = this;
  247. // 无索引
  248. this.datas = [];
  249. // 以key为索引indexedDB
  250. this.items = {};
  251. // 以排序为索引
  252. this.nodes = [];
  253. // 根节点
  254. this.children = [];
  255. // 树设置
  256. this.setting = setting;
  257. if (this.setting.markFoldKey) {
  258. const markStr = getLocalCache(this.setting.markFoldKey);
  259. const markData = markStr ? markStr.split('|') : ['', ''];
  260. this.markFold = markData[0] === this.setting.markFoldSubKey && markData[1]
  261. ? _.map(markData[1].split(','), _.toInteger) : [];
  262. }
  263. // if (this.setting.treeCacheKey) {
  264. // localforage.getItem(this.setting.treeCacheKey).then(function (v) {
  265. // self.markFold = v && v.markFold ? v.markFold : [];
  266. // });
  267. // }
  268. }
  269. /**
  270. * 树结构根据显示排序
  271. */
  272. sortTreeNode(isResort) {
  273. const self = this;
  274. const addSortNodes = function (nodes) {
  275. if (!nodes) { return }
  276. for (let i = 0; i < nodes.length; i++) {
  277. self.nodes.push(nodes[i]);
  278. nodes[i].index = self.nodes.length - 1;
  279. if (!isResort) {
  280. nodes[i].children = self.getChildren(nodes[i]);
  281. } else {
  282. nodes[i].children.sort(function (a, b) {
  283. return a.order - b.order;
  284. })
  285. }
  286. addSortNodes(nodes[i].children);
  287. }
  288. };
  289. this.nodes = [];
  290. if (!isResort) {
  291. this.children = this.getChildren();
  292. } else {
  293. this.children.sort(function (a, b) {
  294. return a.order - b.order;
  295. })
  296. }
  297. addSortNodes(this.children);
  298. }
  299. /**
  300. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  301. * @param datas
  302. */
  303. loadDatas(datas) {
  304. self = this;
  305. // 清空旧数据
  306. this.items = {};
  307. this.nodes = [];
  308. this.datas = [];
  309. this.children = [];
  310. // 加载全部数据
  311. datas.sort(function (a, b) {
  312. return a.level - b.level;
  313. });
  314. for (const data of datas) {
  315. const keyName = itemsPre + data[this.setting.id];
  316. if (!this.items[keyName]) {
  317. const item = JSON.parse(JSON.stringify(data));
  318. item.children = [];
  319. item.expanded = true;
  320. item.visible = true;
  321. this.items[keyName] = item;
  322. this.datas.push(item);
  323. if (item[setting.pid] === setting.rootId) {
  324. this.children.push(item);
  325. } else {
  326. const parent = this.getParent(item);
  327. if (parent) {
  328. parent.children.push(item);
  329. }
  330. }
  331. }
  332. }
  333. this.children.sort(function (a, b) {
  334. return a.order - b.order;
  335. });
  336. this.sortTreeNode(true);
  337. if (this.setting.autoExpand >= 0) this.expandByLevel(this.setting.autoExpand);
  338. if (this.setting.markFoldKey) this.expandByCustom(function (node) {
  339. return self.markFold.indexOf(node[self.setting.id]) === -1;
  340. });
  341. }
  342. getItemsByIndex(index) {
  343. return this.nodes[index];
  344. }
  345. /**
  346. * 根据id获取树结构节点数据
  347. * @param {Number} id
  348. * @returns {Object}
  349. */
  350. getItems(id) {
  351. return this.items[itemsPre + id];
  352. };
  353. getNodeIndex(node) {
  354. return this.nodes.indexOf(node);
  355. }
  356. /**
  357. * 查找node的parent
  358. * @param {Object} node
  359. * @returns {Object}
  360. */
  361. getParent(node) {
  362. return this.getItems(node[this.setting.pid]);
  363. };
  364. getAllParents(node) {
  365. const parents = [];
  366. if (node.full_path && node.full_path !== '') {
  367. const parentIds = node.full_path.split('-');
  368. for (const id of parentIds) {
  369. if (id !== node[this.setting.id]) {
  370. parents.push(this.getItems(id));
  371. }
  372. }
  373. } else {
  374. let vP = this.getParent(node);
  375. while (vP) {
  376. parents.push(vP);
  377. vP = this.getParent(vP);
  378. }
  379. }
  380. return parents;
  381. }
  382. /**
  383. * 查找node的前兄弟节点
  384. * @param node
  385. * @returns {*}
  386. */
  387. getPreSiblingNode(node) {
  388. if (!node) return null;
  389. const parent = this.getParent(node);
  390. const siblings = parent ? parent.children : this.children;
  391. const index = siblings.indexOf(node);
  392. return (index > 0) ? siblings[index - 1] : null;
  393. }
  394. /**
  395. * 查找node的后兄弟节点
  396. * @param node
  397. * @returns {*}
  398. */
  399. getNextSiblingNode(node) {
  400. const parent = this.getParent(node);
  401. const siblings = parent ? parent.children : this.children;
  402. const index = siblings.indexOf(node);
  403. if (index >= 0 && index < siblings.length - 1) {
  404. return siblings[index + 1];
  405. } else {
  406. return null;
  407. }
  408. }
  409. /**
  410. * 根据path查找完整节点
  411. * @param {Number} path
  412. */
  413. getFullPathNodes(path) {
  414. const self = this, ids = path.split('-');
  415. if (ids.length > 0) {
  416. return this.nodes.filter((x) => {
  417. return ids.indexOf('' + x[self.setting.id]) >= 0;
  418. });
  419. } else {
  420. return [];
  421. }
  422. };
  423. /**
  424. * 查询node的已下载子节点
  425. * @param {Object} node
  426. * @returns {Array}
  427. */
  428. getChildren(node) {
  429. const setting = this.setting;
  430. const pid = node ? node[setting.id] : setting.rootId;
  431. const children = this.datas.filter(function (x) {
  432. return x[setting.pid] === pid;
  433. });
  434. children.sort(function (a, b) {
  435. return a.order - b.order;
  436. });
  437. return children;
  438. };
  439. /**
  440. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  441. * @param node
  442. * @returns {*}
  443. * @private
  444. */
  445. _recursiveGetPosterity(node) {
  446. let posterity = node.children;
  447. for (const c of node.children) {
  448. posterity = posterity.concat(this._recursiveGetPosterity(c));
  449. }
  450. return posterity;
  451. };
  452. /**
  453. * 查询node的已下载的全部后代
  454. * @param {Object} node
  455. * @returns {Array}
  456. */
  457. getPosterity(node) {
  458. const self = this;
  459. let posterity;
  460. if (node.full_path !== '') {
  461. const reg = new RegExp('^' + node.full_path + '-');
  462. posterity = this.datas.filter(function (x) {
  463. return reg.test(x.full_path);
  464. });
  465. } else {
  466. posterity = this._recursiveGetPosterity(node);
  467. }
  468. posterity.sort(function (x, y) {
  469. return self.getNodeIndex(x) - self.getNodeIndex(y);
  470. });
  471. return posterity;
  472. };
  473. /**
  474. * 查询node是否是父节点的最后一个子节点
  475. * @param {Object} node
  476. * @returns {boolean}
  477. */
  478. isLastSibling(node) {
  479. const siblings = this.getChildren(this.getParent(node));
  480. return (siblings && siblings.length > 0) ? node.order === siblings[siblings.length - 1].order : false;
  481. };
  482. /**
  483. * 提取节点key和索引数据
  484. * @param {Object} node - 节点
  485. * @returns {key}
  486. */
  487. getNodeKeyData(node) {
  488. const data = {};
  489. for (const key of this.setting.keys) {
  490. data[key] = node[key];
  491. }
  492. return data;
  493. };
  494. /**
  495. * 得到树结构构成id
  496. * @param node
  497. * @returns {*}
  498. */
  499. getNodeKey(node) {
  500. return node[this.setting.id];
  501. };
  502. _markFold(node) {
  503. if (!this.setting.markFoldKey || !this.setting.markFoldSubKey) return;
  504. // if (!this.setting.treeCacheKey) return;
  505. if (!node.expanded) {
  506. if (this.markFold.indexOf(node[this.setting.id]) === -1) this.markFold.push(node[this.setting.id]);
  507. } else {
  508. if (this.markFold.indexOf(node[this.setting.id]) >= 0) this.markFold.splice(this.markFold.indexOf(node[this.setting.id]), 1);
  509. }
  510. }
  511. _saveMarkFold() {
  512. if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  513. setLocalCache(this.setting.markFoldKey, this.setting.markFoldSubKey + '|' + this.markFold.join(','));
  514. }
  515. // if (this.setting.treeCacheKey) {
  516. // localforage.setItem(this.setting.treeCacheKey, {
  517. // markFold: this.markFold,
  518. // time: new Date(),
  519. // });
  520. // }
  521. }
  522. /**
  523. * 刷新子节点是否可见
  524. * @param {Object} node
  525. * @private
  526. */
  527. _refreshChildrenVisible(node) {
  528. if (!node.children) {
  529. node.children = this.getChildren(node);
  530. }
  531. if (node.children && node.children.length > 0) {
  532. for (const child of node.children) {
  533. child.visible = node.expanded && node.visible;
  534. this._refreshChildrenVisible(child);
  535. }
  536. }
  537. };
  538. /**
  539. * 设置节点是否展开, 并控制子节点可见
  540. * @param {Object} node
  541. * @param {Boolean} expanded
  542. */
  543. setExpanded(node, expanded) {
  544. node.expanded = expanded;
  545. this._markFold(node);
  546. this._refreshChildrenVisible(node);
  547. this._saveMarkFold();
  548. };
  549. /**
  550. * 递归 设置节点展开状态
  551. * @param {Array} nodes - 需要设置状态的节点
  552. * @param {Object} parent - nodes的父节点
  553. * @param {Function} checkFun - 判断节点展开状态的方法
  554. * @private
  555. */
  556. _recursiveExpand(nodes, parent, checkFun) {
  557. for (const node of nodes) {
  558. const expanded = checkFun(node);
  559. if (node.expanded !== expanded) {
  560. node.expanded = expanded;
  561. this._markFold(node);
  562. }
  563. node.visible = parent ? (parent.expanded && parent.visible) : true;
  564. this._recursiveExpand(node.children, node, checkFun);
  565. }
  566. }
  567. /**
  568. * 自定义展开规则
  569. * @param checkFun
  570. */
  571. expandByCustom(checkFun) {
  572. this._recursiveExpand(this.children, null, checkFun);
  573. this._saveMarkFold();
  574. }
  575. /**
  576. * 展开到第几层
  577. * @param {Number} level - 展开层数
  578. */
  579. expandByLevel(level) {
  580. this.expandByCustom(function (n) {
  581. return n.level < level;
  582. });
  583. }
  584. /**
  585. * 自动展开节点node
  586. * @param node
  587. * @returns {*}
  588. */
  589. autoExpandNode(node) {
  590. const parents = this.getAllParents(node);
  591. const reload = [];
  592. for (const p of parents) {
  593. if (!p.expanded) {
  594. reload.push(p);
  595. this.setExpanded(p, true);
  596. }
  597. }
  598. return reload;
  599. }
  600. /**
  601. * 加载数据(动态),只加载不同部分
  602. * @param {Array} datas
  603. * @return {Array} 加载到树的数据
  604. * @privateA
  605. */
  606. _updateData(datas) {
  607. datas = datas instanceof Array ? datas : [datas];
  608. let loadedData = [];
  609. for (const data of datas) {
  610. let node = this.getItems(data[this.setting.id]);
  611. if (node) {
  612. for (const prop in data) {
  613. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  614. if (prop === this.setting.pid) {
  615. loadedData.push(this.getItems(node[this.setting.pid]));
  616. loadedData.push(this.getItems(data[this.setting.pid]));
  617. }
  618. if (prop === this.setting.order) {
  619. loadedData = loadedData.concat(this.getPosterity(node));
  620. }
  621. node[prop] = data[prop];
  622. }
  623. }
  624. loadedData.push(node);
  625. }
  626. }
  627. loadedData = _.uniq(loadedData);
  628. for (const node of loadedData) {
  629. if (node) {
  630. node.children = this.getChildren(node);
  631. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  632. } else {
  633. this.children = this.getChildren(null);
  634. }
  635. }
  636. this.sortTreeNode(true);
  637. return loadedData;
  638. };
  639. /**
  640. * 加载数据(动态),只加载不同部分
  641. * @param {Array} datas
  642. * @return {Array} 加载到树的数据
  643. * @privateA
  644. */
  645. _loadData(datas) {
  646. datas = datas instanceof Array ? datas : [datas];
  647. const loadedData = [], resortData = [];
  648. for (const data of datas) {
  649. let node = this.getItems(data[this.setting.id]);
  650. if (node) {
  651. const parent = this.getItems(node[this.setting.pid]);
  652. for (const prop in data) {
  653. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  654. node[prop] = data[prop];
  655. if (parent && resortData.indexOf(parent) === -1) {
  656. resortData.push(parent);
  657. }
  658. }
  659. }
  660. loadedData.push(node);
  661. } else {
  662. const keyName = itemsPre + data[this.setting.id];
  663. const node = JSON.parse(JSON.stringify(data));
  664. this.items[keyName] = node;
  665. this.datas.push(node);
  666. node.expanded = true;
  667. node.visible = true;
  668. loadedData.push(node);
  669. if (resortData.indexOf(node) === -1) {
  670. resortData.push(node);
  671. }
  672. const parent = this.getItems(node[this.setting.pid]);
  673. if (parent && resortData.indexOf(parent) === -1) {
  674. resortData.push(parent);
  675. } else {
  676. resortData.push(this.setting.rootId);
  677. }
  678. }
  679. }
  680. for (const node of resortData) {
  681. if (node && node !== this.setting.rootId) {
  682. node.children = this.getChildren(node);
  683. } else {
  684. this.children = this.getChildren(null);
  685. }
  686. }
  687. this.sortTreeNode(true);
  688. for (const node of loadedData) {
  689. if (!node.expanded) {
  690. this.setExpanded(node, true);
  691. }
  692. }
  693. return loadedData;
  694. };
  695. /**
  696. * 清理数据(动态)
  697. * @param datas
  698. * @private
  699. */
  700. _freeData(datas) {
  701. datas = datas instanceof Array ? datas : [datas];
  702. const freeDatas = [];
  703. const removeArrayData = function (array, data) {
  704. const index = array.indexOf(data);
  705. array.splice(index, 1);
  706. };
  707. for (const data of datas) {
  708. const node = this.getItems(data[this.setting.id]);
  709. if (node) {
  710. freeDatas.push(node);
  711. node.deleteIndex = this.nodes.indexOf(node);
  712. delete this.items[itemsPre + node[this.setting.id]];
  713. if (node[this.setting.pid] !== this.setting.rootId) {
  714. const parent = this.getItems(node[this.setting.pid]);
  715. if (parent) {
  716. removeArrayData(parent.children, node);
  717. }
  718. } else {
  719. removeArrayData(this.children, node);
  720. }
  721. removeArrayData(this.datas, node);
  722. }
  723. }
  724. for (const node of freeDatas) {
  725. removeArrayData(this.nodes, node);
  726. }
  727. return freeDatas;
  728. };
  729. /**
  730. * 加载需展开的数据
  731. * @param {Array} datas
  732. * @returns {Array}
  733. * @private
  734. */
  735. _loadExpandData(datas) {
  736. datas = datas instanceof Array ? datas : [datas];
  737. const loadedData = [], existData = [], expandData = [], resortData = [];
  738. for (const data of datas) {
  739. let node = this.getItems(data[this.setting.id]);
  740. if (node) {
  741. existData.push(node);
  742. } else {
  743. const keyName = itemsPre + data[this.setting.id];
  744. const node = JSON.parse(JSON.stringify(data));
  745. this.items[keyName] = node;
  746. this.datas.push(node);
  747. node.expanded = false;
  748. node.visible = true;
  749. loadedData.push(node);
  750. if (resortData.indexOf(node) === -1) {
  751. resortData.push(node);
  752. }
  753. const parent = this.getItems(node[this.setting.pid]);
  754. if (parent && resortData.indexOf(parent) === -1) {
  755. resortData.push(parent);
  756. }
  757. }
  758. }
  759. for (const node of resortData) {
  760. node.children = this.getChildren(node);
  761. }
  762. this.sortTreeNode(true);
  763. for (const node of loadedData) {
  764. if (!node.expanded) {
  765. this.setExpanded(node, true);
  766. }
  767. }
  768. for (const node of existData) {
  769. const parent = this.getItems(node[this.setting.pid]);
  770. if (expandData.indexOf(parent) === -1) {
  771. expandData.push(parent);
  772. if (!parent.expanded) {
  773. this.setExpanded(parent, true);
  774. }
  775. }
  776. if (!node.expanded) {
  777. this.setExpanded(node, true);
  778. }
  779. }
  780. return [loadedData, expandData];
  781. };
  782. /**
  783. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  784. *
  785. * @param data - 更新的数据 {update, create, delete}
  786. * @returns {{}}
  787. */
  788. loadPostData(data) {
  789. const result = {};
  790. if (data.delete) {
  791. result.delete = this._freeData(data.delete);
  792. }
  793. if (data.create) {
  794. result.create = this._loadData(data.create);
  795. }
  796. if (data.update) {
  797. result.update = this._updateData(data.update);
  798. }
  799. return result;
  800. }
  801. /**
  802. * 加载子节点
  803. * @param {Object} node
  804. * @param {function} callback
  805. */
  806. loadChildren(node, callback) {
  807. if (this.setting.url !== '') {
  808. const self = this;
  809. postData(this.setting.url, { postType: 'load-child', id: this.getNodeKeyData(node) }, function (data) {
  810. self._loadData(data);
  811. callback();
  812. });
  813. }
  814. };
  815. _getDefaultNodeData(node) {
  816. const result = {};
  817. for (const prop in node) {
  818. if (['children', 'visible', 'expanded'].indexOf(prop) >= 0) continue;
  819. result[prop] = node[prop];
  820. }
  821. return result;
  822. }
  823. getDefaultData(node) {
  824. if (node instanceof Array) {
  825. const arr = [];
  826. for (const n of node) {
  827. arr.push(this._getDefaultNodeData(n));
  828. }
  829. return arr;
  830. } else {
  831. this._getDefaultNodeData(node);
  832. }
  833. }
  834. }
  835. class MeasureTree extends BaseTree {
  836. addData(datas) {
  837. const loadedData = [];
  838. for (const data of datas) {
  839. let node = this.getItems(data[this.setting.id]);
  840. if (node) {
  841. for (const prop in data) {
  842. if (data[prop] !== undefined) {
  843. node[prop] = data[prop];
  844. }
  845. }
  846. loadedData.push(node);
  847. } else {
  848. const keyName = itemsPre + data[this.setting.id];
  849. const node = JSON.parse(JSON.stringify(data));
  850. this.items[keyName] = node;
  851. this.datas.push(node);
  852. node.expanded = false;
  853. node.visible = true;
  854. loadedData.push(node);
  855. }
  856. }
  857. this.sortTreeNode();
  858. for (const node of loadedData) {
  859. const children = node.children;
  860. if (!node.expanded && children.length > 0) {
  861. node.expanded = true;
  862. this._refreshChildrenVisible(node);
  863. }
  864. }
  865. return loadedData;
  866. }
  867. removeData(datas) {
  868. datas.sort(function (a, b) {
  869. return b.level - a.level;
  870. });
  871. const removeArrayData = function (array, data) {
  872. const index = array.indexOf(data);
  873. array.splice(index, 1);
  874. };
  875. for (const data of datas) {
  876. const node = this.getItems(data[this.setting.id]);
  877. if (node && this.getChildren(node).length === 0) {
  878. delete this.items[itemsPre + node[this.setting.id]];
  879. if (node[this.setting.pid] !== this.setting.rootId) {
  880. const parent = this.items[itemsPre + node[this.setting.pid]];
  881. removeArrayData(parent.children, node);
  882. }
  883. removeArrayData(this.datas, node);
  884. removeArrayData(this.nodes, node);
  885. }
  886. }
  887. };
  888. loadLeafData(data) {
  889. const datas = data instanceof Array ? data : [data];
  890. for (const d of datas) {
  891. let node = this.getItems(d[this.setting.id]);
  892. if (node && node.is_leaf) {
  893. for (const prop in d) {
  894. if (data[prop] !== undefined) {
  895. node[prop] = d[prop];
  896. }
  897. }
  898. }
  899. }
  900. };
  901. }
  902. class FxTree extends BaseTree {
  903. /**
  904. * 检查节点是否是最底层项目节
  905. * @param node
  906. * @returns {boolean}
  907. */
  908. isLeafXmj(node) {
  909. if (node.b_code && node.b_code !== '') {
  910. return false;
  911. }
  912. for (const child of node.children) {
  913. if (!child.b_code || child.b_code === '') {
  914. return false;
  915. }
  916. }
  917. return true;
  918. }
  919. /**
  920. * 查询最底层项目节(本身或父项)
  921. * @param {Object} node - 查询节点
  922. * @returns {Object}
  923. */
  924. getLeafXmjParent(node) {
  925. let parent = node;
  926. while (parent) {
  927. if (this.isLeafXmj(parent)) {
  928. return parent;
  929. } else {
  930. parent = this.getParent(parent);
  931. }
  932. }
  933. return null;
  934. }
  935. /**
  936. * 展开至最底层项目节
  937. */
  938. expandToLeafXmj() {
  939. const self = this;
  940. this.expandByCustom(function (node) {
  941. if (node.b_code && node.b_code !== '') {
  942. return false;
  943. } else {
  944. return !self.isLeafXmj(node);
  945. }
  946. })
  947. }
  948. /**
  949. * 展开至计算项
  950. */
  951. expandByCalcFields() {
  952. const self = this;
  953. this.expandByCustom(function (node) {
  954. for (const field of self.setting.calcFields) {
  955. if (node[field]) {
  956. return true;
  957. }
  958. }
  959. return false;
  960. })
  961. }
  962. }
  963. class LedgerTree extends FxTree {
  964. /**
  965. *
  966. * @param parent
  967. * @param node
  968. * @private
  969. */
  970. _getNodesParents(parents, nodes) {
  971. for (const node of nodes) {
  972. const parent = this.getParent(node);
  973. if (parent) {
  974. const paths = this.getFullPathNodes(parent.full_path);
  975. for (const p of paths) {
  976. if (parents.indexOf(p) === -1) {
  977. parents.push(p);
  978. }
  979. }
  980. }
  981. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  982. parents.push(node);
  983. }
  984. }
  985. }
  986. _getReCalcNodes(reCalcNodes, nodes) {
  987. for (const node of nodes) {
  988. const parent = this.getParent(node);
  989. if (parent) {
  990. const paths = this.getFullPathNodes(parent.full_path);
  991. for (const p of paths) {
  992. if (reCalcNodes.indexOf(p) === -1) {
  993. reCalcNodes.push(p);
  994. }
  995. }
  996. }
  997. // 最底层项目节,也需要计算
  998. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  999. reCalcNodes.push(node);
  1000. //}
  1001. }
  1002. }
  1003. /**
  1004. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  1005. *
  1006. * @param data - 更新的数据 {update, create, delete}
  1007. * @returns {{}}
  1008. */
  1009. loadPostData(data) {
  1010. const result = {}, reCalcNodes = [];
  1011. if (!data) return result;
  1012. if (data.delete) {
  1013. result.delete = this._freeData(data.delete);
  1014. this._getReCalcNodes(reCalcNodes, result.delete);
  1015. }
  1016. if (data.create) {
  1017. result.create = this._loadData(data.create);
  1018. this._getReCalcNodes(reCalcNodes, result.create);
  1019. }
  1020. if (data.update) {
  1021. result.update = this._updateData(data.update);
  1022. this._getReCalcNodes(reCalcNodes, result.update);
  1023. }
  1024. reCalcNodes.sort((a, b) => {
  1025. return b.level - a.level;
  1026. });
  1027. for (const node of reCalcNodes) {
  1028. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1029. }
  1030. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1031. return result;
  1032. }
  1033. }
  1034. class ReviseTree extends LedgerTree {
  1035. checkNodeUsed(node, pos) {
  1036. if (node.children && node.children.length > 0) {
  1037. for (const child of node.children) {
  1038. const used = this.checkNodeUsed(child, pos);
  1039. if (used) return used;
  1040. }
  1041. } else {
  1042. if (node.used) return node.used;
  1043. const posRange = pos.getLedgerPos(node.id);
  1044. if (posRange && posRange.length > 0) {
  1045. for (const p of posRange) {
  1046. if (p.used) return p.used;
  1047. }
  1048. }
  1049. }
  1050. return false;
  1051. }
  1052. }
  1053. class StageTree extends FxTree {
  1054. /**
  1055. * 构造函数
  1056. */
  1057. constructor(setting) {
  1058. super(setting);
  1059. // stage关联索引
  1060. this.stageItems = {};
  1061. }
  1062. /**
  1063. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1064. * @param datas
  1065. */
  1066. loadDatas(datas) {
  1067. super.loadDatas(datas);
  1068. // 清空旧数据
  1069. this.stageItems = {};
  1070. // 加载全部数据
  1071. for (const data of this.datas) {
  1072. const keyName = itemsPre + data[this.setting.stageId];
  1073. this.stageItems[keyName] = data;
  1074. }
  1075. }
  1076. getStageItems(id) {
  1077. return this.stageItems[itemsPre + id];
  1078. }
  1079. loadStageData(datas, fieldPre, fields) {
  1080. datas = datas instanceof Array ? datas : [datas];
  1081. const loadedData = [];
  1082. for (const data of datas) {
  1083. let node = this.getStageItems(data.lid);
  1084. if (node) {
  1085. for (const prop of fields) {
  1086. if (data[prop] !== undefined) {
  1087. node[fieldPre + prop] = data[prop];
  1088. }
  1089. }
  1090. loadedData.push(node);
  1091. }
  1092. }
  1093. }
  1094. loadPreStageData(datas) {
  1095. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1096. }
  1097. loadCurStageData(datas) {
  1098. this.loadStageData(datas, '', this.setting.updateFields);
  1099. }
  1100. /**
  1101. * 加载数据(动态),只加载不同部分
  1102. * @param {Array} datas
  1103. * @return {Array} 加载到树的数据
  1104. * @privateA
  1105. */
  1106. _updateData(datas) {
  1107. datas = datas instanceof Array ? datas : [datas];
  1108. let loadedData = [];
  1109. for (const data of datas) {
  1110. let node = this.getItems(data[this.setting.id]);
  1111. if (node) {
  1112. for (const prop in data) {
  1113. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1114. }
  1115. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1116. if (prop === this.setting.pid) {
  1117. loadedData.push(this.getItems(node[this.setting.pid]));
  1118. loadedData.push(this.getItems(data[this.setting.pid]));
  1119. }
  1120. node[prop] = data[prop];
  1121. }
  1122. }
  1123. loadedData.push(node);
  1124. }
  1125. }
  1126. loadedData = _.uniq(loadedData);
  1127. for (const node of loadedData) {
  1128. node.children = this.getChildren(node);
  1129. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1130. }
  1131. this.sortTreeNode(true);
  1132. return loadedData;
  1133. };
  1134. /**
  1135. * 加载数据(动态),只加载不同部分
  1136. * @param {Array} datas
  1137. * @return {Array} 加载到树的数据
  1138. * @privateA
  1139. */
  1140. _updateStageData(datas) {
  1141. datas = datas instanceof Array ? datas : [datas];
  1142. const loadedData = [];
  1143. for (const data of datas) {
  1144. let node = this.getStageItems(data.lid);
  1145. if (node) {
  1146. for (const prop of this.setting.updateFields) {
  1147. if (data[prop] !== undefined) {
  1148. node[prop] = data[prop];
  1149. }
  1150. }
  1151. loadedData.push(node);
  1152. }
  1153. }
  1154. return loadedData;
  1155. };
  1156. /**
  1157. *
  1158. * @param parent
  1159. * @param node
  1160. * @private
  1161. */
  1162. _getNodesParents(parents, nodes) {
  1163. for (const node of nodes) {
  1164. const parent = this.getParent(node);
  1165. if (parent) {
  1166. const paths = this.getFullPathNodes(parent.full_path);
  1167. for (const p of paths) {
  1168. if (parents.indexOf(p) === -1) {
  1169. parents.push(p);
  1170. }
  1171. }
  1172. }
  1173. if (node.children && node.children.length > 0) {
  1174. parents.push(node);
  1175. }
  1176. }
  1177. }
  1178. _updateDgnData(datas) {
  1179. datas = datas instanceof Array ? datas : [datas];
  1180. let loadedData = [];
  1181. for (const data of datas) {
  1182. let node = this.getStageItems(data.id);
  1183. if (node) {
  1184. for (const prop in data) {
  1185. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1186. node[prop] = data[prop];
  1187. }
  1188. }
  1189. loadedData.push(node);
  1190. }
  1191. }
  1192. return loadedData;
  1193. }
  1194. /**
  1195. * 提交数据至后端,返回的前端树结构应刷新的部分
  1196. * StageTree仅有更新CurStage部分,不需要增删
  1197. *
  1198. * @param data - 需要更新的数据
  1199. * @returns {Array} - 界面需要刷新的数据
  1200. */
  1201. loadPostStageData(data) {
  1202. let result, parents = [];
  1203. if (data.bills) {
  1204. result = this._updateData(data.bills);
  1205. this._getNodesParents(parents, result);
  1206. }
  1207. if (data.curStageData) {
  1208. result = this._updateStageData(data.curStageData);
  1209. this._getNodesParents(parents, result);
  1210. }
  1211. if (data.dgn) {
  1212. const dgnResult = this._updateDgnData(data.dgn);
  1213. result = result ? result.concat(dgnResult) : dgnResult;
  1214. }
  1215. result = result ? result.concat(parents) : parents;
  1216. result.sort((a, b) => {
  1217. return b.level - a.level;
  1218. });
  1219. for (const node of result) {
  1220. treeCalc.calculateNode(this, node);
  1221. }
  1222. return result;
  1223. }
  1224. }
  1225. class MasterTree extends FxTree {
  1226. /**
  1227. * 构造函数
  1228. */
  1229. constructor(setting) {
  1230. super(setting);
  1231. // 关联索引
  1232. this.masterItems = {};
  1233. }
  1234. /**
  1235. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1236. * @param datas
  1237. */
  1238. loadDatas(datas) {
  1239. super.loadDatas(datas);
  1240. // 清空旧数据
  1241. this.masterItems = {};
  1242. // minor数据缓存
  1243. this.minorData = {};
  1244. // 加载全部数据
  1245. for (const data of this.datas) {
  1246. const keyName = itemsPre + data[this.setting.masterId];
  1247. this.masterItems[keyName] = data;
  1248. }
  1249. }
  1250. /**
  1251. * 根据关联id,查找节点
  1252. * @param id
  1253. * @returns {*}
  1254. */
  1255. getMasterItems(id) {
  1256. return this.masterItems[itemsPre + id];
  1257. }
  1258. /**
  1259. * 加载关联数据
  1260. *
  1261. * @param {Array|Object}datas - 需要关联的数据
  1262. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1263. * @param {Array} fields - 关联字段
  1264. * @returns {Array}
  1265. */
  1266. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1267. for (const cf of calcFields) {
  1268. this.setting.calcFields.push(cf + fieldSuf);
  1269. }
  1270. if (!datas) return;
  1271. datas = datas instanceof Array ? datas : [datas];
  1272. this.minorData[fieldSuf] = datas;
  1273. const loadedData = [];
  1274. for (const data of datas) {
  1275. let node = this.getMasterItems(data[this.setting.minorId]);
  1276. if (node) {
  1277. for (const prop of fields) {
  1278. if (data[prop] !== undefined) {
  1279. node[prop + fieldSuf] = data[prop];
  1280. }
  1281. }
  1282. loadedData.push(node);
  1283. }
  1284. }
  1285. return loadedData;
  1286. }
  1287. }
  1288. class FilterTree extends BaseTree {
  1289. addData(data, fields) {
  1290. const item = {};
  1291. for (const prop in data) {
  1292. if (fields.indexOf(prop) >= 0) {
  1293. item[prop] = data[prop];
  1294. }
  1295. }
  1296. const keyName = itemsPre + item[this.setting.id];
  1297. if (!this.items[keyName]) {
  1298. item.children = [];
  1299. item.is_leaf = true;
  1300. item.expanded = true;
  1301. item.visible = true;
  1302. this.items[keyName] = item;
  1303. this.datas.push(item);
  1304. if (item[setting.pid] === setting.rootId) {
  1305. this.children.push(item);
  1306. } else {
  1307. const parent = this.getParent(item);
  1308. if (parent) {
  1309. parent.is_leaf = false;
  1310. parent.children.push(item);
  1311. }
  1312. }
  1313. } else {
  1314. return this.items[keyName];
  1315. }
  1316. return item;
  1317. }
  1318. }
  1319. class GatherTree extends BaseTree {
  1320. clearDatas() {
  1321. this.items = {};
  1322. this.nodes = [];
  1323. this.datas = [];
  1324. this.children = [];
  1325. }
  1326. get newId() {
  1327. if (!this._maxId) {
  1328. this._maxId = 0;
  1329. }
  1330. this._maxId++;
  1331. return this._maxId;
  1332. }
  1333. addNode(data, parent) {
  1334. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1335. let item = _.find(this.items, data);
  1336. if (item) return item;
  1337. item = data;
  1338. item[this.setting.id] = this.newId;
  1339. const keyName = itemsPre + item[this.setting.id];
  1340. item.children = [];
  1341. item.is_leaf = true;
  1342. item.expanded = true;
  1343. item.visible = true;
  1344. this.items[keyName] = item;
  1345. this.datas.push(item);
  1346. if (parent) {
  1347. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1348. item[this.setting.level] = parent[this.setting.level] + 1;
  1349. item[this.setting.order] = parent.children.length + 1;
  1350. parent.is_leaf = false;
  1351. parent.children.push(item);
  1352. } else {
  1353. item[this.setting.fullPath] = '' + item[this.setting.id];
  1354. item[this.setting.level] = 1;
  1355. item[this.setting.order] = this.children.length + 1;
  1356. this.children.push(item);
  1357. }
  1358. return item;
  1359. }
  1360. sortTreeNodeCustom(field, fun, isResort) {
  1361. const self = this;
  1362. const sortNodes = function (nodes) {
  1363. nodes.sort(function (a, b) {
  1364. return fun(a[field], b[field]);
  1365. });
  1366. for (const [i, node] of nodes.entries()) {
  1367. node.order = i + 1;
  1368. }
  1369. };
  1370. const addSortNodes = function (nodes) {
  1371. if (!nodes) { return }
  1372. for (let i = 0; i < nodes.length; i++) {
  1373. self.nodes.push(nodes[i]);
  1374. nodes[i].index = self.nodes.length - 1;
  1375. if (!isResort) {
  1376. nodes[i].children = self.getChildren(nodes[i]);
  1377. }
  1378. sortNodes(nodes[i].children);
  1379. addSortNodes(nodes[i].children);
  1380. }
  1381. };
  1382. this.nodes = [];
  1383. if (!isResort) {
  1384. this.children = this.getChildren();
  1385. }
  1386. sortNodes(this.children);
  1387. addSortNodes(this.children);
  1388. }
  1389. }
  1390. class CompareTree extends FxTree {
  1391. constructor(setting) {
  1392. super(setting);
  1393. this._newId = 1;
  1394. }
  1395. get newId() {
  1396. return this._newId++;
  1397. }
  1398. findCompareNode(node, parent) {
  1399. if (this.setting.findNode) {
  1400. return this.setting.findNode(this, node, parent);
  1401. } else {
  1402. const siblings = parent ? parent.children : this.children;
  1403. return siblings.find(function (x) {
  1404. return node.b_code
  1405. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1406. : x.code === node.code && x.name === node.name;
  1407. });
  1408. }
  1409. }
  1410. loadCompareNode(source, node, parent, loadFun) {
  1411. let cur = this.findCompareNode(node, parent);
  1412. if (!cur) {
  1413. const siblings = parent ? parent.children : this.children;
  1414. const id = this.newId;
  1415. cur = {
  1416. children: [], pos: [],
  1417. code: node.code, b_code: node.b_code, name: node.name,
  1418. unit: node.unit, unit_price: node.unit_price,
  1419. };
  1420. cur[this.setting.id] = id;
  1421. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1422. cur[this.setting.full_path] = parent ? parent[this.setting.full_path] + '-' + id : '' + id;
  1423. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1424. cur[this.setting.order] = siblings.length + 1;
  1425. siblings.push(cur);
  1426. this.datas.push(cur);
  1427. }
  1428. loadFun(cur, node, source);
  1429. for (const c of node.children) {
  1430. this.loadCompareNode(source, c, cur, loadFun);
  1431. }
  1432. }
  1433. generateSortNodes() {
  1434. const self = this;
  1435. const addSortNode = function (node) {
  1436. self.nodes.push(node);
  1437. for (const c of node.children) {
  1438. addSortNode(c);
  1439. }
  1440. }
  1441. this.nodes = [];
  1442. for (const n of this.children) {
  1443. addSortNode(n);
  1444. }
  1445. }
  1446. loadCompareTree(data, loadFun) {
  1447. for (const c of data.billsTree.children) {
  1448. this.loadCompareNode(data, c, null, loadFun);
  1449. }
  1450. }
  1451. calculateDiffer() {
  1452. if (this.setting.calcDiffer) {
  1453. for (const d of this.datas) {
  1454. this.setting.calcDiffer(d);
  1455. }
  1456. }
  1457. }
  1458. loadCompareData(data1, data2) {
  1459. this.loadCompareTree(data1, this.setting.loadInfo1);
  1460. this.loadCompareTree(data2, this.setting.loadInfo2);
  1461. for (const d of this.datas) {
  1462. d.is_leaf = d.children.length === 0;
  1463. d.expanded = true;
  1464. d.visible = true;
  1465. this.items[itemsPre + d[this.setting.id]] = d;
  1466. }
  1467. this.generateSortNodes();
  1468. this.calculateDiffer();
  1469. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1470. }
  1471. }
  1472. class TreeGatherTree extends FxTree {
  1473. constructor(setting) {
  1474. super(setting);
  1475. this._newId = 1;
  1476. }
  1477. get newId() {
  1478. return this._newId++;
  1479. }
  1480. loadGatherNode(node, parent, index, loadFun) {
  1481. const siblings = parent ? parent.children : this.children;
  1482. let cur = siblings.find(function (x) {
  1483. return node.b_code
  1484. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1485. : x.code === node.code && x.name === node.name;
  1486. });
  1487. if (!cur) {
  1488. const id = this.newId;
  1489. cur = {
  1490. id: id,
  1491. pid: parent ? parent.id : this.setting.rootId,
  1492. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1493. level: parent ? parent.level + 1 : 1,
  1494. order: siblings.length + 1,
  1495. children: [],
  1496. code: node.code, b_code: node.b_code, name: node.name,
  1497. unit: node.unit, unit_price: node.unit_price,
  1498. };
  1499. siblings.push(cur);
  1500. this.datas.push(cur);
  1501. }
  1502. loadFun(cur, node, index);
  1503. for (const c of node.children) {
  1504. this.loadGatherNode(c, cur, index, loadFun);
  1505. }
  1506. }
  1507. generateSortNodes() {
  1508. const self = this;
  1509. const addSortNode = function (node) {
  1510. self.nodes.push(node);
  1511. for (const c of node.children) {
  1512. addSortNode(c);
  1513. }
  1514. }
  1515. this.nodes = [];
  1516. for (const n of this.children) {
  1517. addSortNode(n);
  1518. }
  1519. }
  1520. loadGatherTree(data, index, loadFun) {
  1521. for (const c of data.billsTree.children) {
  1522. this.loadGatherNode(c, null, index, loadFun);
  1523. }
  1524. // todo load Pos Data;
  1525. }
  1526. calculateSum() {
  1527. if (this.setting.calcSum) {
  1528. for (const d of this.datas) {
  1529. this.setting.calcSum(d, this.count);
  1530. }
  1531. }
  1532. }
  1533. loadGatherData(datas) {
  1534. this.count = datas.length;
  1535. for (const [i, data] of datas.entries()) {
  1536. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1537. }
  1538. for (const d of this.datas) {
  1539. d.is_leaf = d.children.length === 0;
  1540. d.expanded = true;
  1541. d.visible = true;
  1542. this.items[itemsPre + d[this.setting.id]] = d;
  1543. }
  1544. this.generateSortNodes();
  1545. this.calculateSum();
  1546. }
  1547. }
  1548. if (type === 'base') {
  1549. return new BaseTree(setting);
  1550. } else if (type === 'fx') {
  1551. return new FxTree(setting);
  1552. } else if (type === 'stage') {
  1553. return new StageTree(setting);
  1554. } else if (type === 'ledger') {
  1555. return new LedgerTree(setting);
  1556. } else if (type === 'revise') {
  1557. return new ReviseTree(setting);
  1558. } else if (type === 'measure') {
  1559. return new MeasureTree(setting);
  1560. } else if (type === 'master') {
  1561. return new MasterTree(setting);
  1562. } else if (type === 'filter') {
  1563. return new FilterTree(setting);
  1564. } else if (type === 'gather') {
  1565. return new GatherTree(setting);
  1566. } else if (type === 'compare') {
  1567. return new CompareTree(setting);
  1568. } else if (type === 'tree-gather') {
  1569. return new TreeGatherTree(setting);
  1570. }
  1571. };
  1572. const treeCalc = {
  1573. mapTreeNode: function (tree) {
  1574. let map = {}, maxLevel = 0;
  1575. for (const node of tree.nodes) {
  1576. let levelArr = map[node.level];
  1577. if (!levelArr) {
  1578. levelArr = [];
  1579. map[node.level] = levelArr;
  1580. }
  1581. if (node.level > maxLevel) {
  1582. maxLevel = node.level;
  1583. }
  1584. levelArr.push(node);
  1585. }
  1586. return [maxLevel, map];
  1587. },
  1588. getMaxLevel: function (tree) {
  1589. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1590. },
  1591. calculateNode: function (tree, node) {
  1592. if (node.children && node.children.length > 0) {
  1593. const gather = node.children.reduce(function (rst, x) {
  1594. const result = {};
  1595. for (const cf of tree.setting.calcFields) {
  1596. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1597. }
  1598. return result;
  1599. });
  1600. // 汇总子项
  1601. for (const cf of tree.setting.calcFields) {
  1602. if (gather[cf]) {
  1603. node[cf] = gather[cf];
  1604. } else {
  1605. node[cf] = null;
  1606. }
  1607. }
  1608. }
  1609. // 自身运算
  1610. if (tree.setting.calcFun) {
  1611. tree.setting.calcFun(node);
  1612. }
  1613. },
  1614. calculateLevelNode: function (tree, level) {
  1615. const nodes = tree.datas.filter((n) => { return n.level === level });
  1616. for (const node of nodes) {
  1617. this.calculateNode(tree, node);
  1618. }
  1619. },
  1620. calculateAll: function (tree) {
  1621. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1622. for (let i = maxLevel; i >= 0; i--) {
  1623. const levelNodes = levelMap[i];
  1624. if (levelNodes && levelNodes.length > 0) {
  1625. for (const node of levelNodes) {
  1626. this.calculateNode(tree, node);
  1627. }
  1628. }
  1629. }
  1630. },
  1631. calculateParent: function (tree, node) {
  1632. const nodes = tree.getFullPathNodes(node.full_path);
  1633. nodes.sort((a, b) => {
  1634. return b.level - a.level;
  1635. });
  1636. for (const n of nodes) {
  1637. this.calculateNode(tree, n);
  1638. }
  1639. return nodes;
  1640. }
  1641. };