From d08442bfed262bc5db189dc88684e4c84cb6916c Mon Sep 17 00:00:00 2001 From: Nguyen Dac Thach Date: Tue, 14 Feb 2023 09:40:30 +0700 Subject: [PATCH] introduce AlloyContentAreaItemRenderer and refactor --- .../CustomizedRenderingInitialization.cs | 2 +- .../AlloyContentAreaItemRenderer.cs} | 20 ++++--- .../Rendering/AlloyContentAreaRenderer.cs | 55 ------------------- templates/Alloy.Mvc/Views/AlloyPageBase.cs | 27 +++++++++ .../Alloy.Mvc/Views/ProductPage/Index.cshtml | 2 +- .../Alloy.Mvc/Views/StandardPage/Index.cshtml | 2 +- .../Alloy.Mvc/Views/StartPage/Index.cshtml | 2 +- 7 files changed, 42 insertions(+), 68 deletions(-) rename templates/Alloy.Mvc/{Views/CustomContentAreaItemCssRenderingBase.cs => Business/Rendering/AlloyContentAreaItemRenderer.cs} (78%) delete mode 100644 templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaRenderer.cs create mode 100644 templates/Alloy.Mvc/Views/AlloyPageBase.cs diff --git a/templates/Alloy.Mvc/Business/Initialization/CustomizedRenderingInitialization.cs b/templates/Alloy.Mvc/Business/Initialization/CustomizedRenderingInitialization.cs index 3b7e3b0..9996c8f 100644 --- a/templates/Alloy.Mvc/Business/Initialization/CustomizedRenderingInitialization.cs +++ b/templates/Alloy.Mvc/Business/Initialization/CustomizedRenderingInitialization.cs @@ -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() - .AddTransient(); + .AddSingleton(); } public void Initialize(InitializationEngine context) => diff --git a/templates/Alloy.Mvc/Views/CustomContentAreaItemCssRenderingBase.cs b/templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaItemRenderer.cs similarity index 78% rename from templates/Alloy.Mvc/Views/CustomContentAreaItemCssRenderingBase.cs rename to templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaItemRenderer.cs index c364811..54fd9ba 100644 --- a/templates/Alloy.Mvc/Views/CustomContentAreaItemCssRenderingBase.cs +++ b/templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaItemRenderer.cs @@ -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 : RazorPage where TModel : class +public class AlloyContentAreaItemRenderer { - public abstract override Task ExecuteAsync(); + private readonly IContentAreaLoader _contentAreaLoader; + + public AlloyContentAreaItemRenderer(IContentAreaLoader contentAreaLoader) + { + _contentAreaLoader = contentAreaLoader; + } /// /// Gets a CSS class used for styling based on a tag name (ie a Bootstrap class name) @@ -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().LoadDisplayOption(contentAreaItem); + var displayOption = _contentAreaLoader.LoadDisplayOption(contentAreaItem); var cssClasses = new StringBuilder(); if (displayOption != null) diff --git a/templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaRenderer.cs b/templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaRenderer.cs deleted file mode 100644 index 46e5e5c..0000000 --- a/templates/Alloy.Mvc/Business/Rendering/AlloyContentAreaRenderer.cs +++ /dev/null @@ -1,55 +0,0 @@ -using EPiServer.Core.Html.StringParsing; -using EPiServer.Web.Mvc.Html; -using Microsoft.AspNetCore.Mvc.Rendering; -using static Alloy.Mvc._1.Globals; - -namespace Alloy.Mvc._1.Business.Rendering; - -/// -/// Extends the default to apply custom CSS classes to each . -/// -public class AlloyContentAreaRenderer : ContentAreaRenderer -{ - protected override string GetContentAreaItemCssClass(IHtmlHelper htmlHelper, ContentAreaItem contentAreaItem) - { - var baseItemClass = base.GetContentAreaItemCssClass(htmlHelper, contentAreaItem); - var tag = GetContentAreaItemTemplateTag(htmlHelper, contentAreaItem); - - return $"block {baseItemClass} {GetTypeSpecificCssClasses(contentAreaItem)} {GetCssClassForTag(tag)} {tag}"; - } - - /// - /// Gets a CSS class used for styling based on a tag name (ie a Bootstrap class name) - /// - /// Any tag name available, see - private static string GetCssClassForTag(string tagName) - { - if (string.IsNullOrEmpty(tagName)) - { - return string.Empty; - } - - return tagName.ToLowerInvariant() switch - { - ContentAreaTags.FullWidth => "col-12", - ContentAreaTags.WideWidth => "col-12 col-md-8", - ContentAreaTags.HalfWidth => "col-12 col-sm-6", - ContentAreaTags.NarrowWidth => "col-12 col-sm-6 col-md-4", - _ => string.Empty, - }; - } - - private static string GetTypeSpecificCssClasses(ContentAreaItem contentAreaItem) - { - var content = contentAreaItem.GetContent(); - var cssClass = content == null ? string.Empty : content.GetOriginalType().Name.ToLowerInvariant(); - - if (content is ICustomCssInContentArea customClassContent && - !string.IsNullOrWhiteSpace(customClassContent.ContentAreaCssClass)) - { - cssClass += $" {customClassContent.ContentAreaCssClass}"; - } - - return cssClass; - } -} diff --git a/templates/Alloy.Mvc/Views/AlloyPageBase.cs b/templates/Alloy.Mvc/Views/AlloyPageBase.cs new file mode 100644 index 0000000..cdb5606 --- /dev/null +++ b/templates/Alloy.Mvc/Views/AlloyPageBase.cs @@ -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 : RazorPage where TModel : class +{ + private readonly AlloyContentAreaItemRenderer _alloyContentAreaItemRenderer; + + public abstract override Task ExecuteAsync(); + + public AlloyPageBase() : this(ServiceLocator.Current.GetInstance()) + { + } + + public AlloyPageBase(AlloyContentAreaItemRenderer alloyContentAreaItemRenderer) + { + _alloyContentAreaItemRenderer = alloyContentAreaItemRenderer; + } + + protected void OnItemRendered(ContentAreaItem contentAreaItem, TagHelperContext context, TagHelperOutput output) + { + _alloyContentAreaItemRenderer.RenderContentAreaItemCss(contentAreaItem, context, output); + } +} diff --git a/templates/Alloy.Mvc/Views/ProductPage/Index.cshtml b/templates/Alloy.Mvc/Views/ProductPage/Index.cshtml index ae722df..1533b2f 100644 --- a/templates/Alloy.Mvc/Views/ProductPage/Index.cshtml +++ b/templates/Alloy.Mvc/Views/ProductPage/Index.cshtml @@ -1,4 +1,4 @@ -@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase> +@inherits Alloy.Mvc._1.Views.AlloyPageBase> @{ Layout = "~/Views/Shared/Layouts/_TwoPlusOne.cshtml"; } diff --git a/templates/Alloy.Mvc/Views/StandardPage/Index.cshtml b/templates/Alloy.Mvc/Views/StandardPage/Index.cshtml index 922429a..294f481 100644 --- a/templates/Alloy.Mvc/Views/StandardPage/Index.cshtml +++ b/templates/Alloy.Mvc/Views/StandardPage/Index.cshtml @@ -1,4 +1,4 @@ -@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase> +@inherits Alloy.Mvc._1.Views.AlloyPageBase> @{ Layout = "~/Views/Shared/Layouts/_LeftNavigation.cshtml"; } diff --git a/templates/Alloy.Mvc/Views/StartPage/Index.cshtml b/templates/Alloy.Mvc/Views/StartPage/Index.cshtml index e11a31c..a0b85f9 100644 --- a/templates/Alloy.Mvc/Views/StartPage/Index.cshtml +++ b/templates/Alloy.Mvc/Views/StartPage/Index.cshtml @@ -1,4 +1,4 @@ -@inherits Alloy.Mvc._1.Views.CustomContentAreaItemCssRenderingBase> +@inherits Alloy.Mvc._1.Views.AlloyPageBase>