-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathSGridCore.cs
606 lines (516 loc) · 21.2 KB
/
SGridCore.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
using GridCore.Columns;
using GridCore.DataAnnotations;
using GridCore.Filtering;
using GridCore.Pagination;
using GridCore.Resources;
using GridCore.Searching;
using GridCore.Sorting;
using GridCore.Totals;
using GridShared;
using GridShared.Columns;
using GridShared.DataAnnotations;
using GridShared.Grouping;
using GridShared.Pagination;
using GridShared.Sorting;
using GridShared.Style;
using GridShared.Totals;
using GridShared.Utility;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
namespace GridCore
{
/// <summary>
/// Grid.Mvc base class
/// </summary>
public class SGridCore<T> : GridBase<T>, ISGrid<T>
{
internal IGridAnnotationsProvider _annotations;
protected IColumnBuilder<T> _columnBuilder;
protected GridColumnCollection<T> _columnsCollection;
internal SearchGridItemsProcessor<T> _currentSearchItemsProcessor;
internal FilterGridItemsProcessor<T> _currentFilterItemsProcessor;
internal SortGridItemsProcessor<T> _currentSortItemsProcessor;
internal TotalsGridItemsProcessor<T> _currentTotalsItemsProcessor;
protected int _displayingItemsCount = -1; // count of displaying items (if using pagination)
protected PagingType _pagingType = PagingType.None;
protected IGridPager _pager;
protected IGridItemsProcessor<T> _pagerProcessor;
protected IGridSettingsProvider _settings;
public SGridCore()
{ }
public SGridCore(IEnumerable<T> items, QueryDictionary<string> query, bool renderOnlyRows,
string pagerViewName = GridPager.DefaultPagerViewName, IColumnBuilder<T> columnBuilder = null)
: this(items, query.ToStringValuesDictionary(), renderOnlyRows, pagerViewName, columnBuilder)
{
}
public SGridCore(IEnumerable<T> items, QueryDictionary<StringValues> query, bool renderOnlyRows,
string pagerViewName = GridPager.DefaultPagerViewName, IColumnBuilder<T> columnBuilder = null)
: this(items, query, columnBuilder)
{
if (PagingType != PagingType.Virtualization)
{
((GridPager)_pager).TemplateName = pagerViewName;
}
RenderOptions.RenderRowsOnly = renderOnlyRows;
}
public SGridCore(IEnumerable<T> items, QueryDictionary<string> query, IColumnBuilder<T> columnBuilder = null)
: this(items, query.ToStringValuesDictionary(), columnBuilder)
{
}
public SGridCore(IEnumerable<T> items, QueryDictionary<StringValues> query, IColumnBuilder<T> columnBuilder = null)
: this()
{
BeforeItems = items.AsQueryable();
#region init default properties
Query = query;
//set up sort settings:
_settings = new QueryStringGridSettingsProvider(Query);
Sanitizer = new Sanitizer();
EmptyGridText = Strings.DefaultGridEmptyText;
Language = Strings.Lang;
_currentSortItemsProcessor = new SortGridItemsProcessor<T>(this, _settings.SortSettings);
_currentFilterItemsProcessor = new FilterGridItemsProcessor<T>(this, _settings.FilterSettings);
_currentSearchItemsProcessor = new SearchGridItemsProcessor<T>(this, _settings.SearchSettings);
_currentTotalsItemsProcessor = new TotalsGridItemsProcessor<T>(this);
AddItemsPreProcessor(_currentFilterItemsProcessor);
AddItemsPreProcessor(_currentSearchItemsProcessor);
InsertItemsProcessor(0, _currentSortItemsProcessor);
SetTotalsProcessor(_currentTotalsItemsProcessor);
#endregion init default properties
_annotations = new GridCoreAnnotationsProvider();
//Set up column collection:
if (columnBuilder == null)
_columnBuilder = new DefaultColumnCoreBuilder<T>(this, _annotations);
else
_columnBuilder = columnBuilder;
_columnsCollection = new GridColumnCollection<T>(this, _columnBuilder, _settings.SortSettings);
RenderOptions = new GridRenderOptions();
ApplyGridSettings();
var urlParameters = CustomQueryStringBuilder.Convert(query);
int page = 0;
int startIndex = 0;
int virtualizedCount = 0;
bool noTotals = false;
string startIndexParameter = urlParameters[GridPager.DefaultStartIndexQueryParameter];
string virtualizedCountParameter = urlParameters[GridPager.DefaultVirtualizedCountQueryParameter];
string noTotalsParameter = urlParameters[GridPager.DefaultNoTotalsParameter];
if (!string.IsNullOrEmpty(startIndexParameter) && !string.IsNullOrWhiteSpace(virtualizedCountParameter))
{
PagingType = PagingType.Virtualization;
int.TryParse(startIndexParameter, out startIndex);
int.TryParse(virtualizedCountParameter, out virtualizedCount);
((GridPager)Pager).StartIndex = startIndex;
((GridPager)Pager).VirtualizedCount = virtualizedCount;
if(bool.TryParse(noTotalsParameter, out noTotals))
((GridPager)Pager).NoTotals = noTotals;
}
else
{
string pageParameter = urlParameters[((GridPager)Pager).ParameterName];
if (pageParameter != null)
int.TryParse(pageParameter, out page);
if (page == 0)
page++;
((GridPager)Pager).CurrentPage = page;
}
}
/// <summary>
/// Grid columns collection
/// </summary>
public IGridColumnCollection<T> Columns
{
get { return _columnsCollection; }
}
public SearchOptions SearchOptions { get; set; } = new SearchOptions() { Enabled = false };
public bool ExtSortingEnabled { get; set; }
public bool HiddenExtSortingHeader { get; set; } = false;
public bool GroupingEnabled { get; set; }
public bool SyncButtonEnabled { get; set; } = false;
public bool ClearFiltersButtonEnabled { get; set; } = false;
/// <summary>
/// Sets or get default value of sorting for all adding columns
/// </summary>
public bool DefaultSortEnabled
{
get { return _columnBuilder.DefaultSortEnabled; }
set { _columnBuilder.DefaultSortEnabled = value; }
}
public GridSortMode GridSortMode
{
get { return _columnBuilder.DefaultGridSortMode; }
set { _columnBuilder.DefaultGridSortMode = value; }
}
/// <summary>
/// Set or get default value of filtering for all adding columns
/// </summary>
public bool DefaultFilteringEnabled
{
get { return _columnBuilder.DefaultFilteringEnabled; }
set { _columnBuilder.DefaultFilteringEnabled = value; }
}
public GridRenderOptions RenderOptions { get; set; }
/// <summary>
/// Provides settings, using by the grid
/// </summary>
public override IGridSettingsProvider Settings
{
get { return _settings; }
set
{
_settings = value;
_currentSortItemsProcessor.UpdateSettings(_settings.SortSettings);
_currentFilterItemsProcessor.UpdateSettings(_settings.FilterSettings);
_currentSearchItemsProcessor.UpdateSettings(_settings.SearchSettings);
}
}
public IGridItemsProcessor<T> PagerProcessor { get { return _pagerProcessor; } }
public IGridItemsProcessor<T> SearchProcessor { get { return _currentSearchItemsProcessor; } }
public IGridItemsProcessor<T> FilterProcessor { get { return _currentFilterItemsProcessor; } }
public IGridItemsProcessor<T> SortProcessor { get { return _currentSortItemsProcessor; } }
public IGridItemsProcessor<T> TotalsProcessor { get { return _currentTotalsItemsProcessor; } }
public MethodInfo RemoveDiacritics { get; set; } = null;
/// <summary>
/// Items, displaying in the grid view
/// </summary>
public IEnumerable<object> ItemsToDisplay
{
get { return (IEnumerable<object>)GetItemsToDisplay(); }
}
public virtual IEnumerable<T> GetItemsToDisplay()
{
PrepareItemsToDisplay();
return AfterItems;
}
/// <summary>
/// Methods returns items that will need to be displayed
/// </summary>
public virtual void SetToListAsyncFunc(Func<IQueryable<T>, Task<IList<T>>> toListAsync)
{
ToListAsync = toListAsync;
}
/// <summary>
/// Methods returns items that will need to be displayed
/// </summary>
public virtual async Task<IEnumerable<T>> GetItemsToDisplayAsync(Func<IQueryable<T>, Task<IList<T>>> toListAsync)
{
SetToListAsyncFunc(toListAsync);
return (IEnumerable<T>)await GetItemsToDisplayAsync();
}
/// <summary>
/// Methods returns items that will need to be displayed
/// </summary>
public virtual async Task<IEnumerable<object>> GetItemsToDisplayAsync()
{
await PrepareItemsToDisplayAsync(ToListAsync);
return (IEnumerable<object>)AfterItems;
}
/// <summary>
/// Provides query, using by the grid
/// </summary>
public QueryDictionary<StringValues> Query { get; set; }
#region IGrid Members
/// <summary>
/// Count of current displaying items
/// </summary>
public virtual int DisplayingItemsCount
{
get
{
if (_displayingItemsCount >= 0)
return _displayingItemsCount;
_displayingItemsCount = GetItemsToDisplay().Count();
return _displayingItemsCount;
}
}
/// <summary>
/// Count of current displaying items
/// </summary>
public virtual async Task<int> GetDisplayingItemsCountAsync()
{
if (_displayingItemsCount >= 0)
return _displayingItemsCount;
_displayingItemsCount = (await GetItemsToDisplayAsync()).Count();
return _displayingItemsCount;
}
/// <summary>
/// Enable or disable paging for the grid
/// </summary>
[Obsolete("This property is obsolete. Use PagingType property", true)]
public bool EnablePaging
{
get {
return _pagingType == PagingType.Pagination;
}
set { }
}
/// <summary>
/// Enable or disable paging type for the grid
/// </summary>
public PagingType PagingType
{
get { return _pagingType; }
set
{
if (_pagingType == value) return;
_pagingType = value;
if (_pagingType != PagingType.None)
{
if (_pagerProcessor == null)
_pagerProcessor = new PagerGridItemsProcessor<T>(this);
AddItemsProcessor(_pagerProcessor);
}
else
{
RemoveItemsProcessor(_pagerProcessor);
}
}
}
public string Language { get; set; }
/// <summary>
/// Gets or set Grid column values sanitizer
/// </summary>
public ISanitizer Sanitizer { get; set; }
/// <summary>
/// Grid mode
/// </summary>
public GridMode Mode { get { return GridMode.Grid; } }
/// <summary>
/// Get value for creating items
/// </summary>
public bool CreateEnabled { get { return false; } }
/// <summary>
/// Get value for reading items
/// </summary>
public bool ReadEnabled { get { return false; } }
/// <summary>
/// Get value for updating items
/// </summary>
public bool UpdateEnabled { get { return false; } }
/// <summary>
/// Get value for deleting items
/// </summary>
public bool DeleteEnabled { get { return false; } }
/// <summary>
/// Manage pager properties
/// </summary>
public IGridPager Pager
{
get { return _pager ?? (_pager = new GridPager(this)); }
set { _pager = value; }
}
/// <summary>
/// Keys for subgrid
/// </summary>
public string[] SubGridKeys { get; set; }
/// <summary>
/// Get foreign key values for subgrid records
/// </summary>
public string[] GetSubGridKeyValues(object item)
{
List<string> values = new List<string>();
foreach (var key in SubGridKeys)
{
string value = item.GetType().GetProperty(key).GetValue(item).ToString();
values.Add(value);
}
return values.ToArray();
}
IGridColumnCollection IGrid.Columns
{
get { return Columns; }
}
/// <summary>
/// Sum enabled for some columns
/// </summary>
public bool IsSumEnabled { get { return Columns.Any(r => ((ITotalsColumn)r).IsSumEnabled); } }
/// <summary>
/// Average enabled for some columns
/// </summary>
public bool IsAverageEnabled { get { return Columns.Any(r => ((ITotalsColumn)r).IsAverageEnabled); } }
/// <summary>
/// Max enabled for some columns
/// </summary>
public bool IsMaxEnabled { get { return Columns.Any(r => ((ITotalsColumn)r).IsMaxEnabled); } }
/// <summary>
/// Min enabled for some columns
/// </summary>
public bool IsMinEnabled { get { return Columns.Any(r => ((ITotalsColumn)r).IsMinEnabled); } }
/// <summary>
/// Calculation enabled for some columns
/// </summary>
public bool IsCalculationEnabled { get { return Columns.Any(r => ((ITotalsColumn)r).IsCalculationEnabled); } }
/// <summary>
/// Grid direction
/// </summary>
public GridDirection Direction { get; set; } = GridDirection.LTR;
/// <summary>
/// Get value for table layout
/// </summary>
public TableLayout TableLayout { get; set; } = TableLayout.Auto;
/// <summary>
/// Get value for table width
/// </summary>
public string Width { get; set; } = "auto";
/// <summary>
/// Get value for table height
/// </summary>
public string Height { get; set; } = "auto";
public CssFramework CssFramework { get; set; }
public HtmlClass HtmlClass { get; set; }
#endregion IGrid Members
/// <summary>
/// Applies data annotations settings
/// </summary>
protected internal void ApplyGridSettings()
{
GridTableAttribute opt = _annotations.GetAnnotationForTable<T>();
if (opt == null) return;
PagingType = opt.PagingType;
if (PagingType == PagingType.Pagination)
{
if (opt.PageSize > 0)
Pager.PageSize = opt.PageSize;
if (opt.PagingMaxDisplayedPages > 0 && Pager is GridPager)
{
(Pager as GridPager).MaxDisplayedPages = opt.PagingMaxDisplayedPages;
}
}
}
/// <summary>
/// Generates columns for all properties of the model
/// </summary>
public virtual void AutoGenerateColumns()
{
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// if any property has a position attribute it's necessary
// to create a new array before adding columns to the collection
if (properties.SelectMany(r => r.CustomAttributes).SelectMany(r => r.NamedArguments).Any(r => r.MemberName == "Position"))
{
PropertyInfo[] newProperties = new PropertyInfo[properties.Length];
foreach (PropertyInfo pi in properties)
{
int? position = null;
if (pi.CustomAttributes.Count() > 0)
{
foreach (var a in pi.CustomAttributes)
{
if (a.NamedArguments.Any(r => r.MemberName == "Position"))
{
position = (int)a.NamedArguments.First(r => r.MemberName == "Position").TypedValue.Value;
}
}
}
if (position.HasValue)
newProperties[position.Value] = pi;
}
properties = newProperties;
}
foreach (PropertyInfo pi in properties)
{
bool isKey = false;
if (pi.CustomAttributes.Count() > 0)
{
foreach (var a in pi.CustomAttributes)
{
if (a.AttributeType.Name.Equals("KeyAttribute"))
{
isKey = true;
}
}
}
if (pi.CanRead)
{
if (isKey)
{
Columns.Add(pi).SetPrimaryKey(true);
}
else
{
Columns.Add(pi);
}
}
}
}
public TotalsDTO GetTotals()
{
var totals = new TotalsDTO();
if (IsSumEnabled)
foreach (ITotalsColumn column in Columns)
{
if (column.IsSumEnabled)
totals.Sum.Add(((IGridColumn)column).Name, column.SumValue);
}
if (IsAverageEnabled)
foreach (ITotalsColumn column in Columns)
{
if (column.IsAverageEnabled)
totals.Average.Add(((IGridColumn)column).Name, column.AverageValue);
}
if (IsMaxEnabled)
foreach (ITotalsColumn column in Columns)
{
if (column.IsMaxEnabled)
totals.Max.Add(((IGridColumn)column).Name, column.MaxValue);
}
if (IsMinEnabled)
foreach (ITotalsColumn column in Columns)
{
if (column.IsMinEnabled)
totals.Min.Add(((IGridColumn)column).Name, column.MinValue);
}
if (IsCalculationEnabled)
foreach (ITotalsColumn column in Columns)
{
if (column.IsCalculationEnabled)
totals.Calculations.Add(((IGridColumn)column).Name, column.CalculationValues);
}
return totals;
}
public string GetState()
{
var jsonOptions = new JsonSerializerOptions();
jsonOptions.Converters.Add(new StringValuesConverter());
string jsonQuery = JsonSerializer.Serialize(Query, jsonOptions);
return jsonQuery.GridStateEncode();
}
public IList<object> GetValuesToDisplay(string columnName, IEnumerable<object> items)
{
var column = Columns.SingleOrDefault(r => r.Name == columnName);
if (column == null)
return new List<object>();
return ((IGridColumn<T>)column).Group.GetColumnValues((items as IEnumerable<T>).AsQueryable()).ToList();
}
public IList<object> GetGroupValues(IColumnGroup<T> group, IEnumerable<object> items)
{
if (group == null)
return new List<object>();
return group.GetColumnValues((items as IEnumerable<T>).AsQueryable()).ToList();
}
public IColumnGroup<T> GetGroup(string columnName)
{
var column = Columns.SingleOrDefault(r => r.Name == columnName);
if (column == null)
return null;
return ((IGridColumn<T>)column).Group;
}
public IEnumerable<object> GetItemsToDisplay(IList<Tuple<string, object>> values, IEnumerable<object> items)
{
if (values.Count == 0)
return items;
var itms = (items as IEnumerable<T>).AsQueryable();
foreach (var value in values)
{
var column = Columns.SingleOrDefault(r => r.Name == value.Item1);
if (column == null)
continue;
itms = ((IGridColumn<T>)column).Group.ApplyFilter(itms, value.Item2);
}
return (IEnumerable<object>)itms;
}
}
}