feat: #7743, widgets/admin

v1.18.x
Baris Usakli 6 years ago
parent 2258452c3e
commit 96ebe7b5f7

@ -1,95 +1,76 @@
'use strict'; 'use strict';
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const util = require('util');
var async = require('async'); const readFileAsync = util.promisify(fs.readFile);
var nconf = require('nconf');
var benchpress = require('benchpressjs');
var plugins = require('../plugins');
var groups = require('../groups');
var admin = module.exports;
admin.get = function (callback) {
async.parallel({
areas: admin.getAreas,
availableWidgets: getAvailableWidgets,
}, function (err, widgetData) {
if (err) {
return callback(err);
}
callback(null, { const path = require('path');
templates: buildTemplatesFromAreas(widgetData.areas), const nconf = require('nconf');
areas: widgetData.areas, const benchpress = require('benchpressjs');
availableWidgets: widgetData.availableWidgets,
}); const compileParseAsync = util.promisify(benchpress.compileParse);
});
const plugins = require('../plugins');
const groups = require('../groups');
const index = require('./index');
const admin = module.exports;
admin.get = async function () {
const [areas, availableWidgets] = await Promise.all([
admin.getAreas(),
getAvailableWidgets(),
]);
return {
templates: buildTemplatesFromAreas(areas),
areas: areas,
availableWidgets: availableWidgets,
};
}; };
admin.getAreas = function (callback) { admin.getAreas = async function () {
async.waterfall([ const defaultAreas = [
function (next) { { name: 'Global Sidebar', template: 'global', location: 'sidebar' },
var defaultAreas = [ { name: 'Global Header', template: 'global', location: 'header' },
{ name: 'Global Sidebar', template: 'global', location: 'sidebar' }, { name: 'Global Footer', template: 'global', location: 'footer' },
{ 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' },
{ name: 'Group Page (Left)', template: 'groups/details.tpl', location: 'left' }, ];
{ name: 'Group Page (Right)', template: 'groups/details.tpl', location: 'right' },
]; const areas = await plugins.fireHook('filter:widgets.getAreas', defaultAreas);
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next); areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' });
}, const areaData = await Promise.all(areas.map(area => index.getArea(area.template, area.location)));
function (areas, next) { areas.forEach((area, i) => {
areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' }); area.data = areaData[i];
async.map(areas, function (area, next) { });
require('./index').getArea(area.template, area.location, function (err, areaData) { return areas;
area.data = areaData;
next(err, area);
});
}, next);
},
], callback);
}; };
function getAvailableWidgets(callback) { async function getAvailableWidgets() {
async.parallel({ const [availableWidgets, adminTemplate] = await Promise.all([
availableWidgets: function (next) { plugins.fireHook('filter:widgets.getWidgets', []),
plugins.fireHook('filter:widgets.getWidgets', [], next); renderAdminTemplate(),
}, ]);
adminTemplate: function (next) { availableWidgets.forEach(function (w) {
renderAdminTemplate(next); w.content += adminTemplate;
},
}, function (err, results) {
if (err) {
return callback(err);
}
results.availableWidgets.forEach(function (w) {
w.content += results.adminTemplate;
});
callback(null, results.availableWidgets);
}); });
return availableWidgets;
} }
function renderAdminTemplate(callback) { async function renderAdminTemplate() {
async.waterfall([ const [source, groupsData] = await Promise.all([
function (next) { getSource(),
async.parallel({ groups.getNonPrivilegeGroups('groups:createtime', 0, -1),
source: async.apply(getSource), ]);
groups: async.apply(groups.getNonPrivilegeGroups, 'groups:createtime', 0, -1), groupsData.sort((a, b) => b.system - a.system);
}, next); return await compileParseAsync(source, { groups: groupsData });
},
function (results, next) {
results.groups.sort((a, b) => b.system - a.system);
benchpress.compileParse(results.source, { groups: results.groups }, next);
},
], callback);
} }
function getSource(callback) { async function getSource() {
fs.readFile(path.resolve(nconf.get('views_dir'), 'admin/partials/widget-settings.tpl'), 'utf8', callback); return await readFileAsync(path.resolve(nconf.get('views_dir'), 'admin/partials/widget-settings.tpl'), 'utf8');
} }
function buildTemplatesFromAreas(areas) { function buildTemplatesFromAreas(areas) {

Loading…
Cancel
Save