path_tree.js 59 KB

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