|
@@ -10,7 +10,7 @@
|
|
|
|
|
|
const itemsPre = 'id_';
|
|
|
|
|
|
-class billsTree {
|
|
|
+class baseTree {
|
|
|
/**
|
|
|
* 构造函数
|
|
|
*/
|
|
@@ -60,7 +60,11 @@ class billsTree {
|
|
|
});
|
|
|
return children;
|
|
|
};
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 获取节点的 index
|
|
|
+ * @param node
|
|
|
+ * @returns {number}
|
|
|
+ */
|
|
|
getNodeSerialNo(node) {
|
|
|
return this.nodes.indexOf(node);
|
|
|
}
|
|
@@ -95,7 +99,6 @@ class billsTree {
|
|
|
}
|
|
|
addSortNodes(this.children);
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 加载数据(初始化), 并给数据添加部分树结构必须数据
|
|
|
* @param datas
|
|
@@ -165,37 +168,47 @@ class billsTree {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * 检查节点是否是最底层项目节
|
|
|
- * @param node
|
|
|
- * @returns {boolean}
|
|
|
+ * 根据 字段名称 获取数据
|
|
|
+ * @param fields
|
|
|
+ * @returns {Array}
|
|
|
*/
|
|
|
- isLeafXmj(node) {
|
|
|
- if (node.b_code && node.b_code !== '') {
|
|
|
- return false;
|
|
|
- }
|
|
|
- for (const child of node.children) {
|
|
|
- if (!child.b_code || child.b_code === '') {
|
|
|
- return false;
|
|
|
+ getDatas (fields) {
|
|
|
+ const datas = [];
|
|
|
+ for (const node of this.nodes) {
|
|
|
+ if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
|
|
|
+ const data = {};
|
|
|
+ for (const field of fields) {
|
|
|
+ data[field] = node[field];
|
|
|
}
|
|
|
+ datas.push(data);
|
|
|
}
|
|
|
- return true;
|
|
|
+ return datas;
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
- * 查询最底层项目节(本身或父项)
|
|
|
- * @param {Object} node - 查询节点
|
|
|
- * @returns {Object}
|
|
|
+ * 排除 某些字段 获取数据
|
|
|
+ * @param fields
|
|
|
+ * @returns {Array}
|
|
|
*/
|
|
|
- getLeafXmjParent(node) {
|
|
|
- let parent = node;
|
|
|
- while (parent) {
|
|
|
- if (this.isLeafXmj(parent)) {
|
|
|
- return parent;
|
|
|
- } else {
|
|
|
- parent = this.getParent(parent);
|
|
|
+ getDatasWithout (fields) {
|
|
|
+ const datas = [];
|
|
|
+ for (const node of this.nodes) {
|
|
|
+ if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
|
|
|
+ const data = {};
|
|
|
+ for (const field in node) {
|
|
|
+ if (fields.indexOf(field) === -1) {
|
|
|
+ data[field] = node[field];
|
|
|
+ }
|
|
|
}
|
|
|
+ datas.push(data);
|
|
|
}
|
|
|
- return null;
|
|
|
+ return datas;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取默认数据 剔除一些树结构需要的缓存数据
|
|
|
+ * @returns {Array}
|
|
|
+ */
|
|
|
+ getDefaultDatas() {
|
|
|
+ return this.getDatasWithout(['expanded', 'visible', 'children', 'index']);
|
|
|
}
|
|
|
|
|
|
_mapTreeNode () {
|
|
@@ -250,37 +263,149 @@ class billsTree {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- getDatas (fields) {
|
|
|
- const datas = [];
|
|
|
- for (const node of this.nodes) {
|
|
|
- if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
|
|
|
- const data = {};
|
|
|
- for (const field of fields) {
|
|
|
- data[field] = node[field];
|
|
|
+class billsTree extends baseTree {
|
|
|
+ /**
|
|
|
+ * 检查节点是否是最底层项目节
|
|
|
+ * @param node
|
|
|
+ * @returns {boolean}
|
|
|
+ */
|
|
|
+ isLeafXmj(node) {
|
|
|
+ if (node.b_code && node.b_code !== '') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (const child of node.children) {
|
|
|
+ if (!child.b_code || child.b_code === '') {
|
|
|
+ return false;
|
|
|
}
|
|
|
- datas.push(data);
|
|
|
}
|
|
|
- return datas;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- getDatasWithout (fields) {
|
|
|
- const datas = [];
|
|
|
- for (const node of this.nodes) {
|
|
|
- if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
|
|
|
- const data = {};
|
|
|
- for (const field in node) {
|
|
|
- if (fields.indexOf(field) === -1) {
|
|
|
- data[field] = node[field];
|
|
|
+class filterTree extends baseTree {
|
|
|
+ addData(data, fields) {
|
|
|
+ const item = {};
|
|
|
+ for (const prop in data) {
|
|
|
+ if (fields.indexOf(prop) >= 0) {
|
|
|
+ item[prop] = data[prop];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const keyName = itemsPre + item[this.setting.id];
|
|
|
+ if (!this.items[keyName]) {
|
|
|
+ item.children = [];
|
|
|
+ item.is_leaf = true;
|
|
|
+ item.expanded = true;
|
|
|
+ item.visible = true;
|
|
|
+ this.items[keyName] = item;
|
|
|
+ this.datas.push(item);
|
|
|
+ if (item[this.setting.pid] === this.setting.rootId) {
|
|
|
+ this.children.push(item);
|
|
|
+ } else {
|
|
|
+ const parent = this.getParent(item);
|
|
|
+ if (parent) {
|
|
|
+ parent.is_leaf = false;
|
|
|
+ parent.children.push(item);
|
|
|
}
|
|
|
}
|
|
|
- datas.push(data);
|
|
|
+ } else {
|
|
|
+ return this.items[keyName];
|
|
|
}
|
|
|
- return datas;
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class filterGatherTree extends baseTree {
|
|
|
+
|
|
|
+ clearDatas() {
|
|
|
+ this.items = {};
|
|
|
+ this.nodes = [];
|
|
|
+ this.datas = [];
|
|
|
+ this.children = [];
|
|
|
}
|
|
|
|
|
|
- getDefaultDatas() {
|
|
|
- return this.getDatasWithout(['expanded', 'visible', 'children', 'index']);
|
|
|
+ get newId() {
|
|
|
+ if (!this._maxId) {
|
|
|
+ this._maxId = 0;
|
|
|
+ }
|
|
|
+ this._maxId++;
|
|
|
+ return this._maxId;
|
|
|
+ }
|
|
|
+
|
|
|
+ addNode(data, parent) {
|
|
|
+ data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
|
|
|
+ let item = this.ctx.helper._.find(this.items, data);
|
|
|
+ if (item) return item;
|
|
|
+
|
|
|
+ item = data;
|
|
|
+ item[this.setting.id] = this.newId;
|
|
|
+ const keyName = itemsPre + item[this.setting.id];
|
|
|
+ item.children = [];
|
|
|
+ item.is_leaf = true;
|
|
|
+ item.expanded = true;
|
|
|
+ item.visible = true;
|
|
|
+ this.items[keyName] = item;
|
|
|
+ this.datas.push(item);
|
|
|
+ if (parent) {
|
|
|
+ item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
|
|
|
+ item[this.setting.level] = parent[this.setting.level] + 1;
|
|
|
+ item[this.setting.order] = parent.children.length + 1;
|
|
|
+ parent.is_leaf = false;
|
|
|
+ parent.children.push(item);
|
|
|
+ } else {
|
|
|
+ item[this.setting.fullPath] = '' + item[this.setting.id];
|
|
|
+ item[this.setting.level] = 1;
|
|
|
+ item[this.setting.order] = this.children.length + 1;
|
|
|
+ this.children.push(item);
|
|
|
+ }
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+
|
|
|
+ sortTreeNodeCustom(field, fun, isResort) {
|
|
|
+ const self = this;
|
|
|
+ const sortNodes = function (nodes) {
|
|
|
+ nodes.sort(function (a, b) {
|
|
|
+ return fun(a[field], b[field]);
|
|
|
+ });
|
|
|
+ for (const [i, node] of nodes.entries()) {
|
|
|
+ node.order = i + 1;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ 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]);
|
|
|
+ }
|
|
|
+ sortNodes(nodes[i].children);
|
|
|
+ addSortNodes(nodes[i].children);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ this.nodes = [];
|
|
|
+ if (!isResort) {
|
|
|
+ this.children = this.getChildren();
|
|
|
+ }
|
|
|
+ sortNodes(this.children);
|
|
|
+ addSortNodes(this.children);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -355,4 +480,6 @@ class pos {
|
|
|
module.exports = {
|
|
|
billsTree,
|
|
|
pos,
|
|
|
+ filterTree,
|
|
|
+ filterGatherTree,
|
|
|
};
|