path_tree.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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.b_code === '203-3-2') console.log(d);
  1206. if (d.settle_status) return false;
  1207. if (!this.price || this.price.length === 0) return false;
  1208. const setting = this.setting;
  1209. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1210. const checkRela = function(rela_lid) {
  1211. if (!rela_lid || rela_lid.length === 0) return false;
  1212. for (const lid of rela_lid) {
  1213. if (pid.indexOf(lid) >= 0) return true;
  1214. }
  1215. return false;
  1216. };
  1217. let p = this.rela_price.find(x => {
  1218. return x.b_code === d.b_code &&
  1219. ((!x.name && !d.name) || x.name === d.name) &&
  1220. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1221. checkZero(x.org_price - d.unit_price) &&
  1222. checkRela(x.rela_lid);
  1223. });
  1224. if (!p) p = this.common_price.find(x => {
  1225. return x.b_code === d.b_code &&
  1226. ((!x.name && !d.name) || x.name === d.name) &&
  1227. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1228. checkZero(x.org_price - d.unit_price);
  1229. });
  1230. if (!p) return false;
  1231. d.org_price = p.org_price;
  1232. d.unit_price = p.new_price;
  1233. d.deal_tp = ZhCalc.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1234. d.sgfh_tp = ZhCalc.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1235. d.sjcl_tp = ZhCalc.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1236. d.qtcl_tp = ZhCalc.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1237. d.total_price = ZhCalc.mul(d.quantity, d.unit_price, this.decimal.tp);
  1238. return true;
  1239. }
  1240. loadDatas(datas) {
  1241. super.loadDatas(datas);
  1242. if (this.price.length > 0) {
  1243. for (const d of this.datas) {
  1244. if (d.children && d.children.length > 0) continue;
  1245. if (!d.b_code) continue;
  1246. this.checkRevisePrice(d);
  1247. }
  1248. }
  1249. }
  1250. loadPostReivsePrice(data) {
  1251. let result = false;
  1252. if (!this.price) return result;
  1253. for (const d of data) {
  1254. if (!d.is_leaf || !d.b_code) continue;
  1255. if (this.checkRevisePrice(d)) result = true;
  1256. }
  1257. return result;
  1258. }
  1259. loadPostData(data) {
  1260. const setting = this.setting;
  1261. const result = {}, reCalcNodes = [];
  1262. if (!data) return result;
  1263. if (data.delete) {
  1264. result.delete = this._freeData(data.delete);
  1265. this._getReCalcNodes(reCalcNodes, result.delete);
  1266. }
  1267. if (data.create) {
  1268. result.create = this._loadData(data.create);
  1269. this.loadPostReivsePrice(result.create);
  1270. this._getReCalcNodes(reCalcNodes, result.create);
  1271. }
  1272. if (data.update) {
  1273. result.update = this._updateData(data.update);
  1274. this.loadPostReivsePrice(result.update);
  1275. this._getReCalcNodes(reCalcNodes, result.update);
  1276. }
  1277. this.sortByLevelConverse(reCalcNodes);
  1278. for (const node of reCalcNodes) {
  1279. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1280. }
  1281. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1282. return result;
  1283. }
  1284. }
  1285. class StageTree extends FxTree {
  1286. /**
  1287. * 构造函数
  1288. */
  1289. constructor(setting) {
  1290. super(setting);
  1291. // stage关联索引
  1292. this.stageItems = {};
  1293. }
  1294. _loadLockedNodes(ids){
  1295. this.Locked = ids || [];
  1296. for (const f of this.Locked) {
  1297. f.ass_ledger_id = f.ass_ledger_id ? f.ass_ledger_id.split(',') : [];
  1298. for (const id of f.ass_ledger_id) {
  1299. const node = this.getItems(id);
  1300. node.locked = true;
  1301. const posterity = this.getPosterity(node);
  1302. for (const pn of posterity) {
  1303. pn.locked = true;
  1304. pn.lock = true;
  1305. }
  1306. }
  1307. }
  1308. }
  1309. /**
  1310. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1311. * @param datas
  1312. */
  1313. loadDatas(datas, locked) {
  1314. super.loadDatas(datas);
  1315. // 清空旧数据
  1316. this.stageItems = {};
  1317. // 加载全部数据
  1318. for (const data of this.datas) {
  1319. const keyName = itemsPre + data[this.setting.stageId];
  1320. this.stageItems[keyName] = data;
  1321. }
  1322. this._loadLockedNodes(locked)
  1323. }
  1324. getStageItems(id) {
  1325. return this.stageItems[itemsPre + id];
  1326. }
  1327. loadStageData(datas, fieldPre, fields) {
  1328. datas = datas instanceof Array ? datas : [datas];
  1329. const loadedData = [];
  1330. for (const data of datas) {
  1331. let node = this.getStageItems(data.lid);
  1332. if (node) {
  1333. for (const prop of fields) {
  1334. if (data[prop] !== undefined) {
  1335. node[fieldPre + prop] = data[prop];
  1336. }
  1337. }
  1338. loadedData.push(node);
  1339. }
  1340. }
  1341. }
  1342. loadPreStageData(datas) {
  1343. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1344. }
  1345. loadCurStageData(datas) {
  1346. this.loadStageData(datas, '', this.setting.updateFields);
  1347. }
  1348. /**
  1349. * 加载数据(动态),只加载不同部分
  1350. * @param {Array} datas
  1351. * @return {Array} 加载到树的数据
  1352. * @privateA
  1353. */
  1354. _updateData(datas) {
  1355. datas = datas instanceof Array ? datas : [datas];
  1356. let loadedData = [];
  1357. for (const data of datas) {
  1358. let node = this.getItems(data[this.setting.id]);
  1359. if (node) {
  1360. for (const prop in data) {
  1361. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1362. }
  1363. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1364. if (prop === this.setting.pid) {
  1365. loadedData.push(this.getItems(node[this.setting.pid]));
  1366. loadedData.push(this.getItems(data[this.setting.pid]));
  1367. }
  1368. node[prop] = data[prop];
  1369. }
  1370. }
  1371. loadedData.push(node);
  1372. }
  1373. }
  1374. loadedData = _.uniq(loadedData);
  1375. for (const node of loadedData) {
  1376. node.children = this.getChildren(node);
  1377. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1378. }
  1379. this.sortTreeNode(true);
  1380. return loadedData;
  1381. };
  1382. /**
  1383. * 加载数据(动态),只加载不同部分
  1384. * @param {Array} datas
  1385. * @return {Array} 加载到树的数据
  1386. * @privateA
  1387. */
  1388. _updateStageData(datas) {
  1389. datas = datas instanceof Array ? datas : [datas];
  1390. const loadedData = [];
  1391. for (const data of datas) {
  1392. let node = this.getStageItems(data.lid);
  1393. if (node) {
  1394. for (const prop of this.setting.updateFields) {
  1395. if (data[prop] !== undefined) {
  1396. node[prop] = data[prop];
  1397. }
  1398. }
  1399. loadedData.push(node);
  1400. }
  1401. }
  1402. return loadedData;
  1403. };
  1404. /**
  1405. *
  1406. * @param parent
  1407. * @param node
  1408. * @private
  1409. */
  1410. _getNodesParents(parents, nodes) {
  1411. for (const node of nodes) {
  1412. const parent = this.getParent(node);
  1413. if (parent) {
  1414. const paths = this.getFullPathNodes(parent[this.setting.fullPath]);
  1415. for (const p of paths) {
  1416. if (parents.indexOf(p) === -1) {
  1417. parents.push(p);
  1418. }
  1419. }
  1420. }
  1421. if (node.children && node.children.length > 0) {
  1422. parents.push(node);
  1423. }
  1424. }
  1425. }
  1426. _updateDgnData(datas) {
  1427. datas = datas instanceof Array ? datas : [datas];
  1428. let loadedData = [];
  1429. for (const data of datas) {
  1430. let node = this.getStageItems(data.id);
  1431. if (node) {
  1432. for (const prop in data) {
  1433. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1434. node[prop] = data[prop];
  1435. }
  1436. }
  1437. loadedData.push(node);
  1438. }
  1439. }
  1440. return loadedData;
  1441. }
  1442. /**
  1443. * 提交数据至后端,返回的前端树结构应刷新的部分
  1444. * StageTree仅有更新CurStage部分,不需要增删
  1445. *
  1446. * @param data - 需要更新的数据
  1447. * @returns {Array} - 界面需要刷新的数据
  1448. */
  1449. loadPostStageData(data) {
  1450. let result, parents = [];
  1451. if (data.bills) {
  1452. result = this._updateData(data.bills);
  1453. this._getNodesParents(parents, result);
  1454. }
  1455. if (data.curStageData) {
  1456. result = this._updateStageData(data.curStageData);
  1457. this._getNodesParents(parents, result);
  1458. }
  1459. if (data.dgn) {
  1460. const dgnResult = this._updateDgnData(data.dgn);
  1461. result = result ? result.concat(dgnResult) : dgnResult;
  1462. }
  1463. result = result ? result.concat(parents) : parents;
  1464. this.sortByLevelConverse(result);
  1465. for (const node of result) {
  1466. treeCalc.calculateNode(this, node);
  1467. }
  1468. return result;
  1469. }
  1470. getLockedInfo(node) {
  1471. const ownerNodes = this.getAllParents(node);
  1472. const lockedInfo = [];
  1473. for (const on of ownerNodes) {
  1474. const filter = this.Locked.filter(x => { return x.ass_ledger_id.indexOf(on.ledger_id + '') >= 0; });
  1475. lockedInfo.push(...filter);
  1476. }
  1477. return lockedInfo;
  1478. }
  1479. }
  1480. class MasterTree extends FxTree {
  1481. /**
  1482. * 构造函数
  1483. */
  1484. constructor(setting) {
  1485. super(setting);
  1486. // 关联索引
  1487. this.masterItems = {};
  1488. }
  1489. /**
  1490. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1491. * @param datas
  1492. */
  1493. loadDatas(datas) {
  1494. super.loadDatas(datas);
  1495. // 清空旧数据
  1496. this.masterItems = {};
  1497. // minor数据缓存
  1498. this.minorData = {};
  1499. // 加载全部数据
  1500. for (const data of this.datas) {
  1501. const keyName = itemsPre + data[this.setting.masterId];
  1502. this.masterItems[keyName] = data;
  1503. }
  1504. }
  1505. /**
  1506. * 根据关联id,查找节点
  1507. * @param id
  1508. * @returns {*}
  1509. */
  1510. getMasterItems(id) {
  1511. return this.masterItems[itemsPre + id];
  1512. }
  1513. /**
  1514. * 加载关联数据
  1515. *
  1516. * @param {Array|Object}datas - 需要关联的数据
  1517. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1518. * @param {Array} fields - 关联字段
  1519. * @returns {Array}
  1520. */
  1521. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1522. for (const cf of calcFields) {
  1523. this.setting.calcFields.push(cf + fieldSuf);
  1524. }
  1525. if (!datas) return;
  1526. datas = datas instanceof Array ? datas : [datas];
  1527. this.minorData[fieldSuf] = datas;
  1528. const loadedData = [];
  1529. for (const data of datas) {
  1530. let node = this.getMasterItems(data[this.setting.minorId]);
  1531. if (node) {
  1532. for (const prop of fields) {
  1533. if (data[prop] !== undefined) {
  1534. node[prop + fieldSuf] = data[prop];
  1535. }
  1536. }
  1537. loadedData.push(node);
  1538. }
  1539. }
  1540. return loadedData;
  1541. }
  1542. }
  1543. class FilterTree extends BaseTree {
  1544. clearDatas() {
  1545. this.items = {};
  1546. this.nodes = [];
  1547. this.datas = [];
  1548. this.children = [];
  1549. }
  1550. addData(data, fields) {
  1551. const item = {};
  1552. for (const prop in data) {
  1553. if (fields.indexOf(prop) >= 0) {
  1554. item[prop] = data[prop];
  1555. }
  1556. }
  1557. const keyName = itemsPre + item[this.setting.id];
  1558. if (!this.items[keyName]) {
  1559. item.children = [];
  1560. item.is_leaf = true;
  1561. item.expanded = true;
  1562. item.visible = true;
  1563. this.items[keyName] = item;
  1564. this.datas.push(item);
  1565. if (item[setting.pid] === setting.rootId) {
  1566. this.children.push(item);
  1567. } else {
  1568. const parent = this.getParent(item);
  1569. if (parent) {
  1570. parent.is_leaf = false;
  1571. parent.children.push(item);
  1572. }
  1573. }
  1574. } else {
  1575. return this.items[keyName];
  1576. }
  1577. return item;
  1578. }
  1579. }
  1580. class GatherTree extends BaseTree {
  1581. clearDatas() {
  1582. this.items = {};
  1583. this.nodes = [];
  1584. this.datas = [];
  1585. this.children = [];
  1586. }
  1587. get newId() {
  1588. if (!this._maxId) {
  1589. this._maxId = 0;
  1590. }
  1591. this._maxId++;
  1592. return this._maxId;
  1593. }
  1594. addNode(data, parent) {
  1595. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1596. let item = _.find(this.items, data);
  1597. if (item) return item;
  1598. item = data;
  1599. item[this.setting.id] = this.newId;
  1600. const keyName = itemsPre + item[this.setting.id];
  1601. item.children = [];
  1602. item.is_leaf = true;
  1603. item.expanded = true;
  1604. item.visible = true;
  1605. this.items[keyName] = item;
  1606. this.datas.push(item);
  1607. if (parent) {
  1608. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1609. item[this.setting.level] = parent[this.setting.level] + 1;
  1610. item[this.setting.order] = parent.children.length + 1;
  1611. parent.is_leaf = false;
  1612. parent.children.push(item);
  1613. } else {
  1614. item[this.setting.fullPath] = '' + item[this.setting.id];
  1615. item[this.setting.level] = 1;
  1616. item[this.setting.order] = this.children.length + 1;
  1617. this.children.push(item);
  1618. }
  1619. return item;
  1620. }
  1621. sortTreeNodeCustom(field, fun, isResort) {
  1622. const self = this;
  1623. const sortNodes = function (nodes) {
  1624. nodes.sort(function (a, b) {
  1625. return fun(a[field], b[field]);
  1626. });
  1627. for (const [i, node] of nodes.entries()) {
  1628. node[self.setting.order] = i + 1;
  1629. }
  1630. };
  1631. const addSortNodes = function (nodes) {
  1632. if (!nodes) { return }
  1633. for (let i = 0; i < nodes.length; i++) {
  1634. self.nodes.push(nodes[i]);
  1635. nodes[i].index = self.nodes.length - 1;
  1636. if (!isResort) {
  1637. nodes[i].children = self.getChildren(nodes[i]);
  1638. }
  1639. sortNodes(nodes[i].children);
  1640. addSortNodes(nodes[i].children);
  1641. }
  1642. };
  1643. this.nodes = [];
  1644. if (!isResort) {
  1645. this.children = this.getChildren();
  1646. }
  1647. sortNodes(this.children);
  1648. addSortNodes(this.children);
  1649. }
  1650. }
  1651. class CompareTree extends FxTree {
  1652. constructor(setting) {
  1653. super(setting);
  1654. this._newId = 1;
  1655. }
  1656. get newId() {
  1657. return this._newId++;
  1658. }
  1659. findCompareNode(node, parent) {
  1660. if (this.setting.findNode) {
  1661. return this.setting.findNode(this, node, parent);
  1662. } else {
  1663. const siblings = parent ? parent.children : this.children;
  1664. return siblings.find(function (x) {
  1665. return node.b_code
  1666. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1667. : x.code === node.code && x.name === node.name;
  1668. });
  1669. }
  1670. }
  1671. loadCompareNode(source, node, parent, loadFun) {
  1672. let cur = this.findCompareNode(node, parent);
  1673. if (!cur) {
  1674. const siblings = parent ? parent.children : this.children;
  1675. const id = this.newId;
  1676. cur = {
  1677. children: [], pos: [],
  1678. code: node.code, b_code: node.b_code, name: node.name,
  1679. unit: node.unit, unit_price: node.unit_price,
  1680. };
  1681. cur[this.setting.id] = id;
  1682. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1683. cur[this.setting.fullPath] = parent ? parent[this.setting.fullPath] + '-' + id : '' + id;
  1684. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1685. cur[this.setting.order] = siblings.length + 1;
  1686. siblings.push(cur);
  1687. this.datas.push(cur);
  1688. }
  1689. loadFun(cur, node, source);
  1690. for (const c of node.children) {
  1691. this.loadCompareNode(source, c, cur, loadFun);
  1692. }
  1693. }
  1694. generateSortNodes() {
  1695. const self = this;
  1696. const addSortNode = function (node) {
  1697. self.nodes.push(node);
  1698. for (const c of node.children) {
  1699. addSortNode(c);
  1700. }
  1701. }
  1702. this.nodes = [];
  1703. for (const n of this.children) {
  1704. addSortNode(n);
  1705. }
  1706. }
  1707. loadCompareTree(data, loadFun) {
  1708. for (const c of data.billsTree.children) {
  1709. this.loadCompareNode(data, c, null, loadFun);
  1710. }
  1711. }
  1712. calculateDiffer() {
  1713. if (this.setting.calcDiffer) {
  1714. for (const d of this.datas) {
  1715. this.setting.calcDiffer(d);
  1716. }
  1717. }
  1718. }
  1719. loadCompareData(data1, data2) {
  1720. this.loadCompareTree(data1, this.setting.loadInfo1);
  1721. this.loadCompareTree(data2, this.setting.loadInfo2);
  1722. for (const d of this.datas) {
  1723. d.is_leaf = d.children.length === 0;
  1724. d.expanded = true;
  1725. d.visible = true;
  1726. this.items[itemsPre + d[this.setting.id]] = d;
  1727. }
  1728. this.generateSortNodes();
  1729. this.calculateDiffer();
  1730. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1731. }
  1732. }
  1733. class TreeGatherTree extends FxTree {
  1734. constructor(setting) {
  1735. super(setting);
  1736. this._newId = 1;
  1737. }
  1738. get newId() {
  1739. return this._newId++;
  1740. }
  1741. loadGatherNode(node, parent, index, loadFun) {
  1742. const siblings = parent ? parent.children : this.children;
  1743. let cur = siblings.find(function (x) {
  1744. return node.b_code
  1745. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1746. : x.code === node.code && x.name === node.name;
  1747. });
  1748. if (!cur) {
  1749. const id = this.newId;
  1750. cur = {
  1751. id: id,
  1752. pid: parent ? parent.id : this.setting.rootId,
  1753. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1754. level: parent ? parent.level + 1 : 1,
  1755. order: siblings.length + 1,
  1756. children: [],
  1757. code: node.code, b_code: node.b_code, name: node.name,
  1758. unit: node.unit, unit_price: node.unit_price,
  1759. };
  1760. siblings.push(cur);
  1761. this.datas.push(cur);
  1762. }
  1763. loadFun(cur, node, index);
  1764. for (const c of node.children) {
  1765. this.loadGatherNode(c, cur, index, loadFun);
  1766. }
  1767. }
  1768. generateSortNodes() {
  1769. const self = this;
  1770. const addSortNode = function (node) {
  1771. self.nodes.push(node);
  1772. for (const c of node.children) {
  1773. addSortNode(c);
  1774. }
  1775. };
  1776. this.nodes = [];
  1777. for (const n of this.children) {
  1778. addSortNode(n);
  1779. }
  1780. }
  1781. loadGatherTree(data, index, loadFun) {
  1782. for (const c of data.billsTree.children) {
  1783. this.loadGatherNode(c, null, index, loadFun);
  1784. }
  1785. // todo load Pos Data;
  1786. }
  1787. calculateSum() {
  1788. if (this.setting.calcSum) {
  1789. for (const d of this.datas) {
  1790. this.setting.calcSum(d, this.count);
  1791. }
  1792. }
  1793. }
  1794. loadGatherData(datas) {
  1795. this.count = datas.length;
  1796. for (const [i, data] of datas.entries()) {
  1797. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1798. }
  1799. for (const d of this.datas) {
  1800. d.is_leaf = d.children.length === 0;
  1801. d.expanded = true;
  1802. d.visible = true;
  1803. this.items[itemsPre + d[this.setting.id]] = d;
  1804. }
  1805. this.generateSortNodes();
  1806. this.calculateSum();
  1807. }
  1808. }
  1809. class FinalTree extends BaseTree {
  1810. constructor(setting) {
  1811. super(setting);
  1812. this._newId = 1;
  1813. }
  1814. get newId() {
  1815. return this._newId++;
  1816. }
  1817. loadNode(node, parent, loadFun) {
  1818. if (node.b_code) return;
  1819. const siblings = parent ? parent.children : this.children;
  1820. let cur = siblings.find(function (x) {
  1821. return x.code === node.code && x.name === node.name;
  1822. });
  1823. if (!cur) {
  1824. const id = this.newId;
  1825. cur = {
  1826. id: id,
  1827. pid: parent ? parent.id : this.setting.rootId,
  1828. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1829. level: parent ? parent.level + 1 : 1,
  1830. order: siblings.length + 1,
  1831. children: [],
  1832. code: node.code, name: node.name, unit: node.unit,
  1833. };
  1834. siblings.push(cur);
  1835. this.datas.push(cur);
  1836. }
  1837. loadFun(cur, node);
  1838. for (const c of node.children) {
  1839. this.loadNode(c, cur, loadFun);
  1840. }
  1841. }
  1842. loadTree(tree, loadFun) {
  1843. for (const node of tree.children) {
  1844. this.loadNode(node, null, loadFun);
  1845. }
  1846. }
  1847. generateSortNodes() {
  1848. const self = this;
  1849. const addSortNode = function (node) {
  1850. self.nodes.push(node);
  1851. for (const c of node.children) {
  1852. addSortNode(c);
  1853. }
  1854. };
  1855. this.nodes = [];
  1856. for (const n of this.children) {
  1857. addSortNode(n);
  1858. }
  1859. }
  1860. afterLoad(fun) {
  1861. for (const d of this.datas) {
  1862. fun && fun(d);
  1863. d.is_leaf = d.children.length === 0;
  1864. d.expanded = true;
  1865. d.visible = true;
  1866. this.items[itemsPre + d[this.setting.id]] = d;
  1867. }
  1868. this.generateSortNodes();
  1869. }
  1870. clearFinal() {
  1871. this.datas = this.datas.filter(x => {
  1872. return !!x.base;
  1873. });
  1874. this.datas.forEach(x => {
  1875. delete x.total_price;
  1876. delete x.dgn_qty1;
  1877. delete x.dgn_qty2;
  1878. delete x.dgn_qty;
  1879. delete x.dgn_price;
  1880. delete x.final_dgn_qty1;
  1881. delete x.final_dgn_qty2;
  1882. delete x.final_dgn_qty;
  1883. delete x.final_dgn_price;
  1884. delete x.final_tp;
  1885. delete x.grow_dgn_qty1;
  1886. delete x.grow_dgn_qty2;
  1887. delete x.grow_dgn_qty;
  1888. delete x.grow_tp;
  1889. });
  1890. this.loadDatas(this.datas);
  1891. }
  1892. resortChildrenByCustom(fun) {
  1893. for (const n of this.nodes) {
  1894. if (n.children && n.children.length > 1) {
  1895. n.children.sort(fun);
  1896. n.children.forEach((x, y) => { x.order = y + 1; });
  1897. }
  1898. }
  1899. this.generateSortNodes();
  1900. }
  1901. }
  1902. if (type === 'base') {
  1903. return new BaseTree(setting);
  1904. } else if (type === 'fx') {
  1905. return new FxTree(setting);
  1906. } else if (type === 'stage') {
  1907. return new StageTree(setting);
  1908. } else if (type === 'ledger') {
  1909. return new LedgerTree(setting);
  1910. } else if (type === 'revise') {
  1911. return new ReviseTree(setting);
  1912. } else if (type === 'measure') {
  1913. return new MeasureTree(setting);
  1914. } else if (type === 'master') {
  1915. return new MasterTree(setting);
  1916. } else if (type === 'filter') {
  1917. return new FilterTree(setting);
  1918. } else if (type === 'gather') {
  1919. return new GatherTree(setting);
  1920. } else if (type === 'compare') {
  1921. return new CompareTree(setting);
  1922. } else if (type === 'tree-gather') {
  1923. return new TreeGatherTree(setting);
  1924. } else if (type === 'final') {
  1925. return new FinalTree(setting);
  1926. }
  1927. };
  1928. const treeCalc = {
  1929. mapTreeNode: function (tree) {
  1930. const setting = tree.setting;
  1931. let map = {}, maxLevel = 0;
  1932. for (const node of tree.nodes) {
  1933. let levelArr = map[node[setting.level]];
  1934. if (!levelArr) {
  1935. levelArr = [];
  1936. map[node[setting.level]] = levelArr;
  1937. }
  1938. if (node[setting.level] > maxLevel) {
  1939. maxLevel = node[setting.level];
  1940. }
  1941. levelArr.push(node);
  1942. }
  1943. return [maxLevel, map];
  1944. },
  1945. getMaxLevel: function (tree) {
  1946. const setting = tree.setting;
  1947. return Math.max.apply(Math, tree.datas.map(function (o) { return o[setting.level] }));
  1948. },
  1949. calculateNode: function (tree, node) {
  1950. if (node.children && node.children.length > 0) {
  1951. const gather = node.children.reduce(function (rst, x) {
  1952. const result = {};
  1953. for (const cf of tree.setting.calcFields) {
  1954. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1955. }
  1956. return result;
  1957. });
  1958. // 汇总子项
  1959. for (const cf of tree.setting.calcFields) {
  1960. if (gather[cf]) {
  1961. node[cf] = gather[cf];
  1962. } else {
  1963. node[cf] = null;
  1964. }
  1965. }
  1966. }
  1967. // 自身运算
  1968. if (tree.setting.calcFun) {
  1969. tree.setting.calcFun(node);
  1970. }
  1971. },
  1972. calculateLevelNode: function (tree, level) {
  1973. const setting = tree.setting;
  1974. const nodes = tree.datas.filter((n) => { return n[setting.level] === level });
  1975. for (const node of nodes) {
  1976. this.calculateNode(tree, node);
  1977. }
  1978. },
  1979. calculateAll: function (tree) {
  1980. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1981. for (let i = maxLevel; i >= 0; i--) {
  1982. const levelNodes = levelMap[i];
  1983. if (levelNodes && levelNodes.length > 0) {
  1984. for (const node of levelNodes) {
  1985. this.calculateNode(tree, node);
  1986. }
  1987. }
  1988. }
  1989. },
  1990. calculateParent: function (tree, node, converse = -1) {
  1991. const setting = tree.setting;
  1992. const nodes = tree.getFullPathNodes(node[setting.fullPath]);
  1993. converse === -1 ? tree.sortByLevel(nodes) : tree.sortByLevelConverse(nodes);
  1994. for (const n of nodes) {
  1995. this.calculateNode(tree, n);
  1996. }
  1997. return nodes;
  1998. }
  1999. };