Skip to content

Commit d5bdf61

Browse files
committed
Make CSS configuration non-static
Remove C# 6 dependency in views
1 parent e90fb29 commit d5bdf61

12 files changed

+193
-119
lines changed

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Exceptions\DapperGriddlyException.cs" />
9292
<Compile Include="GriddlyContext.cs" />
9393
<Compile Include="GriddlyCookieFilterValueProvider.cs" />
94+
<Compile Include="GriddlyCss.cs" />
9495
<Compile Include="GriddlyFilterExtensions.cs" />
9596
<Compile Include="GriddlyHtmlFilter.cs" />
9697
<Compile Include="GriddlyExport.cs" />

Griddly.Mvc/GriddlyCss.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Griddly.Mvc
8+
{
9+
public class GriddlyCssIcons
10+
{
11+
public string Calendar { get; set; }
12+
public string Remove { get; set; }
13+
public string ListMultipleSelected { get; set; }
14+
public string ListSingleSelected { get; set; }
15+
public string Check { get; set; }
16+
public string Filter { get; set; }
17+
public string Clear { get; set; }
18+
public string CaretDown { get; set; }
19+
}
20+
21+
public struct GriddlyCss
22+
{
23+
public string TextCenter { get; set; }
24+
public string TextRight { get; set; }
25+
public string FloatRight { get; set; }
26+
public string GriddlyDefault { get; set; }
27+
public string TableDefault { get; set; }
28+
public string ButtonDefault { get; set; }
29+
public GriddlyCssIcons Icons { get; set; }
30+
public bool IsBootstrap4 { get; set; }
31+
32+
public static GriddlyCss Bootstrap3Defaults = new GriddlyCss()
33+
{
34+
TextCenter = "text-center",
35+
TextRight = "text-right",
36+
FloatRight = "pull-right",
37+
GriddlyDefault = null,
38+
TableDefault = "table table-bordered table-hover",
39+
ButtonDefault = "btn btn-default",
40+
Icons = new GriddlyCssIcons()
41+
{
42+
Calendar = "glyphicon glyphicon-calendar",
43+
Remove = "glyphicon glyphicon-remove",
44+
ListMultipleSelected = "glyphicon glyphicon-ok",
45+
ListSingleSelected = "glyphicon glyphicon-record",
46+
Check = "glyphicon glyphicon-check",
47+
Filter = "glyphicon glyphicon-filter",
48+
Clear = "glyphicon glyphicon-ban-circle",
49+
CaretDown = "caret"
50+
}
51+
};
52+
53+
public static GriddlyCss Bootstrap4Defaults = new GriddlyCss()
54+
{
55+
IsBootstrap4 = true,
56+
TextCenter = "text-center",
57+
TextRight = "text-right",
58+
FloatRight = "float-right",
59+
GriddlyDefault = null,
60+
TableDefault = "table table-bordered table-hover",
61+
ButtonDefault = "btn btn-outline-secondary",
62+
Icons = new GriddlyCssIcons()
63+
{
64+
Calendar = "fa fa-calendar-alt",
65+
Remove = "fa fa-times",
66+
ListMultipleSelected = "fa fa-check",
67+
ListSingleSelected = "fas fa-check-circle",
68+
Check = "fa fa-check-square",
69+
Filter = "fa fa-filter",
70+
Clear = "fa fa-ban",
71+
CaretDown = "fa fa-caret-down"
72+
}
73+
};
74+
}
75+
76+
}

Griddly.Mvc/GriddlyExport.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> e
5353
{
5454
var compiledTemplate = expression.Compile();
5555

56-
//if (type == typeof(bool) && (BoolTrueHtml != null || BoolFalseHtml != null))
57-
// template = (row) => (compiledTemplate(row) as bool? == true) ? BoolTrueHtml : BoolFalseHtml;
58-
//else
5956
template = (row) => compiledTemplate(row);
6057
}
6158
}

Griddly.Mvc/GriddlySelectColumn.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class GriddlySelectColumn : GriddlyColumn
1212
{
1313
public Func<object, bool> IsRowSelectable { get; set; }
1414

15-
public GriddlySelectColumn()
15+
public GriddlySelectColumn(GriddlySettings settings)
1616
{
17-
ClassName = $"griddly-select {GriddlySettings.Css.TextCenter}";
17+
ClassName = $"griddly-select {settings.Css.TextCenter}";
1818
}
1919

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

8282
public class GriddlySelectColumn<TRow> : GriddlySelectColumn
8383
{
84+
public GriddlySelectColumn(GriddlySettings<TRow> settings) : base(settings)
85+
{
86+
}
87+
8488
public Func<TRow, object> InputHtmlAttributesTemplate { get; set; }
8589

8690
public new Func<TRow, bool> IsRowSelectable

Griddly.Mvc/GriddlySettings.cs

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,29 @@ namespace Griddly.Mvc
1212
{
1313
public abstract class GriddlySettings: IGriddlyFilterSettings
1414
{
15-
public static class Css
16-
{
17-
public static string TextCenter = "text-center";
18-
public static string TextRight = "text-right";
19-
public static string FloatRight = "pull-right";
20-
public static string GriddlyDefault = null;
21-
public static string TableDefault = "table table-bordered table-hover";
22-
public static string ButtonDefault = "btn btn-default";
23-
24-
public static class Icons
25-
{
26-
public static string Calendar = "glyphicon glyphicon-calendar";
27-
public static string Remove = "glyphicon glyphicon-remove";
28-
public static string ListMultipleSelected = "glyphicon glyphicon-ok";
29-
public static string ListSingleSelected = "glyphicon glyphicon-record";
30-
public static string Check = "glyphicon glyphicon-check";
31-
public static string Filter = "glyphicon glyphicon-filter";
32-
public static string Clear = "glyphicon glyphicon-ban-circle";
33-
public static string CaretDown = "caret";
34-
}
35-
}
15+
public static GriddlyCss DefaultCss = GriddlyCss.Bootstrap3Defaults;
3616

17+
#region Obsolete shims retained only for backward compatibility
3718
[Obsolete("Use GriddlySettings.Css.GriddlyDefault")]
38-
public static string DefaultClassName { get => Css.GriddlyDefault; set => Css.GriddlyDefault = value; }
19+
public static string DefaultClassName { get => DefaultCss.GriddlyDefault; set => DefaultCss.GriddlyDefault = value; }
3920
[Obsolete("Use GriddlySettings.Css.TableDefault")]
40-
public static string DefaultTableClassName { get => Css.TableDefault; set => Css.TableDefault = value; }
21+
public static string DefaultTableClassName { get => DefaultCss.TableDefault; set => DefaultCss.TableDefault = value; }
4122
[Obsolete("Use GriddlySettings.Css.ButtonDefault")]
42-
public static string DefaultButtonClassName { get => Css.ButtonDefault; set => Css.ButtonDefault = value; }
43-
23+
public static string DefaultButtonClassName { get => DefaultCss.ButtonDefault; set => DefaultCss.ButtonDefault = value; }
24+
[Obsolete("Use Css.IsBootstrap4")]
25+
public static bool IsBootstrap4 => DefaultCss.IsBootstrap4;
26+
#endregion
27+
4428
public static string ButtonTemplate = "~/Views/Shared/Griddly/BootstrapButton.cshtml";
4529
public static string ButtonListTemplate = "~/Views/Shared/Griddly/ButtonStrip.cshtml";
46-
public static HtmlString BoolTrueHtml = null;
47-
public static HtmlString BoolFalseHtml = null;
30+
public static HtmlString DefaultBoolTrueHtml = null;
31+
public static HtmlString DefaultBoolFalseHtml = null;
4832
public static int? DefaultPageSize = null;
4933
public static FilterMode? DefaultInitialFilterMode = FilterMode.Form;
5034
//public static FilterMode? DefaultAllowedFilterModes = FilterMode.Inline;
5135
public static bool DefaultShowRowSelectCount = true;
5236
public static bool ExportCurrencySymbol = true;
5337
public static bool DisableHistoryParameters = false;
54-
public static bool IsBootstrap4 = false;
5538

5639
public static Func<GriddlyButton, object> IconTemplate = null;
5740
public static Func<GriddlyResultPage, object> DefaultFooterTemplate = null;
@@ -66,7 +49,7 @@ public static class Icons
6649
public static Action<GriddlySettings, GriddlyResultPage, HtmlHelper, bool> OnBeforeRender = null;
6750
public static Action<GriddlySettings, ControllerContext> OnGriddlyResultExecuting = null;
6851
public static Action<GriddlySettings, GriddlyContext, ControllerContext> OnGriddlyPageExecuting = null;
69-
52+
7053
public GriddlySettings()
7154
{
7255
IdProperty = "Id";
@@ -79,8 +62,8 @@ public GriddlySettings()
7962
HtmlAttributes = new RouteValueDictionary();
8063
TableHtmlAttributes = new RouteValueDictionary();
8164

82-
ClassName = Css.GriddlyDefault;
83-
TableClassName = Css.TableDefault;
65+
ClassName = DefaultCss.GriddlyDefault;
66+
TableClassName = DefaultCss.TableDefault;
8467
FooterTemplate = DefaultFooterTemplate;
8568
HeaderTemplate = DefaultHeaderTemplate;
8669
EmptyGridMessageTemplate = DefaultEmptyGridMessageTemplate;
@@ -93,22 +76,13 @@ public GriddlySettings()
9376

9477
public static void ConfigureBootstrap4Defaults()
9578
{
96-
IsBootstrap4 = true;
97-
Css.TextCenter = "text-center";
98-
Css.TextRight = "text-right";
99-
Css.FloatRight = "float-right";
100-
Css.ButtonDefault = "btn btn-outline-secondary";
101-
102-
Css.Icons.Calendar = "fa fa-calendar-alt";
103-
Css.Icons.Remove = "fa fa-times";
104-
Css.Icons.ListMultipleSelected = "fa fa-check";
105-
Css.Icons.ListSingleSelected = "fas fa-check-circle";
106-
Css.Icons.Check = "fa fa-check-square";
107-
Css.Icons.Filter = "fa fa-filter";
108-
Css.Icons.Clear = "fa fa-ban";
109-
Css.Icons.CaretDown = "fa fa-caret-down";
79+
DefaultCss = GriddlyCss.Bootstrap4Defaults;
11080
}
11181

82+
public GriddlyCss Css = DefaultCss;
83+
public HtmlString BoolTrueHtml = DefaultBoolTrueHtml;
84+
public HtmlString BoolFalseHtml = DefaultBoolFalseHtml;
85+
11286
public string[] DefaultRowIds { get; set; }
11387
public string IdProperty { get; set; }
11488
public string Title { get; set; }
@@ -362,7 +336,7 @@ public GriddlySettings SelectColumn(Expression<Func<object, object>> id, object
362336
{
363337
RowId(id, "id");
364338

365-
return Add(new GriddlySelectColumn()
339+
return Add(new GriddlySelectColumn(this)
366340
{
367341
SummaryValue = summaryValue
368342
});
@@ -375,7 +349,7 @@ public GriddlySettings SelectColumn(Dictionary<string, Func<object, object>> ids
375349
RowIds[x.Key] = x.Value;
376350
}
377351

378-
return Add(new GriddlySelectColumn()
352+
return Add(new GriddlySelectColumn(this)
379353
{
380354
SummaryValue = summaryValue
381355
});
@@ -557,7 +531,7 @@ public GriddlySettings<TRow> SelectColumn(Expression<Func<TRow, object>> id, obj
557531
{
558532
RowId(id, "id");
559533

560-
Add(new GriddlySelectColumn<TRow>()
534+
Add(new GriddlySelectColumn<TRow>(this)
561535
{
562536
SummaryValue = summaryValue,
563537
InputHtmlAttributesTemplate = inputHtmlAttributesTemplate
@@ -573,7 +547,7 @@ public GriddlySettings<TRow> SelectColumn(Dictionary<string, Func<TRow, object>>
573547
RowIds[x.Key] = (z) => x.Value((TRow)z);
574548
}
575549

576-
Add(new GriddlySelectColumn()
550+
Add(new GriddlySelectColumn(this)
577551
{
578552
SummaryValue = summaryValue
579553
});

Griddly/Views/Shared/Griddly/BootstrapButton.cshtml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@model GriddlyButton
1313
@{
1414
bool isListOnly = ViewBag.IsListOnly == true;
15+
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
1516
}
1617
@if (Model is GriddlyHtmlButton)
1718
{
@@ -29,14 +30,14 @@ else
2930
@RenderLink(Model, true, false)
3031
@if (Model.DropdownCaret == GriddlyDropdownCaret.Split)
3132
{
32-
<button type="button" class="dropdown-toggle @GriddlySettings.Css.ButtonDefault" data-toggle="dropdown">
33-
@if (!GriddlySettings.IsBootstrap4)
33+
<button type="button" class="dropdown-toggle @css.ButtonDefault" data-toggle="dropdown">
34+
@if (!css.IsBootstrap4)
3435
{
35-
<span class="@GriddlySettings.Css.Icons.CaretDown"></span>
36+
<span class="@css.Icons.CaretDown"></span>
3637
}
3738
</button>
3839
}
39-
@if (GriddlySettings.IsBootstrap4)
40+
@if (css.IsBootstrap4)
4041
{
4142
<div class="dropdown-menu @(Model.AlignRight ? "dropdown-menu-right float-right" : "")">
4243
@RenderList()
@@ -58,6 +59,8 @@ else
5859

5960
@helper RenderLink(GriddlyButton button, bool isDropdown, bool isMenuItem)
6061
{
62+
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
63+
6164
if (button.ArgumentTemplate != null)
6265
{
6366
button.Argument = button.ArgumentTemplate(ViewData["ResolveContext"]).ToString();
@@ -79,7 +82,7 @@ else
7982

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

82-
<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)"
85+
<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)"
8386
@*onclick="@(button.Action == GriddlyButtonAction.Javascript ? button.Argument : null)"*@
8487
title="@button.Title"
8588
@Html.AttributeIf("target", button.Action == GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
@@ -108,18 +111,20 @@ else
108111
}
109112
@button.Text
110113

111-
@if (isDropdown && button.DropdownCaret == GriddlyDropdownCaret.Inline && !GriddlySettings.IsBootstrap4)
114+
@if (isDropdown && button.DropdownCaret == GriddlyDropdownCaret.Inline && !css.IsBootstrap4)
112115
{
113-
<span class="@GriddlySettings.Css.Icons.CaretDown"></span>
116+
<span class="@css.Icons.CaretDown"></span>
114117
}
115118
</a>
116119
}
117120

118121
@helper RenderList()
119122
{
123+
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
124+
120125
foreach (GriddlyButton button in Model.Buttons)
121126
{
122-
if (GriddlySettings.IsBootstrap4)
127+
if (css.IsBootstrap4)
123128
{
124129
if (button.IsSeparator)
125130
{

Griddly/Views/Shared/Griddly/ButtonStrip.cshtml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
@{
4545
var leftGroups = GetGroups(Model.Where(x => !x.AlignRight && ViewBag.AlignRight != true));
4646
var rightGroups = GetGroups(Model.Where(x => x.AlignRight || ViewBag.AlignRight == true));
47+
48+
var css = (GriddlyCss?)ViewBag.GriddlyCss ?? GriddlySettings.DefaultCss;
4749
}
4850

4951
@if (Model.Any())
5052
{
5153
if (rightGroups.Any())
5254
{
53-
<div class="btn-toolbar @ViewBag.ClassName @(GriddlySettings.Css.FloatRight)">
55+
<div class="btn-toolbar @ViewBag.ClassName @(css.FloatRight)">
5456
@foreach (var group in rightGroups)
5557
{
5658
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
@@ -60,7 +62,7 @@
6062
foreach (GriddlyButton button in group)
6163
{
6264
button.AlignRight = true;
63-
@Html.Partial(GriddlySettings.ButtonTemplate, button);
65+
@Html.Partial(GriddlySettings.ButtonTemplate, button, new ViewDataDictionary() { { "GriddlyCss", css } });
6466
}
6567
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
6668
{
@@ -80,7 +82,7 @@
8082
}
8183
foreach (GriddlyButton button in group)
8284
{
83-
@Html.Partial(GriddlySettings.ButtonTemplate, button);
85+
@Html.Partial(GriddlySettings.ButtonTemplate, button, new ViewDataDictionary() { { "GriddlyCss", css } });
8486
}
8587
if (group.Count() > 1 || group.First().Buttons.Count() == 0)
8688
{

0 commit comments

Comments
 (0)