Browse Source

handle dates and datetimes on datatables MVC UI

pull/4003/head
Halil İbrahim Kalkan 6 years ago
parent
commit
f160b368a9
  1. 19
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/ui-extensions.js
  2. 114
      framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs
  3. 24
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs
  4. 113
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/ObjectExtending/ObjectExtendingPropertyInfoExtensions.cs
  5. 38
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/IBasicObjectExtensionPropertyInfo.cs
  6. 2
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ExtensionPropertyConfiguration.cs
  7. 1
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs
  8. 2
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs

19
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/ui-extensions.js

@ -96,7 +96,7 @@
get: _get get: _get
}; };
})(); })();
function initializeObjectExtensions() { function initializeObjectExtensions() {
var getShortEnumTypeName = function (enumType) { var getShortEnumTypeName = function (enumType) {
@ -209,19 +209,18 @@
orderable: false orderable: false
}; };
if (property.config.typeSimple === 'enum') { if (property.config.typeSimple === 'enum') {
columnConfig.render = function (data, type, row) { columnConfig.render = function(data, type, row) {
var value = getValueFromRow(property, row); var value = getValueFromRow(property, row);
return localizeEnumMember(property, value); return localizeEnumMember(property, value);
} }
} } else {
else if (property.config.typeSimple === 'boolean') { var defaultRenderer = abp.libs.datatables.defaultRenderers[property.config.typeSimple];
columnConfig.render = function (data, type, row) { if (defaultRenderer) {
var value = getValueFromRow(property, row); columnConfig.render = function (data, type, row) {
if (value) { var value = getValueFromRow(property, row);
return '<i class="fa fa-check"></i>'; return defaultRenderer(value);
} else {
return '<i class="fa fa-times"></i>';
} }
} }
} }

114
framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs

@ -1,19 +1,40 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using Microsoft.AspNetCore.Mvc;
namespace Volo.Abp.ObjectExtending namespace Volo.Abp.ObjectExtending
{ {
public static class MvcUiObjectExtensionPropertyInfoExtensions public static class MvcUiObjectExtensionPropertyInfoExtensions
{ {
private static readonly Type[] DateTypes = new[] private static readonly HashSet<Type> NumberTypes = new HashSet<Type> {
{ typeof(int),
typeof(DateTime), typeof(DateTimeOffset) typeof(long),
typeof(byte),
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 GetInputFormatOrNull(this ObjectExtensionPropertyInfo property) public static string GetInputFormatOrNull(this IBasicObjectExtensionPropertyInfo property)
{ {
if (IsDate(property)) if (property.IsDate())
{ {
return "{0:yyyy-MM-dd}"; return "{0:yyyy-MM-dd}";
} }
@ -21,24 +42,85 @@ namespace Volo.Abp.ObjectExtending
return null; return null;
} }
private static bool IsDate(ObjectExtensionPropertyInfo property) public static string GetInputType(this ObjectExtensionPropertyInfo propertyInfo)
{
foreach (var attribute in propertyInfo.Attributes)
{
var inputTypeByAttribute = GetInputTypeFromAttributeOrNull(attribute);
if (inputTypeByAttribute != null)
{
return inputTypeByAttribute;
}
}
return GetInputTypeFromTypeOrNull(propertyInfo.Type)
?? "text"; //default
}
private static string GetInputTypeFromAttributeOrNull(Attribute attribute)
{ {
if (!DateTypes.Contains(property.Type)) if (attribute is EmailAddressAttribute)
{ {
return false; return "email";
} }
var dataTypeAttribute = property if (attribute is UrlAttribute)
.Attributes {
.OfType<DataTypeAttribute>() return "url";
.FirstOrDefault(); }
if (attribute is HiddenInputAttribute)
{
return "hidden";
}
if (attribute is PhoneAttribute)
{
return "tel";
}
if (dataTypeAttribute == null) if (attribute is DataTypeAttribute dataTypeAttribute)
{ {
return false; 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 dataTypeAttribute.DataType == DataType.Date; return null;
}
private static string GetInputTypeFromTypeOrNull(Type type)
{
if (type == typeof(bool))
{
return "checkbox";
}
if (type == typeof(DateTime))
{
return "datetime-local";
}
if (NumberTypes.Contains(type))
{
return "number";
}
return null;
} }
} }
} }

24
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs

@ -107,9 +107,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending
var extensionPropertyDto = new ExtensionPropertyDto var extensionPropertyDto = new ExtensionPropertyDto
{ {
Type = TypeHelper.GetFullNameHandlingNullableAndGenerics(propertyConfig.Type), Type = TypeHelper.GetFullNameHandlingNullableAndGenerics(propertyConfig.Type),
TypeSimple = propertyConfig.Type.IsEnum TypeSimple = GetSimpleTypeName(propertyConfig),
? "enum"
: TypeHelper.GetSimplifiedName(propertyConfig.Type),
Attributes = new List<ExtensionPropertyAttributeDto>(), Attributes = new List<ExtensionPropertyAttributeDto>(),
DisplayName = CreateDisplayNameDto(propertyConfig), DisplayName = CreateDisplayNameDto(propertyConfig),
Configuration = new Dictionary<string, object>(), Configuration = new Dictionary<string, object>(),
@ -161,6 +159,26 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending
return extensionPropertyDto; return extensionPropertyDto;
} }
protected virtual string GetSimpleTypeName(ExtensionPropertyConfiguration propertyConfig)
{
if (propertyConfig.Type.IsEnum)
{
return "enum";
}
if (propertyConfig.IsDate())
{
return "date";
}
if (propertyConfig.IsDateTime())
{
return "datetime";
}
return TypeHelper.GetSimplifiedName(propertyConfig.Type);
}
protected virtual LocalizableStringDto CreateDisplayNameDto(ExtensionPropertyConfiguration propertyConfig) protected virtual LocalizableStringDto CreateDisplayNameDto(ExtensionPropertyConfiguration propertyConfig)
{ {
if (propertyConfig.DisplayName == null) if (propertyConfig.DisplayName == null)

113
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/ObjectExtending/ObjectExtendingPropertyInfoExtensions.cs

@ -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;
} }
} }
} }

38
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/IBasicObjectExtensionPropertyInfo.cs

@ -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; }
}
}

