path_tree.js 61 KB

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