mirror of https://github.com/abpframework/abp.git
10 changed files with 78 additions and 55 deletions
@ -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> widgetOptions, |
||||
|
IPageWidgetManager pageWidgetManager) |
||||
|
{ |
||||
|
DefaultViewComponentHelper = defaultViewComponentHelper; |
||||
|
PageWidgetManager = pageWidgetManager; |
||||
|
Options = widgetOptions.Value; |
||||
|
} |
||||
|
|
||||
|
public Task<IHtmlContent> 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<IHtmlContent> 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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<IHtmlContent> RenderAsync(IViewComponentHelper componentHelper, string widgetName, object args = 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> widgetOptions, IPageWidgetManager pageWidgetManager) |
|
||||
{ |
|
||||
_pageWidgetManager = pageWidgetManager; |
|
||||
_widgetOptions = widgetOptions.Value; |
|
||||
} |
|
||||
|
|
||||
public async Task<IHtmlContent> 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()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,6 +1,11 @@ |
|||||
@page |
@page |
||||
@using Volo.Abp.AspNetCore.Mvc.UI.Widgets |
@using DashboardDemo.Web.Pages.Components.MySimpleWidget |
||||
@model DashboardDemo.Web.Pages.MyWidgetsModel |
@model DashboardDemo.Web.Pages.MyWidgetsModel |
||||
@inject IWidgetRenderer WidgetRenderer |
|
||||
|
|
||||
@await WidgetRenderer.RenderAsync(Component, "MySimpleWidget") |
@await Component.InvokeAsync(typeof(MySimpleWidgetViewComponent)) |
||||
|
<hr /> |
||||
|
@await Component.InvokeAsync("MyCustomNameWidget") |
||||
|
<hr /> |
||||
|
<vc:my-custom-name-widget></vc:my-custom-name-widget> |
||||
|
@*<hr /> |
||||
|
@await WidgetRenderer.RenderAsync(Component, "MySimpleWidget")*@ |
||||
|
|||||
@ -1,4 +1,5 @@ |
|||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
||||
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI |
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI |
||||
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap |
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap |
||||
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling |
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling |
||||
|
@addTagHelper *, DashboardDemo.Web |
||||
|
|||||
Loading…
Reference in new issue