Salmon/Salmon.Web/Components/PrettyValueDisplay.razor
2025-02-03 19:16:31 +01:00

155 lines
3.9 KiB
Plaintext

@using Salmon.Core.Cliff
@if(Value is null)
{
<span>(null)</span>
}
else
{
if(Value is bool b)
{
if(b)
{
<span style="color:green;">
<i class="bi bi-check"></i>
true
</span>
}
else
{
<span style="color:red;">
<i class="bi bi-ban"></i>
false
</span>
}
}
else if (IsIntegerType(Value))
{
<span style="color:darkblue;">
@PrettifyInteger(Value)
</span>
}
else if (IsFloatingType(Value))
{
<span style="color:darkblue;">
@PrettifyFloating(Value)
</span>
}
else if (Predicate is not null && Predicate == "parent")
{
<a href=@($"Element/{System.Web.HttpUtility.UrlEncode(Value.ToString())}")>@Value.ToString()</a>
}
else if (Predicate is not null && Predicate == "Uri")
{
<a href=@Value.ToString()>@Value.ToString()</a>
}
else
{
<span>@Value.ToString()</span>
}
<CopyButton Value=@Value.ToString() />
}
@code {
[Parameter]
public object? Value { get; set; }
[Parameter]
public string Predicate { get; set; }
public static bool IsNumericType(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
public static bool IsFloatingType(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
public static bool IsIntegerType(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return true;
default:
return false;
}
}
public static string PrettifyFloating(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Decimal:
return ((Decimal)o).ToString("### ### ### ### ### ###");
case TypeCode.Double:
return ((Double)o).ToString("### ### ### ### ### ###");
case TypeCode.Single:
return ((Single)o).ToString("### ### ### ### ### ###");
default:
throw new ArgumentException();
}
}
public static string PrettifyInteger(object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
return o.ToString();
case TypeCode.UInt16:
return ((UInt16)o).ToString("# ###");
case TypeCode.UInt32:
return ((UInt32)o).ToString("# ###");
case TypeCode.UInt64:
return ((UInt64)o).ToString("# ###");
case TypeCode.Int16:
return ((Int16)o).ToString("# ###");
case TypeCode.Int32:
return ((Int32)o).ToString("# ###");
case TypeCode.Int64:
return ((Int64)o).ToString("# ###");
default:
throw new ArgumentException();
}
}
}