2
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ExtensionPropertyConfiguration.cs

@ -6,7 +6,7 @@ using Volo.Abp.Reflection;
namespace Volo.Abp.ObjectExtending.Modularity namespace Volo.Abp.ObjectExtending.Modularity
{ {
public class ExtensionPropertyConfiguration : IHasNameWithLocalizableDisplayName public class ExtensionPropertyConfiguration : IHasNameWithLocalizableDisplayName, IBasicObjectExtensionPropertyInfo
{ {
[NotNull] [NotNull]
public EntityExtensionConfiguration EntityExtensionConfiguration { get; } public EntityExtensionConfiguration EntityExtensionConfiguration { get; }

1
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/Modularity/ModuleExtensionConfigurationHelper.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace Volo.Abp.ObjectExtending.Modularity namespace Volo.Abp.ObjectExtending.Modularity

2
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs

@ -7,7 +7,7 @@ using Volo.Abp.Reflection;
namespace Volo.Abp.ObjectExtending namespace Volo.Abp.ObjectExtending
{ {
public class ObjectExtensionPropertyInfo : IHasNameWithLocalizableDisplayName public class ObjectExtensionPropertyInfo : IHasNameWithLocalizableDisplayName, IBasicObjectExtensionPropertyInfo
{ {
[NotNull] [NotNull]
public ObjectExtensionInfo ObjectExtension { get; } public ObjectExtensionInfo ObjectExtension { get; }

Loading…
Cancel
Save