From fb9b39452f790227092dcc1ab09cb95502347ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 20 Jul 2019 19:14:01 +0300 Subject: [PATCH] Remove WidgetRenreder and decorate DefaultViewComponentHelper instead. --- .../Components/Dashboard/Default.cshtml | 3 +- .../AbpAspNetCoreMvcUiWidgetsModule.cs | 6 +- .../Mvc/UI/Widgets/AbpViewComponentHelper.cs | 58 +++++++++++++++++++ .../Mvc/UI/Widgets/IWidgetRenderer.cs | 12 ---- .../Mvc/UI/Widgets/WidgetRenderer.cs | 27 --------- .../DashboardDemoWebModule.cs | 2 +- .../MySimpleWidgetViewComponent.cs | 6 +- .../DashboardDemo.Web/Pages/MyWidgets.cshtml | 11 +++- .../Pages/MyWidgets.cshtml.cs | 5 -- .../Pages/_ViewImports.cshtml | 3 +- 10 files changed, 78 insertions(+), 55 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml index 65e694b66d..b077947700 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml @@ -5,7 +5,6 @@ @using Volo.Abp.AspNetCore.Mvc.UI.Dashboards @using Volo.Abp.AspNetCore.Mvc.UI.Widgets @inject IAuthorizationService AuthorizationService -@inject IWidgetRenderer WidgetRenderer @inject IGlobalFilterRenderer GlobalFilteRenderer @inject IHtmlLocalizer L @model Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard.DashboardViewModel @@ -47,7 +46,7 @@ - @await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name) + @await Component.InvokeAsync(widgetDefinition.Name) } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpAspNetCoreMvcUiWidgetsModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpAspNetCoreMvcUiWidgetsModule.cs index 8f2d205cad..ebc021f5d9 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpAspNetCoreMvcUiWidgetsModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpAspNetCoreMvcUiWidgetsModule.cs @@ -1,4 +1,6 @@ -using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; +using Microsoft.AspNetCore.Mvc.ViewComponents; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.Modularity; using Volo.Abp.VirtualFileSystem; @@ -13,6 +15,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.AddTransient(); + Configure(options => { options.FileSets.AddEmbedded(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs new file mode 100644 index 0000000000..8d477c637d --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/AbpViewComponentHelper.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.ViewComponents; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets +{ + [Dependency(ReplaceServices = true)] + public class AbpViewComponentHelper : IViewComponentHelper, IViewContextAware, ITransientDependency + { + protected WidgetOptions Options { get; } + protected IPageWidgetManager PageWidgetManager { get; } + protected DefaultViewComponentHelper DefaultViewComponentHelper { get; } + + public AbpViewComponentHelper( + DefaultViewComponentHelper defaultViewComponentHelper, + IOptions widgetOptions, + IPageWidgetManager pageWidgetManager) + { + DefaultViewComponentHelper = defaultViewComponentHelper; + PageWidgetManager = pageWidgetManager; + Options = widgetOptions.Value; + } + + public Task InvokeAsync(string name, object arguments) + { + var widget = Options.Widgets.FirstOrDefault(w => w.Name == name); //Optimize using a dictionary by name + if (widget != null) + { + PageWidgetManager.TryAdd(widget); + } + + return DefaultViewComponentHelper.InvokeAsync(name, arguments); + } + + public Task InvokeAsync(Type componentType, object arguments) + { + var widget = Options.Widgets.FirstOrDefault(w => w.ViewComponentType == componentType); //Optimize using a dictionary by type + if (widget != null) + { + PageWidgetManager.TryAdd(widget); + } + + return DefaultViewComponentHelper.InvokeAsync(componentType, arguments); + } + + public void Contextualize(ViewContext viewContext) + { + DefaultViewComponentHelper.Contextualize(viewContext); + } + } +} \ No newline at end of file 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 deleted file mode 100644 index ab3c4ee09f..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/IWidgetRenderer.cs +++ /dev/null @@ -1,12 +0,0 @@ -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(IViewComponentHelper componentHelper, string widgetName, object args = null); - } -} 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 deleted file mode 100644 index c6c7b059d8..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs +++ /dev/null @@ -1,27 +0,0 @@ -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; - private readonly IPageWidgetManager _pageWidgetManager; - - public WidgetRenderer(IOptions widgetOptions, IPageWidgetManager pageWidgetManager) - { - _pageWidgetManager = pageWidgetManager; - _widgetOptions = widgetOptions.Value; - } - - public async Task RenderAsync(IViewComponentHelper componentHelper, string widgetName, object args = null) - { - var widget = _widgetOptions.Widgets.Single(w=>w.Name.Equals(widgetName)); - _pageWidgetManager.TryAdd(widget); - return await componentHelper.InvokeAsync(widget.ViewComponentType, args ?? new object()); - } - } -} \ No newline at end of file diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs b/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs index 9bd9b9602b..a297b8fddb 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Web/DashboardDemoWebModule.cs @@ -178,7 +178,7 @@ namespace DashboardDemo.Web { options.Widgets.Add( new WidgetDefinition( - "MySimpleWidget", + "MyCustomNameWidget", typeof(MySimpleWidgetViewComponent)) .WithStyles("/Pages/Components/MySimpleWidget/Default.css") .WithScripts("/Pages/Components/MySimpleWidget/Default.js") diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Components/MySimpleWidget/MySimpleWidgetViewComponent.cs b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Components/MySimpleWidget/MySimpleWidgetViewComponent.cs index 73ec57e8f0..da59ba1966 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Components/MySimpleWidget/MySimpleWidgetViewComponent.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/Components/MySimpleWidget/MySimpleWidgetViewComponent.cs @@ -5,12 +5,12 @@ using Volo.Abp.AspNetCore.Mvc.UI.Bundling; namespace DashboardDemo.Web.Pages.Components.MySimpleWidget { - + [ViewComponent(Name = "MyCustomNameWidget")] public class MySimpleWidgetViewComponent : AbpViewComponent { public IViewComponentResult Invoke() { - return View(); + return View("~/Pages/Components/MySimpleWidget/Default.cshtml"); } } @@ -18,7 +18,7 @@ namespace DashboardDemo.Web.Pages.Components.MySimpleWidget { public override void ConfigureBundle(BundleConfigurationContext context) { - context.Files.AddIfNotContains("~/Pages/Components/MySimpleWidget/Default.css"); + context.Files.AddIfNotContains("/Pages/Components/MySimpleWidget/Default.css"); } } } diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml index 8fb0d776cd..568d04ac9f 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml @@ -1,6 +1,11 @@ @page -@using Volo.Abp.AspNetCore.Mvc.UI.Widgets +@using DashboardDemo.Web.Pages.Components.MySimpleWidget @model DashboardDemo.Web.Pages.MyWidgetsModel -@inject IWidgetRenderer WidgetRenderer -@await WidgetRenderer.RenderAsync(Component, "MySimpleWidget") +@await Component.InvokeAsync(typeof(MySimpleWidgetViewComponent)) +
+@await Component.InvokeAsync("MyCustomNameWidget") +
+ +@*
+@await WidgetRenderer.RenderAsync(Component, "MySimpleWidget")*@ diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml.cs b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml.cs index bd26191de0..d119e37d09 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/MyWidgets.cshtml.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace DashboardDemo.Web.Pages diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/_ViewImports.cshtml b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/_ViewImports.cshtml index c1da1f5f10..9104382b7c 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/_ViewImports.cshtml +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/_ViewImports.cshtml @@ -1,4 +1,5 @@ @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI @addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap -@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling \ No newline at end of file +@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling +@addTagHelper *, DashboardDemo.Web