/// <summary> /// Получить список из булевых значений /// </summary> /// <param name="isNullable"></param> /// <param name="opts"></param> /// <returns></returns> internal static List <SelectListItem> GetBooleanList(bool isNullable, GenericInterfaceOptions opts) { var list = new List <SelectListItem>(); if (isNullable) { list.Add(new SelectListItem { Selected = true, Text = opts.NotSelectedText, Value = null }); } list.AddRange(new List <SelectListItem> { new SelectListItem { Selected = !isNullable, Text = opts.TextOnFalse, Value = false.ToString(), }, new SelectListItem { Text = opts.TextOnTrue, Value = true.ToString() } }); return(list); }
/// <summary> /// Конструктор /// </summary> /// <param name="serviceProvider"></param> /// <param name="bagOptions"></param> /// <param name="options"></param> public GenericUserInterfaceBag(IServiceProvider serviceProvider, GenericUserInterfaceBagOptions bagOptions, GenericInterfaceOptions options) { ApplicationHostUrl = bagOptions.ApplicationHostUrl; SelectListDataProviders = new ConcurrentDictionary <string, Type>(bagOptions.SelectListDataProviders); DefaultInterfaceOverriders = new ConcurrentDictionary <Type, Type>(bagOptions.DefaultInterfaceOverriders); AutoCompletionDataProviders = new ConcurrentDictionary <string, Type>(bagOptions.AutoCompletionDataProviders); ServiceProvider = serviceProvider; Options = options; }
/// <summary> /// Создать дефолтный пустой портфель интерфейсов /// </summary> /// <returns></returns> public static GenericUserInterfaceBag CreateDefaultBag() { var serviceProvider = new ServiceCollection().BuildServiceProvider(); return(new GenericUserInterfaceBag(serviceProvider, new GenericUserInterfaceBagOptions { AutoCompletionDataProviders = new Dictionary <string, Type>(), SelectListDataProviders = new Dictionary <string, Type>(), DefaultInterfaceOverriders = new Dictionary <Type, Type>() }, GenericInterfaceOptions.Default())); }
/// <summary> /// Построить /// </summary> public void Build() { if (InterfaceOptions == null) { InterfaceOptions = GenericInterfaceOptions.Default(); } ServiceCollection.AddSingleton(InterfaceOptions); ServiceCollection.AddSingleton(new GenericUserInterfaceBagOptions { SelectListDataProviders = SelectListDataProviders, AutoCompletionDataProviders = AutoCompletionDataProviders, DefaultInterfaceOverriders = DefaultInterfaceOverriders, ApplicationHostUrl = _applicationHostUrl }); ServiceCollection.AddSingleton <GenericUserInterfaceBag>(); }
/// <summary> /// Создать из типа /// </summary> /// <param name="type"></param> /// <param name="valueJson"></param> /// <param name="opts"></param> /// <returns></returns> private GenerateGenericUserInterfaceModel CreateFromType(Type type, string valueJson = null, GenericInterfaceOptions opts = null) { var desc = TypeDescriptionBuilder.GetTypeDescriptionResult(type); var main = desc.GetMainTypeDescription(); if (!main.IsClass) { throw new InvalidOperationException(ExceptionTexts.NonComplexTypesAreNotSupported); } var typeChecker = new CrocoClassDescriptionChecker(); if (typeChecker.IsRecursiveType(desc)) { throw new InvalidOperationException(ExceptionTexts.RecursiveTypesAreNotSupported); } if (typeChecker.HasMultiDimensionalArrays(desc)) { throw new InvalidOperationException(ExceptionTexts.ClassesWithMultiDimensionalArrayPropertiesAreNotSupported); } if (opts == null) { opts = GenericInterfaceOptions.Default(); } var prefix = string.Empty; var blocks = GenericUserInterfaceModelBuilderExtensions.GetBlocks(prefix, desc.GetMainTypeDescription(), desc, opts); return(new GenerateGenericUserInterfaceModel { TypeDescription = desc, Interface = new GenericInterfaceModel { Prefix = prefix, Blocks = blocks }, ValueJson = valueJson }); }
internal static List <UserInterfaceBlock> GetBlocks(string prefix, CrocoTypeDescription currentType, CrocoTypeDescriptionResult desc, GenericInterfaceOptions opts) { List <UserInterfaceBlock> result = new List <UserInterfaceBlock>(); foreach (var prop in currentType.Properties) { result.Add(GetBlockFromProperty(prefix, prop, desc, opts)); } return(result); }
private static UserInterfaceBlock GetBlockForEnumerable(string prefix, CrocoPropertyReferenceDescription prop, CrocoTypeDescriptionResult desc, GenericInterfaceOptions opts) { var type = desc.GetTypeDescription(prop.TypeDisplayFullName); if (!type.ArrayDescription.IsArray) { throw new InvalidOperationException("Type is not of type enumerable"); } var enumeratedType = desc.GetTypeDescription(type.ArrayDescription.ElementDiplayFullTypeName); if (enumeratedType.IsPrimitive) { return(new UserInterfaceBlock(prop) { InterfaceType = UserInterfaceType.MultipleDropDownList, DropDownData = GetSelectListData(prop, desc, opts) }); } var newPrefix = AddPropNameToPrefix(prefix, prop.PropertyDescription.PropertyName); return(new UserInterfaceBlock(prop) { InterfaceType = UserInterfaceType.GenericInterfaceForArray, InnerGenericInterface = new GenericInterfaceModel { Prefix = newPrefix, Blocks = GetBlocks(newPrefix, enumeratedType, desc, opts) } }); }
private static DropDownListData GetSelectListData(CrocoPropertyReferenceDescription propDescr, CrocoTypeDescriptionResult main, GenericInterfaceOptions opts) { var emptySelectListPredicates = new List <Func <CrocoTypeDescription, bool> > { x => x.ArrayDescription.IsArray, x => x.ExtractType() == typeof(DateTime) || x.ExtractType() == typeof(DateTime?) }; var propTypeDesc = main.GetTypeDescription(propDescr.TypeDisplayFullName); if (propTypeDesc.ArrayDescription.IsArray) { var enumeratedType = main.GetTypeDescription(propTypeDesc.ArrayDescription.ElementDiplayFullTypeName); if (enumeratedType.IsEnumeration) { return(new DropDownListData { SelectList = enumeratedType.EnumDescription.EnumValues.Select(x => new SelectListItem { Text = x.DisplayName, Value = x.StringRepresentation }).ToList(), CanAddNewItem = false }); } } if (propTypeDesc.ExtractType() == typeof(bool) || propTypeDesc.ExtractType() == typeof(bool?)) { return(new DropDownListData { SelectList = MySelectListItemExtensions.GetBooleanList(propTypeDesc.IsNullable, opts), CanAddNewItem = false }); } if (emptySelectListPredicates.Any(x => x(propTypeDesc))) { return(new DropDownListData { SelectList = new List <SelectListItem>(), CanAddNewItem = true }); } if (propTypeDesc.IsEnumeration) { return(ForEnumDropDownDataBuilder.GetDropDownListData(propTypeDesc, opts)); } return(null); }
private static UserInterfaceBlock GetBlockFromProperty(string prefix, CrocoPropertyReferenceDescription prop, CrocoTypeDescriptionResult main, GenericInterfaceOptions opts) { var propTypeDescription = main.GetTypeDescription(prop.TypeDisplayFullName); if (!propTypeDescription.IsClass && !propTypeDescription.ArrayDescription.IsArray) { var type = GetUserInterfaceType(propTypeDescription); var selectList = GetSelectListData(prop, main, opts); return(new UserInterfaceBlock(prop) { InterfaceType = type, DropDownData = selectList, NumberBoxData = GetNumberBoxDataForProperty(propTypeDescription, type) }); } if (propTypeDescription.ArrayDescription.IsArray) { return(GetBlockForEnumerable(prefix, prop, main, opts)); } if (propTypeDescription.IsClass) { var propDesc = main.GetTypeDescription(propTypeDescription.TypeDisplayFullName); var newPrefix = AddPropNameToPrefix(prefix, prop.PropertyDescription.PropertyName); return(new UserInterfaceBlock(prop) { InterfaceType = UserInterfaceType.GenericInterfaceForClass, InnerGenericInterface = new GenericInterfaceModel { Prefix = newPrefix, Blocks = GetBlocks(newPrefix, propDesc, main, opts) } }); } throw new Exception("Not supported"); }
/// <summary> /// Конструктор /// </summary> /// <param name="type"></param> /// <param name="valueJson"></param> /// <param name="opts"></param> public GenericUserInterfaceModelBuilder(Type type, string valueJson, GenericInterfaceOptions opts) { Result = CreateFromType(type, valueJson, opts); }
/// <summary> /// Конструктор /// </summary> /// <param name="type"></param> /// <param name="opts"></param> public GenericUserInterfaceModelBuilder(Type type, GenericInterfaceOptions opts) : this(type, null, opts) { }
/// <summary> /// Конструктор /// </summary> /// <param name="opts"></param> /// <param name="bag"></param> public GenericUserInterfaceModelBuilder(GenericInterfaceOptions opts, GenericUserInterfaceBag bag) : base(typeof(TModel), null, opts) { Bag = bag; }
public static DropDownListData GetDropDownListData(CrocoTypeDescription propTypeDesc, GenericInterfaceOptions opts) { if (!propTypeDesc.IsEnumeration) { throw new InvalidOperationException($"{propTypeDesc.FullTypeName} is not enum"); } var enumsList = new List <SelectListItem>(); if (propTypeDesc.IsNullable) { enumsList.Add(new SelectListItem { Value = null, Selected = true, Text = opts.NotSelectedText }); } enumsList.AddRange(propTypeDesc.EnumDescription.EnumValues.Select(x => new SelectListItem { Value = x.StringRepresentation, Text = x.DisplayName })); if (!enumsList.Any(x => x.Selected)) { var first = enumsList.FirstOrDefault(); if (first != null) { first.Selected = true; } } return(new DropDownListData { CanAddNewItem = false, SelectList = enumsList }); }