function objSortBykey (objArr, key) {
let result = objArr.slice(0);
return result.sort((a, b) => a[key] - b[key]);
}
export function TREEOBJ (pidKey, childrenKey, label, topPid) {
// 对象主体
this.pidKey = pidKey; // 所属父节点id key
this.topPid = topPid; // 顶级父节点id
this.childrenKey = childrenKey; // 子节点children
this.label = label; // 节点labe 的key值
this.create = function (data, pidKey = this.pidKey, pid) {
pid = pid || this.topPid; // 0为最高父级节点父id
let list = [];
data.map(item => {
if (item[pidKey] == pid) {
item[childrenKey] = this.create(data, pidKey, item.id);
list.push(item);
}
});
return list;
};
this.getTreeDataList = function (data) {
const res = [];
function getData (data) {
data.forEach(v => {
res.push(v);
if (v[childrenKey]) {
getData(v[childrenKey]);
}
});
}
getData(data);
return res;
};
this.getTreeNodeCount = function (data) {
return this.getTreeDataList(data).length || null;
};
this.sortTreeNodeChildrenByKey = function (data, id, key) {
this.getTreeItem(data, id, item => {
if (item) {
item[childrenKey] = objSortBykey(item[childrenKey], key);
}
});
return data;
};
this.getTreeItemByKey = function (data, key, val, callBack) {
data.map(item => {
if (item[key] == val) {
callBack && (typeof callBack == 'function') && callBack(item);
} else {
if (item[childrenKey]) {
this.getTreeItemByKey(item[childrenKey], key, val, callBack);
}
}
});
};
this.getTreeNodeById = function (data, id) {
let result = null; // 运行结果
this.getTreeItem(data, id, item => {
result = item;
});
return result;
};
this.setTreeNodeValue = function (data, id, key, value) {
this.getTreeItem(data, id, item => {
item[key] = value;
});
};
this.getTreeNodeValueByKey = function (data, id, key) {
let value = null;
this.getTreeItem(data, id, item => {
value = item[key];
});
return value;
};
this.delTreeNodeKey = function (data, id, key) {
let value = null;
this.getTreeItem(data, id, item => {
value = item[key];
if (key in item) {
delete item[key];
}
});
return value;
};
this.appendChildrenNode = function (data, id, node = {}) {
this.getTreeItem(data, id, item => {
if (item) {
if (item[childrenKey]) {
item[childrenKey].push(node);
} else {
item[childrenKey] = [];
item[childrenKey].push(node);
}
}
});
return data;
};
this.clearChildrenNode = function (data, id) {
this.getTreeItem(data, id, item => {
if (item) {
item[childrenKey] = [];
}
});
};
this.delChildrenNodeById = function (data, id, childId) {
this.getTreeItem(data, id, item => {
if (item) {
(item[childrenKey] || []).forEach((v, idx) => {
if (v.id == childId) {
item[childrenKey].splice(idx, 1);
}
});
}
});
};
this.getParentNode = function (data, id) {
let currentNode = null;
let parentNode = null;
this.getTreeItem(data, id, item => {
if (item) {
currentNode = item;
}
});
if (currentNode) {
this.getTreeItem(data, currentNode[this.pidKey], item => {
if (item) {
parentNode = item;
}
});
}
return parentNode;
};
this.getParentNodes = function (data, id) {
let parentNodes = this.getLevelNode(data, id);
return parentNodes;
};
this.getChildrenNodes = function (data, id) {
let res = [];
let childrens = [];
this.getTreeItem(data, id, item => {
if (item) {
childrens = item[childrenKey];
}
});
function getChs (data) {
for (let i = 0, len = data.length; i < len; i++) {
res.push(data[i]);
if (data[i][childrenKey] && data[i][childrenKey].length > 0) {
getChs(data[i][childrenKey]);
}
}
}
getChs(childrens);
return res;
};
this.getChildrenNode = function (data, id) {
let childrens = [];
this.getTreeItem(data, id, item => {
if (item) {
childrens = item[childrenKey];
}
});
return childrens;
};
this.getTreeLevelNode = function (data, id, topPid = this.topPid, result, pidKey = this.pidKey) {
let currentId = id;
let node = this.getTreeNodeById(data, currentId);
result.push(node);
if (node && node[pidKey] && node[pidKey] !== topPid) {
this.getTreeLevelNode(data, node[pidKey], topPid, result, pidKey);
}
};
this.getLevelNode = function (data, id, topPid = this.topPid, pidKey = this.pidKey) {
let result = [];
this.getTreeLevelNode(data, id, topPid, result, pidKey);
return result || [];
};
this.getNodeLevel = function (data, id) {
const res = this.getLevelNode(data, id);
return res.length || 0;
};
}
module.exports = { TREEOBJ };