DynamicGridColumns.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 DynamicGridColumn Add<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. {
  61. var prop = DatabaseSchema.Property(member);
  62. var col = new DynamicGridColumn
  63. {
  64. ColumnName = prop.Name,
  65. Width = width ?? prop.Editor.Width,
  66. Alignment = alignment ?? prop.Editor.Alignment,
  67. Format = format ?? prop.Editor.Format,
  68. Editor = prop.Editor.CloneEditor(),
  69. Caption = caption ?? prop.Caption
  70. };
  71. Add(col);
  72. return col;
  73. }
  74. }
  75. public class DynamicGridColumns<T> : DynamicGridColumns
  76. {
  77. public DynamicGridColumn Add<TProperty>(Expression<Func<T, TProperty>> member, int width, string caption, string format, Alignment alignment)
  78. {
  79. return Add<T, TProperty>(member, width, caption, format, alignment);
  80. }
  81. }
  82. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
  83. {
  84. public string Header { get; set; } = header;
  85. public DynamicColumnBase StartColumn { get; set; } = start;
  86. public DynamicColumnBase EndColumn { get; set; } = end;
  87. }
  88. public class DynamicGridColumnGrouping
  89. {
  90. public List<DynamicGridColumnGroup> Groups = new();
  91. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
  92. {
  93. Groups.Add(new(header, start, end));
  94. return this;
  95. }
  96. }
  97. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  98. {
  99. }