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-06-25 18:55:22 -04:00
|
|
|
templates = require('templates.js'),
|
|
|
|
|
|
2014-02-19 17:07:47 -05:00
|
|
|
plugins = require('./plugins'),
|
2014-06-25 18:55:22 -04:00
|
|
|
db = require('./database');
|
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 = [];
|
|
|
|
|
|
2014-03-31 12:27:24 -04:00
|
|
|
async.parallel({
|
|
|
|
|
global: function(next) {
|
|
|
|
|
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);
|
|
|
|
|
|
2014-02-19 19:19:25 -05:00
|
|
|
async.eachSeries(widgets, function(widget, next) {
|
2014-04-16 21:05:14 -04:00
|
|
|
if (!!widget.data['registered-only'] && uid === 0) {
|
|
|
|
|
return 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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-31 16:19:57 -04:00
|
|
|
Widgets.reset = function(callback) {
|
2014-04-08 16:52:52 -04:00
|
|
|
var defaultAreas = [
|
2014-05-01 14:04:27 -04:00
|
|
|
{ name: 'Draft Zone', template: 'global', location: 'drafts' }
|
2014-04-08 16:52:52 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('filter:widgets.getAreas', defaultAreas, function(err, areas) {
|
2014-03-31 16:19:57 -04:00
|
|
|
var drafts = [];
|
|
|
|
|
|
|
|
|
|
async.each(areas, function(area, next) {
|
|
|
|
|
Widgets.getArea(area.template, area.location, function(err, areaData) {
|
|
|
|
|
drafts = drafts.concat(areaData);
|
|
|
|
|
area.widgets = [];
|
|
|
|
|
Widgets.setArea(area, next);
|
|
|
|
|
});
|
|
|
|
|
}, function(err) {
|
|
|
|
|
Widgets.setArea({
|
|
|
|
|
template: 'global',
|
|
|
|
|
location: 'drafts',
|
|
|
|
|
widgets: drafts
|
|
|
|
|
}, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
}(exports));
|