2015-07-15 19:01:23 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
config = require('../config'),
|
2017-02-03 16:15:11 +03:00
|
|
|
utils = require('../utils'),
|
2015-07-15 19:01:23 +03:00
|
|
|
errors = require('../errors'),
|
2016-10-06 15:27:35 +03:00
|
|
|
i18n = require('../i18n'),
|
2017-03-03 01:05:35 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2017-03-13 23:13:17 +03:00
|
|
|
themeUtils = require('../themes'),
|
2015-07-15 19:01:23 +03:00
|
|
|
themeHandler;
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler = {
|
|
|
|
// ### configHbsForContext Middleware
|
|
|
|
// Setup handlebars for the current context (admin or theme)
|
|
|
|
configHbsForContext: function configHbsForContext(req, res, next) {
|
2017-03-13 19:30:35 +03:00
|
|
|
// Static information, same for every request unless the settings change
|
|
|
|
// @TODO: bind this once and then update based on events?
|
2017-03-14 20:03:36 +03:00
|
|
|
var blogData = {
|
2017-02-03 16:15:11 +03:00
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
|
|
|
facebook: settingsCache.get('facebook'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
|
|
|
timezone: settingsCache.get('activeTimezone'),
|
|
|
|
navigation: settingsCache.get('navigation'),
|
|
|
|
icon: settingsCache.get('icon'),
|
|
|
|
cover: settingsCache.get('cover'),
|
|
|
|
logo: settingsCache.get('logo'),
|
|
|
|
amp: settingsCache.get('amp')
|
|
|
|
},
|
2017-03-14 20:03:36 +03:00
|
|
|
labsData = _.cloneDeep(settingsCache.get('labs')),
|
|
|
|
themeData = {};
|
|
|
|
|
|
|
|
if (themeUtils.getActive()) {
|
|
|
|
themeData.posts_per_page = themeUtils.getActive().config('posts_per_page');
|
|
|
|
}
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// Request-specific information
|
|
|
|
// These things are super dependent on the request, so they need to be in middleware
|
2017-03-14 20:03:36 +03:00
|
|
|
blogData.url = utils.url.urlFor('home', {secure: req.secure}, true);
|
2017-03-13 19:30:35 +03:00
|
|
|
|
|
|
|
// Pass 'secure' flag to the view engine
|
|
|
|
// so that templates can choose to render https or http 'url', see url utility
|
|
|
|
res.locals.secure = req.secure;
|
|
|
|
|
|
|
|
// @TODO: only do this if something changed?
|
2017-02-03 16:15:11 +03:00
|
|
|
hbs.updateTemplateOptions({
|
|
|
|
data: {
|
2017-03-14 20:03:36 +03:00
|
|
|
blog: blogData,
|
|
|
|
labs: labsData,
|
|
|
|
config: themeData
|
2017-02-03 16:15:11 +03:00
|
|
|
}
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
next();
|
|
|
|
},
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
// ### Activate Theme
|
|
|
|
// Helper for updateActiveTheme
|
2017-03-13 23:13:17 +03:00
|
|
|
activateTheme: function activateTheme(blogApp) {
|
|
|
|
var hbsOptions = {
|
2017-03-13 19:30:35 +03:00
|
|
|
partialsDir: [config.get('paths').helperTemplates],
|
|
|
|
onCompile: function onCompile(exhbs, source) {
|
|
|
|
return exhbs.handlebars.compile(source, {preventIndent: true});
|
|
|
|
}
|
|
|
|
};
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
if (themeUtils.getActive().hasPartials()) {
|
|
|
|
hbsOptions.partialsDir.push(themeUtils.getActive().partialsPath);
|
|
|
|
}
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// reset the asset hash
|
|
|
|
config.set('assetHash', null);
|
|
|
|
// clear the view cache
|
|
|
|
blogApp.cache = {};
|
|
|
|
// Set the views and engine
|
2017-03-13 23:13:17 +03:00
|
|
|
blogApp.set('views', themeUtils.getActive().path);
|
2015-10-11 23:21:54 +03:00
|
|
|
blogApp.engine('hbs', hbs.express3(hbsOptions));
|
|
|
|
|
|
|
|
// Set active theme variable on the express server
|
2017-03-13 19:30:35 +03:00
|
|
|
// Note: this is effectively the "mounted" theme, which has been loaded into the express app
|
2017-03-13 23:13:17 +03:00
|
|
|
blogApp.set('activeTheme', themeUtils.getActive().name);
|
2015-10-11 23:21:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// ### updateActiveTheme
|
|
|
|
// Updates the blogApp's activeTheme variable and subsequently
|
|
|
|
// activates that theme's views with the hbs templating engine if it
|
|
|
|
// is not yet activated.
|
2016-07-21 14:26:16 +03:00
|
|
|
updateActiveTheme: function updateActiveTheme(req, res, next) {
|
2017-03-03 01:05:35 +03:00
|
|
|
var blogApp = req.app,
|
2017-03-13 23:13:17 +03:00
|
|
|
// We use the settingsCache here, because the setting will be set, even if the theme itself is
|
|
|
|
// not usable because it is invalid or missing.
|
2017-03-13 19:30:35 +03:00
|
|
|
activeThemeName = settingsCache.get('activeTheme'),
|
|
|
|
mountedThemeName = blogApp.get('activeTheme');
|
2016-07-21 14:26:16 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// This means that the theme hasn't been loaded yet i.e. there is no active theme
|
2017-03-13 23:13:17 +03:00
|
|
|
if (!themeUtils.getActive()) {
|
2017-03-13 19:30:35 +03:00
|
|
|
// This is the one place we ACTUALLY throw an error for a missing theme
|
|
|
|
// As it's a request we cannot serve
|
|
|
|
return next(new errors.InternalServerError({
|
|
|
|
message: i18n.t('errors.middleware.themehandler.missingTheme', {theme: activeThemeName})
|
|
|
|
}));
|
|
|
|
|
|
|
|
// If there is an active theme AND it has changed, call activate
|
|
|
|
} else if (activeThemeName !== mountedThemeName) {
|
|
|
|
// This is effectively "mounting" a theme into express, the theme is already "active"
|
2017-03-13 23:13:17 +03:00
|
|
|
themeHandler.activateTheme(blogApp);
|
2017-03-03 01:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
2015-10-11 23:21:54 +03:00
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themeHandler;
|