DynamicGridColumns.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.ContainsKey(type))
  15. {
  16. _columnscache[type] = new List<DynamicGridColumn>();
  17. var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
  18. foreach (var prop in properties)
  19. {
  20. if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
  21. {
  22. var col = new DynamicGridColumn
  23. {
  24. ColumnName = prop.Name,
  25. Width = prop.Editor.Width,
  26. Alignment = prop.Editor.Alignment,
  27. Format = prop.Editor.Format,
  28. Editor = prop.Editor.CloneEditor(),
  29. Caption = prop.Caption
  30. };
  31. _columnscache[type].Add(col);
  32. }
  33. }
  34. }
  35. AddRange(_columnscache[type]);
  36. return this;
  37. }
  38. public DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
  39. Alignment alignment)
  40. {
  41. var name = CoreUtils.GetFullPropertyName(member, ".");
  42. var result = new DynamicGridColumn
  43. {
  44. ColumnName = name,
  45. Caption = caption,
  46. Width = width,
  47. Format = format,
  48. Alignment = alignment
  49. };
  50. Add(result);
  51. return result;
  52. }
  53. public static DynamicGridColumn CreateColumn<TType, TProperty>(
  54. Expression<Func<TType, TProperty>> member,
  55. int? width = null,
  56. string? caption = null,
  57. string? format = null,
  58. Alignment? alignment = null)
  59. {
  60. var prop = DatabaseSchema.Property(member);
  61. var col = new DynamicGridColumn
  62. {
  63. ColumnName = prop.Name,
  64. Width = width ?? prop.Editor.Width,
  65. Alignment = alignment ?? prop.Editor.Alignment,
  66. Format = format ?? prop.Editor.Format,
  67. Editor = prop.Editor.CloneEditor(),
  68. Caption = caption ?? prop.Caption
  69. };
  70. return col;
  71. }
  72. public DynamicGridColumn Add<TType, TProperty>(
  73. Expression<Func<TType, TProperty>> member,
  74. int? width = null,
  75. string? caption = null,
  76. string? format = null,
  77. Alignment? alignment = null
  78. )
  79. {
  80. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  81. Add(col);
  82. return col;
  83. }
  84. }
  85. public class DynamicGridColumns<T> : DynamicGridColumns
  86. {
  87. public DynamicGridColumn Add<TProperty>(Expression<Func<T, TProperty>> member, int width, string caption, string format, Alignment alignment)
  88. {
  89. return Add<T, TProperty>(member, width, caption, format, alignment);
  90. }
  91. }
  92. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  93. {
  94. public string Header { get; set; } = header;
  95. public DynamicColumnBase StartColumn { get; set; } = start;
  96. public DynamicColumnBase EndColumn { get; set; } = end;
  97. public object? Tag { get; set; } = tag;
  98. }
  99. public class DynamicGridColumnGrouping
  100. {
  101. public List<DynamicGridColumnGroup> Groups = new();
  102. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  103. {
  104. Groups.Add(new(header, start, end, tag: tag));
  105. return this;
  106. }
  107. }
  108. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  109. {
  110. }