ems-basedata-wdz
Version:
ems-basedata by xianer
86 lines (81 loc) • 2.57 kB
JavaScript
function three(data, option = {}) {
this.json = [];
this.dataSource = data;
this.children = option.children;
this.selected = option.selected;
this.option = option;
this.getRootMenu();
if (option.root) {
this.json = [{
expand: true,
title: '/',
children: this.json
}];
}
for (const m of this.json) {
this.pushNode(m.menuId, m, [m]);
}
!!option.sort && this.sort(this.json);
}
three.prototype.getRootMenu = function() {
for (const m of this.dataSource) {
m.selected = !!(this.selected && this.selected.indexOf(m.menuId) >= 0);
m.checked = !!(this.children && this.children.indexOf(m.menuId) >= 0);
if (this.option.checked.includes(m.menuId)) {
m.checked = true;
}
m.expand = !this.option.noExpand;
if (m.selected) {
m.expand = true;
}
if (this.option.delItem.length && this.option.delItem.includes(m.menuId)) {
m.delItem = true;
}
if (!m.menuModuleId) {
m.expand = false;
!m.delItem && this.json.push(Object.assign({}, m, {
title: `${m.menuName} ${m.categoryCode ? m.categoryCode : ''}`,
children: []
}));
}
}
};
three.prototype.pushNode = function(id, node, parentList = []) {
for (const m of this.dataSource) {
if (m.menuModuleId == id && !m.delItem) {
node.children = !node.children ? [] : node.children;
const newItem = Object.assign({}, m, {
title: `${m.menuName} ${m.categoryCode ? m.categoryCode : ''}`,
children: [],
parentNode: node.menuName ? node.menuName : ''
});
node.children.push(newItem);
// 如果这个是选中项 就设置他的父级列表为展开
if (newItem.expand) {
newItem.expand = false;
this.setParentExpand(parentList, newItem);
}
// 循环添加父级列表 用于处理展开tree
parentList = [...parentList, newItem];
this.pushNode(newItem.menuId, newItem, parentList);
}
}
};
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);
}
}
};
three.prototype.setParentExpand = function(parentList = [], lastItem) {
const obj = parentList.find(item => item.menuId === lastItem.menuModuleId);
if (obj) {
obj.expand = true;
this.setParentExpand(parentList, obj);
}
};
export default three;