mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
closes #5082
This commit is contained in:
@@ -32,20 +32,12 @@ module.exports = function(middleware) {
|
|||||||
middleware.admin.buildHeader = function(req, res, next) {
|
middleware.admin.buildHeader = function(req, res, next) {
|
||||||
res.locals.renderAdminHeader = true;
|
res.locals.renderAdminHeader = true;
|
||||||
|
|
||||||
async.parallel({
|
controllers.api.getConfig(req, res, function(err, config) {
|
||||||
config: function(next) {
|
|
||||||
controllers.api.getConfig(req, res, next);
|
|
||||||
},
|
|
||||||
footer: function(next) {
|
|
||||||
req.app.render('admin/footer', {}, next);
|
|
||||||
}
|
|
||||||
}, function(err, results) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.locals.config = results.config;
|
res.locals.config = config;
|
||||||
res.locals.adminFooter = results.footer;
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -122,4 +114,9 @@ module.exports = function(middleware) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
middleware.admin.renderFooter = function(req, res, data, next) {
|
||||||
|
req.app.render('admin/footer', data, next);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,14 +2,12 @@
|
|||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var nconf = require('nconf');
|
var nconf = require('nconf');
|
||||||
var validator = require('validator');
|
|
||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var user = require('../user');
|
var user = require('../user');
|
||||||
var meta = require('../meta');
|
var meta = require('../meta');
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
var navigation = require('../navigation');
|
var navigation = require('../navigation');
|
||||||
var translator = require('../../public/src/modules/translator');
|
|
||||||
|
|
||||||
var controllers = {
|
var controllers = {
|
||||||
api: require('../controllers/api'),
|
api: require('../controllers/api'),
|
||||||
@@ -21,34 +19,25 @@ module.exports = function(middleware) {
|
|||||||
middleware.buildHeader = function(req, res, next) {
|
middleware.buildHeader = function(req, res, next) {
|
||||||
res.locals.renderHeader = true;
|
res.locals.renderHeader = true;
|
||||||
res.locals.isAPI = false;
|
res.locals.isAPI = false;
|
||||||
|
async.waterfall([
|
||||||
middleware.applyCSRF(req, res, function() {
|
function(next) {
|
||||||
|
middleware.applyCSRF(req, res, next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
config: function(next) {
|
config: function(next) {
|
||||||
controllers.api.getConfig(req, res, next);
|
controllers.api.getConfig(req, res, next);
|
||||||
},
|
},
|
||||||
footer: function(next) {
|
|
||||||
req.app.render('footer', {
|
|
||||||
loggedIn: !!req.uid,
|
|
||||||
title: validator.escape(String(meta.config.title || meta.config.browserTitle || 'NodeBB'))
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
plugins: function(next) {
|
plugins: function(next) {
|
||||||
plugins.fireHook('filter:middleware.buildHeader', {req: req, locals: res.locals}, next);
|
plugins.fireHook('filter:middleware.buildHeader', {req: req, locals: res.locals}, next);
|
||||||
}
|
}
|
||||||
}, function(err, results) {
|
}, next);
|
||||||
if (err) {
|
},
|
||||||
return next(err);
|
function(results, next) {
|
||||||
}
|
|
||||||
|
|
||||||
res.locals.config = results.config;
|
res.locals.config = results.config;
|
||||||
|
|
||||||
translator.translate(results.footer, results.config.defaultLang, function(parsedTemplate) {
|
|
||||||
res.locals.footer = parsedTemplate;
|
|
||||||
next();
|
next();
|
||||||
});
|
}
|
||||||
});
|
], next);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
middleware.renderHeader = function(req, res, data, callback) {
|
middleware.renderHeader = function(req, res, data, callback) {
|
||||||
@@ -166,6 +155,14 @@ module.exports = function(middleware) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
middleware.renderFooter = function(req, res, data, callback) {
|
||||||
|
plugins.fireHook('filter:middleware.renderFooter', {templateValues: data, req: req, res: res}, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
req.app.render('footer', data.templateValues, callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
function modifyTitle(obj) {
|
function modifyTitle(obj) {
|
||||||
var title = controllers.helpers.buildTitle('[[pages:home]]');
|
var title = controllers.helpers.buildTitle('[[pages:home]]');
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async');
|
||||||
var nconf = require('nconf');
|
var nconf = require('nconf');
|
||||||
var validator = require('validator');
|
var validator = require('validator');
|
||||||
|
|
||||||
@@ -18,7 +19,6 @@ module.exports = function(middleware) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.send(str);
|
self.send(str);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,12 +27,16 @@ module.exports = function(middleware) {
|
|||||||
fn = options;
|
fn = options;
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
if ('function' !== typeof fn) {
|
||||||
plugins.fireHook('filter:' + template + '.build', {req: req, res: res, templateData: options}, function(err, data) {
|
fn = defaultFn;
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ajaxifyData;
|
||||||
|
async.waterfall([
|
||||||
|
function(next) {
|
||||||
|
plugins.fireHook('filter:' + template + '.build', {req: req, res: res, templateData: options}, next);
|
||||||
|
},
|
||||||
|
function(data, next) {
|
||||||
options = data.templateData;
|
options = data.templateData;
|
||||||
|
|
||||||
options.loggedIn = !!req.uid;
|
options.loggedIn = !!req.uid;
|
||||||
@@ -53,52 +57,56 @@ module.exports = function(middleware) {
|
|||||||
return res.json(options);
|
return res.json(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('function' !== typeof fn) {
|
ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
|
||||||
fn = defaultFn;
|
|
||||||
}
|
|
||||||
|
|
||||||
var ajaxifyData = JSON.stringify(options);
|
async.parallel({
|
||||||
ajaxifyData = ajaxifyData.replace(/<\//g, '<\\/');
|
header: function(next) {
|
||||||
|
renderHeaderFooter('renderHeader', req, res, options, next);
|
||||||
render.call(self, template, options, function(err, str) {
|
},
|
||||||
if (err) {
|
content: function(next) {
|
||||||
return fn(err);
|
render.call(self, template, options, next);
|
||||||
|
},
|
||||||
|
footer: function(next) {
|
||||||
|
renderHeaderFooter('renderFooter', req, res, options, next);
|
||||||
}
|
}
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function(results, next) {
|
||||||
|
var str = results.header +
|
||||||
|
(res.locals.postHeader || '') +
|
||||||
|
results.content +
|
||||||
|
(res.locals.preFooter || '') +
|
||||||
|
results.footer;
|
||||||
|
|
||||||
str = (res.locals.postHeader ? res.locals.postHeader : '') + str + (res.locals.preFooter ? res.locals.preFooter : '');
|
translate(str, req, res, next);
|
||||||
|
},
|
||||||
if (res.locals.footer) {
|
function(translated, next) {
|
||||||
str = str + res.locals.footer;
|
next(null, translated + '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>');
|
||||||
} else if (res.locals.adminFooter) {
|
|
||||||
str = str + res.locals.adminFooter;
|
|
||||||
}
|
}
|
||||||
|
], fn);
|
||||||
if (res.locals.renderHeader || res.locals.renderAdminHeader) {
|
|
||||||
var method = res.locals.renderHeader ? middleware.renderHeader : middleware.admin.renderHeader;
|
|
||||||
method(req, res, options, function(err, template) {
|
|
||||||
if (err) {
|
|
||||||
return fn(err);
|
|
||||||
}
|
|
||||||
str = template + str;
|
|
||||||
var language = res.locals.config ? res.locals.config.userLang || 'en_GB' : 'en_GB';
|
|
||||||
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
|
|
||||||
translator.translate(str, language, function(translated) {
|
|
||||||
translated = translator.unescape(translated);
|
|
||||||
translated = translated + '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
|
||||||
fn(err, translated);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
str = str + '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
|
||||||
fn(err, str);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function renderHeaderFooter(method, req, res, options, next) {
|
||||||
|
if (res.locals.renderHeader) {
|
||||||
|
middleware[method](req, res, options, next);
|
||||||
|
} else if (res.locals.renderAdminHeader) {
|
||||||
|
middleware.admin[method](req, res, options, next);
|
||||||
|
} else {
|
||||||
|
next(null, '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function translate(str, req, res, next) {
|
||||||
|
var language = res.locals.config ? res.locals.config.userLang || 'en_GB' : 'en_GB';
|
||||||
|
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
|
||||||
|
translator.translate(str, language, function(translated) {
|
||||||
|
next(null, translator.unescape(translated));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function buildBodyClass(req) {
|
function buildBodyClass(req) {
|
||||||
var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
||||||
var parts = clean.split('/').slice(0, 3);
|
var parts = clean.split('/').slice(0, 3);
|
||||||
|
|||||||
Reference in New Issue
Block a user