Skip to content

Commit

Permalink
Added row support. This closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Sep 19, 2015
1 parent 42bc5f9 commit 06ad16a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
76 changes: 72 additions & 4 deletions BootstrapAwareContentAreaRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public class BootstrapAwareContentAreaRenderer : ContentAreaRenderer
private static IEnumerable<DisplayModeFallback> _fallbacks;
private Action<HtmlNode, ContentAreaItem, IContent> _elementStartTagRenderCallback;

public BootstrapAwareContentAreaRenderer()
{
ReadRegisteredDisplayModes();
}

public bool RowSupportEnabled { get; set; }

protected void SetElementStartTagRenderCallback(Action<HtmlNode, ContentAreaItem, IContent> callback)
{
_elementStartTagRenderCallback = callback;
Expand All @@ -34,6 +41,70 @@ protected override string GetContentAreaItemCssClass(HtmlHelper htmlHelper, Cont
baseClasses);
}

protected override void RenderContentAreaItems(HtmlHelper htmlHelper, IEnumerable<ContentAreaItem> 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("<div class=\"row row" + row.Key + "\">");
base.RenderContentAreaItems(htmlHelper, row);
htmlHelper.ViewContext.Writer.Write("</div>");
}
}

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,
Expand All @@ -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();

Expand Down Expand Up @@ -100,8 +170,6 @@ protected override void RenderContentAreaItem(

private static string GetCssClassesForTag(string tagName)
{
ReadRegisteredDisplayModes();

if (string.IsNullOrWhiteSpace(tagName))
{
tagName = ContentAreaTags.FullWidth;
Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit 06ad16a

Please sign in to comment.