2014-03-29 14:44:51 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2014-02-19 15:07:38 -05:00
|
|
|
var async = require('async'),
|
|
|
|
|
winston = require('winston'),
|
2014-02-19 17:07:47 -05:00
|
|
|
plugins = require('./plugins'),
|
2014-02-20 18:10:06 -05:00
|
|
|
db = require('./database'),
|
|
|
|
|
templates = require('./../public/src/templates');
|
2014-02-19 15:07:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
(function(Widgets) {
|
|
|
|
|
|
2014-02-19 17:07:47 -05:00
|
|
|
Widgets.render = function(uid, area, callback) {
|
|
|
|
|
if (!area.location || !area.template) {
|
|
|
|
|
callback({
|
|
|
|
|
error: 'Missing location and template data'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rendered = [];
|
|
|
|
|
|
|
|
|
|
Widgets.getArea(area.template, area.location, function(err, widgets) {
|
2014-02-19 19:19:25 -05:00
|
|
|
async.eachSeries(widgets, function(widget, next) {
|
2014-02-19 17:07:47 -05:00
|
|
|
plugins.fireHook('filter:widget.render:' + widget.widget, {
|
|
|
|
|
uid: uid,
|
|
|
|
|
area: area,
|
|
|
|
|
data: widget.data
|
2014-02-20 18:10:06 -05:00
|
|
|
}, function(err, html){
|
|
|
|
|
if (widget.data.container && widget.data.container.match('{body}')) {
|
2014-03-29 14:44:51 -04:00
|
|
|
html = templates.parse(widget.data.container, {
|
2014-02-20 18:10:06 -05:00
|
|
|
title: widget.data.title,
|
|
|
|
|
body: html
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 17:07:47 -05:00
|
|
|
rendered.push({
|
2014-02-20 18:10:06 -05:00
|
|
|
html: html
|
2014-02-19 17:07:47 -05:00
|
|
|
});
|
2014-02-19 17:22:26 -05:00
|
|
|
|
|
|
|
|
next(err);
|
2014-02-19 17:07:47 -05:00
|
|
|
});
|
|
|
|
|
}, function(err) {
|
|
|
|
|
callback(err, rendered);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-19 16:11:16 -05:00
|
|
|
Widgets.getArea = function(template, location, callback) {
|
|
|
|
|
db.getObjectField('widgets:' + template, location, function(err, widgets) {
|
2014-02-20 19:18:23 +01:00
|
|
|
if (!widgets) {
|
|
|
|
|
return callback(err, []);
|
|
|
|
|
}
|
2014-02-19 16:11:16 -05:00
|
|
|
callback(err, JSON.parse(widgets));
|
2014-03-29 14:44:51 -04:00
|
|
|
});
|
2014-02-19 16:11:16 -05:00
|
|
|
};
|
|
|
|
|
|
2014-02-19 17:07:47 -05:00
|
|
|
Widgets.setArea = function(area, callback) {
|
2014-02-19 17:22:26 -05:00
|
|
|
if (!area.location || !area.template) {
|
2014-02-19 15:07:38 -05:00
|
|
|
callback({
|
|
|
|
|
error: 'Missing location and template data'
|
2014-02-19 16:11:16 -05:00
|
|
|
});
|
2014-02-19 15:07:38 -05:00
|
|
|
}
|
|
|
|
|
|
2014-02-19 17:07:47 -05:00
|
|
|
db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), function(err) {
|
2014-02-19 15:07:38 -05:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}(exports));
|