2019-12-08 00:51:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
public class ClassMapper
|
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
private readonly Dictionary<Func<string>, Func<bool>> _map = new();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
public ClassMapper() : this(string.Empty)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
public ClassMapper(string originalClass)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
OriginalClass = originalClass;
|
2022-12-11 15:57:57 +08:00
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
public string Class => ToString();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
public string OriginalClass { get; internal set; }
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use ToString() instead")]
|
|
|
|
|
public string AsString() => ToString();
|
|
|
|
|
|
|
|
|
|
public override string ToString() => string.Join(' ', _map.Where(i => i.Value()).Select(i => i.Key()));
|
|
|
|
|
|
|
|
|
|
public ClassMapper Add(string name) => Get(() => name);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
public ClassMapper Get(Func<string> funcName)
|
|
|
|
|
{
|
2022-12-11 15:57:57 +08:00
|
|
|
|
_map.Add(funcName, () => !string.IsNullOrEmpty(funcName()));
|
2019-12-08 00:51:07 +08:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClassMapper GetIf(Func<string> funcName, Func<bool> func)
|
|
|
|
|
{
|
2020-05-10 15:42:02 +08:00
|
|
|
|
_map.Add(funcName, func);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
public ClassMapper If(string name, Func<bool> func) => GetIf(() => name, func);
|
2019-12-09 23:42:25 +08:00
|
|
|
|
|
|
|
|
|
public ClassMapper Clear()
|
|
|
|
|
{
|
2020-05-10 15:42:02 +08:00
|
|
|
|
_map.Clear();
|
2019-12-20 00:20:53 +08:00
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
|
2019-12-20 00:20:53 +08:00
|
|
|
|
|
2019-12-09 23:42:25 +08:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
2020-05-10 15:42:02 +08:00
|
|
|
|
}
|