diff --git a/src/categories.js b/src/categories.js index 539a643f80..3233f2dbcd 100644 --- a/src/categories.js +++ b/src/categories.js @@ -292,7 +292,7 @@ var privileges = require('./privileges'); for (i; i < len; ++i) { category = categories[i]; - if (!category.hasOwnProperty('parentCid')) { + if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { category.parentCid = 0; } diff --git a/src/controllers/unread.js b/src/controllers/unread.js index 2e988601c2..c610b91723 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -100,12 +100,13 @@ function getWatchedCategories(uid, selectedCid, callback) { privileges.categories.filterCids('read', cids, uid, next); }, function (cids, next) { - categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'link', 'color', 'bgColor'], next); + categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'link', 'color', 'bgColor', 'parentCid'], next); }, function (categoryData, next) { categoryData = categoryData.filter(function(category) { return category && !category.link; }); + var selectedCategory; categoryData.forEach(function(category) { category.selected = parseInt(category.cid, 10) === parseInt(selectedCid, 10); @@ -113,11 +114,27 @@ function getWatchedCategories(uid, selectedCid, callback) { selectedCategory = category; } }); - next(null, {categories: categoryData, selectedCategory: selectedCategory}); + + var categoriesData = []; + var tree = categories.getTree(categoryData, 0); + + tree.forEach(function(category) { + recursive(category, categoriesData, ''); + }); + + next(null, {categories: categoriesData, selectedCategory: selectedCategory}); } ], callback); } +function recursive(category, categoriesData, level) { + category.level = level; + categoriesData.push(category); + + category.children.forEach(function(child) { + recursive(child, categoriesData, '    ' + level); + }); +} unreadController.unreadTotal = function(req, res, next) { var filter = req.params.filter || '';