path_tree.js 61 KB

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