From e851e723e098ae0b5e1dd2b0ccbf85aedb5eb315 Mon Sep 17 00:00:00 2001 From: DianaXWiki <139217939+DianaXWiki@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:00:47 +0200 Subject: [PATCH] Add getExtensionsSync for admin panel extensions --- www/admin/inner.js | 75 +++++++++++++++---------------------- www/common/extensions.js | 20 ++++++++++ www/common/sframe-common.js | 1 + 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/www/admin/inner.js b/www/admin/inner.js index 250aaa24d..465371b0d 100644 --- a/www/admin/inner.js +++ b/www/admin/inner.js @@ -172,24 +172,20 @@ define([ const blocks = sidebar.blocks; // EXTENSION_POINT:ADMIN_CATEGORY - async function processExtensions(id) { - const extensions = await common.getExtensions(id); - extensions.forEach(_ext => { - _ext.then(ext => { - if (!ext || !ext.id || !ext.name || !ext.content) { - return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext); - } - if (categories[ext.id]) { - return console.error('Extension point ID already used', ext); - } - categories[ext.id] = { - icon: ext.icon, - name: ext.name, - content: ext.content - }; - }) - }) - } + common.getExtensionsSync('ADMIN_CATEGORY').forEach(ext => { + if (!ext || !ext.id || !ext.name || !ext.content) { + return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext); + } + if (categories[ext.id]) { + return console.error('Extension point ID already used', ext); + } + console.error(ext); + categories[ext.id] = { + icon: ext.icon, + name: ext.name, + content: ext.content + }; + }); const flushCache = (cb) => { @@ -3931,36 +3927,27 @@ define([ let utils = { h, Util, Hash }; - common.getExtensions('ADMIN_ITEM').forEach(_ext => { - // console.log("item") - _ext.then(ext => { - if (!ext || !ext.id || typeof(ext.getContent) !== "function") { - return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext); - } - if (sidebar.hasItem(ext.id)) { - return console.error('Extension point ID already used', ext); - } - sidebar.addItem(ext.id, cb => { - ext.getContent(common, blocks, utils, content => { - console.log("Generated content for", ext.id, ":", content); - cb(content); - }); - }, { - noTitle: !ext.title, - noHint: !ext.description, - title: ext.title, - hint: ext.description + common.getExtensionsSync('ADMIN_ITEM').forEach(ext => { + if (!ext || !ext.id || typeof(ext.getContent) !== "function") { + return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext); + } + if (sidebar.hasItem(ext.id)) { + return console.error('Extension point ID already used', ext); + } + sidebar.addItem(ext.id, cb => { + ext.getContent(common, blocks, utils, content => { + cb(content); }); - }).catch(error => console.error("Error loading ADMIN_CATEGORY extension:", error)); + }, { + noTitle: !ext.title, + noHint: !ext.description, + title: ext.title, + hint: ext.description + }); }); - (async () => { - await processExtensions('ADMIN_CATEGORY'); - sidebar.makeLeftside(categories); - })(); + sidebar.makeLeftside(categories); }; - - var updateStatus = APP.updateStatus = function (cb) { sFrameChan.query('Q_ADMIN_RPC', { cmd: 'INSTANCE_STATUS', diff --git a/www/common/extensions.js b/www/common/extensions.js index 9081b257f..9f0ca2f92 100644 --- a/www/common/extensions.js +++ b/www/common/extensions.js @@ -35,6 +35,26 @@ define([ }); }; + ext.getExtensionsSync = id => { + let e = ext[id]; + if (!Array.isArray(e)) { e = []; } + + return e.map(_ext => { + if (typeof _ext.check !== "function") { + return _ext; + } + + const extPassed = _ext.check(); + + if (extPassed) { + return _ext; + } else { + return null; + } + }).filter(ext => ext !== null); + }; + + if (!Array.isArray(Extensions) || !Extensions.length) { return ext; } let all = Extensions.slice(); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index c297b5273..8310deb27 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -778,6 +778,7 @@ define([ }; funcs.getExtensions = Ext.getExtensions; + funcs.getExtensionsSync = Ext.getExtensionsSync; funcs.mailbox = {};