|
|
@ -302,18 +302,22 @@ Categories.flattenCategories = function (allCategories, categoryData) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Recursively build tree
|
|
|
|
* build tree from flat list of categories
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param categories {array} flat list of categories
|
|
|
|
* @param categories {array} flat list of categories
|
|
|
|
* @param parentCid {number} start from 0 to build full tree
|
|
|
|
* @param parentCid {number} start from 0 to build full tree
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Categories.getTree = function (categories, parentCid) {
|
|
|
|
Categories.getTree = function (categories, parentCid) {
|
|
|
|
|
|
|
|
parentCid = parentCid || 0;
|
|
|
|
const cids = categories.map(category => category.cid);
|
|
|
|
const cids = categories.map(category => category.cid);
|
|
|
|
const cidToCategory = _.zipObject(cids, _.cloneDeep(categories));
|
|
|
|
const cidToCategory = {};
|
|
|
|
const tree = buildRecursive(categories, parentCid || 0);
|
|
|
|
const parents = {};
|
|
|
|
|
|
|
|
cids.forEach((cid, index) => {
|
|
|
|
|
|
|
|
cidToCategory[cid] = categories[index];
|
|
|
|
|
|
|
|
parents[cid] = _.clone(categories[index]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function buildRecursive(categories, parentCid) {
|
|
|
|
const tree = [];
|
|
|
|
var tree = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
categories.forEach(function (category) {
|
|
|
|
categories.forEach(function (category) {
|
|
|
|
if (category) {
|
|
|
|
if (category) {
|
|
|
@ -324,17 +328,26 @@ Categories.getTree = function (categories, parentCid) {
|
|
|
|
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
|
|
|
|
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
|
|
|
|
category.parentCid = 0;
|
|
|
|
category.parentCid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (category.parentCid === parentCid) {
|
|
|
|
if (category.parentCid === parentCid) {
|
|
|
|
tree.push(category);
|
|
|
|
tree.push(category);
|
|
|
|
category.parent = cidToCategory[parentCid];
|
|
|
|
category.parent = parents[parentCid];
|
|
|
|
category.children = buildRecursive(categories, category.cid);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const parent = cidToCategory[category.parentCid];
|
|
|
|
|
|
|
|
if (parent && parent.cid !== category.cid) {
|
|
|
|
|
|
|
|
category.parent = parents[category.parentCid];
|
|
|
|
|
|
|
|
parent.children = parent.children || [];
|
|
|
|
|
|
|
|
parent.children.push(category);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function sortTree(tree) {
|
|
|
|
tree.sort((a, b) => a.order - b.order);
|
|
|
|
tree.sort((a, b) => a.order - b.order);
|
|
|
|
return tree;
|
|
|
|
if (tree.children) {
|
|
|
|
|
|
|
|
sortTree(tree.children);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortTree(tree);
|
|
|
|
|
|
|
|
|
|
|
|
categories.forEach(c => calculateTopicPostCount(c));
|
|
|
|
categories.forEach(c => calculateTopicPostCount(c));
|
|
|
|
return tree;
|
|
|
|
return tree;
|
|
|
|