Skip to content

Commit

Permalink
introduce AlloyContentAreaItemRenderer and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thng committed Feb 14, 2023
1 parent 348e1b5 commit d08442b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void ConfigureContainer(ServiceConfigurationContext context)
context.ConfigurationComplete += (o, e) =>
// Register custom implementations that should be used in favour of the default implementations
context.Services.AddTransient<IContentRenderer, ErrorHandlingContentRenderer>()
.AddTransient<ContentAreaRenderer, AlloyContentAreaRenderer>();
.AddSingleton<AlloyContentAreaItemRenderer, AlloyContentAreaItemRenderer>();
}

public void Initialize(InitializationEngine context) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using EPiServer.ServiceLocation;
using EPiServer.Web;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using static Alloy.Mvc._1.Globals;
using Alloy.Mvc._1.Business.Rendering;
using System.Text;

namespace Alloy.Mvc._1.Views;
namespace Alloy.Mvc._1.Business.Rendering;

public abstract class CustomContentAreaItemCssRenderingBase<TModel> : RazorPage<TModel> where TModel : class
public class AlloyContentAreaItemRenderer
{
public abstract override Task ExecuteAsync();
private readonly IContentAreaLoader _contentAreaLoader;

public AlloyContentAreaItemRenderer(IContentAreaLoader contentAreaLoader)
{
_contentAreaLoader = contentAreaLoader;
}

/// <summary>
/// Gets a CSS class used for styling based on a tag name (ie a Bootstrap class name)
Expand Down Expand Up @@ -48,9 +50,9 @@ private static string GetTypeSpecificCssClasses(ContentAreaItem contentAreaItem)
return cssClass;
}

protected void OnItemRendered(ContentAreaItem contentAreaItem, TagHelperContext context, TagHelperOutput output)
public void RenderContentAreaItemCss(ContentAreaItem contentAreaItem, TagHelperContext context, TagHelperOutput output)
{
var displayOption = ServiceLocator.Current.GetInstance<IContentAreaLoader>().LoadDisplayOption(contentAreaItem);
var displayOption = _contentAreaLoader.LoadDisplayOption(contentAreaItem);
var cssClasses = new StringBuilder();

if (displayOption != null)
Expand Down
55 changes: 0 additions & 55 deletions templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaRenderer.cs

This file was deleted.

27 changes: 27 additions & 0 deletions templates/Alloy.Mvc/Views/AlloyPageBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Alloy.Mvc._1.Business.Rendering;
using EPiServer.ServiceLocation;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Alloy.Mvc._1.Views;

public abstract class AlloyPageBase<TModel> : RazorPage<TModel> where TModel : class
{
private readonly AlloyContentAreaItemRenderer _alloyContentAreaItemRenderer;

public abstract override Task ExecuteAsync();

public AlloyPageBase() : this(ServiceLocator.Current.GetInstance<AlloyContentAreaItemRenderer>())
{
}

public AlloyPageBase(AlloyContentAreaItemRenderer alloyContentAreaItemRenderer)
{
_alloyContentAreaItemRenderer = alloyContentAreaItemRenderer;
}

protected void OnItemRendered(ContentAreaItem contentAreaItem, TagHelperContext context, TagHelperOutput output)
{
_alloyContentAreaItemRenderer.RenderContentAreaItemCss(contentAreaItem, context, output);
}
}
2 changes: 1 addition & 1 deletion templates/Alloy.Mvc/Views/ProductPage/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase<PageViewModel<ProductPage>>
@inherits Alloy.Mvc._1.Views.AlloyPageBase<PageViewModel<ProductPage>>

@{ Layout = "~/Views/Shared/Layouts/_TwoPlusOne.cshtml"; }

Expand Down
2 changes: 1 addition & 1 deletion templates/Alloy.Mvc/Views/StandardPage/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase<PageViewModel<Alloy.Mvc._1.Models.Pages.StandardPage>>
@inherits Alloy.Mvc._1.Views.AlloyPageBase<PageViewModel<Alloy.Mvc._1.Models.Pages.StandardPage>>

@{ Layout = "~/Views/Shared/Layouts/_LeftNavigation.cshtml"; }

Expand Down
2 changes: 1 addition & 1 deletion templates/Alloy.Mvc/Views/StartPage/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase<PageViewModel<Alloy.Mvc._1.Models.Pages.StartPage>>
@inherits Alloy.Mvc._1.Views.AlloyPageBase<PageViewModel<Alloy.Mvc._1.Models.Pages.StartPage>>

<div class="start">
<div epi-property="@Model.CurrentPage.MainContentArea" class="row">
Expand Down

0 comments on commit d08442b

Please sign in to comment.