diff --git a/BootstrapAwareContentAreaRenderer.cs b/BootstrapAwareContentAreaRenderer.cs index 8083a4a..e422359 100644 --- a/BootstrapAwareContentAreaRenderer.cs +++ b/BootstrapAwareContentAreaRenderer.cs @@ -17,6 +17,13 @@ public class BootstrapAwareContentAreaRenderer : ContentAreaRenderer private static IEnumerable _fallbacks; private Action _elementStartTagRenderCallback; + public BootstrapAwareContentAreaRenderer() + { + ReadRegisteredDisplayModes(); + } + + public bool RowSupportEnabled { get; set; } + protected void SetElementStartTagRenderCallback(Action callback) { _elementStartTagRenderCallback = callback; @@ -34,6 +41,70 @@ protected override string GetContentAreaItemCssClass(HtmlHelper htmlHelper, Cont baseClasses); } + protected override void RenderContentAreaItems(HtmlHelper htmlHelper, IEnumerable contentAreaItems) + { + var isRowSupportedValue = htmlHelper.ViewContext.ViewData["rowsupport"]; + bool? isRowSupported = null; + + if (isRowSupportedValue is bool) + { + isRowSupported = (bool) isRowSupportedValue; + } + + var addRowMarkup = (!isRowSupported.HasValue && RowSupportEnabled) || (isRowSupported.HasValue ? isRowSupported.Value : false); + + // there is no need to proceed if row rendering support is disabled + if (!addRowMarkup) + { + base.RenderContentAreaItems(htmlHelper, contentAreaItems); + return; + } + + var items = contentAreaItems.ToList(); + var rowWidthState = 0; + var itemInfos = items.Select(item => + { + var tag = GetContentAreaItemTemplateTag(htmlHelper, item); + var columnWidth = GetColumnWidth(tag); + rowWidthState += columnWidth; + return new + { + ContentAreaItem = item, + Tag = tag, + ColumnWidth = columnWidth, + RowWidthState = rowWidthState, + RowNumber = rowWidthState%12 == 0 ? rowWidthState/12 - 1 : rowWidthState/12 + }; + }).ToList(); + + // if tags exists wrap items with row or not then use the default rendering + var tagExists = itemInfos.Any(ii => !string.IsNullOrEmpty(ii.Tag)); + if (!tagExists) + { + base.RenderContentAreaItems(htmlHelper, items); + return; + } + + var rows = itemInfos.GroupBy(a => a.RowNumber, a => a.ContentAreaItem); + foreach (var row in rows) + { + htmlHelper.ViewContext.Writer.Write("
"); + base.RenderContentAreaItems(htmlHelper, row); + htmlHelper.ViewContext.Writer.Write("
"); + } + } + + public static int GetColumnWidth(string tag) + { + var fallback = _fallbacks.FirstOrDefault(f => f.Tag == tag); + if (fallback == null) + { + return 12; + } + + return fallback.LargeScreenWidth; + } + protected override void RenderContentAreaItem( HtmlHelper htmlHelper, ContentAreaItem contentAreaItem, @@ -42,13 +113,12 @@ protected override void RenderContentAreaItem( string cssClass) { var originalWriter = htmlHelper.ViewContext.Writer; - var tempWriter = new StringWriter(); htmlHelper.ViewContext.Writer = tempWriter; - var content = contentAreaItem.GetContent(ContentRepository); try { + var content = contentAreaItem.GetContent(ContentRepository); base.RenderContentAreaItem(htmlHelper, contentAreaItem, templateTag, htmlTag, cssClass); var contentItemContent = tempWriter.ToString(); @@ -100,8 +170,6 @@ protected override void RenderContentAreaItem( private static string GetCssClassesForTag(string tagName) { - ReadRegisteredDisplayModes(); - if (string.IsNullOrWhiteSpace(tagName)) { tagName = ContentAreaTags.FullWidth; diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index bb6e8f5..ad3eba6 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -12,7 +12,7 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("0b623313-e88f-4f24-8240-66934f8e6b02")] -[assembly: AssemblyVersion("2.1.0.0")] -[assembly: AssemblyFileVersion("2.1.0.0")] -[assembly: AssemblyInformationalVersion("2.1.0")] +[assembly: AssemblyVersion("2.2.0.0")] +[assembly: AssemblyFileVersion("2.2.0.0")] +[assembly: AssemblyInformationalVersion("2.2.0")] [assembly: InternalsVisibleTo("EPiBootstrapArea.Tests")]