path_tree.js 65 KB

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