-
Notifications
You must be signed in to change notification settings - Fork 134
/
GridBase.cs
163 lines (140 loc) · 5.32 KB
/
GridBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using GridCore.Pagination;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GridCore
{
/// <summary>
/// Base implementation of the Grid.Mvc
/// </summary>
public abstract class GridBase<T>
{
//pre-processors process items before adds to main collection (filtering and searching)
private readonly List<IGridItemsProcessor<T>> _preprocessors = new List<IGridItemsProcessor<T>>();
//processors process items after adds to main collection (sorting and paging)
private readonly List<IGridItemsProcessor<T>> _processors = new List<IGridItemsProcessor<T>>();
private IGridItemsProcessor<T> _totalsprocessor;
protected IQueryable<T> BeforeItems; //items before processors
protected IEnumerable<T> AfterItems; //items after processors
protected Func<IQueryable<T>, Task<IList<T>>> ToListAsync;
private int _itemsCount = -1; // total items count on collection
private bool _itemsPreProcessed; //is preprocessors launched?
private bool _itemsProcessed; //is processors launched?
private Func<T, string> _rowCssClassesContraint;
public abstract IGridSettingsProvider Settings { get; set; }
public IQueryable<T> GridItems
{
get
{
//call preprocessors before:
PreProcess();
return BeforeItems;
}
}
private void PreProcess()
{
if (!_itemsPreProcessed)
{
_itemsPreProcessed = true;
foreach (var gridItemsProcessor in _preprocessors)
{
BeforeItems = gridItemsProcessor.Process(BeforeItems);
}
// added to avoid 2nd EF opened task if counting later
_itemsCount = BeforeItems.Count();
// calculate totals
_totalsprocessor.Process(BeforeItems);
}
}
/// <summary>
/// Text in empty grid (no items for display)
/// </summary>
public string EmptyGridText { get; set; }
/// <summary>
/// Total count of items in the grid
/// </summary>
public int ItemsCount
{
get
{
//call preprocessors before:
PreProcess();
return _itemsCount;
}
set
{
_itemsCount = value; //value can be set by pager (for minimizing db calls)
}
}
#region Custom row css classes
public void SetRowCssClassesContraint(Func<T, string> contraint)
{
_rowCssClassesContraint = contraint;
}
public string GetRowCssClasses(object item)
{
if (_rowCssClassesContraint == null)
return string.Empty;
var typed = (T)item;
if (typed == null)
throw new InvalidCastException(string.Format("The item must be of type '{0}'", typeof(T).FullName));
return _rowCssClassesContraint(typed);
}
#endregion
protected void PrepareItemsToDisplay()
{
PrepareItemsToDisplayAsync().Wait();
}
protected async Task PrepareItemsToDisplayAsync(Func<IQueryable<T>, Task<IList<T>>> toListAsync = null)
{
if (!_itemsProcessed)
{
_itemsProcessed = true;
IQueryable<T> itemsToProcess = GridItems;
foreach (var processor in _processors.Where(p => p != null))
{
if (processor.GetType().Equals(typeof(PagerGridItemsProcessor<T>)))
itemsToProcess = ((PagerGridItemsProcessor<T>)processor).Process(itemsToProcess, ItemsCount);
else
itemsToProcess = processor.Process(itemsToProcess);
}
if (toListAsync == null)
AfterItems = itemsToProcess.ToList();
else
AfterItems = await toListAsync(itemsToProcess);
}
}
#region Processors methods
protected void AddItemsProcessor(IGridItemsProcessor<T> processor)
{
if (!_processors.Contains(processor))
_processors.Add(processor);
}
protected void RemoveItemsProcessor(IGridItemsProcessor<T> processor)
{
if (_processors.Contains(processor))
_processors.Remove(processor);
}
protected void AddItemsPreProcessor(IGridItemsProcessor<T> processor)
{
if (!_preprocessors.Contains(processor))
_preprocessors.Add(processor);
}
protected void RemoveItemsPreProcessor(IGridItemsProcessor<T> processor)
{
if (_preprocessors.Contains(processor))
_preprocessors.Remove(processor);
}
protected void InsertItemsProcessor(int position, IGridItemsProcessor<T> processor)
{
if (!_processors.Contains(processor))
_processors.Insert(position, processor);
}
protected void SetTotalsProcessor(IGridItemsProcessor<T> processor)
{
_totalsprocessor = processor;
}
#endregion
}
}