path_tree.js 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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. const sortRule = this.setting.sort || [['porder', 'asc']];
  137. if (ledgerPos instanceof Array) {
  138. ledgerPos.sort(function (a, b) {
  139. for (const sr of sortRule) {
  140. const iSort = sr[1] === 'asc' ? a[sr[0]] - b[sr[0]] : b[sr[0]] - a[sr[0]];
  141. if (iSort) return iSort;
  142. }
  143. })
  144. }
  145. }
  146. /**
  147. * 计算全部
  148. */
  149. calculateAll() {
  150. if (!this.setting.calcFun) { return; }
  151. for (const pos of this.datas) {
  152. this.setting.calcFun(pos);
  153. }
  154. }
  155. set sort(sort) {
  156. this.setting.sort = sort;
  157. for (const key in this.ledgerPos) {
  158. this.resortLedgerPos(this.ledgerPos[key]);
  159. }
  160. }
  161. }
  162. class StagePosData extends PosData {
  163. loadStageData(datas, fieldPre, fields) {
  164. if (!datas) { return; }
  165. datas = datas instanceof Array ? datas : [datas];
  166. const loadedData = [];
  167. for (const data of datas) {
  168. let node = this.getPos(data.pid);
  169. if (node) {
  170. for (const prop of fields) {
  171. if (data[prop] !== undefined) {
  172. node[fieldPre + prop] = data[prop];
  173. }
  174. }
  175. if (this.setting.calcFun) {
  176. this.setting.calcFun(node);
  177. }
  178. loadedData.push(node);
  179. }
  180. }
  181. }
  182. loadPreStageData(datas) {
  183. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  184. }
  185. loadCurStageData(datas) {
  186. this.loadStageData(datas, '', this.setting.updateFields);
  187. }
  188. }
  189. class MasterPosData extends PosData {
  190. /**
  191. * 构造函数
  192. * @param {id|Number, masterId|Number} setting
  193. */
  194. constructor(setting) {
  195. super(setting);
  196. // 关联索引
  197. this.masterItems = {};
  198. }
  199. /**
  200. * 加载主数据
  201. * @param datas
  202. */
  203. loadDatas(datas) {
  204. super.loadDatas(datas);
  205. // 清空旧数据
  206. this.masterItems = {};
  207. // minor数据缓存
  208. this.minorData = {};
  209. // 加载全部数据
  210. for (const data of this.datas) {
  211. const keyName = itemsPre + data[this.setting.masterId];
  212. this.masterItems[keyName] = data;
  213. }
  214. }
  215. /**
  216. * 根据关联id,查找节点
  217. * @param id
  218. * @returns {*}
  219. */
  220. getMasterItems(id) {
  221. return this.masterItems[itemsPre + id];
  222. }
  223. /**
  224. * 加载关联数据
  225. *
  226. * @param {Array|Object}datas - 需要关联的数据
  227. * @param {String} fieldPre - 关联字段前缀(关联结果)
  228. * @param {Array} fields - 关联字段
  229. * @returns {Array}
  230. */
  231. loadMinorData(datas, fieldSuf, fields) {
  232. if (!datas) return;
  233. datas = datas instanceof Array ? datas : [datas];
  234. this.minorData[fieldSuf] = datas;
  235. const loadedData = [];
  236. for (const data of datas) {
  237. let node = this.getMasterItems(data[this.setting.minorId]);
  238. if (node) {
  239. for (const prop of fields) {
  240. if (data[prop] !== undefined) {
  241. node[prop + fieldSuf] = data[prop];
  242. }
  243. }
  244. loadedData.push(node);
  245. }
  246. }
  247. return loadedData;
  248. }
  249. }
  250. const createNewPathTree = function (type, setting) {
  251. class BaseTree {
  252. /**
  253. * 构造函数
  254. */
  255. constructor(setting) {
  256. const self = this;
  257. // 无索引
  258. this.datas = [];
  259. // 以key为索引indexedDB
  260. this.items = {};
  261. // 以排序为索引
  262. this.nodes = [];
  263. // 根节点
  264. this.children = [];
  265. // 树设置
  266. this.setting = setting;
  267. this.hasMark = false;
  268. if (this.setting.markExpandKey) {
  269. const markStr = getLocalCache(this.setting.markExpandKey);
  270. const markData = markStr ? markStr.split('|') : ['', ''];
  271. this.hasMark = markData[0] === this.setting.markExpandSubKey;
  272. this.markExpand = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  273. } else if (this.setting.markFoldKey) {
  274. const markStr = getLocalCache(this.setting.markFoldKey);
  275. const markData = markStr ? markStr.split('|') : ['', ''];
  276. this.hasMark = markData[0] === this.setting.markFoldSubKey;
  277. this.markFold = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  278. }
  279. // if (this.setting.treeCacheKey) {
  280. // localforage.getItem(this.setting.treeCacheKey).then(function (v) {
  281. // self.markFold = v && v.markFold ? v.markFold : [];
  282. // });
  283. // }
  284. }
  285. /**
  286. * 树结构根据显示排序
  287. */
  288. sortTreeNode(isResort) {
  289. const self = this;
  290. const addSortNodes = function (nodes) {
  291. if (!nodes) { return }
  292. for (let i = 0; i < nodes.length; i++) {
  293. self.nodes.push(nodes[i]);
  294. nodes[i].index = self.nodes.length - 1;
  295. if (!isResort) {
  296. nodes[i].children = self.getChildren(nodes[i]);
  297. } else {
  298. nodes[i].children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; })
  299. }
  300. addSortNodes(nodes[i].children);
  301. }
  302. };
  303. this.nodes = [];
  304. if (!isResort) {
  305. this.children = this.getChildren();
  306. } else {
  307. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  308. }
  309. addSortNodes(this.children);
  310. }
  311. /**
  312. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  313. * @param datas
  314. */
  315. loadDatas(datas) {
  316. const self = this;
  317. // 清空旧数据
  318. this.items = {};
  319. this.nodes = [];
  320. this.datas = [];
  321. this.children = [];
  322. // 加载全部数据
  323. datas.sort(function (a, b) {
  324. return a.level - b.level;
  325. });
  326. for (const data of datas) {
  327. const keyName = itemsPre + data[this.setting.id];
  328. if (this.items[keyName]) continue;
  329. const item = JSON.parse(JSON.stringify(data));
  330. item.children = [];
  331. item.expanded = true;
  332. item.visible = true;
  333. if (item[setting.pid] === setting.rootId) {
  334. this.children.push(item);
  335. } else {
  336. const parent = this.getParent(item);
  337. if (!parent) continue;
  338. parent.children.push(item);
  339. }
  340. this.items[keyName] = item;
  341. this.datas.push(item);
  342. }
  343. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  344. this.sortTreeNode(true);
  345. if (this.hasMark) {
  346. if (this.setting.markExpandKey) {
  347. this.expandByCustom(node => {
  348. return self.markExpand.indexOf(node[self.setting.id]) >= 0;
  349. });
  350. } else if (this.setting.markFoldKey) {
  351. this.expandByCustom(function (node) {
  352. return self.markFold.indexOf(node[self.setting.id]) === -1;
  353. });
  354. }
  355. } else {
  356. if (this.setting.autoExpand >= 0) {
  357. this.expandByLevel(this.setting.autoExpand);
  358. for (const node of this.nodes) {
  359. this._markExpandFold(node);
  360. }
  361. this._saveMarkExpandFold();
  362. }
  363. }
  364. }
  365. getItemsByIndex(index) {
  366. return this.nodes[index];
  367. }
  368. /**
  369. * 根据id获取树结构节点数据
  370. * @param {Number} id
  371. * @returns {Object}
  372. */
  373. getItems(id) {
  374. return this.items[itemsPre + id];
  375. };
  376. getNodeIndex(node) {
  377. return this.nodes.indexOf(node);
  378. }
  379. /**
  380. * 查找node的parent
  381. * @param {Object} node
  382. * @returns {Object}
  383. */
  384. getParent(node) {
  385. return this.getItems(node[this.setting.pid]);
  386. };
  387. getTopParent(node) {
  388. const parents = this.getAllParents(node);
  389. parents.sort((a, b) => { return a.level - b.level; });
  390. return parents[0];
  391. };
  392. getAllParents(node) {
  393. const parents = [];
  394. if (!node) return parents;
  395. if (node.full_path && node.full_path !== '') {
  396. const parentIds = node.full_path.split('-');
  397. parentIds.length = parentIds.length - 1;
  398. for (const id of parentIds) {
  399. if (id !== node[this.setting.id]) {
  400. parents.push(this.getItems(id));
  401. }
  402. }
  403. } else {
  404. let vP = this.getParent(node);
  405. while (vP) {
  406. parents.push(vP);
  407. vP = this.getParent(vP);
  408. }
  409. }
  410. return parents;
  411. }
  412. /**
  413. * 查找node的前兄弟节点
  414. * @param node
  415. * @returns {*}
  416. */
  417. getPreSiblingNode(node) {
  418. if (!node) return null;
  419. const parent = this.getParent(node);
  420. const siblings = parent ? parent.children : this.children;
  421. const index = siblings.indexOf(node);
  422. return (index > 0) ? siblings[index - 1] : null;
  423. }
  424. /**
  425. * 查找node的后兄弟节点
  426. * @param node
  427. * @returns {*}
  428. */
  429. getNextSiblingNode(node) {
  430. const parent = this.getParent(node);
  431. const siblings = parent ? parent.children : this.children;
  432. const index = siblings.indexOf(node);
  433. if (index >= 0 && index < siblings.length - 1) {
  434. return siblings[index + 1];
  435. } else {
  436. return null;
  437. }
  438. }
  439. /**
  440. * 根据path查找完整节点
  441. * @param {Number} path
  442. */
  443. getFullPathNodes(path) {
  444. const self = this, ids = path.split('-');
  445. if (ids.length > 0) {
  446. return this.nodes.filter((x) => {
  447. return ids.indexOf('' + x[self.setting.id]) >= 0;
  448. });
  449. } else {
  450. return [];
  451. }
  452. };
  453. /**
  454. * 查询node的已下载子节点
  455. * @param {Object} node
  456. * @returns {Array}
  457. */
  458. getChildren(node) {
  459. const setting = this.setting;
  460. const pid = node ? node[setting.id] : setting.rootId;
  461. const children = this.datas.filter(function (x) {
  462. return x[setting.pid] === pid;
  463. });
  464. children.sort((a, b) => { return a[setting.order] - b[setting.order]; });
  465. return children;
  466. };
  467. /**
  468. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  469. * @param node
  470. * @returns {*}
  471. * @private
  472. */
  473. _recursiveGetPosterity(node) {
  474. let posterity = node.children;
  475. for (const c of node.children) {
  476. posterity = posterity.concat(this._recursiveGetPosterity(c));
  477. }
  478. return posterity;
  479. };
  480. /**
  481. * 查询node的已下载的全部后代
  482. * @param {Object} node
  483. * @returns {Array}
  484. */
  485. getPosterity(node) {
  486. const self = this;
  487. let posterity;
  488. if (node.full_path !== '') {
  489. const reg = new RegExp('^' + node.full_path + '-');
  490. posterity = this.datas.filter(function (x) {
  491. return reg.test(x.full_path);
  492. });
  493. } else {
  494. posterity = this._recursiveGetPosterity(node);
  495. }
  496. posterity.sort(function (x, y) {
  497. return self.getNodeIndex(x) - self.getNodeIndex(y);
  498. });
  499. return posterity;
  500. };
  501. /**
  502. * 查询node是否是父节点的最后一个子节点
  503. * @param {Object} node
  504. * @returns {boolean}
  505. */
  506. isLastSibling(node) {
  507. const siblings = this.getChildren(this.getParent(node));
  508. return (siblings && siblings.length > 0) ? node[this.setting.order] === siblings[siblings.length - 1][this.setting.order] : false;
  509. };
  510. /**
  511. * 提取节点key和索引数据
  512. * @param {Object} node - 节点
  513. * @returns {key}
  514. */
  515. getNodeKeyData(node) {
  516. const data = {};
  517. for (const key of this.setting.keys) {
  518. data[key] = node[key];
  519. }
  520. return data;
  521. };
  522. /**
  523. * 得到树结构构成id
  524. * @param node
  525. * @returns {*}
  526. */
  527. getNodeKey(node) {
  528. return node[this.setting.id];
  529. };
  530. _markExpandFold(node) {
  531. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  532. if (node.expanded) {
  533. if (this.markExpand.indexOf(node[this.setting.id]) === -1)
  534. this.markExpand.push(node[this.setting.id]);
  535. } else {
  536. if (this.markExpand.indexOf(node[this.setting.id]) >= 0)
  537. this.markExpand.splice(this.markExpand.indexOf(node[this.setting.id]), 1);
  538. }
  539. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  540. if (!node.expanded) {
  541. if (this.markFold.indexOf(node[this.setting.id]) === -1)
  542. this.markFold.push(node[this.setting.id]);
  543. } else {
  544. if (this.markFold.indexOf(node[this.setting.id]) >= 0)
  545. this.markFold.splice(this.markFold.indexOf(node[this.setting.id]), 1);
  546. }
  547. }
  548. }
  549. _saveMarkExpandFold() {
  550. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  551. setLocalCache(this.setting.markExpandKey, this.setting.markExpandSubKey + '|' + this.markExpand.join(','));
  552. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  553. setLocalCache(this.setting.markFoldKey, this.setting.markFoldSubKey + '|' + this.markFold.join(','));
  554. }
  555. // if (this.setting.treeCacheKey) {
  556. // localforage.setItem(this.setting.treeCacheKey, {
  557. // markFold: this.markFold,
  558. // time: new Date(),
  559. // });
  560. // }
  561. }
  562. /**
  563. * 刷新子节点是否可见
  564. * @param {Object} node
  565. * @private
  566. */
  567. _refreshChildrenVisible(node) {
  568. if (!node.children) {
  569. node.children = this.getChildren(node);
  570. }
  571. if (node.children && node.children.length > 0) {
  572. for (const child of node.children) {
  573. child.visible = node.expanded && node.visible;
  574. this._refreshChildrenVisible(child);
  575. }
  576. }
  577. };
  578. /**
  579. * 设置节点是否展开, 并控制子节点可见
  580. * @param {Object} node
  581. * @param {Boolean} expanded
  582. */
  583. setExpanded(node, expanded) {
  584. node.expanded = expanded;
  585. this._markExpandFold(node);
  586. this._refreshChildrenVisible(node);
  587. this._saveMarkExpandFold();
  588. };
  589. /**
  590. * 递归 设置节点展开状态
  591. * @param {Array} nodes - 需要设置状态的节点
  592. * @param {Object} parent - nodes的父节点
  593. * @param {Function} checkFun - 判断节点展开状态的方法
  594. * @private
  595. */
  596. _recursiveExpand(nodes, parent, checkFun) {
  597. for (const node of nodes) {
  598. const expanded = checkFun(node);
  599. if (node.expanded !== expanded) {
  600. node.expanded = expanded;
  601. this._markExpandFold(node);
  602. }
  603. node.visible = parent ? (parent.expanded && parent.visible) : true;
  604. this._recursiveExpand(node.children, node, checkFun);
  605. }
  606. }
  607. /**
  608. * 自定义展开规则
  609. * @param checkFun
  610. */
  611. expandByCustom(checkFun) {
  612. this._recursiveExpand(this.children, null, checkFun);
  613. this._saveMarkExpandFold();
  614. }
  615. /**
  616. * 展开到第几层
  617. * @param {Number} level - 展开层数
  618. */
  619. expandByLevel(level) {
  620. this.expandByCustom(function (n) {
  621. return n.level < level;
  622. });
  623. }
  624. /**
  625. * 自动展开节点node
  626. * @param node
  627. * @returns {*}
  628. */
  629. autoExpandNode(node) {
  630. const parents = this.getAllParents(node);
  631. const reload = [];
  632. for (const p of parents) {
  633. if (!p.expanded) {
  634. reload.push(p);
  635. this.setExpanded(p, true);
  636. }
  637. }
  638. return reload;
  639. }
  640. /**
  641. * 加载数据(动态),只加载不同部分
  642. * @param {Array} datas
  643. * @return {Array} 加载到树的数据
  644. * @privateA
  645. */
  646. _updateData(datas) {
  647. datas = datas instanceof Array ? datas : [datas];
  648. let loadedData = [];
  649. for (const data of datas) {
  650. let node = this.getItems(data[this.setting.id]);
  651. if (node) {
  652. for (const prop in data) {
  653. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  654. if (prop === this.setting.pid) {
  655. loadedData.push(this.getItems(node[this.setting.pid]));
  656. loadedData.push(this.getItems(data[this.setting.pid]));
  657. }
  658. if (prop === this.setting.order) {
  659. loadedData = loadedData.concat(this.getPosterity(node));
  660. }
  661. node[prop] = data[prop];
  662. }
  663. }
  664. loadedData.push(node);
  665. }
  666. }
  667. loadedData = _.uniq(loadedData);
  668. for (const node of loadedData) {
  669. if (node) {
  670. node.children = this.getChildren(node);
  671. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  672. } else {
  673. this.children = this.getChildren(null);
  674. }
  675. }
  676. this.sortTreeNode(true);
  677. return loadedData;
  678. };
  679. /**
  680. * 加载数据(动态),只加载不同部分
  681. * @param {Array} datas
  682. * @return {Array} 加载到树的数据
  683. * @privateA
  684. */
  685. _loadData(datas) {
  686. datas = datas instanceof Array ? datas : [datas];
  687. const loadedData = [], resortData = [];
  688. for (const data of datas) {
  689. let node = this.getItems(data[this.setting.id]);
  690. if (node) {
  691. const parent = this.getItems(node[this.setting.pid]);
  692. for (const prop in data) {
  693. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  694. node[prop] = data[prop];
  695. if (parent && resortData.indexOf(parent) === -1) {
  696. resortData.push(parent);
  697. }
  698. }
  699. }
  700. loadedData.push(node);
  701. } else {
  702. const keyName = itemsPre + data[this.setting.id];
  703. const node = JSON.parse(JSON.stringify(data));
  704. this.items[keyName] = node;
  705. this.datas.push(node);
  706. node.expanded = true;
  707. node.visible = true;
  708. loadedData.push(node);
  709. if (resortData.indexOf(node) === -1) {
  710. resortData.push(node);
  711. }
  712. const parent = this.getItems(node[this.setting.pid]);
  713. if (parent && resortData.indexOf(parent) === -1) {
  714. resortData.push(parent);
  715. } else {
  716. resortData.push(this.setting.rootId);
  717. }
  718. }
  719. }
  720. for (const node of resortData) {
  721. if (node && node !== this.setting.rootId) {
  722. node.children = this.getChildren(node);
  723. } else {
  724. this.children = this.getChildren(null);
  725. }
  726. }
  727. this.sortTreeNode(true);
  728. for (const node of loadedData) {
  729. if (!node.expanded) {
  730. this.setExpanded(node, true);
  731. }
  732. }
  733. return loadedData;
  734. };
  735. /**
  736. * 清理数据(动态)
  737. * @param datas
  738. * @private
  739. */
  740. _freeData(datas) {
  741. datas = datas instanceof Array ? datas : [datas];
  742. const freeDatas = [];
  743. const removeArrayData = function (array, data) {
  744. const index = array.indexOf(data);
  745. array.splice(index, 1);
  746. };
  747. for (const data of datas) {
  748. const node = this.getItems(data[this.setting.id]);
  749. if (node) {
  750. freeDatas.push(node);
  751. node.deleteIndex = this.nodes.indexOf(node);
  752. delete this.items[itemsPre + node[this.setting.id]];
  753. if (node[this.setting.pid] !== this.setting.rootId) {
  754. const parent = this.getItems(node[this.setting.pid]);
  755. if (parent) {
  756. removeArrayData(parent.children, node);
  757. }
  758. } else {
  759. removeArrayData(this.children, node);
  760. }
  761. removeArrayData(this.datas, node);
  762. }
  763. }
  764. for (const node of freeDatas) {
  765. removeArrayData(this.nodes, node);
  766. }
  767. return freeDatas;
  768. };
  769. /**
  770. * 加载需展开的数据
  771. * @param {Array} datas
  772. * @returns {Array}
  773. * @private
  774. */
  775. _loadExpandData(datas) {
  776. datas = datas instanceof Array ? datas : [datas];
  777. const loadedData = [], existData = [], expandData = [], resortData = [];
  778. for (const data of datas) {
  779. let node = this.getItems(data[this.setting.id]);
  780. if (node) {
  781. existData.push(node);
  782. } else {
  783. const keyName = itemsPre + data[this.setting.id];
  784. const node = JSON.parse(JSON.stringify(data));
  785. this.items[keyName] = node;
  786. this.datas.push(node);
  787. node.expanded = false;
  788. node.visible = true;
  789. loadedData.push(node);
  790. if (resortData.indexOf(node) === -1) {
  791. resortData.push(node);
  792. }
  793. const parent = this.getItems(node[this.setting.pid]);
  794. if (parent && resortData.indexOf(parent) === -1) {
  795. resortData.push(parent);
  796. }
  797. }
  798. }
  799. for (const node of resortData) {
  800. node.children = this.getChildren(node);
  801. }
  802. this.sortTreeNode(true);
  803. for (const node of loadedData) {
  804. if (!node.expanded) {
  805. this.setExpanded(node, true);
  806. }
  807. }
  808. for (const node of existData) {
  809. const parent = this.getItems(node[this.setting.pid]);
  810. if (expandData.indexOf(parent) === -1) {
  811. expandData.push(parent);
  812. if (!parent.expanded) {
  813. this.setExpanded(parent, true);
  814. }
  815. }
  816. if (!node.expanded) {
  817. this.setExpanded(node, true);
  818. }
  819. }
  820. return [loadedData, expandData];
  821. };
  822. /**
  823. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  824. *
  825. * @param data - 更新的数据 {update, create, delete}
  826. * @returns {{}}
  827. */
  828. loadPostData(data) {
  829. const result = {};
  830. if (data.delete) {
  831. result.delete = this._freeData(data.delete);
  832. }
  833. if (data.create) {
  834. result.create = this._loadData(data.create);
  835. }
  836. if (data.update) {
  837. result.update = this._updateData(data.update);
  838. }
  839. return result;
  840. }
  841. /**
  842. * 加载子节点
  843. * @param {Object} node
  844. * @param {function} callback
  845. */
  846. loadChildren(node, callback) {
  847. if (this.setting.url !== '') {
  848. const self = this;
  849. postData(this.setting.url, { postType: 'load-child', id: this.getNodeKeyData(node) }, function (data) {
  850. self._loadData(data);
  851. callback();
  852. });
  853. }
  854. };
  855. _getDefaultNodeData(node) {
  856. const result = {};
  857. for (const prop in node) {
  858. if (['children', 'visible', 'expanded'].indexOf(prop) >= 0) continue;
  859. result[prop] = node[prop];
  860. }
  861. return result;
  862. }
  863. getDefaultData(node) {
  864. if (node instanceof Array) {
  865. const arr = [];
  866. for (const n of node) {
  867. arr.push(this._getDefaultNodeData(n));
  868. }
  869. return arr;
  870. } else {
  871. this._getDefaultNodeData(node);
  872. }
  873. }
  874. checkParent(node, field = 'check') {
  875. const parent = this.getParent(node);
  876. return parent
  877. ? (parent[field] ? parent[field] : this.checkParent(parent, field))
  878. : false;
  879. }
  880. checkChildren(node, field = 'check') {
  881. for (const child of node.children) {
  882. if (child[field]) {
  883. return true;
  884. } else if (this.checkChildren(child, field)) {
  885. return true;
  886. }
  887. }
  888. return false;
  889. }
  890. }
  891. class MeasureTree extends BaseTree {
  892. addData(datas) {
  893. const loadedData = [];
  894. for (const data of datas) {
  895. let node = this.getItems(data[this.setting.id]);
  896. if (node) {
  897. for (const prop in data) {
  898. if (data[prop] !== undefined) {
  899. node[prop] = data[prop];
  900. }
  901. }
  902. loadedData.push(node);
  903. } else {
  904. const keyName = itemsPre + data[this.setting.id];
  905. const node = JSON.parse(JSON.stringify(data));
  906. this.items[keyName] = node;
  907. this.datas.push(node);
  908. node.expanded = false;
  909. node.visible = true;
  910. loadedData.push(node);
  911. }
  912. }
  913. this.sortTreeNode();
  914. for (const node of loadedData) {
  915. const children = node.children;
  916. if (!node.expanded && children.length > 0) {
  917. node.expanded = true;
  918. this._refreshChildrenVisible(node);
  919. }
  920. }
  921. return loadedData;
  922. }
  923. removeData(datas) {
  924. datas.sort(function (a, b) {
  925. return b.level - a.level;
  926. });
  927. const removeArrayData = function (array, data) {
  928. const index = array.indexOf(data);
  929. array.splice(index, 1);
  930. };
  931. for (const data of datas) {
  932. const node = this.getItems(data[this.setting.id]);
  933. if (node && this.getChildren(node).length === 0) {
  934. delete this.items[itemsPre + node[this.setting.id]];
  935. if (node[this.setting.pid] !== this.setting.rootId) {
  936. const parent = this.items[itemsPre + node[this.setting.pid]];
  937. removeArrayData(parent.children, node);
  938. }
  939. removeArrayData(this.datas, node);
  940. removeArrayData(this.nodes, node);
  941. }
  942. }
  943. };
  944. loadLeafData(data) {
  945. const datas = data instanceof Array ? data : [data];
  946. for (const d of datas) {
  947. let node = this.getItems(d[this.setting.id]);
  948. if (node && node.is_leaf) {
  949. for (const prop in d) {
  950. if (data[prop] !== undefined) {
  951. node[prop] = d[prop];
  952. }
  953. }
  954. }
  955. }
  956. };
  957. }
  958. class FxTree extends BaseTree {
  959. /**
  960. * 检查节点是否是最底层项目节
  961. * @param node
  962. * @returns {boolean}
  963. */
  964. isLeafXmj(node) {
  965. if (node.b_code && node.b_code !== '') {
  966. return false;
  967. }
  968. for (const child of node.children) {
  969. if (!child.b_code || child.b_code === '') {
  970. return false;
  971. }
  972. }
  973. return true;
  974. }
  975. /**
  976. * 查询最底层项目节(本身或父项)
  977. * @param {Object} node - 查询节点
  978. * @returns {Object}
  979. */
  980. getLeafXmjParent(node) {
  981. let parent = node;
  982. while (parent) {
  983. if (this.isLeafXmj(parent)) {
  984. return parent;
  985. } else {
  986. parent = this.getParent(parent);
  987. }
  988. }
  989. return null;
  990. }
  991. /**
  992. * 展开至最底层项目节
  993. */
  994. expandToLeafXmj() {
  995. const self = this;
  996. this.expandByCustom(function (node) {
  997. if (node.b_code && node.b_code !== '') {
  998. return false;
  999. } else {
  1000. return !self.isLeafXmj(node);
  1001. }
  1002. })
  1003. }
  1004. /**
  1005. * 展开至计算项
  1006. */
  1007. expandByCalcFields() {
  1008. const self = this;
  1009. this.expandByCustom(function (node) {
  1010. for (const field of self.setting.calcFields) {
  1011. if (node[field]) {
  1012. return true;
  1013. }
  1014. }
  1015. return false;
  1016. })
  1017. }
  1018. checkCodeType() {
  1019. for (const node of this.nodes) {
  1020. if (!node.code) continue;
  1021. if (node.code.indexOf('-') >= 0) {
  1022. return '07';
  1023. } else if (node.code.length > 5) {
  1024. const num = _.toNumber(node.code);
  1025. if (num && num > 10000) return '18';
  1026. }
  1027. }
  1028. return '18';
  1029. }
  1030. getCodeSplit() {
  1031. return this.checkCodeType() === '07' ? '-' : '0';
  1032. }
  1033. }
  1034. class LedgerTree extends FxTree {
  1035. /**
  1036. *
  1037. * @param parent
  1038. * @param node
  1039. * @private
  1040. */
  1041. _getNodesParents(parents, nodes) {
  1042. for (const node of nodes) {
  1043. const parent = this.getParent(node);
  1044. if (parent) {
  1045. const paths = this.getFullPathNodes(parent.full_path);
  1046. for (const p of paths) {
  1047. if (parents.indexOf(p) === -1) {
  1048. parents.push(p);
  1049. }
  1050. }
  1051. }
  1052. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1053. parents.push(node);
  1054. }
  1055. }
  1056. }
  1057. _getReCalcNodes(reCalcNodes, nodes) {
  1058. for (const node of nodes) {
  1059. const parent = this.getParent(node);
  1060. if (parent) {
  1061. const paths = this.getFullPathNodes(parent.full_path);
  1062. for (const p of paths) {
  1063. if (reCalcNodes.indexOf(p) === -1) {
  1064. reCalcNodes.push(p);
  1065. }
  1066. }
  1067. }
  1068. // 最底层项目节,也需要计算
  1069. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1070. reCalcNodes.push(node);
  1071. //}
  1072. }
  1073. }
  1074. /**
  1075. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  1076. *
  1077. * @param data - 更新的数据 {update, create, delete}
  1078. * @returns {{}}
  1079. */
  1080. loadPostData(data) {
  1081. const result = {}, reCalcNodes = [];
  1082. if (!data) return result;
  1083. if (data.delete) {
  1084. result.delete = this._freeData(data.delete);
  1085. this._getReCalcNodes(reCalcNodes, result.delete);
  1086. }
  1087. if (data.create) {
  1088. result.create = this._loadData(data.create);
  1089. this._getReCalcNodes(reCalcNodes, result.create);
  1090. }
  1091. if (data.update) {
  1092. result.update = this._updateData(data.update);
  1093. this._getReCalcNodes(reCalcNodes, result.update);
  1094. }
  1095. reCalcNodes.sort((a, b) => {
  1096. return b.level - a.level;
  1097. });
  1098. for (const node of reCalcNodes) {
  1099. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1100. }
  1101. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1102. return result;
  1103. }
  1104. }
  1105. class ReviseTree extends LedgerTree {
  1106. constructor (setting) {
  1107. super(setting);
  1108. this.price = [];
  1109. }
  1110. checkNodeUsed(node, pos) {
  1111. if (node.children && node.children.length > 0) {
  1112. for (const child of node.children) {
  1113. const used = this.checkNodeUsed(child, pos);
  1114. if (used) return used;
  1115. }
  1116. } else {
  1117. if (node.used) return node.used;
  1118. const posRange = pos.getLedgerPos(node.id);
  1119. if (posRange && posRange.length > 0) {
  1120. for (const p of posRange) {
  1121. if (p.used) return p.used;
  1122. }
  1123. }
  1124. }
  1125. return false;
  1126. }
  1127. loadRevisePrice(price, decimal) {
  1128. this.decimal = decimal;
  1129. this.price = price || [];
  1130. this.rela_price = [];
  1131. this.common_price = [];
  1132. this.price.forEach(x => {
  1133. if (x.rela_lid) {
  1134. x.rela_lid = x.rela_lid.split(',');
  1135. this.rela_price.push(x);
  1136. } else {
  1137. this.common_price.push(x);
  1138. }
  1139. });
  1140. }
  1141. checkRevisePrice(d) {
  1142. const setting = this.setting;
  1143. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1144. const checkRela = function(rela_lid) {
  1145. if (!rela_lid || rela_lid.length === 0) return false;
  1146. for (const lid of rela_lid) {
  1147. if (pid.indexOf(lid) >= 0) return true;
  1148. }
  1149. return false;
  1150. };
  1151. let p = this.rela_price.find(x => {
  1152. return x.b_code === d.b_code &&
  1153. ((!x.name && !d.name) || x.name === d.name) &&
  1154. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1155. checkZero(x.org_price - d.unit_price) &&
  1156. checkRela(x.rela_lid);
  1157. });
  1158. if (!p) p = this.common_price.find(x => {
  1159. return x.b_code === d.b_code &&
  1160. ((!x.name && !d.name) || x.name === d.name) &&
  1161. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1162. checkZero(x.org_price - d.unit_price);
  1163. });
  1164. if (!p) return false;
  1165. d.org_price = p.org_price;
  1166. d.unit_price = p.new_price;
  1167. d.deal_tp = ZhCalc.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1168. d.sgfh_tp = ZhCalc.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1169. d.sjcl_tp = ZhCalc.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1170. d.qtcl_tp = ZhCalc.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1171. d.total_price = ZhCalc.mul(d.quantity, d.unit_price, this.decimal.tp);
  1172. return true;
  1173. }
  1174. loadDatas(datas) {
  1175. super.loadDatas(datas);
  1176. if (this.price.length > 0) {
  1177. for (const d of this.datas) {
  1178. if (d.children && d.children.length > 0) continue;
  1179. if (!d.b_code) continue;
  1180. this.checkRevisePrice(d);
  1181. }
  1182. }
  1183. }
  1184. loadPostReivsePrice(data) {
  1185. let result = false;
  1186. if (!this.price) return result;
  1187. for (const d of data) {
  1188. if (!d.is_leaf || !d.b_code) continue;
  1189. if (this.checkRevisePrice(d)) result = true;
  1190. }
  1191. return result;
  1192. }
  1193. loadPostData(data) {
  1194. const result = {}, reCalcNodes = [];
  1195. if (!data) return result;
  1196. if (data.delete) {
  1197. result.delete = this._freeData(data.delete);
  1198. this._getReCalcNodes(reCalcNodes, result.delete);
  1199. }
  1200. if (data.create) {
  1201. result.create = this._loadData(data.create);
  1202. this.loadPostReivsePrice(result.create);
  1203. this._getReCalcNodes(reCalcNodes, result.create);
  1204. }
  1205. if (data.update) {
  1206. result.update = this._updateData(data.update);
  1207. this.loadPostReivsePrice(result.update);
  1208. this._getReCalcNodes(reCalcNodes, result.update);
  1209. }
  1210. reCalcNodes.sort((a, b) => {
  1211. return b.level - a.level;
  1212. });
  1213. for (const node of reCalcNodes) {
  1214. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1215. }
  1216. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1217. return result;
  1218. }
  1219. }
  1220. class StageTree extends FxTree {
  1221. /**
  1222. * 构造函数
  1223. */
  1224. constructor(setting) {
  1225. super(setting);
  1226. // stage关联索引
  1227. this.stageItems = {};
  1228. }
  1229. _loadLockedNodes(ids){
  1230. this.Locked = ids || [];
  1231. for (const f of this.Locked) {
  1232. const node = this.getItems(f.ledger_id);
  1233. node.locked = true;
  1234. const posterity = this.getPosterity(node);
  1235. for (const pn of posterity) {
  1236. pn.locked = true;
  1237. pn.lock = true;
  1238. }
  1239. }
  1240. }
  1241. /**
  1242. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1243. * @param datas
  1244. */
  1245. loadDatas(datas, locked) {
  1246. super.loadDatas(datas);
  1247. // 清空旧数据
  1248. this.stageItems = {};
  1249. // 加载全部数据
  1250. for (const data of this.datas) {
  1251. const keyName = itemsPre + data[this.setting.stageId];
  1252. this.stageItems[keyName] = data;
  1253. }
  1254. this._loadLockedNodes(locked)
  1255. }
  1256. // 解锁相关
  1257. _loadPwdCache() {
  1258. const cache = getLocalCache(this.pwdCacheKey);
  1259. if (!cache) return;
  1260. const cacheArr = cache.split('|');
  1261. for (const ca of cacheArr) {
  1262. if (!ca) continue;
  1263. const [ledger_id, pwd] = ca.split(':');
  1264. if (!ledger_id || !pwd) continue;
  1265. const p = this.pwd.find(x => {return x.ledger_id == ledger_id});
  1266. if (p) p.check = p.pwd === pwd;
  1267. }
  1268. }
  1269. loadPwd(data, cacheKey, confirmList) {
  1270. this.loadingPwd = true;
  1271. try {
  1272. this.pwdCacheKey = cacheKey;
  1273. this.confirmList = confirmList;
  1274. this.pwd = data;
  1275. this._loadPwdCache();
  1276. this._loadOnlineConfirm();
  1277. for (const p of this.pwd) {
  1278. p.node = this.getItems(p.ledger_id);
  1279. this.lockNode(p, !p.check);
  1280. }
  1281. } catch(err) {}
  1282. this.loadingPwd = false;
  1283. }
  1284. // 确认相关
  1285. _loadOnlineConfirm() {
  1286. const cacheArr = this.confirmList;
  1287. for (const ca of cacheArr) {
  1288. if (!ca) continue;
  1289. if (!ca.ledger_id) continue;
  1290. const p = this.pwd.find(x => {return x.ledger_id == ca.ledger_id});
  1291. if (p) {
  1292. p.confirm = true;
  1293. p.confirm_time = ca.create_time;
  1294. }
  1295. }
  1296. }
  1297. getStageItems(id) {
  1298. return this.stageItems[itemsPre + id];
  1299. }
  1300. loadStageData(datas, fieldPre, fields) {
  1301. datas = datas instanceof Array ? datas : [datas];
  1302. const loadedData = [];
  1303. for (const data of datas) {
  1304. let node = this.getStageItems(data.lid);
  1305. if (node) {
  1306. for (const prop of fields) {
  1307. if (data[prop] !== undefined) {
  1308. node[fieldPre + prop] = data[prop];
  1309. }
  1310. }
  1311. loadedData.push(node);
  1312. }
  1313. }
  1314. }
  1315. loadPreStageData(datas) {
  1316. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1317. }
  1318. loadCurStageData(datas) {
  1319. this.loadStageData(datas, '', this.setting.updateFields);
  1320. }
  1321. /**
  1322. * 加载数据(动态),只加载不同部分
  1323. * @param {Array} datas
  1324. * @return {Array} 加载到树的数据
  1325. * @privateA
  1326. */
  1327. _updateData(datas) {
  1328. datas = datas instanceof Array ? datas : [datas];
  1329. let loadedData = [];
  1330. for (const data of datas) {
  1331. let node = this.getItems(data[this.setting.id]);
  1332. if (node) {
  1333. for (const prop in data) {
  1334. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1335. }
  1336. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1337. if (prop === this.setting.pid) {
  1338. loadedData.push(this.getItems(node[this.setting.pid]));
  1339. loadedData.push(this.getItems(data[this.setting.pid]));
  1340. }
  1341. node[prop] = data[prop];
  1342. }
  1343. }
  1344. loadedData.push(node);
  1345. }
  1346. }
  1347. loadedData = _.uniq(loadedData);
  1348. for (const node of loadedData) {
  1349. node.children = this.getChildren(node);
  1350. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1351. }
  1352. this.sortTreeNode(true);
  1353. return loadedData;
  1354. };
  1355. /**
  1356. * 加载数据(动态),只加载不同部分
  1357. * @param {Array} datas
  1358. * @return {Array} 加载到树的数据
  1359. * @privateA
  1360. */
  1361. _updateStageData(datas) {
  1362. datas = datas instanceof Array ? datas : [datas];
  1363. const loadedData = [];
  1364. for (const data of datas) {
  1365. let node = this.getStageItems(data.lid);
  1366. if (node) {
  1367. for (const prop of this.setting.updateFields) {
  1368. if (data[prop] !== undefined) {
  1369. node[prop] = data[prop];
  1370. }
  1371. }
  1372. loadedData.push(node);
  1373. }
  1374. }
  1375. return loadedData;
  1376. };
  1377. /**
  1378. *
  1379. * @param parent
  1380. * @param node
  1381. * @private
  1382. */
  1383. _getNodesParents(parents, nodes) {
  1384. for (const node of nodes) {
  1385. const parent = this.getParent(node);
  1386. if (parent) {
  1387. const paths = this.getFullPathNodes(parent.full_path);
  1388. for (const p of paths) {
  1389. if (parents.indexOf(p) === -1) {
  1390. parents.push(p);
  1391. }
  1392. }
  1393. }
  1394. if (node.children && node.children.length > 0) {
  1395. parents.push(node);
  1396. }
  1397. }
  1398. }
  1399. _updateDgnData(datas) {
  1400. datas = datas instanceof Array ? datas : [datas];
  1401. let loadedData = [];
  1402. for (const data of datas) {
  1403. let node = this.getStageItems(data.id);
  1404. if (node) {
  1405. for (const prop in data) {
  1406. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1407. node[prop] = data[prop];
  1408. }
  1409. }
  1410. loadedData.push(node);
  1411. }
  1412. }
  1413. return loadedData;
  1414. }
  1415. /**
  1416. * 提交数据至后端,返回的前端树结构应刷新的部分
  1417. * StageTree仅有更新CurStage部分,不需要增删
  1418. *
  1419. * @param data - 需要更新的数据
  1420. * @returns {Array} - 界面需要刷新的数据
  1421. */
  1422. loadPostStageData(data) {
  1423. let result, parents = [];
  1424. if (data.bills) {
  1425. result = this._updateData(data.bills);
  1426. this._getNodesParents(parents, result);
  1427. }
  1428. if (data.curStageData) {
  1429. result = this._updateStageData(data.curStageData);
  1430. this._getNodesParents(parents, result);
  1431. }
  1432. if (data.dgn) {
  1433. const dgnResult = this._updateDgnData(data.dgn);
  1434. result = result ? result.concat(dgnResult) : dgnResult;
  1435. }
  1436. result = result ? result.concat(parents) : parents;
  1437. result.sort((a, b) => {
  1438. return b.level - a.level;
  1439. });
  1440. for (const node of result) {
  1441. treeCalc.calculateNode(this, node);
  1442. }
  1443. return result;
  1444. }
  1445. _savePwdCache() {
  1446. const cache = [];
  1447. for (const p of this.pwd) {
  1448. if (p.check && p.pwd) cache.push(p.ledger_id + ':' + p.pwd);
  1449. }
  1450. setLocalCache(this.pwdCacheKey, cache.join('|'));
  1451. }
  1452. _getPwdPosterity(node) {
  1453. const children = [];
  1454. for (const c of node.children) {
  1455. const cPwd = this.pwd.find(x => {return x.node && x.node.id === c.id});
  1456. if (!cPwd) children.push(c);
  1457. }
  1458. let posterity = [...children];
  1459. for (const c of children) {
  1460. posterity = posterity.concat(this._getPwdPosterity(c));
  1461. }
  1462. return posterity;
  1463. }
  1464. lockNode(p, isLock) {
  1465. const refresh = [];
  1466. p.check = !isLock;
  1467. p.node.lock = isLock;
  1468. refresh.push(this.getNodeIndex(p.node));
  1469. const posterity = this._getPwdPosterity(p.node);
  1470. for (const pn of posterity) {
  1471. pn.lock = pn.locked || isLock;
  1472. refresh.push(this.getNodeIndex(pn));
  1473. }
  1474. if (!this.loadingPwd) this._savePwdCache();
  1475. return refresh;
  1476. }
  1477. getLockedInfo(node) {
  1478. const ownerNodes = this.getAllParents(node);
  1479. const lockedInfo = [];
  1480. for (const on of ownerNodes) {
  1481. const filter = this.Locked.filter(x => { return x.ledger_id === on.ledger_id; });
  1482. lockedInfo.push(...filter);
  1483. }
  1484. return lockedInfo;
  1485. }
  1486. }
  1487. class MasterTree extends FxTree {
  1488. /**
  1489. * 构造函数
  1490. */
  1491. constructor(setting) {
  1492. super(setting);
  1493. // 关联索引
  1494. this.masterItems = {};
  1495. }
  1496. /**
  1497. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1498. * @param datas
  1499. */
  1500. loadDatas(datas) {
  1501. super.loadDatas(datas);
  1502. // 清空旧数据
  1503. this.masterItems = {};
  1504. // minor数据缓存
  1505. this.minorData = {};
  1506. // 加载全部数据
  1507. for (const data of this.datas) {
  1508. const keyName = itemsPre + data[this.setting.masterId];
  1509. this.masterItems[keyName] = data;
  1510. }
  1511. }
  1512. /**
  1513. * 根据关联id,查找节点
  1514. * @param id
  1515. * @returns {*}
  1516. */
  1517. getMasterItems(id) {
  1518. return this.masterItems[itemsPre + id];
  1519. }
  1520. /**
  1521. * 加载关联数据
  1522. *
  1523. * @param {Array|Object}datas - 需要关联的数据
  1524. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1525. * @param {Array} fields - 关联字段
  1526. * @returns {Array}
  1527. */
  1528. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1529. for (const cf of calcFields) {
  1530. this.setting.calcFields.push(cf + fieldSuf);
  1531. }
  1532. if (!datas) return;
  1533. datas = datas instanceof Array ? datas : [datas];
  1534. this.minorData[fieldSuf] = datas;
  1535. const loadedData = [];
  1536. for (const data of datas) {
  1537. let node = this.getMasterItems(data[this.setting.minorId]);
  1538. if (node) {
  1539. for (const prop of fields) {
  1540. if (data[prop] !== undefined) {
  1541. node[prop + fieldSuf] = data[prop];
  1542. }
  1543. }
  1544. loadedData.push(node);
  1545. }
  1546. }
  1547. return loadedData;
  1548. }
  1549. }
  1550. class FilterTree extends BaseTree {
  1551. clearDatas() {
  1552. this.items = {};
  1553. this.nodes = [];
  1554. this.datas = [];
  1555. this.children = [];
  1556. }
  1557. addData(data, fields) {
  1558. const item = {};
  1559. for (const prop in data) {
  1560. if (fields.indexOf(prop) >= 0) {
  1561. item[prop] = data[prop];
  1562. }
  1563. }
  1564. const keyName = itemsPre + item[this.setting.id];
  1565. if (!this.items[keyName]) {
  1566. item.children = [];
  1567. item.is_leaf = true;
  1568. item.expanded = true;
  1569. item.visible = true;
  1570. this.items[keyName] = item;
  1571. this.datas.push(item);
  1572. if (item[setting.pid] === setting.rootId) {
  1573. this.children.push(item);
  1574. } else {
  1575. const parent = this.getParent(item);
  1576. if (parent) {
  1577. parent.is_leaf = false;
  1578. parent.children.push(item);
  1579. }
  1580. }
  1581. } else {
  1582. return this.items[keyName];
  1583. }
  1584. return item;
  1585. }
  1586. }
  1587. class GatherTree extends BaseTree {
  1588. clearDatas() {
  1589. this.items = {};
  1590. this.nodes = [];
  1591. this.datas = [];
  1592. this.children = [];
  1593. }
  1594. get newId() {
  1595. if (!this._maxId) {
  1596. this._maxId = 0;
  1597. }
  1598. this._maxId++;
  1599. return this._maxId;
  1600. }
  1601. addNode(data, parent) {
  1602. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1603. let item = _.find(this.items, data);
  1604. if (item) return item;
  1605. item = data;
  1606. item[this.setting.id] = this.newId;
  1607. const keyName = itemsPre + item[this.setting.id];
  1608. item.children = [];
  1609. item.is_leaf = true;
  1610. item.expanded = true;
  1611. item.visible = true;
  1612. this.items[keyName] = item;
  1613. this.datas.push(item);
  1614. if (parent) {
  1615. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1616. item[this.setting.level] = parent[this.setting.level] + 1;
  1617. item[this.setting.order] = parent.children.length + 1;
  1618. parent.is_leaf = false;
  1619. parent.children.push(item);
  1620. } else {
  1621. item[this.setting.fullPath] = '' + item[this.setting.id];
  1622. item[this.setting.level] = 1;
  1623. item[this.setting.order] = this.children.length + 1;
  1624. this.children.push(item);
  1625. }
  1626. return item;
  1627. }
  1628. sortTreeNodeCustom(field, fun, isResort) {
  1629. const self = this;
  1630. const sortNodes = function (nodes) {
  1631. nodes.sort(function (a, b) {
  1632. return fun(a[field], b[field]);
  1633. });
  1634. for (const [i, node] of nodes.entries()) {
  1635. node[self.setting.order] = i + 1;
  1636. }
  1637. };
  1638. const addSortNodes = function (nodes) {
  1639. if (!nodes) { return }
  1640. for (let i = 0; i < nodes.length; i++) {
  1641. self.nodes.push(nodes[i]);
  1642. nodes[i].index = self.nodes.length - 1;
  1643. if (!isResort) {
  1644. nodes[i].children = self.getChildren(nodes[i]);
  1645. }
  1646. sortNodes(nodes[i].children);
  1647. addSortNodes(nodes[i].children);
  1648. }
  1649. };
  1650. this.nodes = [];
  1651. if (!isResort) {
  1652. this.children = this.getChildren();
  1653. }
  1654. sortNodes(this.children);
  1655. addSortNodes(this.children);
  1656. }
  1657. }
  1658. class CompareTree extends FxTree {
  1659. constructor(setting) {
  1660. super(setting);
  1661. this._newId = 1;
  1662. }
  1663. get newId() {
  1664. return this._newId++;
  1665. }
  1666. findCompareNode(node, parent) {
  1667. if (this.setting.findNode) {
  1668. return this.setting.findNode(this, node, parent);
  1669. } else {
  1670. const siblings = parent ? parent.children : this.children;
  1671. return siblings.find(function (x) {
  1672. return node.b_code
  1673. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1674. : x.code === node.code && x.name === node.name;
  1675. });
  1676. }
  1677. }
  1678. loadCompareNode(source, node, parent, loadFun) {
  1679. let cur = this.findCompareNode(node, parent);
  1680. if (!cur) {
  1681. const siblings = parent ? parent.children : this.children;
  1682. const id = this.newId;
  1683. cur = {
  1684. children: [], pos: [],
  1685. code: node.code, b_code: node.b_code, name: node.name,
  1686. unit: node.unit, unit_price: node.unit_price,
  1687. };
  1688. cur[this.setting.id] = id;
  1689. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1690. cur[this.setting.full_path] = parent ? parent[this.setting.full_path] + '-' + id : '' + id;
  1691. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1692. cur[this.setting.order] = siblings.length + 1;
  1693. siblings.push(cur);
  1694. this.datas.push(cur);
  1695. }
  1696. loadFun(cur, node, source);
  1697. for (const c of node.children) {
  1698. this.loadCompareNode(source, c, cur, loadFun);
  1699. }
  1700. }
  1701. generateSortNodes() {
  1702. const self = this;
  1703. const addSortNode = function (node) {
  1704. self.nodes.push(node);
  1705. for (const c of node.children) {
  1706. addSortNode(c);
  1707. }
  1708. }
  1709. this.nodes = [];
  1710. for (const n of this.children) {
  1711. addSortNode(n);
  1712. }
  1713. }
  1714. loadCompareTree(data, loadFun) {
  1715. for (const c of data.billsTree.children) {
  1716. this.loadCompareNode(data, c, null, loadFun);
  1717. }
  1718. }
  1719. calculateDiffer() {
  1720. if (this.setting.calcDiffer) {
  1721. for (const d of this.datas) {
  1722. this.setting.calcDiffer(d);
  1723. }
  1724. }
  1725. }
  1726. loadCompareData(data1, data2) {
  1727. this.loadCompareTree(data1, this.setting.loadInfo1);
  1728. this.loadCompareTree(data2, this.setting.loadInfo2);
  1729. for (const d of this.datas) {
  1730. d.is_leaf = d.children.length === 0;
  1731. d.expanded = true;
  1732. d.visible = true;
  1733. this.items[itemsPre + d[this.setting.id]] = d;
  1734. }
  1735. this.generateSortNodes();
  1736. this.calculateDiffer();
  1737. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1738. }
  1739. }
  1740. class TreeGatherTree extends FxTree {
  1741. constructor(setting) {
  1742. super(setting);
  1743. this._newId = 1;
  1744. }
  1745. get newId() {
  1746. return this._newId++;
  1747. }
  1748. loadGatherNode(node, parent, index, loadFun) {
  1749. const siblings = parent ? parent.children : this.children;
  1750. let cur = siblings.find(function (x) {
  1751. return node.b_code
  1752. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1753. : x.code === node.code && x.name === node.name;
  1754. });
  1755. if (!cur) {
  1756. const id = this.newId;
  1757. cur = {
  1758. id: id,
  1759. pid: parent ? parent.id : this.setting.rootId,
  1760. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1761. level: parent ? parent.level + 1 : 1,
  1762. order: siblings.length + 1,
  1763. children: [],
  1764. code: node.code, b_code: node.b_code, name: node.name,
  1765. unit: node.unit, unit_price: node.unit_price,
  1766. };
  1767. siblings.push(cur);
  1768. this.datas.push(cur);
  1769. }
  1770. loadFun(cur, node, index);
  1771. for (const c of node.children) {
  1772. this.loadGatherNode(c, cur, index, loadFun);
  1773. }
  1774. }
  1775. generateSortNodes() {
  1776. const self = this;
  1777. const addSortNode = function (node) {
  1778. self.nodes.push(node);
  1779. for (const c of node.children) {
  1780. addSortNode(c);
  1781. }
  1782. };
  1783. this.nodes = [];
  1784. for (const n of this.children) {
  1785. addSortNode(n);
  1786. }
  1787. }
  1788. loadGatherTree(data, index, loadFun) {
  1789. for (const c of data.billsTree.children) {
  1790. this.loadGatherNode(c, null, index, loadFun);
  1791. }
  1792. // todo load Pos Data;
  1793. }
  1794. calculateSum() {
  1795. if (this.setting.calcSum) {
  1796. for (const d of this.datas) {
  1797. this.setting.calcSum(d, this.count);
  1798. }
  1799. }
  1800. }
  1801. loadGatherData(datas) {
  1802. this.count = datas.length;
  1803. for (const [i, data] of datas.entries()) {
  1804. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1805. }
  1806. for (const d of this.datas) {
  1807. d.is_leaf = d.children.length === 0;
  1808. d.expanded = true;
  1809. d.visible = true;
  1810. this.items[itemsPre + d[this.setting.id]] = d;
  1811. }
  1812. this.generateSortNodes();
  1813. this.calculateSum();
  1814. }
  1815. }
  1816. class FinalTree extends BaseTree {
  1817. constructor(setting) {
  1818. super(setting);
  1819. this._newId = 1;
  1820. }
  1821. get newId() {
  1822. return this._newId++;
  1823. }
  1824. loadNode(node, parent, loadFun) {
  1825. if (node.b_code) return;
  1826. const siblings = parent ? parent.children : this.children;
  1827. let cur = siblings.find(function (x) {
  1828. return x.code === node.code && x.name === node.name;
  1829. });
  1830. if (!cur) {
  1831. const id = this.newId;
  1832. cur = {
  1833. id: id,
  1834. pid: parent ? parent.id : this.setting.rootId,
  1835. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1836. level: parent ? parent.level + 1 : 1,
  1837. order: siblings.length + 1,
  1838. children: [],
  1839. code: node.code, name: node.name, unit: node.unit,
  1840. };
  1841. siblings.push(cur);
  1842. this.datas.push(cur);
  1843. }
  1844. loadFun(cur, node);
  1845. for (const c of node.children) {
  1846. this.loadNode(c, cur, loadFun);
  1847. }
  1848. }
  1849. loadTree(tree, loadFun) {
  1850. for (const node of tree.children) {
  1851. this.loadNode(node, null, loadFun);
  1852. }
  1853. }
  1854. generateSortNodes() {
  1855. const self = this;
  1856. const addSortNode = function (node) {
  1857. self.nodes.push(node);
  1858. for (const c of node.children) {
  1859. addSortNode(c);
  1860. }
  1861. };
  1862. this.nodes = [];
  1863. for (const n of this.children) {
  1864. addSortNode(n);
  1865. }
  1866. }
  1867. afterLoad(fun) {
  1868. for (const d of this.datas) {
  1869. fun && fun(d);
  1870. d.is_leaf = d.children.length === 0;
  1871. d.expanded = true;
  1872. d.visible = true;
  1873. this.items[itemsPre + d[this.setting.id]] = d;
  1874. }
  1875. this.generateSortNodes();
  1876. }
  1877. clearFinal() {
  1878. this.datas = this.datas.filter(x => {
  1879. return !!x.base;
  1880. });
  1881. this.datas.forEach(x => {
  1882. delete x.total_price;
  1883. delete x.dgn_qty1;
  1884. delete x.dgn_qty2;
  1885. delete x.dgn_qty;
  1886. delete x.dgn_price;
  1887. delete x.final_dgn_qty1;
  1888. delete x.final_dgn_qty2;
  1889. delete x.final_dgn_qty;
  1890. delete x.final_dgn_price;
  1891. delete x.final_tp;
  1892. delete x.grow_dgn_qty1;
  1893. delete x.grow_dgn_qty2;
  1894. delete x.grow_dgn_qty;
  1895. delete x.grow_tp;
  1896. });
  1897. this.loadDatas(this.datas);
  1898. }
  1899. resortChildrenByCustom(fun) {
  1900. for (const n of this.nodes) {
  1901. if (n.children && n.children.length > 1) {
  1902. n.children.sort(fun);
  1903. n.children.forEach((x, y) => { x.order = y + 1; });
  1904. }
  1905. }
  1906. this.generateSortNodes();
  1907. }
  1908. }
  1909. if (type === 'base') {
  1910. return new BaseTree(setting);
  1911. } else if (type === 'fx') {
  1912. return new FxTree(setting);
  1913. } else if (type === 'stage') {
  1914. return new StageTree(setting);
  1915. } else if (type === 'ledger') {
  1916. return new LedgerTree(setting);
  1917. } else if (type === 'revise') {
  1918. return new ReviseTree(setting);
  1919. } else if (type === 'measure') {
  1920. return new MeasureTree(setting);
  1921. } else if (type === 'master') {
  1922. return new MasterTree(setting);
  1923. } else if (type === 'filter') {
  1924. return new FilterTree(setting);
  1925. } else if (type === 'gather') {
  1926. return new GatherTree(setting);
  1927. } else if (type === 'compare') {
  1928. return new CompareTree(setting);
  1929. } else if (type === 'tree-gather') {
  1930. return new TreeGatherTree(setting);
  1931. } else if (type === 'final') {
  1932. return new FinalTree(setting);
  1933. }
  1934. };
  1935. const treeCalc = {
  1936. mapTreeNode: function (tree) {
  1937. let map = {}, maxLevel = 0;
  1938. for (const node of tree.nodes) {
  1939. let levelArr = map[node.level];
  1940. if (!levelArr) {
  1941. levelArr = [];
  1942. map[node.level] = levelArr;
  1943. }
  1944. if (node.level > maxLevel) {
  1945. maxLevel = node.level;
  1946. }
  1947. levelArr.push(node);
  1948. }
  1949. return [maxLevel, map];
  1950. },
  1951. getMaxLevel: function (tree) {
  1952. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1953. },
  1954. calculateNode: function (tree, node) {
  1955. if (node.children && node.children.length > 0) {
  1956. const gather = node.children.reduce(function (rst, x) {
  1957. const result = {};
  1958. for (const cf of tree.setting.calcFields) {
  1959. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1960. }
  1961. return result;
  1962. });
  1963. // 汇总子项
  1964. for (const cf of tree.setting.calcFields) {
  1965. if (gather[cf]) {
  1966. node[cf] = gather[cf];
  1967. } else {
  1968. node[cf] = null;
  1969. }
  1970. }
  1971. }
  1972. // 自身运算
  1973. if (tree.setting.calcFun) {
  1974. tree.setting.calcFun(node);
  1975. }
  1976. },
  1977. calculateLevelNode: function (tree, level) {
  1978. const nodes = tree.datas.filter((n) => { return n.level === level });
  1979. for (const node of nodes) {
  1980. this.calculateNode(tree, node);
  1981. }
  1982. },
  1983. calculateAll: function (tree) {
  1984. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1985. for (let i = maxLevel; i >= 0; i--) {
  1986. const levelNodes = levelMap[i];
  1987. if (levelNodes && levelNodes.length > 0) {
  1988. for (const node of levelNodes) {
  1989. this.calculateNode(tree, node);
  1990. }
  1991. }
  1992. }
  1993. },
  1994. calculateParent: function (tree, node) {
  1995. const nodes = tree.getFullPathNodes(node.full_path);
  1996. nodes.sort((a, b) => {
  1997. return b.level - a.level;
  1998. });
  1999. for (const n of nodes) {
  2000. this.calculateNode(tree, n);
  2001. }
  2002. return nodes;
  2003. }
  2004. };