Created static object provider + added some properties
This commit is contained in:
@@ -7,8 +7,10 @@ public interface IAdminPropertyGenerator {
|
||||
IAdminPropertyGenerator DisplayValueWhileEditing(bool display);
|
||||
IAdminPropertyGenerator DisplayInListing(bool display = true);
|
||||
IAdminPropertyGenerator Bold(bool isBold = true);
|
||||
IAdminPropertyGenerator Ignore(bool ignore = true);
|
||||
|
||||
IAdminPropertyGenerator DisplayName(string displayName);
|
||||
IAdminPropertyGenerator Description(string description);
|
||||
IAdminPropertyGenerator Prefix(string prefix);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System.Reflection;
|
||||
using HopFrame.Web.Admin.Attributes;
|
||||
using HopFrame.Web.Admin.Attributes.Classes;
|
||||
using HopFrame.Web.Admin.Models;
|
||||
using HopFrame.Web.Admin.Providers;
|
||||
|
||||
namespace HopFrame.Web.Admin.Generators.Implementation;
|
||||
|
||||
@@ -40,7 +41,7 @@ internal class AdminContextGenerator : IAdminContextGenerator {
|
||||
foreach (var property in properties) {
|
||||
var propertyType = property.PropertyType.GenericTypeArguments[0];
|
||||
var pageGeneratorType = typeof(AdminPageGenerator<>).MakeGenericType(propertyType);
|
||||
var generatorInstance = Activator.CreateInstance(pageGeneratorType, [propertyType.Name]);
|
||||
var generatorInstance = Activator.CreateInstance(pageGeneratorType, [property.Name]); // Calls constructor with title attribute
|
||||
|
||||
var populatorMethod = typeof(AdminContextGenerator)
|
||||
.GetMethod(nameof(ApplyConfigurationFromAttributes))?
|
||||
@@ -88,4 +89,15 @@ internal class AdminContextGenerator : IAdminContextGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterPages(AdminPagesContext context, IAdminPagesProvider provider) {
|
||||
var properties = context.GetType().GetProperties();
|
||||
|
||||
foreach (var property in properties) {
|
||||
var page = property.GetValue(context) as AdminPage;
|
||||
if (page is null) continue;
|
||||
|
||||
provider.RegisterAdminPage(page.Title.ToLower(), page);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,9 +24,6 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
|
||||
foreach (var property in properties) {
|
||||
var attributes = property.GetCustomAttributes(false);
|
||||
var ignoreProperty = attributes
|
||||
.SingleOrDefault(a => a is AdminIgnoreAttribute) as AdminIgnoreAttribute;
|
||||
if (ignoreProperty?.OnlyForListing == false) continue;
|
||||
|
||||
var generator = Activator.CreateInstance(typeof(AdminPropertyGenerator), [property.Name, property.PropertyType]) as AdminPropertyGenerator;
|
||||
|
||||
@@ -139,8 +136,12 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
if (attributes.Any(a => a is AdminBoldAttribute))
|
||||
generator.Bold();
|
||||
|
||||
if (attributes.Any(a => a is AdminIgnoreAttribute))
|
||||
if (attributes.Any(a => a is AdminIgnoreAttribute)) {
|
||||
var attribute = attributes.Single(a => a is AdminIgnoreAttribute) as AdminIgnoreAttribute;
|
||||
generator.DisplayInListing(false);
|
||||
generator.Sortable(false);
|
||||
generator.Ignore(attribute?.OnlyForListing == false);
|
||||
}
|
||||
|
||||
if (attributes.Any(a => a is AdminHideValueAttribute))
|
||||
generator.DisplayValueWhileEditing(false);
|
||||
@@ -154,6 +155,11 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
var attribute = attributes.Single(a => a is AdminDescriptionAttribute) as AdminDescriptionAttribute;
|
||||
generator.Description(attribute?.Description);
|
||||
}
|
||||
|
||||
if (attributes.Any(a => a is AdminPrefixAttribute)) {
|
||||
var attribute = attributes.Single(a => a is AdminPrefixAttribute) as AdminPrefixAttribute;
|
||||
generator.Prefix(attribute?.Prefix);
|
||||
}
|
||||
}
|
||||
|
||||
private static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
|
||||
|
||||
@@ -26,6 +26,7 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
|
||||
public IAdminPropertyGenerator DisplayInListing(bool display = true) {
|
||||
_property.DisplayInListing = display;
|
||||
_property.Sortable = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -34,6 +35,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator Ignore(bool ignore = false) {
|
||||
_property.Ignore = ignore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator DisplayName(string displayName) {
|
||||
_property.DisplayName = displayName;
|
||||
return this;
|
||||
@@ -44,6 +50,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator Prefix(string prefix) {
|
||||
_property.Prefix = prefix;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AdminPageProperty Compile() {
|
||||
_property.DisplayName ??= _property.Name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user