Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GraphLabs.Site.Core/Filters/AbstractFilterableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace GraphLabs.Site.Core.Filters
{
public abstract class AbstractFilterableModel<T>
{
public static Expression<Func<T, bool>> CreateFilter(FilterParams filterParams)
public static Expression<Func<T, bool>> CreateFilter(FilterParams<T> filterParams)
{
return t => true;
}
Expand Down
19 changes: 19 additions & 0 deletions GraphLabs.Site.Core/Filters/BoundedFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace GraphLabs.Site.Core.Filters
{
[System.AttributeUsage(System.AttributeTargets.Property)]
public class BoundedFilterAttribute : Attribute, IFilterAttribute
{
private String _name;
private Object[] _limiters;

public BoundedFilterAttribute(string name, Object[] limiters)
{
_name = name;
_limiters = limiters;
}
}
}
42 changes: 40 additions & 2 deletions GraphLabs.Site.Core/Filters/FilterParams.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reflection;

namespace GraphLabs.Site.Core.Filters
{
public class FilterParams
public class FilterParams<T>
{
private readonly NameValueCollection _filterParams;

Expand All @@ -23,7 +25,43 @@ public string GetStringParam(string name)
{
return GetParam(name);
}


public object GetBoundedParam(string name)
{
var index = GetIntParam(name);
if (index == null)
{
return null;
}

var propertyInfo = typeof(T).GetProperty(name);
if (propertyInfo == null) return null;
foreach (var customAttributeData in propertyInfo.CustomAttributes)
{
if (customAttributeData.AttributeType == typeof(BoundedFilterAttribute))
{
return ((ReadOnlyCollection<CustomAttributeTypedArgument>) customAttributeData
.ConstructorArguments[1].Value)[index.Value].Value;
}
}

return null;
}

public int? GetIntParam(string name)
{
try
{
string s = GetParam(name);
return s == null ? (int?) null : Int32.Parse(s);
}
catch (FormatException)
{
return null;
}
}


public bool? GetBoolParam(string name)
{
string s = GetParam(name);
Expand Down
4 changes: 4 additions & 0 deletions GraphLabs.Site.Core/GraphLabs.Site.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
<Reference Include="GraphLabs.Site.Models, Version=0.1.0.461, Culture=neutral, PublicKeyToken=null">
<HintPath>..\GraphLabs.WcfServices\Bin\GraphLabs.Site.Models.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.6.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb">

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот этого референса быть не должно.

<HintPath>..\packages\NUnit.3.6.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -141,6 +144,7 @@
<Compile Include="..\AssemblyVersion.cs">
<Link>Properties\AssemblyVersion.cs</Link>
</Compile>
<Compile Include="Filters\BoundedFilterAttribute.cs" />
<Compile Include="Filters\FilterParams.cs" />
<Compile Include="Filters\AbstractFilterableModel.cs" />
<Compile Include="Filters\IFilterAttribute.cs" />
Expand Down
9 changes: 5 additions & 4 deletions GraphLabs.Site.Models/Groups/GroupModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class GroupModel : AbstractFilterableModel<Group>, IEntityBasedModel<Grou
{
public long Id { get; set; }

[StringFilter("Номер группы")]
// [StringFilter("Номер группы")]
[BoundedFilter("Выбери номер", new Object[]{"Б14-505", "К08-221"})]
public string Name { get; set; }

[StringFilter("Доступность для регистрации")]
Expand All @@ -23,10 +24,10 @@ public class GroupModel : AbstractFilterableModel<Group>, IEntityBasedModel<Grou

public int Number { get; set; }

public static Expression<Func<Group, bool>> CreateFilter(FilterParams filterParams)
public static Expression<Func<Group, bool>> CreateFilter(FilterParams<GroupModel> filterParams)
{
string name = filterParams.GetStringParam("Name");
bool? isOpen = filterParams.GetBoolParam("IsOpen");
string name = (string) filterParams.GetBoundedParam(nameof(Name));
bool? isOpen = filterParams.GetBoolParam(nameof(IsOpen));

return g => (name == null || name.ToLower().Equals(g.Name.ToLower()))
&& (isOpen == null || g.IsOpen == isOpen);
Expand Down
2 changes: 1 addition & 1 deletion GraphLabs.Site/Controllers/GraphLabsFilteringController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionDescriptor.ActionName.Equals(nameof(Index)))//может быть это ограничение лишнее
{
FilterParams filterParams = new FilterParams(Request.QueryString);
FilterParams<TModel> filterParams = new FilterParams<TModel>(Request.QueryString);
Type type = typeof(TModel);
if (!_methodInfos.ContainsKey(type))
{
Expand Down
50 changes: 40 additions & 10 deletions GraphLabs.Site/Views/Helpers/GraphLabsUIFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;


Expand Down Expand Up @@ -28,10 +29,11 @@ public static HtmlInputCheckBox CreateHtmlInputCheckBox()
return checkBox;
}

public static HtmlGenericControl CreateHtmlLabel()
public static HtmlGenericControl CreateHtmlLabel(string labelText)
{
var label = new HtmlGenericControl("label");
label.Attributes.Add("class", "graphlabs-label");
label.InnerText = labelText;
return label;
}

Expand All @@ -53,22 +55,21 @@ public static HtmlInputButton CreateHtmlResetButton()

public static Control CreateInputField(string name, string labelText)
{
var input = GraphLabsUIFactory.CreateHtmlInputText();
var input = CreateHtmlInputText();
input.Name = name;
input.ID = name;

var label = GraphLabsUIFactory.CreateHtmlLabel();
label.InnerText = labelText;
var label = CreateHtmlLabel(labelText);

var div = new HtmlGenericControl("div");
var div = CreateDiv();
div.Controls.Add(label);
label.Controls.Add(input);
return div;
}

public static Control CreateInputCheckBox(string name, string labelText)
{
var input = GraphLabsUIFactory.CreateHtmlInputCheckBox();
var input = CreateHtmlInputCheckBox();
input.Name = name;
input.ID = name;
input.Value = "true";
Expand All @@ -77,15 +78,44 @@ public static Control CreateInputCheckBox(string name, string labelText)
HtmlGenericControl script = new HtmlGenericControl("script");
script.InnerHtml = "function checkboxChange(chkBox) { if (chkBox.value==\"true\") {chkBox.value= \"false\"; chkBox.indeterminate = true;} else { chkBox.value= \"true\"; chkBox.checked=true; } }";

var label = GraphLabsUIFactory.CreateHtmlLabel();
label.InnerText = labelText;
var label = CreateHtmlLabel(labelText);

var div = new HtmlGenericControl("div");
var div = CreateDiv();
div.Controls.Add(label);

label.Controls.Add(input);
label.Controls.Add(script);
return div;
}

public static Control CreateDiv()
{
return new HtmlGenericControl("div");
}

public static Control CreateSelectField(string name, string desc, Dictionary<string, string> options)
{
var div = CreateDiv();
var label = CreateHtmlLabel(desc);

var select = new HtmlGenericControl("select");
select.Attributes.Add("name", name);
foreach (var option in options)
{
select.Controls.Add(CreateHtmlOption(option.Key, option.Value));
}

div.Controls.Add(label);
div.Controls.Add(select);
return div;
}

private static Control CreateHtmlOption(string value, string desc)
{
var option = new HtmlGenericControl("option");
option.Attributes.Add("value", value);
option.InnerText = desc;
return option;
}
}
}
54 changes: 37 additions & 17 deletions GraphLabs.Site/Views/Helpers/GraphLabsWebGrid.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Helpers;
Expand Down Expand Up @@ -38,7 +40,7 @@ public IHtmlString GetHtml(string tableStyle = "webGrid", string headerStyle = "
selectedRowStyle, caption, displayHeader, fillEmptyRows, emptyRowCellValue, columns, exclusions, mode,
firstText, previousText, nextText, lastText, numericLinksCount, htmlAttributes
).ToString());

return new HtmlString(buildHtml.ToString());
}

Expand All @@ -47,39 +49,57 @@ private void AddHtmlFilters(StringBuilder stringBuilder)
var mainDiv = new HtmlGenericControl("div");
var form = GraphLabsUIFactory.CreateHtmlForm();
mainDiv.Controls.Add(form);

if (IsA(_source, typeof(IFilterable<,>)))
{
var e = GetType(_source, typeof(IFilterable<,>));

var genericArgument = e.GetGenericArguments()[1];
foreach (var propertyInfo in genericArgument.GetProperties())
{
foreach (var customAttributeData in propertyInfo.CustomAttributes)
{
if (customAttributeData.AttributeType.GetInterfaces().Contains(typeof(IFilterAttribute)))
{
if (propertyInfo.PropertyType == typeof(bool))
if (customAttributeData.AttributeType == typeof(StringFilterAttribute))
{
form.Controls.Add(GraphLabsUIFactory.CreateInputCheckBox(propertyInfo.Name,
(string) customAttributeData.ConstructorArguments[0].Value));
} else {
form.Controls.Add(GraphLabsUIFactory.CreateInputField(propertyInfo.Name,
(string) customAttributeData.ConstructorArguments[0].Value));
//фильтрация по одному полю с подписью
if (propertyInfo.PropertyType == typeof(bool))
{
form.Controls.Add(GraphLabsUIFactory.CreateInputCheckBox(propertyInfo.Name,
(string) customAttributeData.ConstructorArguments[0].Value));
}
else
{
form.Controls.Add(GraphLabsUIFactory.CreateInputField(propertyInfo.Name,
(string) customAttributeData.ConstructorArguments[0].Value));
}
}
else if (customAttributeData.AttributeType == typeof(BoundedFilterAttribute))
{
//фильтрация с подписанным фильтруемым полем с ограниченным набором значений
var options =
((ReadOnlyCollection<CustomAttributeTypedArgument>) customAttributeData
.ConstructorArguments[1].Value)
.Select(a => a.Value)
.Select((v, i) => new {Key = i, Value = v})
.ToDictionary(o => o.Key.ToString(), o => o.Value.ToString());
form.Controls.Add(GraphLabsUIFactory.CreateSelectField(propertyInfo.Name,
(string) customAttributeData.ConstructorArguments[0].Value, options));
}
}
}
}
}

if (form.Controls.Count == 0)
{
return;
}
if (form.Controls.Count == 0)
{
return;
}

form.Controls.Add(GraphLabsUIFactory.CreateHtmlResetButton());
form.Controls.Add(GraphLabsUIFactory.CreateHtmlSubmitButton());
mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(stringBuilder)));
form.Controls.Add(GraphLabsUIFactory.CreateHtmlResetButton());
form.Controls.Add(GraphLabsUIFactory.CreateHtmlSubmitButton());
mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(stringBuilder)));
}
}

private static Type GetType(object o, Type t)
Expand Down