|
|
|
// const convertTreeToList = (data) => {
|
|
|
|
// var arrayList = [];
|
|
|
|
|
|
|
|
// function recursiveFunction(array) {
|
|
|
|
// for (const iterator of array) {
|
|
|
|
// if (iterator.children && iterator.children.length) {
|
|
|
|
// let clone = Object.assign({}, iterator);
|
|
|
|
// delete clone.children;
|
|
|
|
// arrayList.push(clone);
|
|
|
|
// let list = recursiveFunction(iterator.children);
|
|
|
|
// arrayList.concat(list);
|
|
|
|
// } else {
|
|
|
|
// arrayList.push(iterator);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return arrayList;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return recursiveFunction(data);
|
|
|
|
// };
|
|
|
|
const convertTreeToList = data => {
|
|
|
|
var arrayList = {};
|
|
|
|
|
|
|
|
function recursiveFunction(
|
|
|
|
array,
|
|
|
|
level,
|
|
|
|
hierarchy = [],
|
|
|
|
hierarchy_id = [],
|
|
|
|
parent_id = null
|
|
|
|
) {
|
|
|
|
for (const key in array) {
|
|
|
|
if (array.hasOwnProperty(key)) {
|
|
|
|
let new_hierarchy = [...hierarchy];
|
|
|
|
let new_hierarchy_id = [...hierarchy_id];
|
|
|
|
new_hierarchy.push(key);
|
|
|
|
new_hierarchy_id.push(array[key].id);
|
|
|
|
let clone = Object.assign({}, array[key]);
|
|
|
|
|
|
|
|
//set level
|
|
|
|
clone.lvl = level;
|
|
|
|
//set level
|
|
|
|
clone.parent_id = parent_id;
|
|
|
|
//set hierarchy key Base Tree Array
|
|
|
|
clone.hierarchy = new_hierarchy;
|
|
|
|
//set hierarchy id Base Tree Array
|
|
|
|
clone.hierarchy_id = new_hierarchy_id;
|
|
|
|
//set hasChildren
|
|
|
|
clone.hasChildren = false;
|
|
|
|
|
|
|
|
delete clone.children;
|
|
|
|
arrayList[clone.id] = clone;
|
|
|
|
if (array[key].children && array[key].children.length) {
|
|
|
|
arrayList[clone.id].hasChildren = true;
|
|
|
|
recursiveFunction(
|
|
|
|
array[key].children,
|
|
|
|
level + 1,
|
|
|
|
new_hierarchy,
|
|
|
|
new_hierarchy_id,
|
|
|
|
array[key].id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arrayList;
|
|
|
|
}
|
|
|
|
|
|
|
|
return recursiveFunction(data, 1);
|
|
|
|
};
|
|
|
|
const listSearchSelect = Options => {
|
|
|
|
let treeArray = Options.array ? Options.array : [];
|
|
|
|
var listArray = Options.list ? Options.list : convertTreeToList(treeArray);
|
|
|
|
var fromLevel = Options.fromLevel ? Options.fromLevel : 1;
|
|
|
|
var isShowParent = Options.isShowParent ? Options.isShowParent : false;//bolean
|
|
|
|
var justLevel = Options.justLevel ? Options.justLevel : null;
|
|
|
|
var untilLevel = Options.upToLevel ? Options.upToLevel : "End";
|
|
|
|
var headerLevel = Options.titleLevel ? Options.titleLevel : null;
|
|
|
|
var seperator = Options.seperator ? Options.seperator : " >> ";
|
|
|
|
var selectedIds = Options.selectedIds ? Options.selectedIds : getEndIds();
|
|
|
|
var showVarible = Options.variableShow ? Options.variableShow : "name";
|
|
|
|
var hasDivider = Options.hasDivider ? Options.hasDivider : true;
|
|
|
|
var list = [];
|
|
|
|
|
|
|
|
return process();
|
|
|
|
|
|
|
|
function process() {
|
|
|
|
var headers = [];
|
|
|
|
for (const index in listArray) {
|
|
|
|
if (listArray.hasOwnProperty(index)) {
|
|
|
|
if (selectedIds.includes(listArray[index].id)) {
|
|
|
|
var name = "";
|
|
|
|
var group = "";
|
|
|
|
for (const key in listArray[index].hierarchy_id) {
|
|
|
|
if (listArray[index].hierarchy_id.hasOwnProperty(key)) {
|
|
|
|
let item = listArray[listArray[index].hierarchy_id[key]];
|
|
|
|
if (fromLevel <= key + 1) {
|
|
|
|
name += item[showVarible] + seperator;
|
|
|
|
}
|
|
|
|
if (headerLevel && headerLevel == key + 1) {
|
|
|
|
group = item[showVarible];
|
|
|
|
headers.push(item[showVarible]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name = name.replace(new RegExp(seperator + "$"), "");
|
|
|
|
list.push({ id: listArray[index].id, lastName: listArray[index][showVarible], name: name, group: group });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (headers.length) {
|
|
|
|
let uniqueHeaders = [...new Set(headers)];
|
|
|
|
var newList =[];
|
|
|
|
for (const iterator of uniqueHeaders) {
|
|
|
|
newList.push({ header: iterator });
|
|
|
|
let arrayGrop = list.filter(x => x.group == iterator);
|
|
|
|
newList = [...newList, ...arrayGrop];
|
|
|
|
if (hasDivider) {
|
|
|
|
newList.push({ divider: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list = [...newList];
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEndIds() {
|
|
|
|
var ids = [];
|
|
|
|
for (const key in listArray) {
|
|
|
|
if (listArray.hasOwnProperty(key)) {
|
|
|
|
if (justLevel) {
|
|
|
|
if (listArray[key].lvl == justLevel) {
|
|
|
|
ids.push(listArray[key].id);
|
|
|
|
}
|
|
|
|
} else if (untilLevel == "End") {
|
|
|
|
if (isShowParent || listArray[key].hasChildren == false) {
|
|
|
|
ids.push(listArray[key].id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((listArray[key].lvl <= untilLevel && (isShowParent || listArray[key].hasChildren == false)) || listArray[key].lvl == untilLevel) {
|
|
|
|
ids.push(listArray[key].id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const deleteTreeArray = (treeArray, id, listArray = null) => {
|
|
|
|
if (!listArray) {
|
|
|
|
listArray = convertTreeToList(treeArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
let hierarchy = listArray[id].hierarchy;
|
|
|
|
let array = treeArray;
|
|
|
|
for (const key in hierarchy) {
|
|
|
|
if (hierarchy.hasOwnProperty(key)) {
|
|
|
|
if (key == 0) {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
array.splice([hierarchy[key]], 1);
|
|
|
|
} else {
|
|
|
|
array = array[hierarchy[key]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
array["children"].splice([hierarchy[key]], 1);
|
|
|
|
} else {
|
|
|
|
array = array["children"][hierarchy[key]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return treeArray;
|
|
|
|
};
|
|
|
|
const updateTreeArray = (treeArray, update, id, listArray = null) => {
|
|
|
|
if (!listArray) {
|
|
|
|
listArray = convertTreeToList(treeArray);
|
|
|
|
}
|
|
|
|
let hierarchy = listArray[id].hierarchy;
|
|
|
|
let array = treeArray;
|
|
|
|
for (const key in hierarchy) {
|
|
|
|
if (hierarchy.hasOwnProperty(key)) {
|
|
|
|
if (key == 0) {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
update["children"] = array[hierarchy[key]]["children"];
|
|
|
|
array[hierarchy[key]] = update;
|
|
|
|
} else {
|
|
|
|
array = array[hierarchy[key]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
update["children"] = array[hierarchy[key]]["children"];
|
|
|
|
array["children"][hierarchy[key]] = update;
|
|
|
|
} else {
|
|
|
|
array = array["children"][hierarchy[key]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return treeArray;
|
|
|
|
};
|
|
|
|
const insertTreeArray = (
|
|
|
|
treeArray,
|
|
|
|
store,
|
|
|
|
parent_id = null,
|
|
|
|
listArray = null
|
|
|
|
) => {
|
|
|
|
let array = treeArray;
|
|
|
|
if (!parent_id) {
|
|
|
|
treeArray.push(store);
|
|
|
|
return treeArray;
|
|
|
|
}
|
|
|
|
if (!listArray) {
|
|
|
|
listArray = convertTreeToList(treeArray);
|
|
|
|
}
|
|
|
|
let hierarchy = listArray[parent_id].hierarchy;
|
|
|
|
for (const key in hierarchy) {
|
|
|
|
if (hierarchy.hasOwnProperty(key)) {
|
|
|
|
if (key == 0) {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
array[hierarchy[key]]["children"].push(store);
|
|
|
|
} else {
|
|
|
|
array = array[hierarchy[key]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hierarchy.length - 1 == key) {
|
|
|
|
array["children"][hierarchy[key]]["children"].push(store);
|
|
|
|
} else {
|
|
|
|
array = array["children"][hierarchy[key]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return treeArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
convertTreeToList,
|
|
|
|
insertTreeArray,
|
|
|
|
updateTreeArray,
|
|
|
|
deleteTreeArray,
|
|
|
|
listSearchSelect
|
|
|
|
};
|