DFLayoutLookupFieldProperties.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public class DFLayoutLookupFieldProperties : DFLayoutFieldProperties<string>
  8. {
  9. [DoNotSerialize]
  10. [NullEditor]
  11. public List<string> AdditionalPropertiesList = new List<string>();
  12. [ComboLookupEditor(typeof(PropertyClassLookups))]
  13. public string LookupType { get; set; } = "";
  14. // A JSON-serialized Filter for filtering entries in the lookup editor
  15. [FilterEditor]
  16. public string Filter { get; set; } = "";
  17. /// <summary>
  18. /// A list of comma-separated property names to also pull with the lookup.
  19. /// </summary>
  20. //[TextBoxEditor(ToolTip = "A comma-separated list of property names.")]
  21. [MultiLookupEditor(typeof(AdditionalPropertyLookupGenerator))]
  22. public string AdditionalProperties
  23. {
  24. get => string.Join(',', AdditionalPropertiesList);
  25. set
  26. {
  27. AdditionalPropertiesList = value.Split(',').ToList();
  28. }
  29. }
  30. public override string FormatValue(object? value)
  31. {
  32. return value?.ToString() ?? "";
  33. }
  34. public override object? ParseValue(object? value)
  35. {
  36. return value?.ToString();
  37. }
  38. public override IEnumerable<CoreColumn> GetAdditionalColumns()
  39. {
  40. if(!CoreUtils.TryGetEntity(LookupType, out var entity))
  41. return Enumerable.Empty<CoreColumn>();
  42. return AdditionalPropertiesList.Select(x =>
  43. {
  44. if (CoreUtils.TryGetProperty(entity, x, out var property))
  45. return new CoreColumn
  46. {
  47. ColumnName = $"{Code}.{x}",
  48. DataType = property.PropertyType
  49. };
  50. return null;
  51. }).Where(x => x != null).Cast<CoreColumn>();
  52. }
  53. protected override void LoadProperties()
  54. {
  55. base.LoadProperties();
  56. LookupType = GetProperty("LookupType", "");
  57. Filter = GetProperty("Filter", "");
  58. AdditionalProperties = GetProperty("AdditionalProperties", "");
  59. }
  60. protected override void SaveProperties()
  61. {
  62. base.SaveProperties();
  63. SetProperty("LookupType", LookupType);
  64. SetProperty("Filter", Filter);
  65. SetProperty("AdditionalProperties", AdditionalProperties);
  66. }
  67. private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
  68. {
  69. public AdditionalPropertyLookupGenerator(object[] items) : base(items)
  70. {
  71. }
  72. protected override void DoGenerateLookups()
  73. {
  74. if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
  75. return;
  76. if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
  77. return;
  78. var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true).Keys.ToList();
  79. props.Sort();
  80. foreach (var prop in props)
  81. {
  82. AddValue(prop, prop);
  83. }
  84. }
  85. }
  86. }
  87. }