Files
NodeBB/src/widgets.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-03-29 14:44:51 -04:00
"use strict";
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');
(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);
});
});
};
Widgets.getArea = function(template, location, callback) {
db.getObjectField('widgets:' + template, location, function(err, widgets) {
if (!widgets) {
return callback(err, []);
}
callback(err, JSON.parse(widgets));
2014-03-29 14:44:51 -04: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) {
callback({
error: 'Missing location and template data'
});
}
2014-02-19 17:07:47 -05:00
db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), function(err) {
callback(err);
});
};
}(exports));