ems-basedata-wdz
Version:
ems-basedata by xianer
84 lines (79 loc) • 2.42 kB
JavaScript
function three(data, option = {}) {
this.json = [];
this.dataSource = data;
this.children = option.children;
this.selected = option.selected;
this.currentNodeId = option.currentNodeId; // 所有需要展开的节点
this.isSearch = option.isSearch; // 是否搜索
this.thisNode = option.thisNode; // 选中的节点
this.option = option;
if (option.setCheck) {
const choice = {};
option.setCheck.forEach(item => {
choice[item.resourceId] = true;
});
this.choice = choice;
}
this.getRootResource();
if (option.root) {
this.json = [{
expand: true,
title: '/',
children: this.json
}];
}
for (const m of this.json) {
this.pushNode(m.id, m);
}
!!option.sort && this.sort(this.json);
}
three.prototype.getRootResource = function() {
for (const m of this.dataSource) {
// 在selectInput情况下 展开tree所有节点
if (this.option.onSelectInput || this.option.isSearch) {
m.expand = true;
} else if (this.currentNodeId) {
// 展开需要展开的节点
m.expand = this.currentNodeId.includes(m.id.toString());
m.selected = this.thisNode == m.id.toString();
}
if (this.option.setCheck && this.choice[m.id]) {
m.checked = true;
}
if (this.option.noMatchDisabled) {
m.disabled = m.type !== this.option.noMatchDisabled;
m.disableCheckbox = m.type !== this.option.noMatchDisabled;
}
if (!m.parentId) {
this.json.push(Object.assign({}, m, {
title: m.name,
children: [],
expand: true // 默认一级节点展开
}));
}
}
};
three.prototype.pushNode = function(id, node) {
for (const m of this.dataSource) {
if (m.parentId == id) {
node.children = !node.children ? [] : node.children;
node.children.push(Object.assign({}, m, {
title: m.name,
children: [],
parentNode: node.name || ''
}));
this.pushNode(node.children[node.children.length - 1].id, node.children[node.children.length - 1]);
}
}
};
three.prototype.sort = function(list) {
const list_sort = list.sort(function(a, b) {
return a.sort - b.sort;
});
for (const node of list_sort) {
if (!!node.children && node.children.length > 0) {
this.sort(node.children);
}
}
};
export default three;