DFLayoutLookupFieldProperties.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. [ComboMultiLookupEditor(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. var entity = CoreUtils.GetEntity(LookupType);
  41. if(entity is null)
  42. {
  43. return Enumerable.Empty<CoreColumn>();
  44. }
  45. return AdditionalPropertiesList.Select(x =>
  46. {
  47. if (CoreUtils.TryGetProperty(entity, x, out var property))
  48. return new CoreColumn
  49. {
  50. ColumnName = $"{Code}.{x}",
  51. DataType = property.PropertyType
  52. };
  53. return null;
  54. }).Where(x => x != null).Cast<CoreColumn>();
  55. }
  56. protected override void LoadProperties()
  57. {
  58. base.LoadProperties();
  59. LookupType = GetProperty("LookupType", "");
  60. Filter = GetProperty("Filter", "");
  61. AdditionalProperties = GetProperty("AdditionalProperties", "");
  62. }
  63. protected override void SaveProperties()
  64. {
  65. base.SaveProperties();
  66. SetProperty("LookupType", LookupType);
  67. SetProperty("Filter", Filter);
  68. SetProperty("AdditionalProperties", AdditionalProperties);
  69. }
  70. private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
  71. {
  72. public AdditionalPropertyLookupGenerator(object[] items) : base(items)
  73. {
  74. }
  75. protected override void DoGenerateLookups()
  76. {
  77. if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
  78. return;
  79. if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
  80. return;
  81. var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true);
  82. foreach (var prop in props.Keys)
  83. {
  84. AddValue(prop, prop);
  85. }
  86. }
  87. }
  88. }
  89. }