mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: #7743, widgets/admin
This commit is contained in:
@@ -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');
|
const path = require('path');
|
||||||
var groups = require('../groups');
|
const nconf = require('nconf');
|
||||||
|
const benchpress = require('benchpressjs');
|
||||||
|
|
||||||
var admin = module.exports;
|
const compileParseAsync = util.promisify(benchpress.compileParse);
|
||||||
|
|
||||||
admin.get = function (callback) {
|
const plugins = require('../plugins');
|
||||||
async.parallel({
|
const groups = require('../groups');
|
||||||
areas: admin.getAreas,
|
const index = require('./index');
|
||||||
availableWidgets: getAvailableWidgets,
|
|
||||||
}, function (err, widgetData) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, {
|
const admin = module.exports;
|
||||||
templates: buildTemplatesFromAreas(widgetData.areas),
|
|
||||||
areas: widgetData.areas,
|
admin.get = async function () {
|
||||||
availableWidgets: widgetData.availableWidgets,
|
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 (Left)', template: 'groups/details.tpl', location: 'left' },
|
||||||
{ name: 'Group Page (Right)', template: 'groups/details.tpl', location: 'right' },
|
{ name: 'Group Page (Right)', template: 'groups/details.tpl', location: 'right' },
|
||||||
];
|
];
|
||||||
|
|
||||||
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next);
|
const areas = await plugins.fireHook('filter:widgets.getAreas', defaultAreas);
|
||||||
},
|
|
||||||
function (areas, next) {
|
areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' });
|
||||||
areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' });
|
const areaData = await Promise.all(areas.map(area => index.getArea(area.template, area.location)));
|
||||||
async.map(areas, function (area, next) {
|
areas.forEach((area, i) => {
|
||||||
require('./index').getArea(area.template, area.location, function (err, areaData) {
|
area.data = areaData[i];
|
||||||
area.data = areaData;
|
});
|
||||||
next(err, area);
|
return areas;
|
||||||
});
|
|
||||||
}, 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user