skttl
2/3/2017 - 2:11 PM

Grid view & controls for Skybrud's strongly typed models with Responsive BP

Grid view & controls for Skybrud's strongly typed models with Responsive BP

@using Grid.Web
@using Skybrud.Umbraco.GridData
@inherits UmbracoViewPage<GridDataModel>
@*
    Razor helpers located at the bottom of this file
*@

@if (Model != null)
{
    if (Model.Sections.Any())
    {
        var oneColumn = ((System.Collections.ICollection)Model.Sections).Count == 1;

        if (oneColumn)
        {
            foreach (var section in Model.Sections)
            {
                <section class="gridview">
                    @foreach (var row in section.Rows)
                    {
                        @RenderRow(row, true)
                    }
                </section>
            }
        }
        else
        {
            <section class="row clearfix gridview">
                @foreach (var s in Model.Sections)
                {
                    <div class="col-s-@s.Grid">
                        @foreach (var row in s.Rows)
                        {
                            @RenderRow(row, false)
                        }
                    </div>
                }
            </section>
        }
    }
}

@helper RenderRow(GridRow row, bool singleColumn)
{
    <div class="row clearfix @row.Name.ToLower().Replace(' ', '-')" @row.RenderAttributes()>
        @foreach (var area in row.Areas)
        {
            if (@area.Controls.Any())
            {
                <div class="col-s-@area.Grid" @area.RenderAttributes()>
                    @foreach (var control in area.Controls)
                    {
                        if (control != null && control.Editor != null && control.Editor.View != null)
                        {
                            <text>@Html.Partial("grid/editors/base", control)</text>
                        }
                    }
                </div>
            }
        }
    </div>
}
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using Skybrud.Umbraco.GridData;

namespace Grid.Web
{
    public static class GridExtensions
    {
        public static string EditorView(this GridControl control)
        {
            string view = control.Editor.Render ?? control.Editor.View;
            view = view.ToLower().Replace(".html", ".cshtml");

            if (!view.Contains("/"))
            {
                view = "grid/editors/" + view;
            }

            return view;
        }

        public static MvcHtmlString RenderAttributes(this GridRow row)
        {
            var attrs = new List<string>();
            var cfg = row.Config.JObject;
            var style = row.Styles.JObject;

            if (cfg != null)
            { 
                attrs.AddRange(cfg.Properties().Select(property => property.Name + "=\"" + property.Value + "\""));
            }

            if (style != null)
            {
                var cssVals = style.Properties().Select(property => property.Name + ":" + property.Value + ";").ToList();

                if (cssVals.Any())
                    attrs.Add("style=\"" + string.Join(" ", cssVals) + "\"");
            }

            return new MvcHtmlString(string.Join(" ", attrs));
        }

        public static MvcHtmlString RenderAttributes(this GridArea area)
        {
            var attrs = new List<string>();
            var cfg = area.Config.JObject;
            var style = area.Styles.JObject;

            if (cfg != null) 
            { 
                attrs.AddRange(cfg.Properties().Select(property => property.Name + "=\"" + property.Value + "\""));
            }

            if (style != null)
            {
                var cssVals = style.Properties().Select(property => property.Name + ":" + property.Value + ";").ToList();

                if (cssVals.Any())
                    attrs.Add("style=\"" + string.Join(" ", cssVals) + "\"");
            }

            return new MvcHtmlString(string.Join(" ", attrs));
        }
    }
}
@model Skybrud.Umbraco.GridData.GridControl
@using Skybrud.Umbraco.GridData.Config
@using Skybrud.Umbraco.GridData.Values
@{
    var config = (GridEditorTextConfig)Model.Editor.Config;

    if (config.Markup != null)
    {
        var markup = config.Markup;
    
   
        markup = markup.Replace("#value#", Model.GetValue<GridControlTextValue>().Value);
        markup = markup.Replace("#style#", config.Style);

        <text>
            @Html.Raw(markup)
        </text>
    }
    else
    {
        <text>
            <div style="@config.Style">@(Model.GetValue<GridControlTextValue>().Value)</div>
        </text>
    }
}
@model Skybrud.Umbraco.GridData.GridControl
@using Skybrud.Umbraco.GridData.Values
@using Umbraco.Web.Templates

@Html.Raw(TemplateUtilities.ParseInternalLinks(Model.GetValue<GridControlRichTextValue>().Value))
@using Skybrud.Umbraco.GridData.Config
@using Skybrud.Umbraco.GridData.Values
@model Skybrud.Umbraco.GridData.GridControl

@if (Model.Value != null)
{
    var value = Model.GetValue<GridControlMediaValue>();
    
    var config = (GridEditorMediaConfig)Model.Editor.Config;
    
    var url = value.Image;
    if(config != null && config.Size != null)
    {
        url += "?width=" + config.Size.Width;
        url += "&height=" + config.Size.Height;

        if(value.FocalPoint != null){
            url += "&center=" + value.FocalPoint.Top +"," + value.FocalPoint.Left;
            url += "&mode=crop";
        }
    }
    
    <img src="@url" alt="@(value.JObject.GetValue("altText"))">
    
    if (value.Caption != null)
    {
        <p class="caption">@(value.Caption)</p>
    }
}
@using Skybrud.Umbraco.GridData.Values
@inherits UmbracoViewPage<Skybrud.Umbraco.GridData.GridControl>

@if (Model.Value != null)
{
    var value = Model.GetValue<GridControlMacroValue>();
    string macroAlias = value.MacroAlias;
    ViewDataDictionary parameters = new ViewDataDictionary();
    foreach (dynamic mpd in value.Parameters)
    {
        parameters.Add(mpd.Name, mpd.Value);
    }

    <text>
        @Umbraco.RenderMacro(macroAlias, parameters)
    </text>
}
@using Skybrud.Umbraco.GridData.Values
@model Skybrud.Umbraco.GridData.GridControl
@Html.Raw(Model.GetValue<GridControlEmbedValue>())
@model GridControl
@using Grid.Web
@using Skybrud.Umbraco.GridData

@try
{
    <text>@Html.Partial(Model.EditorView(), Model)</text>
}
catch (Exception ex) { 
    <pre>@ex.ToString()</pre>
}