moved widget code out of controller; cleaned up code into a folder
parent
394d55de99
commit
0f199af84d
@ -1,157 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
var async = require('async'),
|
|
||||||
winston = require('winston'),
|
|
||||||
templates = require('templates.js'),
|
|
||||||
|
|
||||||
plugins = require('./plugins'),
|
|
||||||
db = require('./database');
|
|
||||||
|
|
||||||
|
|
||||||
(function(Widgets) {
|
|
||||||
Widgets.render = function(uid, area, callback) {
|
|
||||||
if (!area.locations || !area.template) {
|
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
Widgets.getAreas(['global', area.template], area.locations, function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var widgetsByLocation = {};
|
|
||||||
|
|
||||||
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: []});
|
|
||||||
}
|
|
||||||
|
|
||||||
async.map(widgetsByLocation[location], function(widget, next) {
|
|
||||||
|
|
||||||
if (!widget || !widget.data || (!!widget.data['hide-registered'] && uid !== 0) || (!!widget.data['hide-guests'] && uid === 0)) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins.fireHook('filter:widget.render:' + widget.widget, {
|
|
||||||
uid: uid,
|
|
||||||
area: area,
|
|
||||||
data: widget.data
|
|
||||||
}, function(err, html) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof html !== 'string') {
|
|
||||||
html = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.data.container && widget.data.container.match('{body}')) {
|
|
||||||
html = templates.parse(widget.data.container, {
|
|
||||||
title: widget.data.title,
|
|
||||||
body: html
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
next(null, {html: html});
|
|
||||||
});
|
|
||||||
}, function(err, widgets) {
|
|
||||||
done(err, {location: location, widgets: widgets.filter(Boolean)});
|
|
||||||
});
|
|
||||||
}, callback);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
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]) {
|
|
||||||
try {
|
|
||||||
returnData[template][location] = JSON.parse(data[index][location]);
|
|
||||||
} catch(err) {
|
|
||||||
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
|
|
||||||
returnData[template][location] = [];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
returnData[template][location] = [];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
callback(null, returnData);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Widgets.getArea = function(template, location, callback) {
|
|
||||||
db.getObjectField('widgets:' + template, location, function(err, widgets) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
if (!widgets) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
widgets = JSON.parse(widgets);
|
|
||||||
} catch(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, widgets);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Widgets.setArea = function(area, callback) {
|
|
||||||
if (!area.location || !area.template) {
|
|
||||||
return callback(new Error('Missing location and template data'));
|
|
||||||
}
|
|
||||||
|
|
||||||
db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Widgets.reset = function(callback) {
|
|
||||||
var defaultAreas = [
|
|
||||||
{ name: 'Draft Zone', template: 'global', location: 'drafts' }
|
|
||||||
];
|
|
||||||
|
|
||||||
plugins.fireHook('filter:widgets.getAreas', defaultAreas, function(err, areas) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
var drafts = [];
|
|
||||||
|
|
||||||
async.each(areas, function(area, next) {
|
|
||||||
Widgets.getArea(area.template, area.location, function(err, areaData) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
drafts = drafts.concat(areaData);
|
|
||||||
area.widgets = [];
|
|
||||||
Widgets.setArea(area, next);
|
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
Widgets.setArea({
|
|
||||||
template: 'global',
|
|
||||||
location: 'drafts',
|
|
||||||
widgets: drafts
|
|
||||||
}, callback);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
}(exports));
|
|
@ -0,0 +1,78 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
plugins = require('../plugins');
|
||||||
|
|
||||||
|
var admin = {};
|
||||||
|
|
||||||
|
admin.get = function(callback) {
|
||||||
|
async.parallel({
|
||||||
|
areas: function(next) {
|
||||||
|
var defaultAreas = [
|
||||||
|
{ name: 'Global Sidebar', template: 'global', location: 'sidebar' },
|
||||||
|
{ name: 'Global Header', template: 'global', location: 'header' },
|
||||||
|
{ name: 'Global Footer', template: 'global', location: 'footer' },
|
||||||
|
|
||||||
|
{ name: 'Group Page (Left)', template: 'groups/details.tpl', location: 'left'},
|
||||||
|
{ name: 'Group Page (Right)', template: 'groups/details.tpl', location: 'right'}
|
||||||
|
];
|
||||||
|
|
||||||
|
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next);
|
||||||
|
},
|
||||||
|
widgets: function(next) {
|
||||||
|
plugins.fireHook('filter:widgets.getWidgets', [], next);
|
||||||
|
}
|
||||||
|
}, function(err, widgetData) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
widgetData.areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' });
|
||||||
|
|
||||||
|
async.each(widgetData.areas, function(area, next) {
|
||||||
|
require('./index').getArea(area.template, area.location, function(err, areaData) {
|
||||||
|
area.data = areaData;
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
}, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
for (var w in widgetData.widgets) {
|
||||||
|
if (widgetData.widgets.hasOwnProperty(w)) {
|
||||||
|
// if this gets anymore complicated, it needs to be a template
|
||||||
|
widgetData.widgets[w].content += "<br /><label>Title:</label><input type=\"text\" class=\"form-control\" name=\"title\" placeholder=\"Title (only shown on some containers)\" /><br /><label>Container:</label><textarea rows=\"4\" class=\"form-control container-html\" name=\"container\" placeholder=\"Drag and drop a container or enter HTML here.\"></textarea><div class=\"checkbox\"><label><input name=\"hide-guests\" type=\"checkbox\"> Hide from anonymous users?</label></div><div class=\"checkbox\"><label><input name=\"hide-registered\" type=\"checkbox\"> Hide from registered users?</input></label></div>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var templates = [],
|
||||||
|
list = {}, index = 0;
|
||||||
|
|
||||||
|
widgetData.areas.forEach(function(area) {
|
||||||
|
if (typeof list[area.template] === 'undefined') {
|
||||||
|
list[area.template] = index;
|
||||||
|
templates.push({
|
||||||
|
template: area.template,
|
||||||
|
areas: []
|
||||||
|
});
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
templates[list[area.template]].areas.push({
|
||||||
|
name: area.name,
|
||||||
|
location: area.location
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(false, {
|
||||||
|
templates: templates,
|
||||||
|
areas: widgetData.areas,
|
||||||
|
widgets: widgetData.widgets
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = admin;
|
@ -0,0 +1,157 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
winston = require('winston'),
|
||||||
|
templates = require('templates.js'),
|
||||||
|
|
||||||
|
plugins = require('../plugins'),
|
||||||
|
db = require('../database');
|
||||||
|
|
||||||
|
var widgets = {};
|
||||||
|
|
||||||
|
widgets.render = function(uid, area, callback) {
|
||||||
|
if (!area.locations || !area.template) {
|
||||||
|
return callback(new Error('[[error:invalid-data]]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
widgets.getAreas(['global', area.template], area.locations, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var widgetsByLocation = {};
|
||||||
|
|
||||||
|
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: []});
|
||||||
|
}
|
||||||
|
|
||||||
|
async.map(widgetsByLocation[location], function(widget, next) {
|
||||||
|
|
||||||
|
if (!widget || !widget.data || (!!widget.data['hide-registered'] && uid !== 0) || (!!widget.data['hide-guests'] && uid === 0)) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins.fireHook('filter:widget.render:' + widget.widget, {
|
||||||
|
uid: uid,
|
||||||
|
area: area,
|
||||||
|
data: widget.data
|
||||||
|
}, function(err, html) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof html !== 'string') {
|
||||||
|
html = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (widget.data.container && widget.data.container.match('{body}')) {
|
||||||
|
html = templates.parse(widget.data.container, {
|
||||||
|
title: widget.data.title,
|
||||||
|
body: html
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
next(null, {html: html});
|
||||||
|
});
|
||||||
|
}, function(err, result) {
|
||||||
|
done(err, {location: location, widgets: result.filter(Boolean)});
|
||||||
|
});
|
||||||
|
}, callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
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]) {
|
||||||
|
try {
|
||||||
|
returnData[template][location] = JSON.parse(data[index][location]);
|
||||||
|
} catch(err) {
|
||||||
|
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
|
||||||
|
returnData[template][location] = [];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
returnData[template][location] = [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(null, returnData);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
widgets.getArea = function(template, location, callback) {
|
||||||
|
db.getObjectField('widgets:' + template, location, function(err, result) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
if (!result) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
result = JSON.parse(result);
|
||||||
|
} catch(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, result);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
widgets.setArea = function(area, callback) {
|
||||||
|
if (!area.location || !area.template) {
|
||||||
|
return callback(new Error('Missing location and template data'));
|
||||||
|
}
|
||||||
|
|
||||||
|
db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
widgets.reset = function(callback) {
|
||||||
|
var defaultAreas = [
|
||||||
|
{ name: 'Draft Zone', template: 'global', location: 'drafts' }
|
||||||
|
];
|
||||||
|
|
||||||
|
plugins.fireHook('filter:widgets.getAreas', defaultAreas, function(err, areas) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
var drafts = [];
|
||||||
|
|
||||||
|
async.each(areas, function(area, next) {
|
||||||
|
widgets.getArea(area.template, area.location, function(err, areaData) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
drafts = drafts.concat(areaData);
|
||||||
|
area.widgets = [];
|
||||||
|
widgets.setArea(area, next);
|
||||||
|
});
|
||||||
|
}, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
widgets.setArea({
|
||||||
|
template: 'global',
|
||||||
|
location: 'drafts',
|
||||||
|
widgets: drafts
|
||||||
|
}, callback);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = widgets;
|
Loading…
Reference in New Issue