-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHtml.cs
More file actions
180 lines (148 loc) · 5.76 KB
/
Copy pathHtml.cs
File metadata and controls
180 lines (148 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using CodeEffects.Rule.Common.Attributes;
namespace CodeEffects.Toolkit
{
public class Html
{
public static string GenerateForm(Type type, FormSettings settings = null)
=> GenerateForm(type, instance: null, prefix: "", depth: 0, settings ?? new FormSettings());
public static string GenerateForm(object instance, FormSettings settings = null)
=> GenerateForm(instance.GetType(), instance, prefix: "", depth: 0, settings ?? new FormSettings());
private static string GenerateForm(Type type, object instance, string prefix, int depth, FormSettings settings)
{
var sa = type.GetCustomAttribute<SourceAttribute>();
if(sa != null) { settings.DefaultNestingDepth = sa.MaxTypeNestingLevel; }
var sb = new StringBuilder();
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanWrite && !IsExcluded(p));
foreach(var prop in props)
{
if(IsValueTyped(prop.PropertyType))
{
var value = instance != null ? prop.GetValue(instance) : null;
sb.AppendLine(RenderForm(prop, prefix, settings, value));
}
else if(IsNestableReferenceType(prop.PropertyType) && depth < settings.DefaultNestingDepth)
{
var fa = prop.GetCustomAttribute<FieldAttribute>();
var nestedInstance = instance != null ? prop.GetValue(instance) : null;
if(settings.GenerateFieldsets) sb.AppendLine($"<fieldset><legend>{fa?.DisplayName ?? prop.Name}</legend>");
sb.AppendLine(GenerateForm(prop.PropertyType, nestedInstance, prop.Name + ".", depth + 1, settings));
if(settings.GenerateFieldsets) sb.AppendLine("</fieldset>");
}
}
return sb.ToString();
}
private static string RenderForm(PropertyInfo prop, string prefix, FormSettings settings, object value)
{
var name = prefix + prop.Name;
var (display, fa) = GetDisplayName(prop, prefix);
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
var label = $"<label for='{name}'>{display}:</label>";
string extra = "";
if(type.IsEnum)
{
// Render enums as drop downs
var options = string.Join("", Enum.GetValues(type)
.Cast<object>()
.Where(v => !IsEnumMemberExcluded(type, v))
.Select(v =>
{
var selected = value != null && Convert.ToInt32(v) == Convert.ToInt32(value) ? " selected" : "";
return $"<option value='{Convert.ToInt32(v)}'{selected}>{v}</option>";
}));
if(settings.DropDownCssClass != null)
extra = $" class='{settings.DropDownCssClass}'";
return $"{label}<select id='{name}' name='{name}'{extra}>{options}</select>";
}
string inputType =
type == typeof(bool) ? "checkbox" :
type == typeof(DateTime) ? "date" :
type == typeof(TimeSpan) ? "time" :
type == typeof(int) || type == typeof(long) || type == typeof(short) ? "number" :
type == typeof(decimal) || type == typeof(double) || type == typeof(float) ? "number" :
type == typeof(Guid) ? "text" :
"text";
if(inputType == "text" && fa != null && fa.Filter != null)
{
if(fa.Filter.ToLower() == "phone") inputType = "tel";
else if(fa.Filter.ToLower() == "email") inputType = "email";
}
if(inputType == "checkbox")
{
var isChecked = value is true ? " checked" : "";
if(settings.CheckboxCssClass != null)
extra = $" class='{settings.CheckboxCssClass}'";
return $"{label}<input type='checkbox' id='{name}' name='{name}'{isChecked}{extra} />";
}
else if(inputType == "number")
{
if(type == typeof(decimal) || type == typeof(double) || type == typeof(float))
extra += " step='any'";
if(fa != null)
{
extra += $" min='{fa?.Min ?? int.MinValue}'";
extra += $" max='{fa?.Max ?? int.MaxValue}'";
}
}
else if(inputType == "text")
{
extra += $" autocomplete='on'";
if(fa != null) extra += $" maxlength='{fa.Max}'"; // Max defaults to long.MaxValue
}
if(settings.InputCssClass != null)
extra += $" class='{settings.InputCssClass}'";
string valueAttr = "";
if(value != null)
{
valueAttr = inputType == "date"
? $" value='{((DateTime)value):yyyy-MM-dd}'"
: $" value='{value}'";
}
return $"{label}<input type='{inputType}' id='{name}' name='{name}'{valueAttr}{extra} />";
}
private static bool IsNestableReferenceType(Type t)
=> t.IsClass && t != typeof(string);
private static bool IsValueTyped(Type t)
{
// We are only interested in value type properties
var underlying = Nullable.GetUnderlyingType(t) ?? t;
return underlying.IsValueType || underlying == typeof(string);
}
private static bool IsExcluded(PropertyInfo prop)
{
// Ignor excluded properties
return prop.GetCustomAttributes(inherit: true)
.Any(a => a is ExcludeFromEvaluationAttribute);
}
private static bool IsEnumMemberExcluded(Type enumType, object value)
{
var field = enumType.GetField(value.ToString());
return field != null && field.GetCustomAttributes(inherit: false)
.Any(a => a is ExcludeFromEvaluationAttribute);
}
private static (string displayName, FieldAttribute fa) GetDisplayName(PropertyInfo prop, string prefix)
{
var displayName = prefix + prop.Name;
var pa = prop.GetCustomAttributes<ParentAttribute>();
var fa = prop.GetCustomAttribute<FieldAttribute>();
if(pa != null && pa.Count() > 0)
{
foreach(var a in pa)
{
if(a.ParentName != null && a.ParentName == prefix.TrimEnd('.'))
{
displayName = a.DisplayName;
break;
}
}
}
else if(fa != null && fa.DisplayName != null)
displayName = fa.DisplayName;
return (displayName, fa);
}
}
}