| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Workflows.Attributes; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.UIHints.Dropdown; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides static drop-down options for a given property. |
| | | 9 | | /// </summary> |
| | | 10 | | public class StaticDropDownOptionsProvider : IPropertyUIHandler |
| | | 11 | | { |
| | 16716 | 12 | | public float Priority => -1; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public ValueTask<IDictionary<string, object>> GetUIPropertiesAsync(PropertyInfo propertyInfo, object? context, Cance |
| | | 16 | | { |
| | 2893 | 17 | | var inputAttribute = propertyInfo.GetCustomAttribute<InputAttribute>(); |
| | 2893 | 18 | | var inputOptions = inputAttribute?.Options; |
| | 2893 | 19 | | var dictionary = new Dictionary<string, object>(); |
| | 2893 | 20 | | var isNullableEnum = false; |
| | | 21 | | |
| | 2893 | 22 | | if (inputOptions == null) |
| | | 23 | | { |
| | | 24 | | // Is the property an enum? |
| | 2827 | 25 | | var wrappedPropertyType = propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTyp |
| | 2827 | 26 | | ? propertyInfo.PropertyType.GetGenericArguments()[0] |
| | 2827 | 27 | | : propertyInfo.PropertyType; |
| | | 28 | | |
| | 2827 | 29 | | if (!wrappedPropertyType.IsEnum) { |
| | | 30 | | |
| | | 31 | | // Is the property a nullable enum? |
| | 1821 | 32 | | var nullablePropertyType = Nullable.GetUnderlyingType(wrappedPropertyType); |
| | 1821 | 33 | | if (nullablePropertyType == null || !nullablePropertyType.IsEnum) { |
| | 1821 | 34 | | return new(dictionary); |
| | | 35 | | } |
| | | 36 | | else { |
| | 0 | 37 | | wrappedPropertyType = nullablePropertyType; |
| | 0 | 38 | | isNullableEnum = true; |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 1006 | 42 | | var enumValues = Enum.GetValues(wrappedPropertyType).Cast<object>().ToList(); |
| | 4448 | 43 | | var enumSelectListItems = enumValues.Select(x => new SelectListItem(x.ToString()!, x.ToString()!)).ToList(); |
| | 1006 | 44 | | if (isNullableEnum) |
| | 0 | 45 | | enumSelectListItems.Insert(0, new SelectListItem("-","")); |
| | 1006 | 46 | | var enumProps = new DropDownProps |
| | 1006 | 47 | | { |
| | 1006 | 48 | | SelectList = new SelectList(enumSelectListItems) |
| | 1006 | 49 | | }; |
| | | 50 | | |
| | 1006 | 51 | | dictionary[InputUIHints.DropDown] = enumProps; |
| | 1006 | 52 | | return new(dictionary); |
| | | 53 | | } |
| | | 54 | | |
| | 440 | 55 | | var selectListItems = (inputOptions as ICollection<string>)?.Select(x => new SelectListItem(x, x)).ToList(); |
| | | 56 | | |
| | 66 | 57 | | if (selectListItems == null) |
| | 0 | 58 | | return new(dictionary); |
| | | 59 | | |
| | 66 | 60 | | var props = new DropDownProps |
| | 66 | 61 | | { |
| | 66 | 62 | | SelectList = new SelectList(selectListItems) |
| | 66 | 63 | | }; |
| | | 64 | | |
| | 66 | 65 | | dictionary[InputUIHints.DropDown] = props; |
| | 66 | 66 | | return new(dictionary); |
| | | 67 | | } |
| | | 68 | | } |