123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368 |
- /**
- * (需使用 decimal.min.js, zh_calc.js)
- *
- * 构建pathTree
- * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
- * @param {Object} setting - 设置
- * @returns {PathTree}
- */
- 'use strict';
- class PosData {
- /**
- * 构造函数
- * @param {id|Number, masterId|Number} setting
- */
- constructor (setting) {
- // 无索引
- this.datas = null;
- // 以key为索引
- this.items = {};
- // 以分类id为索引的有序
- this.ledgerPos = {};
- // pos设置
- this.setting = setting;
- }
- /**
- * 加载部位明细数据
- * @param datas
- */
- loadDatas(datas) {
- this.datas = datas;
- this.items = {};
- this.ledgerPos = {};
- for (const data of this.datas) {
- const key = itemsPre + data[this.setting.id];
- this.items[key] = data;
- const masterKey = itemsPre + data[this.setting.ledgerId];
- if (!this.ledgerPos[masterKey]) {
- this.ledgerPos[masterKey] = [];
- }
- this.ledgerPos[masterKey].push(data);
- }
- for (const prop in this.ledgerPos) {
- if (this.ledgerPos[prop] instanceof Array) {
- this.ledgerPos[prop].sort(function (a, b) {
- return a.porder - b.porder;
- })
- }
- }
- }
- /**
- * 更新数据
- * @param datas
- */
- updateDatas(data) {
- const datas = data instanceof Array ? data : [data];
- const result = { create: [], update: [] };
- for (const d of datas) {
- const key = itemsPre + d[this.setting.id];
- if (!this.items[key]) {
- this.datas.push(d);
- this.items[key] = d;
- const masterKey = itemsPre + d[this.setting.ledgerId];
- if (!this.ledgerPos[masterKey]) {
- this.ledgerPos[masterKey] = [];
- }
- this.ledgerPos[masterKey].push(d);
- result.create.push(d);
- } else {
- const pos = this.items[key];
- for (const prop in d) {
- pos[prop] = d[prop];
- }
- result.update.push(pos);
- }
- }
- return result;
- }
- /**
- * 移除数据
- * @param datas
- */
- removeDatas(data) {
- if (!data) { return; }
- const datas = data instanceof Array ? data : [data];
- for (let i = datas.length - 1; i >= 0; i--) {
- const id = datas[i];
- const d = this.getPos(id);
- this.datas.splice(this.datas.indexOf(d), 1);
- const key = itemsPre + d[this.setting.id];
- delete this.items[key];
- const masterKey = itemsPre + d[this.setting.ledgerId];
- const range = this.ledgerPos[masterKey];
- range.splice(range.indexOf(d), 1);
- if (range.length === 0) {
- delete this.ledgerPos[masterKey];
- }
- }
- }
- /**
- * 移除数据 - 根据分类id
- * @param mid
- */
- removeDatasByMasterId (mid) {
- const masterKey = itemsPre + mid;
- const range = this.ledgerPos[masterKey];
- if (range) {
- delete this.ledgerPos[masterKey];
- for (const r of range) {
- this.datas.splice(this.datas.indexOf(r), 1);
- const key = itemsPre + r[this.setting.id];
- delete this.items[key];
- }
- }
- }
- getPos(id) {
- return this.items[itemsPre + id];
- }
- getLedgerPos(mid) {
- return this.ledgerPos[itemsPre + mid];
- }
- /**
- * 计算全部
- */
- calculateAll() {
- if (!this.setting.calcFun) { return; }
- for (const pos of this.datas) {
- this.setting.calcFun(pos);
- }
- }
- }
- class StagePosData extends PosData {
- loadStageData(datas, fieldPre, fields) {
- if (!datas) { return; }
- datas = datas instanceof Array ? datas : [datas];
- const loadedData = [];
- for (const data of datas) {
- let node = this.getPos(data.pid);
- if (node) {
- for (const prop of fields) {
- if (data[prop] !== undefined) {
- node[fieldPre + prop] = data[prop];
- }
- }
- if (this.setting.calcFun) {
- this.setting.calcFun(node);
- }
- loadedData.push(node);
- }
- }
- }
- loadPreStageData(datas) {
- this.loadStageData(datas, 'pre_', this.setting.updateFields);
- }
- loadCurStageData(datas) {
- this.loadStageData(datas, '', this.setting.updateFields);
- }
- }
- class MasterPosData extends PosData {
- /**
- * 构造函数
- * @param {id|Number, masterId|Number} setting
- */
- constructor (setting) {
- super(setting);
- // 关联索引
- this.masterItems = {};
- }
- /**
- * 加载主数据
- * @param datas
- */
- loadDatas (datas) {
- super.loadDatas(datas);
- // 清空旧数据
- this.masterItems = {};
- // minor数据缓存
- this.minorData = {};
- // 加载全部数据
- for (const data of this.datas) {
- const keyName = itemsPre + data[this.setting.masterId];
- this.masterItems[keyName] = data;
- }
- }
- /**
- * 根据关联id,查找节点
- * @param id
- * @returns {*}
- */
- getMasterItems(id) {
- return this.masterItems[itemsPre + id];
- }
- /**
- * 加载关联数据
- *
- * @param {Array|Object}datas - 需要关联的数据
- * @param {String} fieldPre - 关联字段前缀(关联结果)
- * @param {Array} fields - 关联字段
- * @returns {Array}
- */
- loadMinorData(datas, fieldSuf, fields) {
- if (!datas) { return; }
- datas = datas instanceof Array ? datas : [datas];
- this.minorData[fieldSuf] = datas;
- const loadedData = [];
- for (const data of datas) {
- let node = this.getMasterItems(data[this.setting.minorId]);
- if (node) {
- for (const prop of fields) {
- if (data[prop] !== undefined) {
- node[prop + fieldSuf] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- return loadedData;
- }
- }
- const itemsPre = 'id_';
- const createNewPathTree = function (type, setting) {
- class BaseTree {
- /**
- * 构造函数
- */
- constructor (setting) {
- // 无索引
- this.datas = [];
- // 以key为索引
- this.items = {};
- // 以排序为索引
- this.nodes = [];
- // 根节点
- this.children = [];
- // 树设置
- this.setting = setting;
- }
- /**
- * 树结构根据显示排序
- */
- sortTreeNode (isResort) {
- const self = this;
- const addSortNodes = function (nodes) {
- if (!nodes) { return }
- for (let i = 0; i < nodes.length; i++) {
- self.nodes.push(nodes[i]);
- nodes[i].index = self.nodes.length - 1;
- if (!isResort) {
- nodes[i].children = self.getChildren(nodes[i]);
- } else {
- nodes[i].children.sort(function (a, b) {
- return a.order - b.order;
- })
- }
- addSortNodes(nodes[i].children);
- }
- };
- this.nodes = [];
- addSortNodes(this.children);
- }
- /**
- * 加载数据(初始化), 并给数据添加部分树结构必须数据
- * @param datas
- */
- loadDatas (datas) {
- // 清空旧数据
- this.items = {};
- this.nodes = [];
- this.datas = [];
- this.children = [];
- // 加载全部数据
- datas.sort(function (a, b) {
- return a.level - b.level;
- });
- for (const data of datas) {
- const keyName = itemsPre + data[this.setting.id];
- if (!this.items[keyName]) {
- const item = JSON.parse(JSON.stringify(data));
- item.children = [];
- item.expanded = true;
- item.visible = true;
- this.items[keyName] = item;
- this.datas.push(item);
- if (item[setting.pid] === setting.rootId) {
- this.children.push(item);
- } else {
- const parent = this.getParent(item);
- if (parent) {
- parent.children.push(item);
- }
- }
- }
- }
- this.children.sort(function (a, b) {
- return a.order - b.order;
- });
- this.sortTreeNode(true);
- }
- getItemsByIndex(index) {
- return this.nodes[index];
- }
- /**
- * 根据id获取树结构节点数据
- * @param {Number} id
- * @returns {Object}
- */
- getItems (id) {
- return this.items[itemsPre + id];
- };
- /**
- * 查找node的parent
- * @param {Object} node
- * @returns {Object}
- */
- getParent (node) {
- return this.getItems(node[this.setting.pid]);
- };
- getAllParents (node) {
- const parents = [];
- if (node.full_path && node.full_path !== '') {
- const parentIds = node.full_path.split('.');
- for (const id of parentIds) {
- if (id !== node[this.setting.id]) {
- parents.push(this.getItems(id));
- }
- }
- } else {
- let vP = this.getParent(node);
- while (vP) {
- parents.push(vP);
- vP = this.getParent(vP);
- }
- }
- return parents;
- }
- /**
- * 查找node的前兄弟节点
- * @param node
- * @returns {*}
- */
- getPreSiblingNode(node) {
- if (!node) return null;
- const parent = this.getParent(node);
- const siblings = parent ? parent.children : this.children;
- const index = siblings.indexOf(node);
- return (index > 0) ? siblings[index - 1] : null;
- }
- /**
- * 查找node的后兄弟节点
- * @param node
- * @returns {*}
- */
- getNextSiblingNode(node) {
- const parent = this.getParent(node);
- const siblings = parent ? parent.children : this.children;
- const index = siblings.indexOf(node);
- if (index >= 0 && index < siblings.length - 1) {
- return siblings[index + 1];
- } else {
- return null;
- }
- }
- /**
- * 根据path查找完整节点
- * @param {Number} path
- */
- getFullPathNodes (path) {
- const self = this, ids = path.split('.');
- if (ids.length > 0) {
- return this.nodes.filter((x) => {
- return ids.indexOf('' + x[self.setting.id]) >= 0;
- });
- } else {
- return [];
- }
- };
- /**
- * 查询node的已下载子节点
- * @param {Object} node
- * @returns {Array}
- */
- getChildren (node) {
- const setting = this.setting;
- const pid = node ? node[setting.id] : setting.rootId;
- const children = this.datas.filter(function (x) {
- return x[setting.pid] === pid;
- });
- children.sort(function (a, b) {
- return a.order - b.order;
- });
- return children;
- };
- /**
- * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
- * @param node
- * @returns {*}
- * @private
- */
- _recursiveGetPosterity (node) {
- let posterity = node.children;
- for (const c of node.children) {
- posterity = posterity.concat(this._recursiveGetPosterity(c));
- }
- return posterity;
- };
- /**
- * 查询node的已下载的全部后代
- * @param {Object} node
- * @returns {Array}
- */
- getPosterity (node) {
- if (node.full_path !== '') {
- const reg = new RegExp('^' + node.full_path + '.');
- return this.datas.filter(function (x) {
- return reg.test(x.full_path);
- });
- } else {
- return this._recursiveGetPosterity(node);
- }
- };
- /**
- * 查询node是否是父节点的最后一个子节点
- * @param {Object} node
- * @returns {boolean}
- */
- isLastSibling (node) {
- const siblings = this.getChildren(this.getParent(node));
- return node.order === siblings[siblings.length - 1].order;
- };
- /**
- * 刷新子节点是否可见
- * @param {Object} node
- * @private
- */
- _refreshChildrenVisible (node) {
- if (!node.children) {
- node.children = this.getChildren(node);
- }
- if (node.children && node.children.length > 0) {
- for (const child of node.children) {
- child.visible = node.expanded && node.visible;
- this._refreshChildrenVisible(child);
- }
- }
- };
- /**
- * 设置节点是否展开, 并控制子节点可见
- * @param {Object} node
- * @param {Boolean} expanded
- */
- setExpanded (node, expanded) {
- node.expanded = expanded;
- this._refreshChildrenVisible(node);
- };
- /**
- * 提取节点key和索引数据
- * @param {Object} node - 节点
- * @returns {key}
- */
- getNodeKeyData (node) {
- const data = {};
- for (const key of this.setting.keys) {
- data[key] = node[key];
- }
- return data;
- };
- /**
- * 得到树结构构成id
- * @param node
- * @returns {*}
- */
- getNodeKey (node) {
- return node[this.setting.id];
- };
- /**
- * 递归 设置节点展开状态
- * @param {Array} nodes - 需要设置状态的节点
- * @param {Object} parent - nodes的父节点
- * @param {Function} checkFun - 判断节点展开状态的方法
- * @private
- */
- _recursiveExpand(nodes, parent, checkFun) {
- for (const node of nodes) {
- node.expanded = checkFun(node);
- node.visible = parent ? (parent.expanded && parent.visible) : true;
- this._recursiveExpand(node.children, node, checkFun);
- }
- }
- /**
- * 自定义展开规则
- * @param checkFun
- */
- expandByCustom(checkFun) {
- this._recursiveExpand(this.children, null, checkFun);
- }
- /**
- * 展开到第几层
- * @param {Number} level - 展开层数
- */
- expandByLevel(level) {
- // function recursiveExpand(nodes, parent) {
- // for (const node of nodes) {
- // node.expanded = node.level < level;
- // node.visible = parent ? (parent.expanded && parent.visible) : true;
- // recursiveExpand(node.children, node);
- // }
- // }
- // recursiveExpand(this.children);
- this.expandByCustom(function (n) {
- return n.level < level;
- });
- }
- /**
- * 自动展开节点node
- * @param node
- * @returns {*}
- */
- autoExpandNode(node) {
- const parents = this.getAllParents(node);
- const reload = [];
- for (const p of parents) {
- if (!p.expanded) {
- reload.push(p);
- this.setExpanded(p, true);
- }
- }
- return reload;
- }
- }
- class MeasureTree extends BaseTree {
- addData (datas) {
- const loadedData = [];
- for (const data of datas) {
- let node = this.getItems(data[this.setting.id]);
- if (node) {
- for (const prop in node) {
- if (data[prop] !== undefined) {
- node[prop] = data[prop];
- }
- }
- loadedData.push(node);
- } else {
- const keyName = itemsPre + data[this.setting.id];
- const node = JSON.parse(JSON.stringify(data));
- this.items[keyName] = node;
- this.datas.push(node);
- node.expanded = false;
- node.visible = true;
- loadedData.push(node);
- }
- }
- this.sortTreeNode();
- for (const node of loadedData) {
- const children = node.children;
- if (!node.expanded && children.length > 0) {
- node.expanded = true;
- this._refreshChildrenVisible(node);
- }
- }
- return loadedData;
- }
- removeData (datas) {
- datas.sort(function (a, b) {
- return b.level - a.level;
- });
- const removeArrayData = function (array, data) {
- const index = array.indexOf(data);
- array.splice(index, 1);
- };
- for (const data of datas) {
- const node = this.getItems(data[this.setting.id]);
- if (node && this.getChildren(node).length === 0) {
- delete this.items[itemsPre + node[this.setting.id]];
- if (node[this.setting.pid] !== this.setting.rootId) {
- const parent = this.items[itemsPre + node[this.setting.pid]];
- removeArrayData(parent.children, node);
- }
- removeArrayData(this.datas, node);
- removeArrayData(this.nodes, node);
- }
- }
- };
- loadLeafData (data) {
- const datas = data instanceof Array ? data : [data];
- for (const d of datas) {
- let node = this.getItems(d[this.setting.id]);
- if (node && node.is_leaf) {
- for (const prop in node) {
- if (data[prop] !== undefined) {
- node[prop] = d[prop];
- }
- }
- }
- }
- };
- }
- class FxTree extends BaseTree {
- /**
- * 检查节点是否是最底层项目节
- * @param node
- * @returns {boolean}
- */
- isLeafXmj(node) {
- if (!node.code) {
- return false;
- }
- for (const child of node.children) {
- if (!child.b_code || child.b_code === '') {
- return false;
- }
- }
- return true;
- }
- /**
- * 查询最底层项目节(本身或父项)
- * @param {Object} node - 查询节点
- * @returns {Object}
- */
- getLeafXmjParent(node) {
- let parent = node;
- while (parent) {
- if (this.isLeafXmj(parent)) {
- return parent;
- } else {
- parent = this.getParent(parent);
- }
- }
- return null;
- }
- /**
- * 展开至最底层项目节
- */
- expandToLeafXmj() {
- const self = this;
- this.expandByCustom(function (node) {
- if (node.b_code && node.b_code !== '') {
- return false;
- } else {
- return !self.isLeafXmj(node);
- }
- })
- }
- /**
- * 展开至计算项
- */
- expandByCalcFields() {
- const self = this;
- this.expandByCustom(function (node) {
- for (const field of self.setting.calcFields) {
- if (node[field]) {
- return true;
- }
- }
- return false;
- })
- }
- }
- class LedgerTree extends FxTree {
- /**
- * 加载数据(动态),只加载不同部分
- * @param {Array} datas
- * @return {Array} 加载到树的数据
- * @privateA
- */
- _updateData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- let loadedData = [];
- for (const data of datas) {
- let node = this.getItems(data[this.setting.id]);
- if (node) {
- for (const prop in node) {
- if (data[prop] !== undefined && data[prop] !== node[prop]) {
- if (prop === this.setting.pid) {
- loadedData.push(this.getItems(node[this.setting.pid]));
- loadedData.push(this.getItems(data[this.setting.pid]));
- }
- if (prop === this.setting.order) {
- loadedData = loadedData.concat(this.getPosterity(node));
- }
- node[prop] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- loadedData = _.uniq(loadedData);
- for (const node of loadedData) {
- node.children = this.getChildren(node);
- node.expanded = node.children.length === 0 ? true : node.children[0].visible;
- }
- this.sortTreeNode(true);
- return loadedData;
- };
- /**
- * 加载数据(动态),只加载不同部分
- * @param {Array} datas
- * @return {Array} 加载到树的数据
- * @privateA
- */
- _loadData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- const loadedData = [], resortData = [];
- for (const data of datas) {
- let node = this.getItems(data[this.setting.id]);
- if (node) {
- const parent = this.getItems(node[this.setting.pid]);
- for (const prop in node) {
- if (data[prop] !== undefined && data[prop] !== node[prop]) {
- node[prop] = data[prop];
- if (parent && resortData.indexOf(parent) === -1) {
- resortData.push(parent);
- }
- }
- }
- loadedData.push(node);
- } else {
- const keyName = itemsPre + data[this.setting.id];
- const node = JSON.parse(JSON.stringify(data));
- this.items[keyName] = node;
- this.datas.push(node);
- node.expanded = true;
- node.visible = true;
- loadedData.push(node);
- if (resortData.indexOf(node) === -1) {
- resortData.push(node);
- }
- const parent = this.getItems(node[this.setting.pid]);
- if (parent && resortData.indexOf(parent) === -1) {
- resortData.push(parent);
- }
- }
- }
- for (const node of resortData) {
- node.children = this.getChildren(node);
- }
- this.sortTreeNode(true);
- for (const node of loadedData) {
- if (!node.expanded) {
- this.setExpanded(node, true);
- }
- }
- return loadedData;
- };
- /**
- * 清理数据(动态)
- * @param datas
- * @private
- */
- _freeData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- const freeDatas = [];
- const removeArrayData = function (array, data) {
- const index = array.indexOf(data);
- array.splice(index, 1);
- };
- for (const data of datas) {
- const node = this.getItems(data[this.setting.id]);
- if (node) {
- freeDatas.push(node);
- delete this.items[itemsPre + node[this.setting.id]];
- if (node[this.setting.pid] !== this.setting.rootId) {
- const parent = this.getItems(node[this.setting.pid]);
- if (parent) {
- removeArrayData(parent.children, node);
- }
- }
- removeArrayData(this.datas, node);
- removeArrayData(this.nodes, node);
- }
- }
- return freeDatas;
- };
- /**
- * 加载需展开的数据
- * @param {Array} datas
- * @returns {Array}
- * @private
- */
- _loadExpandData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- const loadedData = [], existData = [], expandData = [], resortData = [];
- for (const data of datas) {
- let node = this.getItems(data[this.setting.id]);
- if (node) {
- existData.push(node);
- } else {
- const keyName = itemsPre + data[this.setting.id];
- const node = JSON.parse(JSON.stringify(data));
- this.items[keyName] = node;
- this.datas.push(node);
- node.expanded = false;
- node.visible = true;
- loadedData.push(node);
- if (resortData.indexOf(node) === -1) {
- resortData.push(node);
- }
- const parent = this.getItems(node[this.setting.pid]);
- if (parent && resortData.indexOf(parent) === -1) {
- resortData.push(parent);
- }
- }
- }
- for (const node of resortData) {
- node.children = this.getChildren(node);
- }
- this.sortTreeNode(true);
- for (const node of loadedData) {
- if (!node.expanded) {
- this.setExpanded(node, true);
- }
- }
- for (const node of existData) {
- const parent = this.getItems(node[this.setting.pid]);
- if (expandData.indexOf(parent) === -1) {
- expandData.push(parent);
- if (!parent.expanded) {
- this.setExpanded(parent, true);
- }
- }
- if (!node.expanded) {
- this.setExpanded(node, true);
- }
- }
- return [loadedData, expandData];
- };
- /**
- *
- * @param parent
- * @param node
- * @private
- */
- _getNodesParents(parents, nodes) {
- for (const node of nodes) {
- const parent = this.getParent(node);
- if (parent) {
- const paths = this.getFullPathNodes(parent.full_path);
- for (const p of paths) {
- if (parents.indexOf(p) === -1) {
- parents.push(p);
- }
- }
- }
- if (this.getItems(node.ledger_id) && node.children.length > 0) {
- parents.push(node);
- }
- }
- }
- _getReCalcNodes(reCalcNodes, nodes) {
- for (const node of nodes) {
- const parent = this.getParent(node);
- if (parent) {
- const paths = this.getFullPathNodes(parent.full_path);
- for (const p of paths) {
- if (reCalcNodes.indexOf(p) === -1) {
- reCalcNodes.push(p);
- }
- }
- }
- // 最底层项目节,也需要计算
- //if (this.getItems(node.ledger_id) && node.children.length > 0) {
- reCalcNodes.push(node);
- //}
- }
- }
- /**
- * 因为提交其他数据,引起的树结构数据更新,调用该方法
- *
- * @param data - 更新的数据 {update, create, delete}
- * @returns {{}}
- */
- loadPostData(data) {
- const result = {}, reCalcNodes = [];
- if (data.update) {
- result.update = this._updateData(data.update);
- this._getReCalcNodes(reCalcNodes, result.update);
- }
- if (data.create) {
- result.create = this._loadData(data.create);
- this._getReCalcNodes(reCalcNodes, result.create);
- }
- if (data.delete) {
- result.delete = this._freeData(data.delete);
- this._getReCalcNodes(reCalcNodes, result.delete);
- }
- reCalcNodes.sort((a, b) => {
- return b.level - a.level;
- });
- for (const node of reCalcNodes) {
- treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
- }
- result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
- return result;
- }
- /**
- * 以下方法需等待响应, 通过callback刷新界面
- */
- /**
- * 加载子节点
- * @param {Object} node
- * @param {function} callback
- */
- loadChildren (node, callback) {
- const self = this;
- const url = this.setting.preUrl ? this.setting.preUrl + '/get-children' : 'get-children';
- postData(url, this.getNodeKeyData(node), function (data) {
- self._loadData(data);
- callback();
- });
- };
- /**
- * 树结构基本操作
- * @param {String} url - 请求地址
- * @param {Object} node - 操作节点
- * @param {String} type - 操作类型
- * @param {function} callback - 界面刷新
- */
- baseOperation (url, node, type, callback) {
- const self = this;
- const data = {
- id: node[this.setting.id],
- postType: type
- };
- postData(url, data, function (datas) {
- const refreshData = self.loadPostData(datas);
- callback(refreshData);
- });
- };
- /**
- * 节点数据编辑
- * @param {String} url - 请求地址
- * @param {Array|Object} updateData - 需更新的数据
- * @param {function} callback - 界面刷新
- */
- update (url, updateData, callback) {
- const self = this;
- postData(url, updateData, function (datas) {
- const refreshData = self.loadPostData(datas);
- callback(refreshData);
- }, function () {
- if (updateData instanceof Array) {
- const result = [];
- for (const data of updateData) {
- result.push(self.getItems(data[self.setting.id]));
- }
- callback(result)
- } else {
- callback([self.getItems(updateData[self.setting.id])]);
- }
- });
- };
- /**
- * 复制粘贴整块(目前仅可粘贴为后项)
- * @param {String} url - 请求地址
- * @param {Object} node - 操作节点
- * @param {Array} block - 被复制整块的节点列表
- * @param {function} callback - 界面刷新
- */
- pasteBlock (url, node, block, callback) {
- const self = this;
- const data = {
- id: node[self.setting.id],
- block: block
- };
- postData(url, data, function (datas) {
- const refreshData = self.loadPostData(datas);
- callback(refreshData);
- });
- };
- /**
- * 提交数据
- * @param {String} url - 请求地址
- * @param {Object} node - 当前选中节点
- * @param {Object} data - 提交的数据
- * @param {function} callback - 界面刷新
- */
- postData (url, node, data, callback) {
- const self = this;
- if (node) {
- data.id = node[self.setting.id];
- }
- postData(url, data, function (datas) {
- const refreshData = self.loadPostData(datas);
- callback(refreshData);
- // const result = {};
- // if (datas.update) {
- // result.update = self._updateData(datas.update);
- // }
- // if (datas.create) {
- // result.create = self._loadData(datas.create);
- // }
- // if (datas.delete) {
- // result.delete = self._freeData(datas.delete);
- // }
- // if (datas.expand) {
- // const [create, update] = self._loadExpandData(datas.expand);
- // result.create = result.create ? result.create.concat(create) : create;
- // result.expand = update;
- // }
- // callback(result);
- });
- };
- }
- class StageTree extends FxTree {
- /**
- * 构造函数
- */
- constructor (setting) {
- super(setting);
- // stage关联索引
- this.stageItems = {};
- }
- /**
- * 加载数据(初始化), 并给数据添加部分树结构必须数据
- * @param datas
- */
- loadDatas (datas) {
- super.loadDatas(datas);
- // 清空旧数据
- this.stageItems = {};
- // 加载全部数据
- for (const data of this.datas) {
- const keyName = itemsPre + data[this.setting.stageId];
- this.stageItems[keyName] = data;
- }
- }
- getStageItems(id) {
- return this.stageItems[itemsPre + id];
- }
- loadStageData(datas, fieldPre, fields) {
- datas = datas instanceof Array ? datas : [datas];
- const loadedData = [];
- for (const data of datas) {
- let node = this.getStageItems(data.lid);
- if (node) {
- for (const prop of fields) {
- if (data[prop] !== undefined) {
- node[fieldPre + prop] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- }
- loadPreStageData(datas) {
- this.loadStageData(datas, 'pre_', this.setting.updateFields);
- }
- loadCurStageData(datas) {
- this.loadStageData(datas, '', this.setting.updateFields);
- }
- /**
- * 加载数据(动态),只加载不同部分
- * @param {Array} datas
- * @return {Array} 加载到树的数据
- * @privateA
- */
- _updateData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- let loadedData = [];
- for (const data of datas) {
- let node = this.getItems(data[this.setting.id]);
- if (node) {
- for (const prop in node) {
- if (prop === this.setting.pid && data[prop] !== node[prop]) {
- }
- if (data[prop] !== undefined && data[prop] !== node[prop]) {
- if (prop === this.setting.pid) {
- loadedData.push(this.getItems(node[this.setting.pid]));
- loadedData.push(this.getItems(data[this.setting.pid]));
- }
- node[prop] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- loadedData = _.uniq(loadedData);
- for (const node of loadedData) {
- node.children = this.getChildren(node);
- node.expanded = node.children.length === 0 ? true : node.children[0].visible;
- }
- this.sortTreeNode(true);
- return loadedData;
- };
- /**
- * 加载数据(动态),只加载不同部分
- * @param {Array} datas
- * @return {Array} 加载到树的数据
- * @privateA
- */
- _updateStageData (datas) {
- datas = datas instanceof Array ? datas : [datas];
- const loadedData = [];
- for (const data of datas) {
- let node = this.getStageItems(data.lid);
- if (node) {
- for (const prop of this.setting.updateFields) {
- if (data[prop] !== undefined) {
- node[prop] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- return loadedData;
- };
- /**
- *
- * @param parent
- * @param node
- * @private
- */
- _getNodesParents(parents, nodes) {
- for (const node of nodes) {
- const parent = this.getParent(node);
- if (parent) {
- const paths = this.getFullPathNodes(parent.full_path);
- for (const p of paths) {
- if (parents.indexOf(p) === -1) {
- parents.push(p);
- }
- }
- }
- if (node.children && node.children.length > 0) {
- parents.push(node);
- }
- }
- }
- _updateDgnData(datas) {
- datas = datas instanceof Array ? datas : [datas];
- let loadedData = [];
- for (const data of datas) {
- let node = this.getStageItems(data.id);
- if (node) {
- for (const prop in node) {
- if (data[prop] !== undefined && data[prop] !== node[prop]) {
- node[prop] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- return loadedData;
- }
- /**
- * 提交数据至后端,返回的前端树结构应刷新的部分
- * StageTree仅有更新CurStage部分,不需要增删
- *
- * @param data - 需要更新的数据
- * @returns {Array} - 界面需要刷新的数据
- */
- loadPostStageData(data) {
- let result, parents = [];
- if (data.bills) {
- result = this._updateData(data.bills);
- this._getNodesParents(parents, result);
- }
- if (data.curStageData) {
- result = this._updateStageData(data.curStageData);
- this._getNodesParents(parents, result);
- }
- if (data.dgn) {
- const dgnResult = this._updateDgnData(data.dgn);
- result = result ? result.concat(dgnResult) : dgnResult;
- }
- result = result ? result.concat(parents) : parents;
- result.sort((a, b) => {
- return b.level - a.level;
- });
- for (const node of result) {
- treeCalc.calculateNode(this, node);
- }
- return result;
- }
- }
- class MasterTree extends FxTree {
- /**
- * 构造函数
- */
- constructor (setting) {
- super(setting);
- // 关联索引
- this.masterItems = {};
- }
- /**
- * 加载数据(初始化), 并给数据添加部分树结构必须数据
- * @param datas
- */
- loadDatas (datas) {
- super.loadDatas(datas);
- // 清空旧数据
- this.masterItems = {};
- // minor数据缓存
- this.minorData = {};
- // 加载全部数据
- for (const data of this.datas) {
- const keyName = itemsPre + data[this.setting.masterId];
- this.masterItems[keyName] = data;
- }
- }
- /**
- * 根据关联id,查找节点
- * @param id
- * @returns {*}
- */
- getMasterItems(id) {
- return this.masterItems[itemsPre + id];
- }
- /**
- * 加载关联数据
- *
- * @param {Array|Object}datas - 需要关联的数据
- * @param {String} fieldPre - 关联字段前缀(关联结果)
- * @param {Array} fields - 关联字段
- * @returns {Array}
- */
- loadMinorData(datas, fieldSuf, fields) {
- if (!datas) { return; }
- datas = datas instanceof Array ? datas : [datas];
- this.minorData[fieldSuf] = datas;
- const loadedData = [];
- for (const data of datas) {
- let node = this.getMasterItems(data[this.setting.minorId]);
- if (node) {
- for (const prop of fields) {
- if (data[prop] !== undefined) {
- node[prop + fieldSuf] = data[prop];
- }
- }
- loadedData.push(node);
- }
- }
- return loadedData;
- }
- }
- if (type === 'base') {
- return new BaseTree(setting);
- } else if (type === 'fx') {
- return new FxTree(setting);
- } else if (type === 'stage') {
- return new StageTree(setting);
- } else if (type === 'ledger') {
- return new LedgerTree(setting);
- } else if (type === 'measure') {
- return new MeasureTree(setting);
- } else if (type === 'master') {
- return new MasterTree(setting);
- }
- };
- const treeCalc = {
- mapTreeNode: function (tree) {
- let map = {}, maxLevel = 0;
- for (const node of tree.nodes) {
- let levelArr = map[node.level];
- if (!levelArr) {
- levelArr = [];
- map[node.level] = levelArr;
- }
- if (node.level > maxLevel) {
- maxLevel = node.level;
- }
- levelArr.push(node);
- }
- return [maxLevel, map];
- },
- getMaxLevel: function (tree) {
- return Math.max.apply(Math, tree.datas.map(function(o) {return o.level}));
- },
- calculateNode: function (tree, node) {
- if (node.children && node.children.length > 0) {
- const gather = node.children.reduce(function (rst, x) {
- const result = {};
- for (const cf of tree.setting.calcFields) {
- result[cf] = ZhCalc.add(rst[cf], x[cf]);
- }
- return result;
- });
- // 汇总子项
- for (const cf of tree.setting.calcFields) {
- if (gather[cf]) {
- node[cf] = gather[cf];
- } else {
- node[cf] = null;
- }
- }
- }
- // 自身运算
- if (tree.setting.calcFun) {
- tree.setting.calcFun(node);
- }
- },
- calculateLevelNode: function (tree, level) {
- const nodes = tree.datas.filter((n) => { return n.level === level });
- for (const node of nodes) {
- this.calculateNode(tree, node);
- }
- },
- calculateAll: function (tree) {
- const [maxLevel, levelMap] = this.mapTreeNode(tree);
- for (let i = maxLevel; i >= 0; i--) {
- const levelNodes = levelMap[i];
- if (levelNodes && levelNodes.length > 0) {
- for (const node of levelNodes) {
- this.calculateNode(tree, node);
- }
- }
- }
- },
- calculateParent: function (tree, node) {
- const nodes = tree.getFullPathNodes(node.full_path);
- nodes.sort((a, b) => {
- return b.level - a.level;
- });
- for (const n of nodes) {
- this.calculateNode(tree, n);
- }
- return nodes;
- }
- };
|