Files
NodeBB/src/widgets/admin.js

98 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2016-05-03 17:17:38 +03:00
var fs = require('fs');
var path = require('path');
var async = require('async');
var nconf = require('nconf');
2018-11-10 20:51:07 -05:00
var benchpress = require('benchpressjs');
2016-05-03 17:17:38 +03:00
var plugins = require('../plugins');
2018-11-10 20:51:07 -05:00
var groups = require('../groups');
2018-11-10 20:51:07 -05:00
var admin = module.exports;
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' },
2017-02-18 12:30:49 -07:00
{ 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);
2016-05-03 17:17:38 +03:00
},
adminTemplate: function (next) {
2018-11-10 20:51:07 -05:00
renderAdminTemplate(next);
2017-02-17 19:31:21 -07:00
},
}, 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);
}
2016-05-03 17:17:38 +03:00
widgetData.widgets.forEach(function (w) {
2016-05-03 17:17:38 +03:00
w.content += widgetData.adminTemplate;
});
2017-02-17 20:20:42 -07:00
var templates = [];
var list = {};
var index = 0;
widgetData.areas.forEach(function (area) {
if (typeof list[area.template] === 'undefined') {
list[area.template] = index;
templates.push({
template: area.template,
2017-02-17 19:31:21 -07:00
areas: [],
});
index += 1;
}
templates[list[area.template]].areas.push({
name: area.name,
2017-02-17 19:31:21 -07:00
location: area.location,
});
});
callback(false, {
templates: templates,
areas: widgetData.areas,
availableWidgets: widgetData.widgets,
});
});
});
};
2018-11-10 20:51:07 -05:00
function renderAdminTemplate(callback) {
async.waterfall([
function (next) {
async.parallel({
source: async.apply(fs.readFile, path.resolve(nconf.get('views_dir'), 'admin/partials/widget-settings.tpl'), 'utf8'),
groups: async.apply(groups.getNonPrivilegeGroups, 'groups:createtime', 0, -1),
}, next);
},
function (results, next) {
results.groups.sort((a, b) => b.system - a.system);
benchpress.compileParse(results.source, { groups: results.groups }, next);
},
], callback);
}