mirror of https://github.com/abpframework/abp.git
8 changed files with 185 additions and 128 deletions
@ -1,116 +1,35 @@ |
|||||
using System; |
using System; |
||||
using System.Collections.Generic; |
|
||||
using System.ComponentModel.DataAnnotations; |
using System.ComponentModel.DataAnnotations; |
||||
using Microsoft.AspNetCore.Mvc; |
using System.Linq; |
||||
|
|
||||
namespace Volo.Abp.ObjectExtending |
namespace Volo.Abp.ObjectExtending |
||||
{ |
{ |
||||
public static class ObjectExtensionPropertyInfoAspNetCoreMvcExtensions |
public static class ObjectExtensionPropertyInfoAspNetCoreMvcExtensions |
||||
{ |
{ |
||||
private static readonly HashSet<Type> NumberTypes = new HashSet<Type> { |
private static readonly Type[] DateTimeTypes = |
||||
typeof(int), |
{ |
||||
typeof(long), |
typeof(DateTime), |
||||
typeof(byte), |
typeof(DateTimeOffset) |
||||
typeof(sbyte), |
|
||||
typeof(short), |
|
||||
typeof(ushort), |
|
||||
typeof(uint), |
|
||||
typeof(long), |
|
||||
typeof(ulong), |
|
||||
typeof(float), |
|
||||
typeof(double), |
|
||||
typeof(int?), |
|
||||
typeof(long?), |
|
||||
typeof(byte?), |
|
||||
typeof(sbyte?), |
|
||||
typeof(short?), |
|
||||
typeof(ushort?), |
|
||||
typeof(uint?), |
|
||||
typeof(long?), |
|
||||
typeof(ulong?), |
|
||||
typeof(float?), |
|
||||
typeof(double?), |
|
||||
}; |
}; |
||||
|
|
||||
public static string GetInputType(this ObjectExtensionPropertyInfo propertyInfo) |
public static bool IsDate(this IBasicObjectExtensionPropertyInfo property) |
||||
{ |
{ |
||||
foreach (var attribute in propertyInfo.Attributes) |
return DateTimeTypes.Contains(property.Type) && |
||||
{ |
property.GetDataTypeOrNull() == DataType.Date; |
||||
var inputTypeByAttribute = GetInputTypeFromAttributeOrNull(attribute); |
|
||||
if (inputTypeByAttribute != null) |
|
||||
{ |
|
||||
return inputTypeByAttribute; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return GetInputTypeFromTypeOrNull(propertyInfo.Type) |
|
||||
?? "text"; //default
|
|
||||
} |
} |
||||
|
|
||||
private static string GetInputTypeFromAttributeOrNull(Attribute attribute) |
public static bool IsDateTime(this IBasicObjectExtensionPropertyInfo property) |
||||
{ |
{ |
||||
if (attribute is EmailAddressAttribute) |
return DateTimeTypes.Contains(property.Type) && |
||||
{ |
!property.IsDate(); |
||||
return "email"; |
|
||||
} |
|
||||
|
|
||||
if (attribute is UrlAttribute) |
|
||||
{ |
|
||||
return "url"; |
|
||||
} |
|
||||
|
|
||||
if (attribute is HiddenInputAttribute) |
|
||||
{ |
|
||||
return "hidden"; |
|
||||
} |
|
||||
|
|
||||
if (attribute is PhoneAttribute) |
|
||||
{ |
|
||||
return "tel"; |
|
||||
} |
|
||||
|
|
||||
if (attribute is DataTypeAttribute dataTypeAttribute) |
|
||||
{ |
|
||||
switch (dataTypeAttribute.DataType) |
|
||||
{ |
|
||||
case DataType.Password: |
|
||||
return "password"; |
|
||||
case DataType.Date: |
|
||||
return "date"; |
|
||||
case DataType.Time: |
|
||||
return "time"; |
|
||||
case DataType.EmailAddress: |
|
||||
return "email"; |
|
||||
case DataType.Url: |
|
||||
return "url"; |
|
||||
case DataType.PhoneNumber: |
|
||||
return "tel"; |
|
||||
case DataType.DateTime: |
|
||||
return "datetime-local"; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return null; |
|
||||
} |
} |
||||
|
|
||||
private static string GetInputTypeFromTypeOrNull(Type type) |
public static DataType? GetDataTypeOrNull(this IBasicObjectExtensionPropertyInfo property) |
||||
{ |
{ |
||||
if (type == typeof(bool)) |
return property |
||||
{ |
.Attributes |
||||
return "checkbox"; |
.OfType<DataTypeAttribute>() |
||||
} |
.FirstOrDefault()?.DataType; |
||||
|
|
||||
if (type == typeof(DateTime)) |
|
||||
{ |
|
||||
return "datetime-local"; |
|
||||
} |
|
||||
|
|
||||
if (NumberTypes.Contains(type)) |
|
||||
{ |
|
||||
return "number"; |
|
||||
} |
|
||||
|
|
||||
return null; |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,38 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.Localization; |
||||
|
|
||||
|
namespace Volo.Abp.ObjectExtending |
||||
|
{ |
||||
|
public interface IBasicObjectExtensionPropertyInfo |
||||
|
{ |
||||
|
[NotNull] |
||||
|
public string Name { get; } |
||||
|
|
||||
|
[NotNull] |
||||
|
public Type Type { get; } |
||||
|
|
||||
|
[NotNull] |
||||
|
public List<Attribute> Attributes { get; } |
||||
|
|
||||
|
[NotNull] |
||||
|
public List<Action<ObjectExtensionPropertyValidationContext>> Validators { get; } |
||||
|
|
||||
|
[CanBeNull] |
||||
|
public ILocalizableString DisplayName { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Uses as the default value if <see cref="DefaultValueFactory"/> was not set.
|
||||
|
/// </summary>
|
||||
|
[CanBeNull] |
||||
|
public object DefaultValue { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Used with the first priority to create the default value for the property.
|
||||
|
/// Uses to the <see cref="DefaultValue"/> if this was not set.
|
||||
|
/// </summary>
|
||||
|
[CanBeNull] |
||||
|
public Func<object> DefaultValueFactory { get; set; } |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue