Skip to content

Commit b6522e3

Browse files
committed
Smooth out parameter handling #93
1 parent 1063ba4 commit b6522e3

28 files changed

+458
-69
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("2.5.1")]
19-
[assembly: AssemblyFileVersion("2.5.1")]
18+
[assembly: AssemblyVersion("2.6.0")]
19+
[assembly: AssemblyFileVersion("2.6.0")]
2020
//[assembly: AssemblyInformationalVersion("2.5-filters")]

Build/Griddly.Core.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<dependency id="CsvHelper" version="2.4.0" />
1818
<dependency id="Dapper" version="1.50.2" />
1919
<dependency id="EPPlus" version="3.1.3.3" />
20+
<dependency id="Newtonsoft.Json" version="11.0.2" />
2021
</group>
2122
</dependencies>
2223
</metadata>

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
<Private>True</Private>
5151
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
5252
</Reference>
53+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
54+
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
55+
</Reference>
5356
<Reference Include="System" />
5457
<Reference Include="System.ComponentModel.DataAnnotations" />
5558
<Reference Include="System.Core" />
@@ -91,10 +94,13 @@
9194
</Compile>
9295
<Compile Include="DynamicLinq.cs" />
9396
<Compile Include="Exceptions\DapperGriddlyException.cs" />
97+
<Compile Include="GriddlyContext.cs" />
98+
<Compile Include="GriddlyCookieFilterValueProvider.cs" />
9499
<Compile Include="GriddlyFilterExtensions.cs" />
95100
<Compile Include="GriddlyHtmlFilter.cs" />
96101
<Compile Include="GriddlyExport.cs" />
97102
<Compile Include="GriddlyFilterBarSettings.cs" />
103+
<Compile Include="GriddlyParameterAttribute.cs" />
98104
<Compile Include="InternalExtensions.cs" />
99105
<Compile Include="GriddlyButton.cs" />
100106
<Compile Include="GriddlyColumn.cs" />

Griddly.Mvc/GriddlyContext.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 GriddlyContext
10+
{
11+
public string Name { get; set; }
12+
public string ParentPath { get; set; }
13+
public bool IsDefaultSkipped { get; set; }
14+
15+
public GriddlyFilterCookieData CookieData { get; set; }
16+
17+
public string CookieName => "gf_" + Name;
18+
19+
public Dictionary<string, object> Defaults { get; set; } = new Dictionary<string, object>();
20+
public Dictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();
21+
}
22+
23+
public class GriddlyFilterCookieData
24+
{
25+
public Dictionary<string, string[]> Values { get; set; }
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Web.Mvc;
9+
10+
namespace Griddly.Mvc
11+
{
12+
public class GriddlyCookieFilterValueProvider : IValueProvider
13+
{
14+
GriddlyFilterCookieData _data;
15+
16+
public GriddlyCookieFilterValueProvider(GriddlyFilterCookieData data)
17+
{
18+
_data = data;
19+
}
20+
21+
public bool ContainsPrefix(string prefix)
22+
{
23+
return _data.Values?.ContainsKey(prefix) == true;
24+
}
25+
26+
public ValueProviderResult GetValue(string key)
27+
{
28+
string[] value = null;
29+
30+
if (_data.Values?.TryGetValue(key, out value) != true)
31+
value = null;
32+
33+
string attemptedValue = null;
34+
35+
if (value != null)
36+
attemptedValue = string.Join(",", value);
37+
38+
return new ValueProviderResult(value, attemptedValue, CultureInfo.CurrentCulture);
39+
}
40+
}
41+
42+
public class GriddlyCookieFilterValueProviderFactory : ValueProviderFactory
43+
{
44+
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
45+
{
46+
if (controllerContext.IsChildAction && controllerContext.HttpContext.Request.QueryString.Count == 0)
47+
{
48+
var context = controllerContext.Controller.GetOrCreateGriddlyContext();
49+
var cookie = controllerContext.HttpContext.Request.Cookies[context.CookieName];
50+
51+
if (cookie != null && !string.IsNullOrWhiteSpace(cookie.Value))
52+
{
53+
try
54+
{
55+
var data = JsonConvert.DeserializeObject<GriddlyFilterCookieData>(cookie.Value);
56+
57+
context.CookieData = data;
58+
context.IsDefaultSkipped = true;
59+
60+
return new GriddlyCookieFilterValueProvider(data);
61+
}
62+
catch
63+
{
64+
// TODO: log it?
65+
}
66+
}
67+
}
68+
69+
return null;
70+
}
71+
}
72+
}

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -158,89 +158,122 @@ public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dict
158158
return new HtmlString(sb.ToString());
159159
}
160160

161-
public static void SetGriddlyDefault<T>(this Controller controller, ref T parameter, string field, T value)
161+
static readonly string _contextKey = "_griddlycontext";
162+
163+
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T parameter, string field, T value)
162164
{
163-
if (controller.ControllerContext.IsChildAction)
165+
var context = controller.ViewData[_contextKey] as GriddlyContext;
166+
167+
if (context != null)
164168
{
165-
if (EqualityComparer<T>.Default.Equals(parameter, default(T)))
169+
context.Defaults[field] = value;
170+
171+
if (controller.ControllerContext.IsChildAction
172+
&& !context.IsDefaultSkipped
173+
&& EqualityComparer<T>.Default.Equals(parameter, default(T)))
174+
{
166175
parameter = value;
167176

168-
controller.ViewData["_griddlyDefault_" + field] = parameter;
177+
context.Parameters[field] = value;
178+
}
169179
}
170-
else
171-
controller.ViewData["_griddlyDefault_" + field] = value;
172180
}
173181

