-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathCGrid.cs
1656 lines (1402 loc) · 67 KB
/
CGrid.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using GridBlazor.Columns;
using GridBlazor.DataAnnotations;
using GridBlazor.Filtering;
using GridBlazor.OData;
using GridBlazor.Pages;
using GridBlazor.Pagination;
using GridBlazor.Resources;
using GridBlazor.Searching;
using GridBlazor.Sorting;
using GridShared;
using GridShared.Columns;
using GridShared.DataAnnotations;
using GridShared.Filtering;
using GridShared.Grouping;
using GridShared.Pagination;
using GridShared.Sorting;
using GridShared.Style;
using GridShared.Totals;
using GridShared.Utility;
using Microsoft.AspNetCore.Components;
#if ! NETSTANDARD2_1
using Microsoft.AspNetCore.Components.Web.Virtualization;
#endif
using Microsoft.Extensions.Primitives;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
namespace GridBlazor
{
/// <summary>
/// Grid.Mvc base class
/// </summary>
public class CGrid<T> : ICGrid<T>
{
private Func<T, string> _rowCssClassesContraint;
private QueryDictionary<StringValues> _query;
private IGridSettingsProvider _settings;
private readonly IGridAnnotationsProvider _annotations;
private readonly IColumnBuilder<T> _columnBuilder;
private readonly GridColumnCollection<T> _columnsCollection;
private readonly PagerGridODataProcessor<T> _currentPagerODataProcessor;
private readonly FilterGridODataProcessor<T> _currentFilterODataProcessor;
private readonly SortGridODataProcessor<T> _currentSortODataProcessor;
private readonly SearchGridODataProcessor<T> _currentSearchODataProcessor;
private readonly ExpandGridODataProcessor<T> _currentExpandODataProcessor;
private IEnumerable<T> _items;
private IEnumerable<object> _selectedItems;
private int _displayingItemsCount = -1; // count of displaying items (if using pagination
private bool _noTotals = false; // controls calls to the back-end for virtualized grids to include totals or not
private PagingType _pagingType;
private IGridPager _pager;
private HttpClient _httpClient;
private Func<QueryDictionary<StringValues>, ItemsDTO<T>> _dataService;
private Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> _dataServiceAsync;
private Func<QueryDictionary<string>, Task<ItemsDTO<T>>> _grpcService;
private ICrudDataService<T> _crudDataService;
private IMemoryDataService<T> _memoryDataService;
public CGrid(HttpClient httpClient, string url, IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(httpClient, url, null, null, null, null, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
[Obsolete("This constructor is obsolete. Use one including an HttpClient parameter.", true)]
public CGrid(string url, IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, url, null, null, null, null, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<StringValues>, ItemsDTO<T>> dataService,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, dataService, null, null, null, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> dataServiceAsync,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, null, dataServiceAsync, null, null, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<string>, Task<ItemsDTO<T>>> grpcService,
QueryDictionary<string> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, null, null, grpcService, null, query.ToStringValuesDictionary(), renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
private CGrid(HttpClient httpClient, string url,
Func<QueryDictionary<StringValues>, ItemsDTO<T>> dataService,
Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> dataServiceAsync,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(httpClient, url, dataService, dataServiceAsync, null, null, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(HttpClient httpClient, string url, IMemoryDataService<T> memoryDataService, IQueryDictionary<StringValues> query,
bool renderOnlyRows, Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(httpClient, url, null, null, null, memoryDataService, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<StringValues>, ItemsDTO<T>> dataService,
IMemoryDataService<T> memoryDataService,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, dataService, null, null, memoryDataService, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> dataServiceAsync,
IMemoryDataService<T> memoryDataService,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, null, dataServiceAsync, null, memoryDataService, query, renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
public CGrid(Func<QueryDictionary<string>, Task<ItemsDTO<T>>> grpcService,
IMemoryDataService<T> memoryDataService,
QueryDictionary<string> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
: this(null, null, null, null, grpcService, memoryDataService, query.ToStringValuesDictionary(), renderOnlyRows, columns, cultureInfo, columnBuilder)
{
}
private CGrid(HttpClient httpClient, string url,
Func<QueryDictionary<StringValues>, ItemsDTO<T>> dataService,
Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> dataServiceAsync,
Func<QueryDictionary<string>, Task<ItemsDTO<T>>> grpcService,
IMemoryDataService<T> memoryDataService,
IQueryDictionary<StringValues> query, bool renderOnlyRows,
Action<IGridColumnCollection<T>> columns = null, CultureInfo cultureInfo = null,
IColumnBuilder<T> columnBuilder = null)
{
_dataServiceAsync = dataServiceAsync;
_dataService = dataService;
_grpcService = grpcService;
_memoryDataService = memoryDataService;
_selectedItems = new List<object>();
Items = new List<T>(); //response.Items;
Url = url;
_httpClient = httpClient;
_query = query as QueryDictionary<StringValues>;
//set up sort settings:
_settings = new QueryStringGridSettingsProvider(_query);
Sanitizer = new Sanitizer();
if (cultureInfo != null)
CultureInfo.CurrentCulture = cultureInfo;
EmptyGridText = Strings.DefaultGridEmptyText;
Language = Strings.Lang;
_annotations = new GridAnnotationsProvider();
//Set up column collection:
if (columnBuilder == null)
_columnBuilder = new DefaultColumnBuilder<T>(this, _annotations);
else
_columnBuilder = columnBuilder;
_columnsCollection = new GridColumnCollection<T>(this, _columnBuilder, _settings.SortSettings);
ComponentOptions = new GridOptions();
Pager = new GridPager(this);
ApplyGridSettings();
SetInitSorting();
_currentPagerODataProcessor = new PagerGridODataProcessor<T>(this);
_currentSortODataProcessor = new SortGridODataProcessor<T>(this, _settings.SortSettings);
_currentFilterODataProcessor = new FilterGridODataProcessor<T>(this, _settings.FilterSettings,
_settings.SearchSettings);
_currentSearchODataProcessor = new SearchGridODataProcessor<T>(this, _settings.SearchSettings);
_currentExpandODataProcessor = new ExpandGridODataProcessor<T>(this);
ComponentOptions.RenderRowsOnly = renderOnlyRows;
columns?.Invoke(Columns);
Mode = GridMode.Grid;
CreateEnabled = false;
ReadEnabled = false;
UpdateEnabled = false;
DeleteEnabled = false;
ButtonComponents = new QueryDictionary<(string Label, Nullable<MarkupString> Content, Type ComponentType,
IList<Action<object>> Actions, IList<Func<object, Task>> Functions, object Object)>();
ButtonCrudComponents = new QueryDictionary<(string Label, Nullable<MarkupString> Content, Type ComponentType,
GridMode GridMode, Func<T, bool> ReadMode, Func<T, bool> UpdateMode, Func<T, bool> DeleteMode,
Func<T, Task<bool>> ReadModeAsync, Func<T, Task<bool>> UpdateModeAsync, Func<T, Task<bool>> DeleteModeAsync,
IList<Action<object>> Actions, IList<Func<object, Task>> Functions, object Object)>();
}
public GridComponent<T> GridComponent { get; set; }
/// <summary>
/// Total count of items in the grid
/// </summary>
public int ItemsCount { get { return _pager.ItemsCount; } }
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;
public bool RearrangeColumnEnabled { get; set; }
/// <summary>
/// Items, displaying in the grid view
/// </summary>
public IEnumerable<object> ItemsToDisplay
{
get { return (IEnumerable<object>)GetItemsToDisplay(); }
}
public IEnumerable<object> SelectedItems
{
get { return _selectedItems; }
set { _selectedItems = value; }
}
/// <summary>
/// Methods returns items that will need to be displayed
/// </summary>
protected internal virtual IEnumerable<T> GetItemsToDisplay()
{
return Items;
}
public IGridColumnCollection<T> Columns
{
get { return _columnsCollection; }
}
IGridColumnCollection IGrid.Columns
{
get { return Columns; }
}
/// <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 GridOptions ComponentOptions { get; set; }
/// <summary>
/// items from server
/// </summary>
public IEnumerable<T> Items {
get => _items;
set {
_displayingItemsCount = -1;
_items = value;
}
}
/// <summary>
/// Provides settings, using by the grid
/// </summary>
public IGridSettingsProvider Settings
{
get { return _settings; }
/**
set
{
_query = value.ToQuery() as QueryDictionary<StringValues>;
if (_pager.CurrentPage > 0)
_query.Add(((GridPager)_pager).ParameterName, _pager.CurrentPage.ToString());
UpdateQueryAndSettings();
}
*/
}
public MethodInfo RemoveDiacritics { get; set; } = null;
private void UpdateQueryAndSettings()
{
_settings = new QueryStringGridSettingsProvider(_query);
SetInitSorting();
_columnsCollection.SortSettings = _settings.SortSettings;
_columnsCollection.UpdateColumnsSorting();
((GridPager)_pager).Query = _query;
_currentSortODataProcessor.UpdateSettings(_settings.SortSettings);
_currentFilterODataProcessor.UpdateSettings(_settings.FilterSettings, _settings.SearchSettings);
_currentSearchODataProcessor.UpdateSettings(_settings.SearchSettings);
}
// keeps initial sorting on the client for OData grids
private void SetInitSorting()
{
string[] sortings = Query.Get(((QueryStringSortSettings)_settings.SortSettings).ColumnQueryParameterName).Count > 0 ?
Query.Get(((QueryStringSortSettings)_settings.SortSettings).ColumnQueryParameterName).ToArray() : null;
if ((_settings.SortSettings.SortValues == null || _settings.SortSettings.SortValues.Count == 0)
&& (sortings == null || sortings.Length == 0)
&& string.IsNullOrWhiteSpace(_settings.SortSettings.ColumnName))
{
var column = _columnsCollection.FirstOrDefault(r => ((ICGridColumn)r).InitialDirection.HasValue);
if (column != null)
{
_settings.SortSettings.ColumnName = column.Name;
_settings.SortSettings.Direction = ((ICGridColumn)column).InitialDirection.Value;
}
}
}
/// <summary>
/// Provides url used by the grid
/// </summary>
public string Url { get; set; }
public HttpClient HttpClient
{
get {
if (_httpClient == null)
_httpClient = new HttpClient();
return _httpClient;
}
}
/// <summary>
/// Provides DataService used by the grid
/// </summary>
public Func<QueryDictionary<StringValues>, ItemsDTO<T>> DataService {
get { return _dataService; }
internal set { _dataService = value; }
}
public Func<QueryDictionary<StringValues>, Task<ItemsDTO<T>>> DataServiceAsync {
get { return _dataServiceAsync; }
internal set { _dataServiceAsync = value; }
}
public Func<QueryDictionary<string>, Task<ItemsDTO<T>>> GrpcService
{
get { return _grpcService; }
internal set { _grpcService = value; }
}
public ServerAPI ServerAPI { get; internal set; } = ServerAPI.ItemsDTO;
internal ExpandGridODataProcessor<T> CurrentExpandODataProcessor { get { return _currentExpandODataProcessor; } }
/// <summary>
/// Provides CrudDataService used by the grid
/// </summary>
public ICrudDataService<T> CrudDataService
{
get {
if (ServerAPI == ServerAPI.OData && _crudDataService == null)
_crudDataService = new ODataService<T>(HttpClient, Url, this);
return _crudDataService;
}
set { _crudDataService = value; }
}
/// <summary>
/// Provides MemoryDataService used by the grid
/// </summary>
public IMemoryDataService<T> MemoryDataService
{
get { return _memoryDataService; }
internal set { _memoryDataService = value; }
}
/// <summary>
/// Provides CrudFileService used by the grid
/// </summary>
public ICrudFileService<T> CrudFileService { get; set; }
/// <summary>
/// Provides query, using by the grid
/// </summary>
public QueryDictionary<StringValues> Query
{
get { return _query; }
set
{
_query = value;
UpdateQueryAndSettings();
}
}
/// <summary>
/// Count of current displaying items
/// </summary>
public virtual int DisplayingItemsCount
{
get
{
if (_displayingItemsCount >= 0)
return _displayingItemsCount;
_displayingItemsCount = GetItemsToDisplay().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 paging type for the grid
/// </summary>
public PagingType PagingType
{
get { return _pagingType; }
set
{
if (_pagingType == value) return;
_pagingType = value;
}
}
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; internal set; }
/// <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 bool ChangeVirtualizedHeight { get; set; } = false;
public bool ModalForms { get; set; } = false;
public string ModalWidth { get; set; }
public string ModalHeight { get; set; }
/// <summary>
/// Get and set export to an Excel file
/// </summary>
public bool ExcelExport { get; internal set; }
/// <summary>
/// Get and set export all rows to an Excel file
/// </summary>
public bool ExcelExportAllRows { get; internal set; }
/// <summary>
/// Get and set Excel file name
/// </summary>
public string ExcelExportFileName { get; internal set; }
/// <summary>
/// Get value for creating items
/// </summary>
public bool CreateEnabled { get; internal set; }
/// <summary>
/// Get value for reading items
/// </summary>
public bool ReadEnabled { get; internal set; }
/// <summary>
/// Get value for reading items
/// </summary>
public Func<T, bool> FuncReadEnabled { get; internal set; }
/// <summary>
/// Get value for updating items
/// </summary>
public bool UpdateEnabled { get; internal set; }
/// <summary>
/// Get value for updating items
/// </summary>
public Func<T, bool> FuncUpdateEnabled { get; internal set; }
/// <summary>
/// Get value for deleting items
/// </summary>
public bool DeleteEnabled { get; internal set; }
/// <summary>
/// Get value for deleting items
/// </summary>
public Func<T, bool> FuncDeleteEnabled { get; internal set; }
/// <summary>
/// Get and set custom create component
/// </summary>
public Type CreateComponent { get; internal set; }
/// <summary>
/// Get and set custom read component
/// </summary>
public Type ReadComponent { get; internal set; }
/// <summary>
/// Get and set custom update component
/// </summary>
public Type UpdateComponent { get; internal set; }
/// <summary>
/// Get and set custom Delete component
/// </summary>
public Type DeleteComponent { get; internal set; }
public IList<Action<object>> CreateActions { get; internal set; }
public IList<Func<object,Task>> CreateFunctions { get; internal set; }
public object CreateObject { get; internal set; }
public IList<Action<object>> ReadActions { get; internal set; }
public IList<Func<object, Task>> ReadFunctions { get; internal set; }
public object ReadObject { get; internal set; }
public IList<Action<object>> UpdateActions { get; internal set; }
public IList<Func<object, Task>> UpdateFunctions { get; internal set; }
public object UpdateObject { get; internal set; }
public IList<Action<object>> DeleteActions { get; internal set; }
public IList<Func<object, Task>> DeleteFunctions { get; internal set; }
public object DeleteObject { get; internal set; }
public QueryDictionary<(string Label, Nullable<MarkupString> Content, Type ComponentType, IList<Action<object>> Actions, IList<Func<object, Task>> Functions, object Object)> ButtonComponents { get; internal set; }
public QueryDictionary<(string Label, Nullable<MarkupString> Content, Type ComponentType, GridMode GridMode, Func<T,bool> ReadMode, Func<T, bool> UpdateMode, Func<T, bool> DeleteMode, Func<T, Task<bool>> ReadModeAsync, Func<T, Task<bool>> UpdateModeAsync, Func<T, Task<bool>> DeleteModeAsync, IList<Action<object>> Actions, IList<Func<object, Task>> Functions, object Object)> ButtonCrudComponents { get; internal set; }
public bool Keyboard { get; internal set; } = false;
public ModifierKey ModifierKey { get; internal set; } = ModifierKey.CtrlKey;
public Nullable<ModifierKey> SelectionKey { get; internal set; } = ModifierKey.ShiftKey;
/// <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>
/// Manage pager properties
/// </summary>
public IGridPager Pager
{
get { return _pager; }
set { _pager = value; }
}
/// <summary>
/// Keys for subgrid
/// </summary>
public (string, string)[] SubGridKeys { get; set; }
/// <summary>
/// Subgrids
/// </summary>
public Func<object[], Task<ICGrid>> SubGrids { get; set; }
/// <summary>
/// Subgrids state
/// </summary>
public bool SubGridsOpened { get; set; } = false;
public Type Type { get { return typeof(T); } }
/// <summary>
/// Get foreign key values for subgrid records
/// </summary>
public QueryDictionary<object> GetSubGridKeyValues(object item)
{
QueryDictionary<object> values = new QueryDictionary<object>();
foreach (var key in SubGridKeys)
{
var value = item.GetType().GetProperty(key.Item1).GetValue(item);
values.Add(key.Item2, value);
}
return values;
}
/// <summary>
/// Get primary key values for CRUD
/// </summary>
public object[] GetPrimaryKeyValues(object item)
{
List<object> values = new List<object>();
foreach (var column in Columns)
{
if (column.IsPrimaryKey)
{
var value = item.GetType().GetProperty(column.FieldName).GetValue(item);
values.Add(value);
}
}
return values.ToArray();
}
/// <summary>
/// Get primary keys for CRUD
/// </summary>
public string[] GetPrimaryKeys()
{
List<string> values = new List<string>();
foreach (var column in Columns)
{
if (column.IsPrimaryKey)
{
values.Add(column.FieldName);
}
}
return values.ToArray();
}
public bool DataAnnotationsValidation { get; set; } = true;
private static readonly Task<bool> InsertColumnSucceded = Task.FromResult(true);
private static readonly Task<bool> InsertColumnFailed = Task.FromResult(false);
/// <inheritdoc/>
public Task<bool> InsertColumn(IGridColumn targetColumn, IGridColumn insertingColumn)
{
var currentPossition = _columnsCollection.IndexOf(insertingColumn);
var targetPossition = _columnsCollection.IndexOf(targetColumn);
if (currentPossition == -1 || targetPossition == -1 || currentPossition == targetPossition)
return InsertColumnFailed;
var index = currentPossition > targetPossition ? targetPossition : targetPossition - 1;
var removed = _columnsCollection.Remove(insertingColumn);
if (!removed)
return InsertColumnFailed;
_columnsCollection.Insert(index, insertingColumn);
return InsertColumnSucceded;
}
/// <summary>
/// Fixed column values for the grid
/// </summary>
public QueryDictionary<object> FixedValues { get; set; } = null;
/// <summary>
/// Function to init values for columns in the Create form
/// </summary>
public Func<T, Task> InitCreateValues { get; set; } = null;
/// <summary>
/// Fixed column values for the OData url expand parameter
/// </summary>
public IEnumerable<string> ODataExpandList { get; set; }
/// <summary>
/// Override OData url expand parameter with list
/// </summary>
public bool ODataOverrideExpandList { get; set; } = false;
/// <summary>
/// Add code to the end of OnAfterRenderAsync method of the component
/// </summary>
public Func<GridComponent<T>, bool, Task> OnAfterRender { get; set; }
/// <summary>
/// Applies data annotations settings
/// </summary>
private 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);
}
}
}
}
/// <summary>
/// Text in empty grid (no items for display)
/// </summary>
public string EmptyGridText { get; set; }
/// <summary>
/// Create button label
/// </summary>
public string CreateLabel { get; set; }
/// <summary>
/// Read button label
/// </summary>
public string ReadLabel { get; set; }
/// <summary>
/// Update button label
/// </summary>
public string UpdateLabel { get; set; }
/// <summary>
/// Delete button label
/// </summary>
public string DeleteLabel { get; set; }
/// <summary>
/// Create button tooltip
/// </summary>
public string CreateTooltip { get; set; } = Strings.CreateItem;
/// <summary>
/// Read button tooltip
/// </summary>
public string ReadTooltip { get; set; } = Strings.ReadItem;
/// <summary>
/// Update button tooltip
/// </summary>
public string UpdateTooltip { get; set; } = Strings.UpdateItem;
/// <summary>
/// Delete button tooltip
/// </summary>
public string DeleteTooltip { get; set; } = Strings.DeleteItem;
/// <summary>
/// Create form label
/// </summary>
public string CreateFormLabel { get; set; }
/// <summary>
/// Read form label
/// </summary>
public string ReadFormLabel { get; set; }
/// <summary>
/// Update form label
/// </summary>
public string UpdateFormLabel { get; set; }
/// <summary>
/// Delete form label
/// </summary>
public string DeleteFormLabel { get; set; }
/// <summary>
/// Create form button label
/// </summary>
public string CreateFormButtonLabel { get; set; }
/// <summary>
/// Update form button label
/// </summary>
public string UpdateFormButtonLabel { get; set; }
/// <summary>
/// Delete form button label
/// </summary>
public string DeleteFormButtonLabel { get; set; }
// <summary>
/// Create CRUD confirmation fields
/// </summary>
public bool CreateConfirmation { get; set; } = false;
public int CreateConfirmationWidth { get; set; } = 5;
public int CreateConfirmationLabelWidth { get; set; } = 2;
/// <summary>
/// Update CRUD confirmation fields
/// </summary>
public bool UpdateConfirmation { get; set; } = false;
public int UpdateConfirmationWidth { get; set; } = 5;
public int UpdateConfirmationLabelWidth { get; set; } = 2;
/// <summary>
/// Delete CRUD confirmation fields
/// </summary>
public bool DeleteConfirmation { get; set; } = false;
public int DeleteConfirmationWidth { get; set; } = 5;
public int DeleteConfirmationLabelWidth { get; set; } = 2;
public bool HeaderCrudButtons { get; set; }
public bool ShowErrorsOnGrid { get; set; } = false;
public bool ThrowExceptions { get; set; } = false;
public string Error { get; set; } = "";
public bool EditAfterInsert { get; set; } = false;
public CssFramework CssFramework { get; set; }
public HtmlClass HtmlClass { get; set; }
#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
/// <summary>
/// Provides query, using by the grid
/// </summary>
public void AddQueryParameter(string parameterName, StringValues parameterValue)
{
if (string.IsNullOrEmpty(parameterName))
throw new ArgumentException("parameterName");
if (parameterName.Equals(QueryStringFilterSettings.DefaultTypeQueryParameter))
throw new ArgumentException("parameterName cannot be " + QueryStringFilterSettings.DefaultTypeQueryParameter);
if (_query.ContainsKey(parameterName))
_query[parameterName] = parameterValue;
else
_query.Add(parameterName, parameterValue);
UpdateQueryAndSettings();
}