DynamicGridColumns.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Configuration;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid;
  8. public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, IUserConfigurationSettings
  9. {
  10. public string GridName { get; set; }
  11. private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
  12. public DynamicGridColumns ExtractColumns(Type type)
  13. {
  14. if (!_columnscache.TryGetValue(type, out List<DynamicGridColumn>? value))
  15. {
  16. value = [];
  17. _columnscache[type] = value;
  18. var defaults = DefaultColumns.GetDefaultColumns(type);
  19. var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
  20. foreach (var prop in properties)
  21. {
  22. if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
  23. {
  24. var defaultCol = defaults.FirstOrDefault(x => x.Property == prop);
  25. if(defaultCol is not null)
  26. {
  27. value.Add(DynamicGridColumn.FromCoreGridColumn(defaultCol));
  28. }
  29. else
  30. {
  31. var col = new DynamicGridColumn
  32. {
  33. ColumnName = prop.Name,
  34. Width = prop.Editor.Width,
  35. Alignment = prop.Editor.Alignment,
  36. Format = prop.Editor.Format,
  37. Editor = prop.Editor.CloneEditor(),
  38. Caption = prop.Caption
  39. };
  40. value.Add(col);
  41. }
  42. }
  43. }
  44. }
  45. AddRange(value);
  46. return this;
  47. }
  48. public static DynamicGridColumn CreateColumn<TType>(
  49. Expression<Func<TType, object?>> member,
  50. int? width = null,
  51. string? caption = null,
  52. string? format = null,
  53. Alignment? alignment = null)
  54. {
  55. var prop = DatabaseSchema.Property(member);
  56. var col = new DynamicGridColumn
  57. {
  58. ColumnName = prop.Name,
  59. Width = width ?? prop.Editor.Width,
  60. Alignment = alignment ?? prop.Editor.Alignment,
  61. Format = format ?? prop.Editor.Format,
  62. Editor = prop.Editor.CloneEditor(),
  63. Caption = caption ?? prop.Caption
  64. };
  65. return col;
  66. }
  67. public DynamicGridColumn Add<TType>(
  68. Expression<Func<TType, object?>> member,
  69. int? width = null,
  70. string? caption = null,
  71. string? format = null,
  72. Alignment? alignment = null
  73. )
  74. {
  75. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  76. Add(col);
  77. return col;
  78. }
  79. }
  80. public class DynamicGridColumns<T> : DynamicGridColumns
  81. {
  82. public DynamicGridColumn Add(Expression<Func<T, object?>> member, int width, string caption, string format, Alignment alignment)
  83. {
  84. return Add<T>(member, width, caption, format, alignment);
  85. }
  86. public DynamicGridColumn Add(
  87. Expression<Func<T, object?>> member,
  88. int? width = null,
  89. string? caption = null,
  90. string? format = null,
  91. Alignment? alignment = null
  92. )
  93. {
  94. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  95. Add(col);
  96. return col;
  97. }
  98. }
  99. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  100. {
  101. public string Header { get; set; } = header;
  102. public DynamicColumnBase StartColumn { get; set; } = start;
  103. public DynamicColumnBase EndColumn { get; set; } = end;
  104. public object? Tag { get; set; } = tag;
  105. }
  106. public class DynamicGridColumnGrouping
  107. {
  108. public List<DynamicGridColumnGroup> Groups = new();
  109. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  110. {
  111. Groups.Add(new(header, start, end, tag: tag));
  112. return this;
  113. }
  114. }
  115. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  116. {
  117. }