From b1582fdd6f0daf02cdce979c92fe8d9c486948c8 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 20 May 2019 16:35:13 +0300 Subject: [PATCH] basic improvements on widget system --- .../Mvc/UI/Widgets/IWidgetRenderer.cs | 12 +++------ .../Mvc/UI/Widgets/WidgetRenderer.cs | 27 +++++++++++++++++++ samples/DashboardDemo/DashboardDemo.sln | 2 +- .../DashboardDemoWebModule.cs | 13 ++++++++- .../src/DashboardDemo.Web/Pages/Index.cshtml | 10 +++---- .../widgets/MyWidgetViewComponent.cshtml | 6 +++++ .../widgets/MyWidgetViewComponent.cshtml.cs | 13 +++++++++ 7 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs create mode 100644 samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml create mode 100644 samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs index 5ef4abadba..7d40aacf3d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs @@ -1,18 +1,12 @@ using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets { public interface IWidgetRenderer : ITransientDependency { - Task RenderAsync(string mywidget); - } - - public class WidgetRenderer : IWidgetRenderer - { - public Task RenderAsync(string mywidget) - { - throw new System.NotImplementedException(); - } + Task RenderAsync(IViewComponentHelper component, string mywidget); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs new file mode 100644 index 0000000000..36e48e2169 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs @@ -0,0 +1,27 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets +{ + public class WidgetRenderer : IWidgetRenderer + { + private readonly WidgetOptions _widgetOptions; + + public WidgetRenderer(IOptions widgetOptions) + { + _widgetOptions = widgetOptions.Value; + } + + public async Task RenderAsync(IViewComponentHelper component, string mywidget) + { + var componentType = _widgetOptions.Widgets.Single(w=>w.Name.Equals(mywidget)).ViewComponentType; + var args = new object(); + + return await component.InvokeAsync(componentType, args); + } + } +} \ No newline at end of file diff --git a/samples/DashboardDemo/DashboardDemo.sln b/samples/DashboardDemo/DashboardDemo.sln index 1195ca560e..c6b0948d70 100644 --- a/samples/DashboardDemo/DashboardDemo.sln +++ b/samples/DashboardDemo/DashboardDemo.sln @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashboardDemo.Application.T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashboardDemo.Web.Tests", "test\DashboardDemo.Web.Tests\DashboardDemo.Web.Tests.csproj", "{5F1B28C6-8D0C-4155-92D0-252F7EA5F674}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DashboardDemo.EntityFrameworkCore.DbMigrations", "src\DashboardDemo.EntityFrameworkCore.DbMigrations\DashboardDemo.EntityFrameworkCore.DbMigrations.csproj", "{0372FA84-C517-4EB3-9A9F-B9ACAC0CA5E0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashboardDemo.EntityFrameworkCore.DbMigrations", "src\DashboardDemo.EntityFrameworkCore.DbMigrations\DashboardDemo.EntityFrameworkCore.DbMigrations.csproj", "{0372FA84-C517-4EB3-9A9F-B9ACAC0CA5E0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs b/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs index abf9653cef..dd52df853e 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using DashboardDemo.EntityFrameworkCore; using DashboardDemo.Localization.DashboardDemo; using DashboardDemo.Menus; +using DashboardDemo.Pages.widgets; using DashboardDemo.Permissions; using Swashbuckle.AspNetCore.Swagger; using Volo.Abp; @@ -18,6 +19,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.AspNetCore.Mvc.UI.Dashboards; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; +using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; @@ -36,7 +38,6 @@ using Volo.Abp.PermissionManagement; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.TenantManagement.Web; - namespace DashboardDemo { [DependsOn( @@ -69,6 +70,7 @@ namespace DashboardDemo var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + ConfigureWidgets(); ConfigureDatabaseServices(); ConfigureAutoMapper(); ConfigureVirtualFileSystem(hostingEnvironment); @@ -78,6 +80,15 @@ namespace DashboardDemo ConfigureSwaggerServices(context.Services); } + private void ConfigureWidgets() + { + Configure(options => + { + options.Widgets.Add(new WidgetDefinition("MyWidget", typeof(MyWidgetViewComponentModel), + new LocalizableString(typeof(DashboardDemoResource), "MyWidgett"))); + }); + } + private void ConfigureDatabaseServices() { Configure(options => diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Index.cshtml b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Index.cshtml index 625c2b1709..45f826d4ff 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Index.cshtml +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Index.cshtml @@ -1,6 +1,8 @@ @page @using DashboardDemo.Pages +@using Volo.Abp.AspNetCore.Mvc.UI.Widgets @inherits DashboardDemoPageBase +@inject IWidgetRenderer WidgetRenderer @model IndexModel @section styles { @@ -8,12 +10,6 @@ @L["Welcome"] -

@L["LongWelcomeMessage"]

- @if (!CurrentUser.IsAuthenticated) - { -

@L["Login"]

- } -
-

abp.io

+ @await WidgetRenderer.RenderAsync(Component, "MyWidget")
\ No newline at end of file diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml new file mode 100644 index 0000000000..2c878e6820 --- /dev/null +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml @@ -0,0 +1,6 @@ +@page +@model DashboardDemo.Pages.widgets.MyWidgetViewComponentModel +@{ +} + +Hiiiiiii diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml.cs b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml.cs new file mode 100644 index 0000000000..1423eb167e --- /dev/null +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/MyWidgetViewComponent.cshtml.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DashboardDemo.Pages.widgets +{ + [ViewComponent] + public class MyWidgetViewComponentModel : ViewComponent + { + public IViewComponentResult Invoke() + { + return View("/Pages/widgets/MyWidgetViewComponent.cshtml", new MyWidgetViewComponentModel()); + } + } +} \ No newline at end of file