path_tree.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /**
  2. * (需使用 decimal.min.js, zh_calc.js)
  3. *
  4. * 构建pathTree
  5. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  6. * @param {Object} setting - 设置
  7. * @returns {PathTree}
  8. */
  9. 'use strict';
  10. const itemsPre = 'id_';
  11. class PosData {
  12. /**
  13. * 构造函数
  14. * @param {id|Number, masterId|Number} setting
  15. */
  16. constructor (setting) {
  17. // 无索引
  18. this.datas = [];
  19. // 以key为索引
  20. this.items = {};
  21. // 以分类id为索引的有序
  22. this.ledgerPos = {};
  23. // pos设置
  24. this.setting = setting;
  25. }
  26. /**
  27. * 加载部位明细数据
  28. * @param datas
  29. */
  30. loadDatas(datas) {
  31. this.datas = datas;
  32. this.items = {};
  33. this.ledgerPos = {};
  34. for (const data of this.datas) {
  35. const key = itemsPre + data[this.setting.id];
  36. this.items[key] = data;
  37. const masterKey = itemsPre + data[this.setting.ledgerId];
  38. if (!this.ledgerPos[masterKey]) {
  39. this.ledgerPos[masterKey] = [];
  40. }
  41. this.ledgerPos[masterKey].push(data);
  42. }
  43. for (const prop in this.ledgerPos) {
  44. this.resortLedgerPos(this.ledgerPos[prop]);
  45. }
  46. }
  47. /**
  48. * 更新数据
  49. * @param datas
  50. */
  51. updateDatas(data) {
  52. const datas = data instanceof Array ? data : [data];
  53. const result = { create: [], update: [] }, resort = [];
  54. for (const d of datas) {
  55. const key = itemsPre + d[this.setting.id];
  56. if (!this.items[key]) {
  57. this.datas.push(d);
  58. this.items[key] = d;
  59. const masterKey = itemsPre + d[this.setting.ledgerId];
  60. if (!this.ledgerPos[masterKey]) {
  61. this.ledgerPos[masterKey] = [];
  62. }
  63. this.ledgerPos[masterKey].push(d);
  64. result.create.push(d);
  65. } else {
  66. const pos = this.items[key];
  67. for (const prop in d) {
  68. pos[prop] = d[prop];
  69. }
  70. result.update.push(pos);
  71. }
  72. const masterKey = itemsPre + d[this.setting.ledgerId];
  73. if (resort.indexOf(masterKey) === -1) {
  74. resort.push(masterKey);
  75. }
  76. }
  77. for (const s of resort) {
  78. this.resortLedgerPos(this.ledgerPos[s]);
  79. }
  80. return result;
  81. }
  82. /**
  83. * 移除数据
  84. * @param datas
  85. */
  86. removeDatas(data) {
  87. if (!data) { return; }
  88. const datas = data instanceof Array ? data : [data];
  89. for (let i = datas.length - 1; i >= 0; i--) {
  90. const id = datas[i];
  91. const d = this.getPos(id);
  92. this.datas.splice(this.datas.indexOf(d), 1);
  93. const key = itemsPre + d[this.setting.id];
  94. delete this.items[key];
  95. const masterKey = itemsPre + d[this.setting.ledgerId];
  96. const range = this.ledgerPos[masterKey];
  97. range.splice(range.indexOf(d), 1);
  98. if (range.length === 0) {
  99. delete this.ledgerPos[masterKey];
  100. }
  101. }
  102. }
  103. /**
  104. * 移除数据 - 根据分类id
  105. * @param mid
  106. */
  107. removeDatasByMasterId (mid) {
  108. const masterKey = itemsPre + mid;
  109. const range = this.ledgerPos[masterKey];
  110. if (range) {
  111. delete this.ledgerPos[masterKey];
  112. for (const r of range) {
  113. this.datas.splice(this.datas.indexOf(r), 1);
  114. const key = itemsPre + r[this.setting.id];
  115. delete this.items[key];
  116. }
  117. }
  118. }
  119. getPos(id) {
  120. return this.items[itemsPre + id];
  121. }
  122. getLedgerPos(mid) {
  123. return this.ledgerPos[itemsPre + mid];
  124. }
  125. resortLedgerPos(ledgerPos) {
  126. if (ledgerPos instanceof Array) {
  127. ledgerPos.sort(function (a, b) {
  128. return a.porder - b.porder;
  129. })
  130. }
  131. }
  132. /**
  133. * 计算全部
  134. */
  135. calculateAll() {
  136. if (!this.setting.calcFun) { return; }
  137. for (const pos of this.datas) {
  138. this.setting.calcFun(pos);
  139. }
  140. }
  141. }
  142. class StagePosData extends PosData {
  143. loadStageData(datas, fieldPre, fields) {
  144. if (!datas) { return; }
  145. datas = datas instanceof Array ? datas : [datas];
  146. const loadedData = [];
  147. for (const data of datas) {
  148. let node = this.getPos(data.pid);
  149. if (node) {
  150. for (const prop of fields) {
  151. if (data[prop] !== undefined) {
  152. node[fieldPre + prop] = data[prop];
  153. }
  154. }
  155. if (this.setting.calcFun) {
  156. this.setting.calcFun(node);
  157. }
  158. loadedData.push(node);
  159. }
  160. }
  161. }
  162. loadPreStageData(datas) {
  163. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  164. }
  165. loadCurStageData(datas) {
  166. this.loadStageData(datas, '', this.setting.updateFields);
  167. }
  168. }
  169. class MasterPosData extends PosData {
  170. /**
  171. * 构造函数
  172. * @param {id|Number, masterId|Number} setting
  173. */
  174. constructor (setting) {
  175. super(setting);
  176. // 关联索引
  177. this.masterItems = {};
  178. }
  179. /**
  180. * 加载主数据
  181. * @param datas
  182. */
  183. loadDatas (datas) {
  184. super.loadDatas(datas);
  185. // 清空旧数据
  186. this.masterItems = {};
  187. // minor数据缓存
  188. this.minorData = {};
  189. // 加载全部数据
  190. for (const data of this.datas) {
  191. const keyName = itemsPre + data[this.setting.masterId];
  192. this.masterItems[keyName] = data;
  193. }
  194. }
  195. /**
  196. * 根据关联id,查找节点
  197. * @param id
  198. * @returns {*}
  199. */
  200. getMasterItems(id) {
  201. return this.masterItems[itemsPre + id];
  202. }
  203. /**
  204. * 加载关联数据
  205. *
  206. * @param {Array|Object}datas - 需要关联的数据
  207. * @param {String} fieldPre - 关联字段前缀(关联结果)
  208. * @param {Array} fields - 关联字段
  209. * @returns {Array}
  210. */
  211. loadMinorData(datas, fieldSuf, fields) {
  212. if (!datas) return;
  213. datas = datas instanceof Array ? datas : [datas];
  214. this.minorData[fieldSuf] = datas;
  215. const loadedData = [];
  216. for (const data of datas) {
  217. let node = this.getMasterItems(data[this.setting.minorId]);
  218. if (node) {
  219. for (const prop of fields) {
  220. if (data[prop] !== undefined) {
  221. node[prop + fieldSuf] = data[prop];
  222. }
  223. }
  224. loadedData.push(node);
  225. }
  226. }
  227. return loadedData;
  228. }
  229. }
  230. const createNewPathTree = function (type, setting) {
  231. class BaseTree {
  232. /**
  233. * 构造函数
  234. */
  235. constructor (setting) {
  236. // 无索引
  237. this.datas = [];
  238. // 以key为索引
  239. this.items = {};
  240. // 以排序为索引
  241. this.nodes = [];
  242. // 根节点
  243. this.children = [];
  244. // 树设置
  245. this.setting = setting;
  246. }
  247. /**
  248. * 树结构根据显示排序
  249. */
  250. sortTreeNode (isResort) {
  251. const self = this;
  252. const addSortNodes = function (nodes) {
  253. if (!nodes) { return }
  254. for (let i = 0; i < nodes.length; i++) {
  255. self.nodes.push(nodes[i]);
  256. nodes[i].index = self.nodes.length - 1;
  257. if (!isResort) {
  258. nodes[i].children = self.getChildren(nodes[i]);
  259. } else {
  260. nodes[i].children.sort(function (a, b) {
  261. return a.order - b.order;
  262. })
  263. }
  264. addSortNodes(nodes[i].children);
  265. }
  266. };
  267. this.nodes = [];
  268. if (!isResort) {
  269. this.children = this.getChildren();
  270. } else {
  271. this.children.sort(function (a, b) {
  272. return a.order - b.order;
  273. })
  274. }
  275. addSortNodes(this.children);
  276. }
  277. /**
  278. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  279. * @param datas
  280. */
  281. loadDatas (datas) {
  282. // 清空旧数据
  283. this.items = {};
  284. this.nodes = [];
  285. this.datas = [];
  286. this.children = [];
  287. // 加载全部数据
  288. datas.sort(function (a, b) {
  289. return a.level - b.level;
  290. });
  291. for (const data of datas) {
  292. const keyName = itemsPre + data[this.setting.id];
  293. if (!this.items[keyName]) {
  294. const item = JSON.parse(JSON.stringify(data));
  295. item.children = [];
  296. item.expanded = true;
  297. item.visible = true;
  298. this.items[keyName] = item;
  299. this.datas.push(item);
  300. if (item[setting.pid] === setting.rootId) {
  301. this.children.push(item);
  302. } else {
  303. const parent = this.getParent(item);
  304. if (parent) {
  305. parent.children.push(item);
  306. }
  307. }
  308. }
  309. }
  310. this.children.sort(function (a, b) {
  311. return a.order - b.order;
  312. });
  313. this.sortTreeNode(true);
  314. if (this.setting.autoExpand >= 0) this.expandByLevel(this.setting.autoExpand);
  315. }
  316. getItemsByIndex(index) {
  317. return this.nodes[index];
  318. }
  319. /**
  320. * 根据id获取树结构节点数据
  321. * @param {Number} id
  322. * @returns {Object}
  323. */
  324. getItems (id) {
  325. return this.items[itemsPre + id];
  326. };
  327. /**
  328. * 查找node的parent
  329. * @param {Object} node
  330. * @returns {Object}
  331. */
  332. getParent (node) {
  333. return this.getItems(node[this.setting.pid]);
  334. };
  335. getAllParents (node) {
  336. const parents = [];
  337. if (node.full_path && node.full_path !== '') {
  338. const parentIds = node.full_path.split('-');
  339. for (const id of parentIds) {
  340. if (id !== node[this.setting.id]) {
  341. parents.push(this.getItems(id));
  342. }
  343. }
  344. } else {
  345. let vP = this.getParent(node);
  346. while (vP) {
  347. parents.push(vP);
  348. vP = this.getParent(vP);
  349. }
  350. }
  351. return parents;
  352. }
  353. /**
  354. * 查找node的前兄弟节点
  355. * @param node
  356. * @returns {*}
  357. */
  358. getPreSiblingNode(node) {
  359. if (!node) return null;
  360. const parent = this.getParent(node);
  361. const siblings = parent ? parent.children : this.children;
  362. const index = siblings.indexOf(node);
  363. return (index > 0) ? siblings[index - 1] : null;
  364. }
  365. /**
  366. * 查找node的后兄弟节点
  367. * @param node
  368. * @returns {*}
  369. */
  370. getNextSiblingNode(node) {
  371. const parent = this.getParent(node);
  372. const siblings = parent ? parent.children : this.children;
  373. const index = siblings.indexOf(node);
  374. if (index >= 0 && index < siblings.length - 1) {
  375. return siblings[index + 1];
  376. } else {
  377. return null;
  378. }
  379. }
  380. /**
  381. * 根据path查找完整节点
  382. * @param {Number} path
  383. */
  384. getFullPathNodes (path) {
  385. const self = this, ids = path.split('-');
  386. if (ids.length > 0) {
  387. return this.nodes.filter((x) => {
  388. return ids.indexOf('' + x[self.setting.id]) >= 0;
  389. });
  390. } else {
  391. return [];
  392. }
  393. };
  394. /**
  395. * 查询node的已下载子节点
  396. * @param {Object} node
  397. * @returns {Array}
  398. */
  399. getChildren (node) {
  400. const setting = this.setting;
  401. const pid = node ? node[setting.id] : setting.rootId;
  402. const children = this.datas.filter(function (x) {
  403. return x[setting.pid] === pid;
  404. });
  405. children.sort(function (a, b) {
  406. return a.order - b.order;
  407. });
  408. return children;
  409. };
  410. /**
  411. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  412. * @param node
  413. * @returns {*}
  414. * @private
  415. */
  416. _recursiveGetPosterity (node) {
  417. let posterity = node.children;
  418. for (const c of node.children) {
  419. posterity = posterity.concat(this._recursiveGetPosterity(c));
  420. }
  421. return posterity;
  422. };
  423. /**
  424. * 查询node的已下载的全部后代
  425. * @param {Object} node
  426. * @returns {Array}
  427. */
  428. getPosterity (node) {
  429. if (node.full_path !== '') {
  430. const reg = new RegExp('^' + node.full_path + '-');
  431. return this.datas.filter(function (x) {
  432. return reg.test(x.full_path);
  433. });
  434. } else {
  435. return this._recursiveGetPosterity(node);
  436. }
  437. };
  438. /**
  439. * 查询node是否是父节点的最后一个子节点
  440. * @param {Object} node
  441. * @returns {boolean}
  442. */
  443. isLastSibling (node) {
  444. const siblings = this.getChildren(this.getParent(node));
  445. return (siblings && siblings.length > 0) ? node.order === siblings[siblings.length - 1].order : false;
  446. };
  447. /**
  448. * 刷新子节点是否可见
  449. * @param {Object} node
  450. * @private
  451. */
  452. _refreshChildrenVisible (node) {
  453. if (!node.children) {
  454. node.children = this.getChildren(node);
  455. }
  456. if (node.children && node.children.length > 0) {
  457. for (const child of node.children) {
  458. child.visible = node.expanded && node.visible;
  459. this._refreshChildrenVisible(child);
  460. }
  461. }
  462. };
  463. /**
  464. * 设置节点是否展开, 并控制子节点可见
  465. * @param {Object} node
  466. * @param {Boolean} expanded
  467. */
  468. setExpanded (node, expanded) {
  469. node.expanded = expanded;
  470. this._refreshChildrenVisible(node);
  471. };
  472. /**
  473. * 提取节点key和索引数据
  474. * @param {Object} node - 节点
  475. * @returns {key}
  476. */
  477. getNodeKeyData (node) {
  478. const data = {};
  479. for (const key of this.setting.keys) {
  480. data[key] = node[key];
  481. }
  482. return data;
  483. };
  484. /**
  485. * 得到树结构构成id
  486. * @param node
  487. * @returns {*}
  488. */
  489. getNodeKey (node) {
  490. return node[this.setting.id];
  491. };
  492. /**
  493. * 递归 设置节点展开状态
  494. * @param {Array} nodes - 需要设置状态的节点
  495. * @param {Object} parent - nodes的父节点
  496. * @param {Function} checkFun - 判断节点展开状态的方法
  497. * @private
  498. */
  499. _recursiveExpand(nodes, parent, checkFun) {
  500. for (const node of nodes) {
  501. node.expanded = checkFun(node);
  502. node.visible = parent ? (parent.expanded && parent.visible) : true;
  503. this._recursiveExpand(node.children, node, checkFun);
  504. }
  505. }
  506. /**
  507. * 自定义展开规则
  508. * @param checkFun
  509. */
  510. expandByCustom(checkFun) {
  511. this._recursiveExpand(this.children, null, checkFun);
  512. }
  513. /**
  514. * 展开到第几层
  515. * @param {Number} level - 展开层数
  516. */
  517. expandByLevel(level) {
  518. this.expandByCustom(function (n) {
  519. return n.level < level;
  520. });
  521. }
  522. /**
  523. * 自动展开节点node
  524. * @param node
  525. * @returns {*}
  526. */
  527. autoExpandNode(node) {
  528. const parents = this.getAllParents(node);
  529. const reload = [];
  530. for (const p of parents) {
  531. if (!p.expanded) {
  532. reload.push(p);
  533. this.setExpanded(p, true);
  534. }
  535. }
  536. return reload;
  537. }
  538. /**
  539. * 加载数据(动态),只加载不同部分
  540. * @param {Array} datas
  541. * @return {Array} 加载到树的数据
  542. * @privateA
  543. */
  544. _updateData (datas) {
  545. datas = datas instanceof Array ? datas : [datas];
  546. let loadedData = [];
  547. for (const data of datas) {
  548. let node = this.getItems(data[this.setting.id]);
  549. if (node) {
  550. for (const prop in node) {
  551. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  552. if (prop === this.setting.pid) {
  553. loadedData.push(this.getItems(node[this.setting.pid]));
  554. loadedData.push(this.getItems(data[this.setting.pid]));
  555. }
  556. if (prop === this.setting.order) {
  557. loadedData = loadedData.concat(this.getPosterity(node));
  558. }
  559. node[prop] = data[prop];
  560. }
  561. }
  562. loadedData.push(node);
  563. }
  564. }
  565. loadedData = _.uniq(loadedData);
  566. for (const node of loadedData) {
  567. if (node) {
  568. node.children = this.getChildren(node);
  569. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  570. } else {
  571. this.children = this.getChildren(null);
  572. }
  573. }
  574. this.sortTreeNode(true);
  575. return loadedData;
  576. };
  577. /**
  578. * 加载数据(动态),只加载不同部分
  579. * @param {Array} datas
  580. * @return {Array} 加载到树的数据
  581. * @privateA
  582. */
  583. _loadData (datas) {
  584. datas = datas instanceof Array ? datas : [datas];
  585. const loadedData = [], resortData = [];
  586. for (const data of datas) {
  587. let node = this.getItems(data[this.setting.id]);
  588. if (node) {
  589. const parent = this.getItems(node[this.setting.pid]);
  590. for (const prop in node) {
  591. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  592. node[prop] = data[prop];
  593. if (parent && resortData.indexOf(parent) === -1) {
  594. resortData.push(parent);
  595. }
  596. }
  597. }
  598. loadedData.push(node);
  599. } else {
  600. const keyName = itemsPre + data[this.setting.id];
  601. const node = JSON.parse(JSON.stringify(data));
  602. this.items[keyName] = node;
  603. this.datas.push(node);
  604. node.expanded = true;
  605. node.visible = true;
  606. loadedData.push(node);
  607. if (resortData.indexOf(node) === -1) {
  608. resortData.push(node);
  609. }
  610. const parent = this.getItems(node[this.setting.pid]);
  611. if (parent && resortData.indexOf(parent) === -1) {
  612. resortData.push(parent);
  613. } else {
  614. resortData.push(this.setting.rootId);
  615. }
  616. }
  617. }
  618. for (const node of resortData) {
  619. if (node && node !== this.setting.rootId) {
  620. node.children = this.getChildren(node);
  621. } else {
  622. this.children = this.getChildren(null);
  623. }
  624. }
  625. this.sortTreeNode(true);
  626. for (const node of loadedData) {
  627. if (!node.expanded) {
  628. this.setExpanded(node, true);
  629. }
  630. }
  631. return loadedData;
  632. };
  633. /**
  634. * 清理数据(动态)
  635. * @param datas
  636. * @private
  637. */
  638. _freeData (datas) {
  639. datas = datas instanceof Array ? datas : [datas];
  640. const freeDatas = [];
  641. const removeArrayData = function (array, data) {
  642. const index = array.indexOf(data);
  643. array.splice(index, 1);
  644. };
  645. for (const data of datas) {
  646. const node = this.getItems(data[this.setting.id]);
  647. if (node) {
  648. freeDatas.push(node);
  649. node.deleteIndex = this.nodes.indexOf(node);
  650. delete this.items[itemsPre + node[this.setting.id]];
  651. if (node[this.setting.pid] !== this.setting.rootId) {
  652. const parent = this.getItems(node[this.setting.pid]);
  653. if (parent) {
  654. removeArrayData(parent.children, node);
  655. }
  656. } else {
  657. removeArrayData(this.children, node);
  658. }
  659. removeArrayData(this.datas, node);
  660. }
  661. }
  662. for(const node of freeDatas) {
  663. removeArrayData(this.nodes, node);
  664. }
  665. return freeDatas;
  666. };
  667. /**
  668. * 加载需展开的数据
  669. * @param {Array} datas
  670. * @returns {Array}
  671. * @private
  672. */
  673. _loadExpandData (datas) {
  674. datas = datas instanceof Array ? datas : [datas];
  675. const loadedData = [], existData = [], expandData = [], resortData = [];
  676. for (const data of datas) {
  677. let node = this.getItems(data[this.setting.id]);
  678. if (node) {
  679. existData.push(node);
  680. } else {
  681. const keyName = itemsPre + data[this.setting.id];
  682. const node = JSON.parse(JSON.stringify(data));
  683. this.items[keyName] = node;
  684. this.datas.push(node);
  685. node.expanded = false;
  686. node.visible = true;
  687. loadedData.push(node);
  688. if (resortData.indexOf(node) === -1) {
  689. resortData.push(node);
  690. }
  691. const parent = this.getItems(node[this.setting.pid]);
  692. if (parent && resortData.indexOf(parent) === -1) {
  693. resortData.push(parent);
  694. }
  695. }
  696. }
  697. for (const node of resortData) {
  698. node.children = this.getChildren(node);
  699. }
  700. this.sortTreeNode(true);
  701. for (const node of loadedData) {
  702. if (!node.expanded) {
  703. this.setExpanded(node, true);
  704. }
  705. }
  706. for (const node of existData) {
  707. const parent = this.getItems(node[this.setting.pid]);
  708. if (expandData.indexOf(parent) === -1) {
  709. expandData.push(parent);
  710. if (!parent.expanded) {
  711. this.setExpanded(parent, true);
  712. }
  713. }
  714. if (!node.expanded) {
  715. this.setExpanded(node, true);
  716. }
  717. }
  718. return [loadedData, expandData];
  719. };
  720. /**
  721. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  722. *
  723. * @param data - 更新的数据 {update, create, delete}
  724. * @returns {{}}
  725. */
  726. loadPostData(data) {
  727. const result = {};
  728. if (data.delete) {
  729. result.delete = this._freeData(data.delete);
  730. }
  731. if (data.create) {
  732. result.create = this._loadData(data.create);
  733. }
  734. if (data.update) {
  735. result.update = this._updateData(data.update);
  736. }
  737. return result;
  738. }
  739. /**
  740. * 加载子节点
  741. * @param {Object} node
  742. * @param {function} callback
  743. */
  744. loadChildren (node, callback) {
  745. if (this.setting.url !== '') {
  746. const self = this;
  747. postData(this.setting.url, {postType: 'load-child', id: this.getNodeKeyData(node)}, function (data) {
  748. self._loadData(data);
  749. callback();
  750. });
  751. }
  752. };
  753. }
  754. class MeasureTree extends BaseTree {
  755. addData (datas) {
  756. const loadedData = [];
  757. for (const data of datas) {
  758. let node = this.getItems(data[this.setting.id]);
  759. if (node) {
  760. for (const prop in node) {
  761. if (data[prop] !== undefined) {
  762. node[prop] = data[prop];
  763. }
  764. }
  765. loadedData.push(node);
  766. } else {
  767. const keyName = itemsPre + data[this.setting.id];
  768. const node = JSON.parse(JSON.stringify(data));
  769. this.items[keyName] = node;
  770. this.datas.push(node);
  771. node.expanded = false;
  772. node.visible = true;
  773. loadedData.push(node);
  774. }
  775. }
  776. this.sortTreeNode();
  777. for (const node of loadedData) {
  778. const children = node.children;
  779. if (!node.expanded && children.length > 0) {
  780. node.expanded = true;
  781. this._refreshChildrenVisible(node);
  782. }
  783. }
  784. return loadedData;
  785. }
  786. removeData (datas) {
  787. datas.sort(function (a, b) {
  788. return b.level - a.level;
  789. });
  790. const removeArrayData = function (array, data) {
  791. const index = array.indexOf(data);
  792. array.splice(index, 1);
  793. };
  794. for (const data of datas) {
  795. const node = this.getItems(data[this.setting.id]);
  796. if (node && this.getChildren(node).length === 0) {
  797. delete this.items[itemsPre + node[this.setting.id]];
  798. if (node[this.setting.pid] !== this.setting.rootId) {
  799. const parent = this.items[itemsPre + node[this.setting.pid]];
  800. removeArrayData(parent.children, node);
  801. }
  802. removeArrayData(this.datas, node);
  803. removeArrayData(this.nodes, node);
  804. }
  805. }
  806. };
  807. loadLeafData (data) {
  808. const datas = data instanceof Array ? data : [data];
  809. for (const d of datas) {
  810. let node = this.getItems(d[this.setting.id]);
  811. if (node && node.is_leaf) {
  812. for (const prop in node) {
  813. if (data[prop] !== undefined) {
  814. node[prop] = d[prop];
  815. }
  816. }
  817. }
  818. }
  819. };
  820. }
  821. class FxTree extends BaseTree {
  822. /**
  823. * 检查节点是否是最底层项目节
  824. * @param node
  825. * @returns {boolean}
  826. */
  827. isLeafXmj(node) {
  828. if (!node.code) {
  829. return false;
  830. }
  831. for (const child of node.children) {
  832. if (!child.b_code || child.b_code === '') {
  833. return false;
  834. }
  835. }
  836. return true;
  837. }
  838. /**
  839. * 查询最底层项目节(本身或父项)
  840. * @param {Object} node - 查询节点
  841. * @returns {Object}
  842. */
  843. getLeafXmjParent(node) {
  844. let parent = node;
  845. while (parent) {
  846. if (this.isLeafXmj(parent)) {
  847. return parent;
  848. } else {
  849. parent = this.getParent(parent);
  850. }
  851. }
  852. return null;
  853. }
  854. /**
  855. * 展开至最底层项目节
  856. */
  857. expandToLeafXmj() {
  858. const self = this;
  859. this.expandByCustom(function (node) {
  860. if (node.b_code && node.b_code !== '') {
  861. return false;
  862. } else {
  863. return !self.isLeafXmj(node);
  864. }
  865. })
  866. }
  867. /**
  868. * 展开至计算项
  869. */
  870. expandByCalcFields() {
  871. const self = this;
  872. this.expandByCustom(function (node) {
  873. for (const field of self.setting.calcFields) {
  874. if (node[field]) {
  875. return true;
  876. }
  877. }
  878. return false;
  879. })
  880. }
  881. }
  882. class LedgerTree extends FxTree {
  883. /**
  884. *
  885. * @param parent
  886. * @param node
  887. * @private
  888. */
  889. _getNodesParents(parents, nodes) {
  890. for (const node of nodes) {
  891. const parent = this.getParent(node);
  892. if (parent) {
  893. const paths = this.getFullPathNodes(parent.full_path);
  894. for (const p of paths) {
  895. if (parents.indexOf(p) === -1) {
  896. parents.push(p);
  897. }
  898. }
  899. }
  900. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  901. parents.push(node);
  902. }
  903. }
  904. }
  905. _getReCalcNodes(reCalcNodes, nodes) {
  906. for (const node of nodes) {
  907. const parent = this.getParent(node);
  908. if (parent) {
  909. const paths = this.getFullPathNodes(parent.full_path);
  910. for (const p of paths) {
  911. if (reCalcNodes.indexOf(p) === -1) {
  912. reCalcNodes.push(p);
  913. }
  914. }
  915. }
  916. // 最底层项目节,也需要计算
  917. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  918. reCalcNodes.push(node);
  919. //}
  920. }
  921. }
  922. /**
  923. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  924. *
  925. * @param data - 更新的数据 {update, create, delete}
  926. * @returns {{}}
  927. */
  928. loadPostData(data) {
  929. const result = {}, reCalcNodes = [];
  930. if (data.delete) {
  931. result.delete = this._freeData(data.delete);
  932. this._getReCalcNodes(reCalcNodes, result.delete);
  933. }
  934. if (data.create) {
  935. result.create = this._loadData(data.create);
  936. this._getReCalcNodes(reCalcNodes, result.create);
  937. }
  938. if (data.update) {
  939. result.update = this._updateData(data.update);
  940. this._getReCalcNodes(reCalcNodes, result.update);
  941. }
  942. reCalcNodes.sort((a, b) => {
  943. return b.level - a.level;
  944. });
  945. for (const node of reCalcNodes) {
  946. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  947. }
  948. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  949. return result;
  950. }
  951. /**
  952. * 以下方法需等待响应, 通过callback刷新界面
  953. */
  954. /**
  955. * 树结构基本操作
  956. * @param {String} url - 请求地址
  957. * @param {Object} node - 操作节点
  958. * @param {String} type - 操作类型
  959. * @param {function} callback - 界面刷新
  960. */
  961. // baseOperation (url, node, type, callback) {
  962. // const self = this;
  963. // const data = {
  964. // id: node[this.setting.id],
  965. // postType: type
  966. // };
  967. // postData(url, data, function (datas) {
  968. // const refreshData = self.loadPostData(datas);
  969. // callback(refreshData);
  970. // });
  971. // };
  972. /**
  973. * 节点数据编辑
  974. * @param {String} url - 请求地址
  975. * @param {Array|Object} updateData - 需更新的数据
  976. * @param {function} callback - 界面刷新
  977. */
  978. // update (url, updateData, callback) {
  979. // const self = this;
  980. // postData(url, updateData, function (datas) {
  981. // const refreshData = self.loadPostData(datas);
  982. // callback(refreshData);
  983. // }, function () {
  984. // if (updateData instanceof Array) {
  985. // const result = [];
  986. // for (const data of updateData) {
  987. // result.push(self.getItems(data[self.setting.id]));
  988. // }
  989. // callback(result)
  990. // } else {
  991. // callback([self.getItems(updateData[self.setting.id])]);
  992. // }
  993. // });
  994. // };
  995. }
  996. class StageTree extends FxTree {
  997. /**
  998. * 构造函数
  999. */
  1000. constructor (setting) {
  1001. super(setting);
  1002. // stage关联索引
  1003. this.stageItems = {};
  1004. }
  1005. /**
  1006. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1007. * @param datas
  1008. */
  1009. loadDatas (datas) {
  1010. super.loadDatas(datas);
  1011. // 清空旧数据
  1012. this.stageItems = {};
  1013. // 加载全部数据
  1014. for (const data of this.datas) {
  1015. const keyName = itemsPre + data[this.setting.stageId];
  1016. this.stageItems[keyName] = data;
  1017. }
  1018. }
  1019. getStageItems(id) {
  1020. return this.stageItems[itemsPre + id];
  1021. }
  1022. loadStageData(datas, fieldPre, fields) {
  1023. datas = datas instanceof Array ? datas : [datas];
  1024. const loadedData = [];
  1025. for (const data of datas) {
  1026. let node = this.getStageItems(data.lid);
  1027. if (node) {
  1028. for (const prop of fields) {
  1029. if (data[prop] !== undefined) {
  1030. node[fieldPre + prop] = data[prop];
  1031. }
  1032. }
  1033. loadedData.push(node);
  1034. }
  1035. }
  1036. }
  1037. loadPreStageData(datas) {
  1038. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1039. }
  1040. loadCurStageData(datas) {
  1041. this.loadStageData(datas, '', this.setting.updateFields);
  1042. }
  1043. /**
  1044. * 加载数据(动态),只加载不同部分
  1045. * @param {Array} datas
  1046. * @return {Array} 加载到树的数据
  1047. * @privateA
  1048. */
  1049. _updateData (datas) {
  1050. datas = datas instanceof Array ? datas : [datas];
  1051. let loadedData = [];
  1052. for (const data of datas) {
  1053. let node = this.getItems(data[this.setting.id]);
  1054. if (node) {
  1055. for (const prop in node) {
  1056. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1057. }
  1058. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1059. if (prop === this.setting.pid) {
  1060. loadedData.push(this.getItems(node[this.setting.pid]));
  1061. loadedData.push(this.getItems(data[this.setting.pid]));
  1062. }
  1063. node[prop] = data[prop];
  1064. }
  1065. }
  1066. loadedData.push(node);
  1067. }
  1068. }
  1069. loadedData = _.uniq(loadedData);
  1070. for (const node of loadedData) {
  1071. node.children = this.getChildren(node);
  1072. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1073. }
  1074. this.sortTreeNode(true);
  1075. return loadedData;
  1076. };
  1077. /**
  1078. * 加载数据(动态),只加载不同部分
  1079. * @param {Array} datas
  1080. * @return {Array} 加载到树的数据
  1081. * @privateA
  1082. */
  1083. _updateStageData (datas) {
  1084. datas = datas instanceof Array ? datas : [datas];
  1085. const loadedData = [];
  1086. for (const data of datas) {
  1087. let node = this.getStageItems(data.lid);
  1088. if (node) {
  1089. for (const prop of this.setting.updateFields) {
  1090. if (data[prop] !== undefined) {
  1091. node[prop] = data[prop];
  1092. }
  1093. }
  1094. loadedData.push(node);
  1095. }
  1096. }
  1097. return loadedData;
  1098. };
  1099. /**
  1100. *
  1101. * @param parent
  1102. * @param node
  1103. * @private
  1104. */
  1105. _getNodesParents(parents, nodes) {
  1106. for (const node of nodes) {
  1107. const parent = this.getParent(node);
  1108. if (parent) {
  1109. const paths = this.getFullPathNodes(parent.full_path);
  1110. for (const p of paths) {
  1111. if (parents.indexOf(p) === -1) {
  1112. parents.push(p);
  1113. }
  1114. }
  1115. }
  1116. if (node.children && node.children.length > 0) {
  1117. parents.push(node);
  1118. }
  1119. }
  1120. }
  1121. _updateDgnData(datas) {
  1122. datas = datas instanceof Array ? datas : [datas];
  1123. let loadedData = [];
  1124. for (const data of datas) {
  1125. let node = this.getStageItems(data.id);
  1126. if (node) {
  1127. for (const prop in data) {
  1128. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1129. node[prop] = data[prop];
  1130. }
  1131. }
  1132. loadedData.push(node);
  1133. }
  1134. }
  1135. return loadedData;
  1136. }
  1137. /**
  1138. * 提交数据至后端,返回的前端树结构应刷新的部分
  1139. * StageTree仅有更新CurStage部分,不需要增删
  1140. *
  1141. * @param data - 需要更新的数据
  1142. * @returns {Array} - 界面需要刷新的数据
  1143. */
  1144. loadPostStageData(data) {
  1145. let result, parents = [];
  1146. if (data.bills) {
  1147. result = this._updateData(data.bills);
  1148. this._getNodesParents(parents, result);
  1149. }
  1150. if (data.curStageData) {
  1151. result = this._updateStageData(data.curStageData);
  1152. this._getNodesParents(parents, result);
  1153. }
  1154. if (data.dgn) {
  1155. const dgnResult = this._updateDgnData(data.dgn);
  1156. result = result ? result.concat(dgnResult) : dgnResult;
  1157. }
  1158. result = result ? result.concat(parents) : parents;
  1159. result.sort((a, b) => {
  1160. return b.level - a.level;
  1161. });
  1162. for (const node of result) {
  1163. treeCalc.calculateNode(this, node);
  1164. }
  1165. return result;
  1166. }
  1167. }
  1168. class MasterTree extends FxTree {
  1169. /**
  1170. * 构造函数
  1171. */
  1172. constructor (setting) {
  1173. super(setting);
  1174. // 关联索引
  1175. this.masterItems = {};
  1176. }
  1177. /**
  1178. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1179. * @param datas
  1180. */
  1181. loadDatas (datas) {
  1182. super.loadDatas(datas);
  1183. // 清空旧数据
  1184. this.masterItems = {};
  1185. // minor数据缓存
  1186. this.minorData = {};
  1187. // 加载全部数据
  1188. for (const data of this.datas) {
  1189. const keyName = itemsPre + data[this.setting.masterId];
  1190. this.masterItems[keyName] = data;
  1191. }
  1192. }
  1193. /**
  1194. * 根据关联id,查找节点
  1195. * @param id
  1196. * @returns {*}
  1197. */
  1198. getMasterItems(id) {
  1199. return this.masterItems[itemsPre + id];
  1200. }
  1201. /**
  1202. * 加载关联数据
  1203. *
  1204. * @param {Array|Object}datas - 需要关联的数据
  1205. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1206. * @param {Array} fields - 关联字段
  1207. * @returns {Array}
  1208. */
  1209. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1210. for (const cf of calcFields) {
  1211. this.setting.calcFields.push(cf+fieldSuf);
  1212. }
  1213. if (!datas) return;
  1214. datas = datas instanceof Array ? datas : [datas];
  1215. this.minorData[fieldSuf] = datas;
  1216. const loadedData = [];
  1217. for (const data of datas) {
  1218. let node = this.getMasterItems(data[this.setting.minorId]);
  1219. if (node) {
  1220. for (const prop of fields) {
  1221. if (data[prop] !== undefined) {
  1222. node[prop + fieldSuf] = data[prop];
  1223. }
  1224. }
  1225. loadedData.push(node);
  1226. }
  1227. }
  1228. return loadedData;
  1229. }
  1230. }
  1231. if (type === 'base') {
  1232. return new BaseTree(setting);
  1233. } else if (type === 'fx') {
  1234. return new FxTree(setting);
  1235. } else if (type === 'stage') {
  1236. return new StageTree(setting);
  1237. } else if (type === 'ledger') {
  1238. return new LedgerTree(setting);
  1239. } else if (type === 'measure') {
  1240. return new MeasureTree(setting);
  1241. } else if (type === 'master') {
  1242. return new MasterTree(setting);
  1243. }
  1244. };
  1245. const treeCalc = {
  1246. mapTreeNode: function (tree) {
  1247. let map = {}, maxLevel = 0;
  1248. for (const node of tree.nodes) {
  1249. let levelArr = map[node.level];
  1250. if (!levelArr) {
  1251. levelArr = [];
  1252. map[node.level] = levelArr;
  1253. }
  1254. if (node.level > maxLevel) {
  1255. maxLevel = node.level;
  1256. }
  1257. levelArr.push(node);
  1258. }
  1259. return [maxLevel, map];
  1260. },
  1261. getMaxLevel: function (tree) {
  1262. return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
  1263. },
  1264. calculateNode: function (tree, node) {
  1265. if (node.children && node.children.length > 0) {
  1266. const gather = node.children.reduce(function (rst, x) {
  1267. const result = {};
  1268. for (const cf of tree.setting.calcFields) {
  1269. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1270. }
  1271. return result;
  1272. });
  1273. // 汇总子项
  1274. for (const cf of tree.setting.calcFields) {
  1275. if (gather[cf]) {
  1276. node[cf] = gather[cf];
  1277. } else {
  1278. node[cf] = null;
  1279. }
  1280. }
  1281. }
  1282. // 自身运算
  1283. if (tree.setting.calcFun) {
  1284. tree.setting.calcFun(node);
  1285. }
  1286. },
  1287. calculateLevelNode: function (tree, level) {
  1288. const nodes = tree.datas.filter((n) => { return n.level === level });
  1289. for (const node of nodes) {
  1290. this.calculateNode(tree, node);
  1291. }
  1292. },
  1293. calculateAll: function (tree) {
  1294. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1295. for (let i = maxLevel; i >= 0; i--) {
  1296. const levelNodes = levelMap[i];
  1297. if (levelNodes && levelNodes.length > 0) {
  1298. for (const node of levelNodes) {
  1299. this.calculateNode(tree, node);
  1300. }
  1301. }
  1302. }
  1303. },
  1304. calculateParent: function (tree, node) {
  1305. const nodes = tree.getFullPathNodes(node.full_path);
  1306. nodes.sort((a, b) => {
  1307. return b.level - a.level;
  1308. });
  1309. for (const n of nodes) {
  1310. this.calculateNode(tree, n);
  1311. }
  1312. return nodes;
  1313. }
  1314. };