Files
NodeBB/src/widgets/admin.js

82 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2020-10-14 20:02:32 -04:00
const webserver = require('../webserver');
2019-07-29 13:34:29 -04:00
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,
};
};
2019-07-29 13:34:29 -04:00
admin.getAreas = async function () {
const areas = await index.getAvailableAreas();
2019-07-29 13:34:29 -04:00
areas.push({ name: 'Draft Zone', template: 'global', location: 'drafts' });
const areaData = await Promise.all(
areas.map(area => index.getArea(area.template, area.location))
);
2019-07-29 13:34:29 -04:00
areas.forEach((area, i) => {
area.data = areaData[i];
});
return areas;
};
2019-07-29 13:34:29 -04:00
async function getAvailableWidgets() {
const [availableWidgets, adminTemplate] = await Promise.all([
plugins.hooks.fire('filter:widgets.getWidgets', []),
2019-07-29 13:34:29 -04:00
renderAdminTemplate(),
]);
2021-02-04 00:01:39 -07:00
availableWidgets.forEach((w) => {
2019-07-29 13:34:29 -04:00
w.content += adminTemplate;
});
2019-07-29 13:34:29 -04:00
return availableWidgets;
}
2019-07-29 13:34:29 -04:00
async function renderAdminTemplate() {
2020-10-14 19:18:07 -04:00
const groupsData = await groups.getNonPrivilegeGroups('groups:createtime', 0, -1);
2019-07-29 13:34:29 -04:00
groupsData.sort((a, b) => b.system - a.system);
2020-10-14 20:02:32 -04:00
return await webserver.app.renderAsync('admin/partials/widget-settings', { groups: groupsData });
}
function buildTemplatesFromAreas(areas) {
const templates = [];
2019-07-29 17:55:48 -04:00
const list = {};
let index = 0;
2021-02-04 00:01:39 -07:00
areas.forEach((area) => {
if (typeof list[area.template] === 'undefined') {
list[area.template] = index;
templates.push({
template: area.template,
areas: [],
2023-10-23 10:31:23 -04:00
widgetCount: area.location !== 'drafts' ? area.data.length : 0,
});
index += 1;
}
templates[list[area.template]].areas.push({
name: area.name,
location: area.location,
});
2023-10-23 10:31:23 -04:00
if (area.location !== 'drafts') {
templates[list[area.template]].widgetCount += area.data.length;
}
});
return templates;
}
2019-07-18 22:35:12 -04:00
require('../promisify')(admin);