widget render change

v1.18.x
barisusakli 11 years ago
parent 7597c654a0
commit ba90b5d113

@ -238,6 +238,9 @@ var db = require('./database'),
Categories.getModerators = function(cid, callback) { Categories.getModerators = function(cid, callback) {
Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) { Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
if (err && err === 'group-not-found') {
return callback(null, []);
}
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -98,21 +98,15 @@ apiController.renderWidgets = function(req, res, next) {
return res.json(200, {}); return res.json(200, {});
} }
async.each(areas.locations, function(location, next) {
widgets.render(uid, { widgets.render(uid, {
template: areas.template, template: areas.template,
url: areas.url, url: areas.url,
location: location locations: areas.locations
}, function(err, widgets) { }, function(err, widgets) {
renderedWidgets.push({ if (err) {
location: location, return next(err);
widgets: widgets }
}); res.json(200, widgets);
next();
});
}, function(err) {
res.json(200, renderedWidgets);
}); });
}; };

@ -9,26 +9,26 @@ var async = require('async'),
(function(Widgets) { (function(Widgets) {
Widgets.render = function(uid, area, callback) { Widgets.render = function(uid, area, callback) {
if (!area.location || !area.template) { if (!area.locations || !area.template) {
callback({ callback({
error: 'Missing location and template data' error: 'Missing location and template data'
}); });
} }
var rendered = []; Widgets.getAreas(['global', area.template], area.locations, function(err, data) {
var widgetsByLocation = {};
async.map(area.locations, function(location, done) {
widgetsByLocation[location] = data.global[location].concat(data[area.template][location]);
async.parallel({ if (!widgetsByLocation[location].length) {
global: function(next) { return done(null, {location: location, widgets: []});
Widgets.getArea('global', area.location, next);
},
local: function(next) {
Widgets.getArea(area.template, area.location, next);
} }
}, function(err, data) {
var widgets = data.global.concat(data.local);
async.eachSeries(widgets, function(widget, next) { async.map(widgetsByLocation[location], function(widget, next) {
if (!widget || !widget.data || (!!widget.data['registered-only'] && uid === 0)) { if (!widget || !widget.data || (!!widget.data['registered-only'] && uid === 0)) {
return next(); return next();
@ -38,7 +38,7 @@ var async = require('async'),
uid: uid, uid: uid,
area: area, area: area,
data: widget.data data: widget.data
}, function(err, html){ }, function(err, html) {
if (widget.data.container && widget.data.container.match('{body}')) { if (widget.data.container && widget.data.container.match('{body}')) {
html = templates.parse(widget.data.container, { html = templates.parse(widget.data.container, {
title: widget.data.title, title: widget.data.title,
@ -46,15 +46,38 @@ var async = require('async'),
}); });
} }
rendered.push({ next(err, {html: html});
html: html
}); });
}, function(err, widgets) {
done(err, {location: location, widgets: widgets.filter(Boolean)});
});
}, callback);
});
};
next(err); Widgets.getAreas = function(templates, locations, callback) {
var keys = templates.map(function(tpl) {
return 'widgets:' + tpl;
});
db.getObjectsFields(keys, locations, function(err, data) {
if (err) {
return callback(err);
}
var returnData = {};
templates.forEach(function(template, index) {
returnData[template] = returnData[template] || {};
locations.forEach(function(location) {
if (data && data[index] && data[index][location]) {
returnData[template][location] = JSON.parse(data[index][location]);
} else {
returnData[template][location] = [];
}
}); });
}, function(err) {
callback(err, rendered);
}); });
callback(null, returnData);
}); });
}; };

Loading…
Cancel
Save