diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index e2303c501c..9ca9052dc9 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -204,7 +204,7 @@ $(document).ready(function () {
}
ajaxify.loadScript(tpl_url, done);
- ajaxify.widgets.render(tpl_url, done);
+ ajaxify.widgets.render(tpl_url, url, done);
$(window).trigger('action:ajaxify.contentLoaded', { url: url, tpl: tpl_url });
diff --git a/public/src/widgets.js b/public/src/widgets.js
index 90b2d355c7..f5f667d2a7 100644
--- a/public/src/widgets.js
+++ b/public/src/widgets.js
@@ -1,5 +1,6 @@
'use strict';
+
(function (ajaxify) {
ajaxify.widgets = {};
@@ -13,63 +14,77 @@
});
};
- ajaxify.widgets.render = function (template, callback) {
+ ajaxify.widgets.render = function (template, url, callback) {
callback = callback || function () {};
-
if (template.match(/^admin/)) {
return callback();
}
- var locations = Object.keys(ajaxify.data.widgets);
+ var widgetLocations = ['sidebar', 'footer', 'header'];
- locations.forEach(function (location) {
- var area = $('#content [widget-area="' + location + '"]');
- if (area.length) {
- return;
+ $('#content [widget-area]').each(function () {
+ var location = $(this).attr('widget-area');
+ if ($.inArray(location, widgetLocations) === -1) {
+ widgetLocations.push(location);
}
+ });
- var widgetsAtLocation = ajaxify.data.widgets[location] || [];
- var html = '';
+ $.get(config.relative_path + '/api/widgets/render?' + config['cache-buster'], {
+ locations: widgetLocations,
+ template: template + '.tpl',
+ url: url,
+ cid: ajaxify.data.cid,
+ isMobile: utils.isMobile(),
+ }, function (renderedAreas) {
+ for (var x = 0; x < renderedAreas.length; x += 1) {
+ var renderedWidgets = renderedAreas[x].widgets;
+ var location = renderedAreas[x].location;
+ var html = '';
- widgetsAtLocation.forEach(function (widget) {
- html += widget.html;
+ for (var i = 0; i < renderedWidgets.length; i += 1) {
+ html += templates.parse(renderedWidgets[i].html, {});
+ }
+
+ var area = $('#content [widget-area="' + location + '"]');
- if (location === 'footer' && !$('#content [widget-area="footer"]').length) {
- $('#content').append($('
diff --git a/src/widgets/admin.js b/src/widgets/admin.js
index 4ca05571f5..dde3aca43d 100644
--- a/src/widgets/admin.js
+++ b/src/widgets/admin.js
@@ -72,7 +72,7 @@ admin.get = function (callback) {
callback(false, {
templates: templates,
areas: widgetData.areas,
- availableWidgets: widgetData.widgets,
+ widgets: widgetData.widgets,
});
});
});
diff --git a/src/widgets/index.js b/src/widgets/index.js
index 982b40c696..2e228dab63 100644
--- a/src/widgets/index.js
+++ b/src/widgets/index.js
@@ -3,35 +3,27 @@
var async = require('async');
var winston = require('winston');
var templates = require('templates.js');
-var _ = require('lodash');
var plugins = require('../plugins');
var translator = require('../translator');
var db = require('../database');
-var apiController = require('../controllers/api');
var widgets = module.exports;
-widgets.render = function (uid, options, callback) {
- if (!options.template) {
+widgets.render = function (uid, area, req, res, callback) {
+ if (!area.locations || !area.template) {
return callback(new Error('[[error:invalid-data]]'));
}
async.waterfall([
function (next) {
- widgets.getWidgetDataForTemplates(['global', options.template], next);
+ widgets.getAreas(['global', area.template], area.locations, next);
},
function (data, next) {
var widgetsByLocation = {};
- delete data.global.drafts;
-
- var locations = _.uniq(Object.keys(data.global).concat(Object.keys(data[options.template])));
-
- var returnData = {};
-
- async.each(locations, function (location, done) {
- widgetsByLocation[location] = (data.global[location] || []).concat(data[options.template][location] || []);
+ async.map(area.locations, function (location, done) {
+ widgetsByLocation[location] = data.global[location].concat(data[area.template][location]);
if (!widgetsByLocation[location].length) {
return done(null, { location: location, widgets: [] });
@@ -41,43 +33,28 @@ widgets.render = function (uid, options, callback) {
if (!widget || !widget.data ||
(!!widget.data['hide-registered'] && uid !== 0) ||
(!!widget.data['hide-guests'] && uid === 0) ||
- (!!widget.data['hide-mobile'] && options.req.useragent.isMobile)) {
+ (!!widget.data['hide-mobile'] && area.isMobile)) {
return next();
}
- renderWidget(widget, uid, options, next);
- }, function (err, renderedWidgets) {
- if (err) {
- return done(err);
- }
- returnData[location] = renderedWidgets.filter(Boolean);
- done();
+ renderWidget(widget, uid, area, req, res, next);
+ }, function (err, result) {
+ done(err, { location: location, widgets: result.filter(Boolean) });
});
- }, function (err) {
- next(err, returnData);
- });
+ }, next);
},
], callback);
};
-function renderWidget(widget, uid, options, callback) {
+function renderWidget(widget, uid, area, req, res, callback) {
async.waterfall([
function (next) {
- if (options.res.locals.isAPI) {
- apiController.loadConfig(options.req, next);
- } else {
- next(null, options.res.locals.config);
- }
- },
- function (config, next) {
- var templateData = _.assign(options.templateData, { config: config });
plugins.fireHook('filter:widget.render:' + widget.widget, {
uid: uid,
- area: options,
- templateData: templateData,
+ area: area,
data: widget.data,
- req: options.req,
- res: options.res,
+ req: req,
+ res: res,
}, next);
},
function (data, next) {
@@ -107,28 +84,23 @@ function renderWidget(widget, uid, options, callback) {
], callback);
}
-widgets.getWidgetDataForTemplates = function (templates, callback) {
+widgets.getAreas = function (templates, locations, callback) {
var keys = templates.map(function (tpl) {
return 'widgets:' + tpl;
});
-
async.waterfall([
function (next) {
- db.getObjects(keys, next);
+ db.getObjectsFields(keys, locations, next);
},
function (data, next) {
var returnData = {};
templates.forEach(function (template, index) {
returnData[template] = returnData[template] || {};
-
- var templateWidgetData = data[index] || {};
- var locations = Object.keys(templateWidgetData);
-
locations.forEach(function (location) {
- if (templateWidgetData && templateWidgetData[location]) {
+ if (data && data[index] && data[index][location]) {
try {
- returnData[template][location] = JSON.parse(templateWidgetData[location]);
+ returnData[template][location] = JSON.parse(data[index][location]);
} catch (err) {
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
returnData[template][location] = [];
diff --git a/test/controllers.js b/test/controllers.js
index 07bd00ec9c..34c515fc7a 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -688,20 +688,21 @@ describe('Controllers', function () {
});
it('should return {} if there is no template or locations', function (done) {
- request(nconf.get('url') + '/api/users', { json: true }, function (err, res, body) {
+ request(nconf.get('url') + '/api/widgets/render', { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
- assert.equal(Object.keys(body.widgets), 0);
+ assert.equal(Object.keys(body), 0);
done();
});
});
it('should render templates', function (done) {
- request(nconf.get('url') + '/api/categories', { json: true }, function (err, res, body) {
+ var url = nconf.get('url') + '/api/widgets/render?template=categories.tpl&url=&isMobile=false&locations%5B%5D=sidebar&locations%5B%5D=footer&locations%5B%5D=header';
+ request(url, { json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
- assert(body.widgets && body.widgets.sidebar);
+ assert(body);
done();
});
});