DynamicGridColumns.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
  49. Alignment alignment)
  50. {
  51. var name = CoreUtils.GetFullPropertyName(member, ".");
  52. var result = new DynamicGridColumn
  53. {
  54. ColumnName = name,
  55. Caption = caption,
  56. Width = width,
  57. Format = format,
  58. Alignment = alignment
  59. };
  60. Add(result);
  61. return result;
  62. }
  63. public static DynamicGridColumn CreateColumn<TType, TProperty>(
  64. Expression<Func<TType, TProperty>> member,
  65. int? width = null,
  66. string? caption = null,
  67. string? format = null,
  68. Alignment? alignment = null)
  69. {
  70. var prop = DatabaseSchema.Property(member);
  71. var col = new DynamicGridColumn
  72. {
  73. ColumnName = prop.Name,
  74. Width = width ?? prop.Editor.Width,
  75. Alignment = alignment ?? prop.Editor.Alignment,
  76. Format = format ?? prop.Editor.Format,
  77. Editor = prop.Editor.CloneEditor(),
  78. Caption = caption ?? prop.Caption
  79. };
  80. return col;
  81. }
  82. public DynamicGridColumn Add<TType, TProperty>(
  83. Expression<Func<TType, TProperty>> member,
  84. int? width = null,
  85. string? caption = null,
  86. string? format = null,
  87. Alignment? alignment = null
  88. )
  89. {
  90. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  91. Add(col);
  92. return col;
  93. }
  94. }
  95. public class DynamicGridColumns<T> : DynamicGridColumns
  96. {
  97. public DynamicGridColumn Add<TProperty>(Expression<Func<T, TProperty>> member, int width, string caption, string format, Alignment alignment)
  98. {
  99. return Add<T, TProperty>(member, width, caption, format, alignment);
  100. }
  101. public DynamicGridColumn Add<TProperty>(
  102. Expression<Func<T, TProperty>> member,
  103. int? width = null,
  104. string? caption = null,
  105. string? format = null,
  106. Alignment? alignment = null
  107. )
  108. {
  109. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  110. Add(col);
  111. return col;
  112. }
  113. }
  114. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  115. {
  116. public string Header { get; set; } = header;
  117. public DynamicColumnBase StartColumn { get; set; } = start;
  118. public DynamicColumnBase EndColumn { get; set; } = end;
  119. public object? Tag { get; set; } = tag;
  120. }
  121. public class DynamicGridColumnGrouping
  122. {
  123. public List<DynamicGridColumnGroup> Groups = new();
  124. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  125. {
  126. Groups.Add(new(header, start, end, tag: tag));
  127. return this;
  128. }
  129. }
  130. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  131. {
  132. }