path_tree.js 65 KB

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