174-
public static void SetGriddlyDefault<T>(this Controller controller, ref T[] parameter, string field, IEnumerable<T> value)
182+
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T[] parameter, string field, IEnumerable<T> value)
175183
{
176-
if (controller.ControllerContext.IsChildAction)
184+
var context = controller.ViewData[_contextKey] as GriddlyContext;
185+
186+
if (context != null)
177187
{
178-
if (parameter == null)
179-
parameter = value.ToArray();
188+
context.Defaults[field] = value;
180189

181-
controller.ViewData["_griddlyDefault_" + field] = parameter;
190+
if (controller.ControllerContext.IsChildAction
191+
&& !context.IsDefaultSkipped
192+
&& parameter == null)
193+
{
194+
parameter = value.ToArray();
195+
}
182196
}
183-
else
184-
controller.ViewData["_griddlyDefault_" + field] = value;
185197
}
186198

187-
public static void SetGriddlyDefault<T>(this Controller controller, ref T?[] parameter, string field, IEnumerable<T> value)
199+
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T?[] parameter, string field, IEnumerable<T> value)
188200
where T : struct
189201
{
190-
if (controller.ControllerContext.IsChildAction)
202+
var context = controller.ViewData[_contextKey] as GriddlyContext;
203+
204+
if (context != null)
191205
{
192-
if (parameter == null)
193-
parameter = value.Cast<T?>().ToArray();
206+
context.Defaults[field] = value;
194207

195-
controller.ViewData["_griddlyDefault_" + field] = parameter;
208+
if (controller.ControllerContext.IsChildAction
209+
&& !context.IsDefaultSkipped
210+
&& parameter == null)
211+
{
212+
parameter = value.Cast<T?>().ToArray();
213+
}
196214
}
197-
else
198-
controller.ViewData["_griddlyDefault_" + field] = value;
199215
}
200216

201-
public static object GetGriddlyDefault(this WebViewPage page, string field)
217+
public static object GetGriddlyParameter(this WebViewPage page, string field)
202218
{
203-
return page.ViewData["_griddlyDefault_" + field];
204-
}
219+
object value = null;
205220

206-
public static void ForceGriddlyDefault(this Controller controller, string field, object value)
207-
{
208-
controller.ViewData["_griddlyDefault_" + field] = value;
221+
if ((page.ViewData[_contextKey] as GriddlyContext)?.Parameters.TryGetValue(field, out value) != true)
222+
value = null;
223+
224+
return value;
209225
}
210226

211227
public static Dictionary<string, object> GetGriddlyDefaults(this WebViewPage page)
212228
{
213229
Dictionary<string, object> defaults = new Dictionary<string, object>();
230+
var context = page.ViewData[_contextKey] as GriddlyContext;
214231

215-
foreach (var key in page.ViewData.Keys.Where(k => k.StartsWith("_griddlyDefault_")))
232+
if (context != null)
216233
{
217-
var value = page.ViewData[key];
218-
string stringValue = null;
219-
220-
Type t = null;
221-
222-
if (value != null)
234+
// TODO: is there any reason to make a new dict vs using the same one? nobody else uses it, right?
235+
foreach (var pair in context.Defaults)
223236
{
224-
t = value.GetType();
237+
var value = pair.Value;
225238

226-
if (t.IsArray)
227-
{
228-
t = t.GetElementType();
239+
if (value != null)
240+
{
241+
Type t = value.GetType();
242+
243+
if (t.IsArray)
244+
{
245+
t = t.GetElementType();
229246

230-
if ((Nullable.GetUnderlyingType(t) ?? t).IsEnum)
231-
value = ((Array)value).Cast<object>().Select(x => x?.ToString()).ToArray();
247+
if ((Nullable.GetUnderlyingType(t) ?? t).IsEnum)
248+
value = ((Array)value).Cast<object>().Select(x => x?.ToString()).ToArray();
249+
}
232250
}
233251

234-
if (stringValue == null)
235-
stringValue = value.ToString();
252+
defaults[pair.Key] = value;
236253
}
237-
238-
defaults[key.Substring("_griddlyDefault_".Length)] = value;
239254
}
240255

241256
return defaults;
242257
}
243258

259+
public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase controller)
260+
{
261+
var context = controller.ViewData[_contextKey] as GriddlyContext;
262+
263+
if (context == null)
264+
{
265+
context = new GriddlyContext()
266+
{
267+
Name = (controller.GetType().Name + "_" + controller.ControllerContext.RouteData.GetRequiredString("action")).ToLower()
268+
};
269+
270+
// NOTE: for 2020 Chris... yes, this is unique for multiple griddlies on a page as it is in the grid action context of each one
271+
controller.ViewData[_contextKey] = context;
272+
}
273+
274+
return context;
275+
}
276+
244277
static IDictionary<string, object> ObjectToDictionary(object value)
245278
{
246279
if (value == null)

0 commit comments

Comments
 (0)