mirror of https://github.com/abpframework/abp.git
12 changed files with 179 additions and 3 deletions
@ -0,0 +1,9 @@ |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
@model LayoutHookViewModel |
|||
@if (Model.Hooks.Any()) |
|||
{ |
|||
foreach (var hook in Model.Hooks) |
|||
{ |
|||
<text>@await Component.InvokeAsync(hook.ComponentType, new { layout = Model.Layout })</text> |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public class LayoutHookInfo |
|||
{ |
|||
/// <summary>
|
|||
/// ViewComponent type.
|
|||
/// </summary>
|
|||
public Type ComponentType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Specifies the layout name to apply this hook.
|
|||
/// null indicates that this hook will be applied to all layouts.
|
|||
/// </summary>
|
|||
public string Layout { get; } |
|||
|
|||
public LayoutHookInfo(Type componentType, string layout = null) |
|||
{ |
|||
ComponentType = componentType; |
|||
Layout = layout; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public class LayoutHookOptions |
|||
{ |
|||
public IDictionary<string, List<LayoutHookInfo>> Hooks { get; } |
|||
|
|||
public LayoutHookOptions() |
|||
{ |
|||
Hooks = new Dictionary<string, List<LayoutHookInfo>>(); |
|||
} |
|||
|
|||
public LayoutHookOptions Add(string name, Type componentType, string layout = null) |
|||
{ |
|||
Hooks |
|||
.GetOrAdd(name, () => new List<LayoutHookInfo>()) |
|||
.Add(new LayoutHookInfo(componentType, layout)); |
|||
|
|||
return this; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public class LayoutHookViewComponent : AbpViewComponent |
|||
{ |
|||
protected LayoutHookOptions Options { get; } |
|||
|
|||
public LayoutHookViewComponent(IOptions<LayoutHookOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual IViewComponentResult Invoke(string name, string layout) |
|||
{ |
|||
var hooks = Options.Hooks.GetOrDefault(name)?.ToArray() ?? Array.Empty<LayoutHookInfo>(); |
|||
|
|||
return View( |
|||
"~/Volo/Abp/AspNetCore/Mvc/UI/Components/LayoutHook/Default.cshtml", |
|||
new LayoutHookViewModel(hooks, layout) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public class LayoutHookViewModel |
|||
{ |
|||
public LayoutHookInfo[] Hooks { get; } |
|||
|
|||
public string Layout { get; } |
|||
|
|||
public LayoutHookViewModel(LayoutHookInfo[] hooks, string layout) |
|||
{ |
|||
Hooks = hooks; |
|||
Layout = layout; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public static class LayoutHooks |
|||
{ |
|||
public static class Head |
|||
{ |
|||
public const string First = "Header.First"; |
|||
public const string Last = "Header.Last"; |
|||
} |
|||
|
|||
public static class Body |
|||
{ |
|||
public const string First = "Body.First"; |
|||
public const string Last = "Body.Last"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Html; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook |
|||
{ |
|||
public static class ViewComponentHelperLayoutHookExtensions |
|||
{ |
|||
public static Task<IHtmlContent> InvokeLayoutHookAsync( |
|||
this IViewComponentHelper componentHelper, |
|||
string name, |
|||
string layout) |
|||
{ |
|||
return componentHelper.InvokeAsync( |
|||
typeof(LayoutHookViewComponent), |
|||
new |
|||
{ |
|||
name = name, |
|||
layout = layout |
|||
} |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue