DynamicGridColumns.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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,
  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. }
  54. public class DynamicGridColumns<T> : DynamicGridColumns
  55. {
  56. public DynamicGridColumn Add<TProperty>(Expression<Func<T, TProperty>> member, int width, string caption, string format, Alignment alignment)
  57. {
  58. return Add<T, TProperty>(member, width, caption, format, alignment);
  59. }
  60. }
  61. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
  62. {
  63. public string Header { get; set; } = header;
  64. public DynamicColumnBase StartColumn { get; set; } = start;
  65. public DynamicColumnBase EndColumn { get; set; } = end;
  66. }
  67. public class DynamicGridColumnGrouping
  68. {
  69. public List<DynamicGridColumnGroup> Groups = new();
  70. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
  71. {
  72. Groups.Add(new(header, start, end));
  73. return this;
  74. }
  75. }
  76. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  77. {
  78. }