From 1f9ddeb9c8751e7eff74e059e7d5dae7093b7df1 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Mon, 19 Oct 2020 15:58:32 +0200 Subject: [PATCH] tryhard --- zero.Core/Plugins/IZeroPlugin.cs | 15 +++--- zero.Debug/TestData/TestPlugin.cs | 18 ++++--- .../App/components/tables/table-filter.vue | 1 - zero.Web.UI/App/core/zero.js | 40 ++++++++++++++- zero.Web.UI/vue.config.js | 6 +++ zero.Web/Defaults/ZeroBackofficePlugin.cs | 49 ++++++++++--------- zero.Web/ZeroBuilder.cs | 2 +- zero.Web/ZeroVue.cs | 21 +++++++- 8 files changed, 111 insertions(+), 41 deletions(-) diff --git a/zero.Core/Plugins/IZeroPlugin.cs b/zero.Core/Plugins/IZeroPlugin.cs index b5ce5738..881b0bb8 100644 --- a/zero.Core/Plugins/IZeroPlugin.cs +++ b/zero.Core/Plugins/IZeroPlugin.cs @@ -1,26 +1,25 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using System.Threading.Tasks; using zero.Core.Options; namespace zero.Core.Plugins { - public abstract class ZeroPlugin + public abstract class ZeroPlugin : IZeroPlugin { - public string Name { get; protected set; } + public IZeroPluginOptions Options { get; } = new ZeroPluginOptions(); - public string Description { get; set; } + public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration) { } - public virtual void ConfigureServices(IServiceCollection services) { } - - public virtual void Configure(IZeroPluginOptions plugin, IZeroOptions zero) { } + public virtual void Configure(IZeroOptions zero) { } } public interface IZeroPlugin { + IZeroPluginOptions Options { get; } + void ConfigureServices(IServiceCollection services, IConfiguration configuration); - void Configure(IZeroPluginOptions plugin, IZeroOptions zero); + void Configure(IZeroOptions zero); } } \ No newline at end of file diff --git a/zero.Debug/TestData/TestPlugin.cs b/zero.Debug/TestData/TestPlugin.cs index 2bc2b332..42049a68 100644 --- a/zero.Debug/TestData/TestPlugin.cs +++ b/zero.Debug/TestData/TestPlugin.cs @@ -2,20 +2,24 @@ using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using zero.Commerce.Options; +using zero.Core.Extensions; +using zero.Core.Messages; using zero.Core.Options; using zero.Core.Plugins; -using zero.Core.Extensions; -using zero.Debug.Sync; using zero.Debug.TestData; -using zero.Core.Messages; namespace zero.TestData { - public class TestPlugin : IZeroPlugin + public class TestPlugin : ZeroPlugin { - public void Configure(IZeroPluginOptions plugin, IZeroOptions zero) + public TestPlugin() + { + Options.Name = "Test plugin"; + } + + + public override void Configure(IZeroOptions zero) { - plugin.Name = "Test Plugin"; //ISection spaceSection = zero.Sections.GetAllItems().FirstOrDefault(x => x.Alias == Constants.Sections.Spaces); //zero.Sections.Remove(spaceSection); @@ -41,7 +45,7 @@ namespace zero.TestData zero.Modules.Add("nested", "Nested", "Add nested modules", "fth-layers", "Misc"); } - public void ConfigureServices(IServiceCollection services, IConfiguration configuration) + public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { services.AddHostedService(); diff --git a/zero.Web.UI/App/components/tables/table-filter.vue b/zero.Web.UI/App/components/tables/table-filter.vue index 9a22a0aa..cf9a51c6 100644 --- a/zero.Web.UI/App/components/tables/table-filter.vue +++ b/zero.Web.UI/App/components/tables/table-filter.vue @@ -80,7 +80,6 @@ } this.loaded = true; - console.info(this.attach); } } diff --git a/zero.Web.UI/App/core/zero.js b/zero.Web.UI/App/core/zero.js index 3858d2b8..c14006f5 100644 --- a/zero.Web.UI/App/core/zero.js +++ b/zero.Web.UI/App/core/zero.js @@ -14,6 +14,7 @@ import routerConfig from '../config/router.config.js' class Zero { static install; + static instance; config = { ...options }; @@ -23,6 +24,7 @@ class Zero #lists = []; #routes = []; #router = null; + #setupDone = false; constructor(vue, opts) @@ -76,10 +78,24 @@ class Zero //console.info(this.#vue.router); + this.#setupDone = true; //EventHub.$emit('zero.setup'); } + /* + * Installs a zero plugin by a given path + * Each plugin is a superset of a vue (client) plugin and can therefore hook into the vue system + */ + useByPath(pluginPath) + { + //const plugin = import('../../../' + pluginPath + '/plugin.js').then(res => + //{ + // this.use(res.default); + //}); + } + + /* * Installs a zero plugin * Each plugin is a superset of a vue (client) plugin and can therefore hook into the vue system @@ -94,7 +110,14 @@ class Zero this.#plugins.push(plugin); // append routes - plugin.routes.forEach(x => this.#routes.push(x)); + if (this.#setupDone) + { + this.#router.addRoutes(plugin.routes); + } + else + { + plugin.routes.forEach(x => this.#routes.push(x)); + } // append editors plugin.editors.forEach(x => this.#editors.push(x)); @@ -146,16 +169,31 @@ Zero.install = (vue, opts) => { const zero = new Zero(vue, opts); + Zero.instance = zero; + Object.defineProperty(vue.prototype, 'zero', { get: () => zero }); zero.setup(); + // router mixin Vue.mixin({ router: zero.router }); + + // add plugins + //__zero.plugins.filter(x => !!x.pluginPath).forEach(x => + //{ + // zero.useByPath(x.pluginPath); + //}); + + //import('@/../zero.Commerce/Plugins/zero.Commerce/plugin.js').then(res => + //{ + // zero.use(res.default); + //}); }; +window.XZERO = Zero; export default Zero; \ No newline at end of file diff --git a/zero.Web.UI/vue.config.js b/zero.Web.UI/vue.config.js index c96b5974..87edba85 100644 --- a/zero.Web.UI/vue.config.js +++ b/zero.Web.UI/vue.config.js @@ -1,3 +1,9 @@ + +//console.log(process.env); +// TODO +// maybe we can make an HTTP request here to get plugin paths and install them +// as well as set other fields in config as aliases. + module.exports = { // disable hashes in filenames diff --git a/zero.Web/Defaults/ZeroBackofficePlugin.cs b/zero.Web/Defaults/ZeroBackofficePlugin.cs index 9adb7e90..a011c21a 100644 --- a/zero.Web/Defaults/ZeroBackofficePlugin.cs +++ b/zero.Web/Defaults/ZeroBackofficePlugin.cs @@ -11,9 +11,34 @@ using zero.Web.Sections; namespace zero.Web.Defaults { - internal class ZeroBackofficePlugin : IZeroPlugin + internal class ZeroBackofficePlugin : ZeroPlugin { - public void ConfigureServices(IServiceCollection services, IConfiguration configuration) + public ZeroBackofficePlugin() + { + Options.Name = "zero.Defaults"; + Options.LocalizationPaths.Add("~/Resources/Localization/zero.{lang}.json"); + } + + + public override void Configure(IZeroOptions zero) + { + zero.Sections.Add(); + zero.Sections.Add(); + zero.Sections.Add(); + zero.Sections.Add(); + zero.Sections.Add(); + + zero.Settings.AddGroup(); + //zero.Settings.AddGroup(); + + zero.Permissions.AddCollection(); + zero.Permissions.AddCollection(); + zero.Permissions.AddCollection(); + zero.Permissions.AddCollection(); + } + + + public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { //services.AddAll(typeof(IValidator<>), ServiceLifetime.Scoped); //services.AddAll(typeof(IValidator), ServiceLifetime.Scoped); @@ -62,25 +87,5 @@ namespace zero.Web.Defaults services.AddTransient(); services.AddTransient(); } - - public void Configure(IZeroPluginOptions plugin, IZeroOptions zero) - { - plugin.Name = "zero.Defaults"; - plugin.LocalizationPaths.Add("~/Resources/Localization/zero.{lang}.json"); - - zero.Sections.Add(); - zero.Sections.Add(); - zero.Sections.Add(); - zero.Sections.Add(); - zero.Sections.Add(); - - zero.Settings.AddGroup(); - //zero.Settings.AddGroup(); - - zero.Permissions.AddCollection(); - zero.Permissions.AddCollection(); - zero.Permissions.AddCollection(); - zero.Permissions.AddCollection(); - } } } \ No newline at end of file diff --git a/zero.Web/ZeroBuilder.cs b/zero.Web/ZeroBuilder.cs index 79ec6aa1..0a7509a7 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -247,7 +247,7 @@ namespace zero.Web plugin.ConfigureServices(Services, Configuration); - Services.Configure(opts => plugin.Configure(new ZeroPluginOptions(), opts)); + Services.Configure(opts => plugin.Configure(opts)); } catch { diff --git a/zero.Web/ZeroVue.cs b/zero.Web/ZeroVue.cs index b03a5627..8c45411c 100644 --- a/zero.Web/ZeroVue.cs +++ b/zero.Web/ZeroVue.cs @@ -54,7 +54,7 @@ namespace zero.Web config.ApiPath = config.Path + "api/"; config.PluginPath = "@/Plugins"; config.Version = Options.ZeroVersion; - config.PluginCount = 2; // TODO Plugins.Count(); + config.PluginCount = Plugins.Count(); config.ErrorFieldNone = Constants.ErrorFieldNone; config.Sections = CreateSections(); config.Translations = CreateTranslations(); @@ -79,6 +79,13 @@ namespace zero.Web }; } + config.Plugins = Plugins.Select(x => new ZeroVuePlugin() + { + Name = x.Options.Name, + Description = x.Options.Description, + PluginPath = x.Options.PluginPath + }).ToList(); + return config; } @@ -297,6 +304,8 @@ namespace zero.Web public string PluginPath { get; set; } + public IList Plugins { get; set; } = new List(); + public string Version { get; set; } public int PluginCount { get; set; } @@ -339,6 +348,16 @@ namespace zero.Web } + public class ZeroVuePlugin + { + public string Name { get; set; } + + public string Description { get; set; } + + public string PluginPath { get; set; } + } + + public class ZeroVueSettingsGroup { public string Name { get; set; }