path_tree.js 72 KB

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