Skip to content

Commit

Permalink
Make CSS configuration non-static
Browse files Browse the repository at this point in the history
Remove C# 6 dependency in views
  • Loading branch information
jehhynes committed Sep 27, 2019
1 parent e90fb29 commit d5bdf61
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 119 deletions.
1 change: 1 addition & 0 deletions Griddly.Mvc/Griddly.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Exceptions\DapperGriddlyException.cs" />
<Compile Include="GriddlyContext.cs" />
<Compile Include="GriddlyCookieFilterValueProvider.cs" />
<Compile Include="GriddlyCss.cs" />
<Compile Include="GriddlyFilterExtensions.cs" />
<Compile Include="GriddlyHtmlFilter.cs" />
<Compile Include="GriddlyExport.cs" />
Expand Down
76 changes: 76 additions & 0 deletions Griddly.Mvc/GriddlyCss.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Griddly.Mvc
{
public class GriddlyCssIcons
{
public string Calendar { get; set; }
public string Remove { get; set; }
public string ListMultipleSelected { get; set; }
public string ListSingleSelected { get; set; }
public string Check { get; set; }
public string Filter { get; set; }
public string Clear { get; set; }
public string CaretDown { get; set; }
}

public struct GriddlyCss
{
public string TextCenter { get; set; }
public string TextRight { get; set; }
public string FloatRight { get; set; }
public string GriddlyDefault { get; set; }
public string TableDefault { get; set; }
public string ButtonDefault { get; set; }
public GriddlyCssIcons Icons { get; set; }
public bool IsBootstrap4 { get; set; }

public static GriddlyCss Bootstrap3Defaults = new GriddlyCss()
{
TextCenter = "text-center",
TextRight = "text-right",
FloatRight = "pull-right",
GriddlyDefault = null,
TableDefault = "table table-bordered table-hover",
ButtonDefault = "btn btn-default",
Icons = new GriddlyCssIcons()
{
Calendar = "glyphicon glyphicon-calendar",
Remove = "glyphicon glyphicon-remove",
ListMultipleSelected = "glyphicon glyphicon-ok",
ListSingleSelected = "glyphicon glyphicon-record",
Check = "glyphicon glyphicon-check",
Filter = "glyphicon glyphicon-filter",
Clear = "glyphicon glyphicon-ban-circle",
CaretDown = "caret"
}
};

public static GriddlyCss Bootstrap4Defaults = new GriddlyCss()
{
IsBootstrap4 = true,
TextCenter = "text-center",
TextRight = "text-right",
FloatRight = "float-right",
GriddlyDefault = null,
TableDefault = "table table-bordered table-hover",
ButtonDefault = "btn btn-outline-secondary",
Icons = new GriddlyCssIcons()
{
Calendar = "fa fa-calendar-alt",
Remove = "fa fa-times",
ListMultipleSelected = "fa fa-check",
ListSingleSelected = "fas fa-check-circle",
Check = "fa fa-check-square",
Filter = "fa fa-filter",
Clear = "fa fa-ban",
CaretDown = "fa fa-caret-down"
}
};
}

}
3 changes: 0 additions & 3 deletions Griddly.Mvc/GriddlyExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> e
{
var compiledTemplate = expression.Compile();

//if (type == typeof(bool) && (BoolTrueHtml != null || BoolFalseHtml != null))
// template = (row) => (compiledTemplate(row) as bool? == true) ? BoolTrueHtml : BoolFalseHtml;
//else
template = (row) => compiledTemplate(row);
}
}
Expand Down
8 changes: 6 additions & 2 deletions Griddly.Mvc/GriddlySelectColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class GriddlySelectColumn : GriddlyColumn
{
public Func<object, bool> IsRowSelectable { get; set; }

public GriddlySelectColumn()
public GriddlySelectColumn(GriddlySettings settings)
{
ClassName = $"griddly-select {GriddlySettings.Css.TextCenter}";
ClassName = $"griddly-select {settings.Css.TextCenter}";
}

public virtual IDictionary<string, object> GenerateInputHtmlAttributes(object row)
Expand Down Expand Up @@ -81,6 +81,10 @@ public override HtmlString RenderUnderlyingValue(object row)

public class GriddlySelectColumn<TRow> : GriddlySelectColumn
{
public GriddlySelectColumn(GriddlySettings<TRow> settings) : base(settings)
{
}

public Func<TRow, object> InputHtmlAttributesTemplate { get; set; }

public new Func<TRow, bool> IsRowSelectable
Expand Down
72 changes: 23 additions & 49 deletions Griddly.Mvc/GriddlySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,29 @@ namespace Griddly.Mvc
{
public abstract class GriddlySettings: IGriddlyFilterSettings
{
public static class Css
{
public static string TextCenter = "text-center";
public static string TextRight = "text-right";
public static string FloatRight = "pull-right";
public static string GriddlyDefault = null;
public static string TableDefault = "table table-bordered table-hover";
public static string ButtonDefault = "btn btn-default";

public static class Icons
{
public static string Calendar = "glyphicon glyphicon-calendar";
public static string Remove = "glyphicon glyphicon-remove";
public static string ListMultipleSelected = "glyphicon glyphicon-ok";
public static string ListSingleSelected = "glyphicon glyphicon-record";
public static string Check = "glyphicon glyphicon-check";
public static string Filter = "glyphicon glyphicon-filter";
public static string Clear = "glyphicon glyphicon-ban-circle";
public static string CaretDown = "caret";
}
}
public static GriddlyCss DefaultCss = GriddlyCss.Bootstrap3Defaults;

#region Obsolete shims retained only for backward compatibility
[Obsolete("Use GriddlySettings.Css.GriddlyDefault")]
public static string DefaultClassName { get => Css.GriddlyDefault; set => Css.GriddlyDefault = value; }
public static string DefaultClassName { get => DefaultCss.GriddlyDefault; set => DefaultCss.GriddlyDefault = value; }
[Obsolete("Use GriddlySettings.Css.TableDefault")]
public static string DefaultTableClassName { get => Css.TableDefault; set => Css.TableDefault = value; }
public static string DefaultTableClassName { get => DefaultCss.TableDefault; set => DefaultCss.TableDefault = value; }
[Obsolete("Use GriddlySettings.Css.ButtonDefault")]
public static string DefaultButtonClassName { get => Css.ButtonDefault; set => Css.ButtonDefault = value; }

public static string DefaultButtonClassName { get => DefaultCss.ButtonDefault; set => DefaultCss.ButtonDefault = value; }
[Obsolete("Use Css.IsBootstrap4")]
public static bool IsBootstrap4 => DefaultCss.IsBootstrap4;
#endregion

public static string ButtonTemplate = "~/Views/Shared/Griddly/BootstrapButton.cshtml";
public static string ButtonListTemplate = "~/Views/Shared/Griddly/ButtonStrip.cshtml";
public static HtmlString BoolTrueHtml = null;
public static HtmlString BoolFalseHtml = null;
public static HtmlString DefaultBoolTrueHtml = null;
public static HtmlString DefaultBoolFalseHtml = null;
public static int? DefaultPageSize = null;
public static FilterMode? DefaultInitialFilterMode = FilterMode.Form;
//public static FilterMode? DefaultAllowedFilterModes = FilterMode.Inline;
public static bool DefaultShowRowSelectCount = true;
public static bool ExportCurrencySymbol = true;
public static bool DisableHistoryParameters = false;
public static bool IsBootstrap4 = false;

public static Func<GriddlyButton, object> IconTemplate = null;
public static Func<GriddlyResultPage, object> DefaultFooterTemplate = null;
Expand All @@ -66,7 +49,7 @@ public static class Icons
public static Action<GriddlySettings, GriddlyResultPage, HtmlHelper, bool> OnBeforeRender = null;
public static Action<GriddlySettings, ControllerContext> OnGriddlyResultExecuting = null;
public static Action<GriddlySettings, GriddlyContext, ControllerContext> OnGriddlyPageExecuting = null;

public GriddlySettings()
{
IdProperty = "Id";
Expand All @@ -79,8 +62,8 @@ public GriddlySettings()
HtmlAttributes = new RouteValueDictionary();
TableHtmlAttributes = new RouteValueDictionary();

ClassName = Css.GriddlyDefault;
TableClassName = Css.TableDefault;
ClassName = DefaultCss.GriddlyDefault;
TableClassName = DefaultCss.TableDefault;
FooterTemplate = DefaultFooterTemplate;
HeaderTemplate = DefaultHeaderTemplate;
EmptyGridMessageTemplate = DefaultEmptyGridMessageTemplate;
Expand All @@ -93,22 +76,13 @@ public GriddlySettings()

public static void ConfigureBootstrap4Defaults()
{
IsBootstrap4 = true;
Css.TextCenter = "text-center";
Css.TextRight = "text-right";
Css.FloatRight = "float-right";
Css.ButtonDefault = "btn btn-outline-secondary";

Css.Icons.Calendar = "fa fa-calendar-alt";
Css.Icons.Remove = "fa fa-times";
Css.Icons.ListMultipleSelected = "fa fa-check";
Css.Icons.ListSingleSelected = "fas fa-check-circle";
Css.Icons.Check = "fa fa-check-square";
Css.Icons.Filter = "fa fa-filter";
Css.Icons.Clear = "fa fa-ban";
Css.Icons.CaretDown = "fa fa-caret-down";
DefaultCss = GriddlyCss.Bootstrap4Defaults;
}

public GriddlyCss Css = DefaultCss;
public HtmlString BoolTrueHtml = DefaultBoolTrueHtml;
public HtmlString BoolFalseHtml = DefaultBoolFalseHtml;

public string[] DefaultRowIds { get; set; }
public string IdProperty { get; set; }
public string Title { get; set; }
Expand Down Expand Up @@ -362,7 +336,7 @@ public GriddlySettings SelectColumn(Expression<Func<object, object>> id, object
{
RowId(id, "id");

return Add(new GriddlySelectColumn()
return Add(new GriddlySelectColumn(this)
{
SummaryValue = summaryValue
});
Expand All @@ -375,7 +349,7 @@ public GriddlySettings SelectColumn(Dictionary<string, Func<object, object>> ids
RowIds[x.Key] = x.Value;
}

return Add(new GriddlySelectColumn()
return Add(new GriddlySelectColumn(this)
{
SummaryValue = summaryValue
});
Expand Down Expand Up @@ -557,7 +531,7 @@ public GriddlySettings<TRow> SelectColumn(Expression<Func<TRow, object>> id, obj
{
RowId(id, "id");

Add(new GriddlySelectColumn<TRow>()
Add(new GriddlySelectColumn<TRow>(this)
{
SummaryValue = summaryValue,
InputHtmlAttributesTemplate = inputHtmlAttributesTemplate
Expand All @@ -573,7 +547,7 @@ public GriddlySettings<TRow> SelectColumn(Dictionary<string, Func<TRow, object>>
RowIds[x.Key] = (z) => x.Value((TRow)z);
}

Add(new GriddlySelectColumn()
Add(new GriddlySelectColumn(this)
{
SummaryValue = summaryValue
});
Expand Down
21 changes: 13 additions & 8 deletions Griddly/Views/Shared/Griddly/BootstrapButton.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@model GriddlyButton
@{
bool isListOnly = ViewBag.IsListOnly == true;
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
}
@if (Model is GriddlyHtmlButton)
{
Expand All @@ -29,14 +30,14 @@ else
@RenderLink(Model, true, false)
@if (Model.DropdownCaret == GriddlyDropdownCaret.Split)
{
<button type="button" class="dropdown-toggle @GriddlySettings.Css.ButtonDefault" data-toggle="dropdown">
@if (!GriddlySettings.IsBootstrap4)
<button type="button" class="dropdown-toggle @css.ButtonDefault" data-toggle="dropdown">
@if (!css.IsBootstrap4)
{
<span class="@GriddlySettings.Css.Icons.CaretDown"></span>
<span class="@css.Icons.CaretDown"></span>
}
</button>
}
@if (GriddlySettings.IsBootstrap4)
@if (css.IsBootstrap4)
{
<div class="dropdown-menu @(Model.AlignRight ? "dropdown-menu-right float-right" : "")">
@RenderList()
Expand All @@ -58,6 +59,8 @@ else

@helper RenderLink(GriddlyButton button, bool isDropdown, bool isMenuItem)
{
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;

if (button.ArgumentTemplate != null)
{
button.Argument = button.ArgumentTemplate(ViewData["ResolveContext"]).ToString();
Expand All @@ -79,7 +82,7 @@ else

bool clearSelectionOnAction = button.ClearSelectionOnAction ?? (button.Action == GriddlyButtonAction.Ajax || button.Action == GriddlyButtonAction.AjaxBulk);

<a data-role="griddly-button" class="@(!isMenuItem ? GriddlySettings.Css.ButtonDefault : null) @button.ClassName @(isDropdown && button.DropdownCaret != GriddlyDropdownCaret.Split ? "dropdown-toggle" : null) @(!button.Enabled || button.EnableOnSelection == true ? "disabled" : null) @(!string.IsNullOrWhiteSpace(button.Icon) ? "btn-with-icon" : null) @(isMenuItem && GriddlySettings.IsBootstrap4 ? "dropdown-item" : null)"
<a data-role="griddly-button" class="@(!isMenuItem ? css.ButtonDefault : null) @button.ClassName @(isDropdown && button.DropdownCaret != GriddlyDropdownCaret.Split ? "dropdown-toggle" : null) @(!button.Enabled || button.EnableOnSelection == true ? "disabled" : null) @(!string.IsNullOrWhiteSpace(button.Icon) ? "btn-with-icon" : null) @(isMenuItem && css.IsBootstrap4 ? "dropdown-item" : null)"
@*onclick="@(button.Action == GriddlyButtonAction.Javascript ? button.Argument : null)"*@
title="@button.Title"
@Html.AttributeIf("target", button.Action == GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
Expand Down Expand Up @@ -108,18 +111,20 @@ else
}
@button.Text

@if (isDropdown && button.DropdownCaret == GriddlyDropdownCaret.Inline && !GriddlySettings.IsBootstrap4)
@if (isDropdown && button.DropdownCaret == GriddlyDropdownCaret.Inline && !css.IsBootstrap4)
{
<span class="@GriddlySettings.Css.Icons.CaretDown"></span>
<span class="@css.Icons.CaretDown"></span>
}
</a>
}

@helper RenderList()
{
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;

foreach (GriddlyButton button in Model.Buttons)
{
if (GriddlySettings.IsBootstrap4)
if (css.IsBootstrap4)
{
if (button.IsSeparator)
{
Expand Down
8 changes: 5 additions & 3 deletions Griddly/Views/Shared/Griddly/ButtonStrip.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
@{
var leftGroups = GetGroups(Model.Where(x => !x.AlignRight && ViewBag.AlignRight != true));
var rightGroups = GetGroups(Model.Where(x => x.AlignRight || ViewBag.AlignRight == true));

var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
}

@if (Model.Any())
{
if (rightGroups.Any())
{
<div class="btn-toolbar @ViewBag.ClassName @(GriddlySettings.Css.FloatRight)">
<div class="btn-toolbar @ViewBag.ClassName @(css.FloatRight)">
@foreach (var group in rightGroups)
{
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
Expand All @@ -60,7 +62,7 @@
foreach (GriddlyButton button in group)
{
button.AlignRight = true;
@Html.Partial(GriddlySettings.ButtonTemplate, button);
@Html.Partial(GriddlySettings.ButtonTemplate, button, new ViewDataDictionary() { { "GriddlyCss", css } });
}
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
{
Expand All @@ -80,7 +82,7 @@
}
foreach (GriddlyButton button in group)
{
@Html.Partial(GriddlySettings.ButtonTemplate, button);
@Html.Partial(GriddlySettings.ButtonTemplate, button, new ViewDataDictionary() { { "GriddlyCss", css } });
}
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
{
Expand Down
Loading

0 comments on commit d5bdf61

Please sign in to comment.