path_tree.js 61 KB

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