mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
made breadcrumbs regular function
less db calls as well
This commit is contained in:
@@ -172,6 +172,21 @@ categoriesController.get = function(req, res, next) {
|
||||
function(payload, next) {
|
||||
categories.getCategoryById(payload, next);
|
||||
},
|
||||
function(categoryData, next) {
|
||||
var breadcrumbs = [
|
||||
{
|
||||
text: categoryData.name,
|
||||
url: nconf.get('relative_path') + '/category/' + categoryData.slug
|
||||
}
|
||||
];
|
||||
helpers.buildBreadcrumbs(categoryData.parentCid, function(err, crumbs) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
|
||||
next(null, categoryData);
|
||||
});
|
||||
},
|
||||
function(categoryData, next) {
|
||||
if (categoryData.link) {
|
||||
return res.redirect(categoryData.link);
|
||||
@@ -243,7 +258,6 @@ categoriesController.get = function(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
data.breadcrumbs = res.locals.breadcrumbs;
|
||||
res.render('category', data);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
var nconf = require('nconf');
|
||||
var nconf = require('nconf'),
|
||||
async = require('async'),
|
||||
validator = require('validator'),
|
||||
|
||||
translator = require('../../public/src/translator'),
|
||||
categories = require('../categories'),
|
||||
meta = require('../meta');
|
||||
|
||||
var helpers = {};
|
||||
|
||||
@@ -32,5 +38,39 @@ helpers.notAllowed = function(req, res, error) {
|
||||
}
|
||||
};
|
||||
|
||||
helpers.buildBreadcrumbs = function(cid, callback) {
|
||||
var breadcrumbs = [];
|
||||
|
||||
async.whilst(function() {
|
||||
return parseInt(cid, 10);
|
||||
}, function(next) {
|
||||
categories.getCategoryFields(cid, ['name', 'slug', 'parentCid'], function(err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
breadcrumbs.unshift({
|
||||
text: validator.escape(data.name),
|
||||
url: nconf.get('relative_path') + '/category/' + data.slug
|
||||
});
|
||||
|
||||
cid = data.parentCid;
|
||||
next();
|
||||
});
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
translator.translate('[[global:home]]', meta.config.defaultLang || 'en_GB', function(translated) {
|
||||
breadcrumbs.unshift({
|
||||
text: translated,
|
||||
url: nconf.get('relative_path') + '/'
|
||||
});
|
||||
|
||||
callback(null, breadcrumbs);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = helpers;
|
||||
@@ -123,6 +123,26 @@ topicsController.get = function(req, res, next) {
|
||||
next(null, topicData);
|
||||
});
|
||||
},
|
||||
function (topicData, next) {
|
||||
var breadcrumbs = [
|
||||
{
|
||||
text: topicData.category.name,
|
||||
url: nconf.get('relative_path') + '/category/' + topicData.category.slug
|
||||
},
|
||||
{
|
||||
text: topicData.title,
|
||||
url: nconf.get('relative_path') + '/topic/' + topicData.slug
|
||||
}
|
||||
];
|
||||
|
||||
helpers.buildBreadcrumbs(topicData.category.parentCid, function(err, crumbs) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
topicData.breadcrumbs = crumbs.concat(breadcrumbs);
|
||||
next(null, topicData);
|
||||
});
|
||||
},
|
||||
function (topicData, next) {
|
||||
var description = '';
|
||||
|
||||
@@ -245,7 +265,6 @@ topicsController.get = function(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
data.breadcrumbs = res.locals.breadcrumbs;
|
||||
res.render('topic', data);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -201,58 +201,6 @@ middleware.isAdmin = function(req, res, next) {
|
||||
});
|
||||
};
|
||||
|
||||
middleware.buildBreadcrumbs = function(req, res, next) {
|
||||
var breadcrumbs = [],
|
||||
findParents = function(cid) {
|
||||
var currentCategory;
|
||||
async.doWhilst(function(next) {
|
||||
categories.getCategoryFields(currentCategory ? currentCategory.parentCid : cid, ['name', 'slug', 'parentCid'], function(err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
breadcrumbs.unshift({
|
||||
text: validator.escape(data.name),
|
||||
url: nconf.get('relative_path') + '/category/' + data.slug
|
||||
});
|
||||
|
||||
currentCategory = data;
|
||||
next();
|
||||
});
|
||||
}, function() {
|
||||
return !!currentCategory.parentCid && currentCategory.parentCid !== '0';
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
winston.warn('[buildBreadcrumb] Could not build breadcrumbs: ' + err.message);
|
||||
}
|
||||
|
||||
// Home breadcrumb
|
||||
translator.translate('[[global:home]]', meta.config.defaultLang || 'en_GB', function(translated) {
|
||||
breadcrumbs.unshift({
|
||||
text: translated,
|
||||
url: nconf.get('relative_path') + '/'
|
||||
});
|
||||
|
||||
res.locals.breadcrumbs = breadcrumbs || [];
|
||||
next();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (req.params.topic_id) {
|
||||
topics.getTopicFields(parseInt(req.params.topic_id, 10), ['cid', 'title', 'slug'], function(err, data) {
|
||||
breadcrumbs.unshift({
|
||||
text: validator.escape(data.title),
|
||||
url: nconf.get('relative_path') + '/topic/' + data.slug
|
||||
});
|
||||
|
||||
findParents(parseInt(data.cid, 10));
|
||||
});
|
||||
} else {
|
||||
findParents(parseInt(req.params.category_id, 10));
|
||||
}
|
||||
};
|
||||
|
||||
middleware.buildHeader = function(req, res, next) {
|
||||
res.locals.renderHeader = true;
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ function staticRoutes(app, middleware, controllers) {
|
||||
function topicRoutes(app, middleware, controllers) {
|
||||
app.get('/api/topic/teaser/:topic_id', controllers.topics.teaser);
|
||||
|
||||
setupPageRoute(app, '/topic/:topic_id/:slug/:post_index?', middleware, [middleware.buildBreadcrumbs], controllers.topics.get);
|
||||
setupPageRoute(app, '/topic/:topic_id/:slug?', middleware, [middleware.buildBreadcrumbs, middleware.addSlug], controllers.topics.get);
|
||||
setupPageRoute(app, '/topic/:topic_id/:slug/:post_index?', middleware, [], controllers.topics.get);
|
||||
setupPageRoute(app, '/topic/:topic_id/:slug?', middleware, [middleware.addSlug], controllers.topics.get);
|
||||
}
|
||||
|
||||
function tagRoutes(app, middleware, controllers) {
|
||||
@@ -55,8 +55,8 @@ function categoryRoutes(app, middleware, controllers) {
|
||||
setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread);
|
||||
app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal);
|
||||
|
||||
setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [middleware.buildBreadcrumbs], controllers.categories.get);
|
||||
setupPageRoute(app, '/category/:category_id/:slug?', middleware, [middleware.buildBreadcrumbs, middleware.addSlug], controllers.categories.get);
|
||||
setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [], controllers.categories.get);
|
||||
setupPageRoute(app, '/category/:category_id/:slug?', middleware, [middleware.addSlug], controllers.categories.get);
|
||||
}
|
||||
|
||||
function accountRoutes(app, middleware, controllers) {
|
||||
|
||||
Reference in New Issue
Block a user