2019-12-11 00:23:10 +08:00
|
|
|
|
using System;
|
2019-12-17 10:21:42 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2019-12-11 00:23:10 +08:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using OneOf;
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
namespace AntBlazor
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public partial class AntIcon : AntDomComponentBase
|
2019-12-11 00:23:10 +08:00
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Spin { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public OneOf<int, string> Rotate { get; set; } = 0;
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Type { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 'fill' | 'outline' | 'twotone';
|
|
|
|
|
/// </summary>
|
2019-12-11 00:23:10 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Theme { get; set; } = AntIconThemeType.Outline;
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string TwotoneColor { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string IconFont { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Width { get; set; } = "1em";
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Height { get; set; } = "1em";
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Fill { get; set; } = "currentColor";
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string TabIndex { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
2019-12-16 15:33:29 +08:00
|
|
|
|
[CascadingParameter]
|
|
|
|
|
public AntButton Button { get; set; }
|
|
|
|
|
|
2019-12-11 00:23:10 +08:00
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public EventCallback<MouseEventArgs> Onclick { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Inject]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private HttpClient HttpClient { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private NavigationManager NavigationManager { get; set; }
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<string, string> _svgCache = new ConcurrentDictionary<string, string>();
|
2019-12-16 18:45:02 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private string SvgImg { get; set; }
|
2019-12-11 00:23:10 +08:00
|
|
|
|
private string SvgStyle { get; set; }
|
|
|
|
|
private string _iconSvg;
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private Uri _baseUrl;
|
2019-12-16 18:45:02 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public void SetClassSet()
|
2019-12-11 00:23:10 +08:00
|
|
|
|
{
|
|
|
|
|
string prefixName = "anticon";
|
|
|
|
|
ClassMapper.Add(prefixName)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{prefixName}-spin", () => Spin || this.Type == "loading");
|
2019-12-11 00:23:10 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
SvgStyle = $"focusable=\"false\" width=\"{Width}\" height=\"{Height}\" fill=\"{Fill}\"";
|
2019-12-11 00:23:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
this.SetClassSet();
|
2019-12-18 16:57:00 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
_baseUrl = NavigationManager.ToAbsoluteUri(NavigationManager.BaseUri);
|
2019-12-16 15:33:29 +08:00
|
|
|
|
|
|
|
|
|
if (this is AntIcon icon)
|
|
|
|
|
{
|
|
|
|
|
Button?.Icons.Add(icon);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 17:17:58 +08:00
|
|
|
|
await base.OnInitializedAsync();
|
2019-12-11 00:23:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 18:45:02 +08:00
|
|
|
|
protected override async Task OnParametersSetAsync()
|
2019-12-11 00:23:10 +08:00
|
|
|
|
{
|
2019-12-16 18:45:02 +08:00
|
|
|
|
await SetupSvgImg();
|
|
|
|
|
await base.OnParametersSetAsync();
|
2019-12-11 00:23:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 18:45:02 +08:00
|
|
|
|
protected async Task SetupSvgImg()
|
2019-12-11 00:23:10 +08:00
|
|
|
|
{
|
2020-03-11 17:37:00 +08:00
|
|
|
|
try
|
2019-12-16 18:45:02 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (_svgCache.TryGetValue($"{Theme}-{Type}", out var svg))
|
2020-03-11 17:37:00 +08:00
|
|
|
|
{
|
|
|
|
|
_iconSvg = svg;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
_iconSvg = await HttpClient.GetStringAsync(new Uri(_baseUrl, $"_content/AntBlazor/icons/{Theme.ToLower()}/{Type.ToLower()}.svg"));
|
|
|
|
|
_svgCache.TryAdd($"{Theme}-{Type}", _iconSvg);
|
2020-03-11 17:37:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SvgImg = _iconSvg.Insert(_iconSvg.IndexOf("svg", StringComparison.Ordinal) + 3, $" {SvgStyle} ");
|
2019-12-16 18:45:02 +08:00
|
|
|
|
}
|
2020-03-11 17:37:00 +08:00
|
|
|
|
catch
|
2019-12-16 18:45:02 +08:00
|
|
|
|
{
|
2020-03-11 17:37:00 +08:00
|
|
|
|
// ignored
|
2019-12-16 18:45:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 16:57:00 +08:00
|
|
|
|
StateHasChanged();
|
2019-12-11 00:23:10 +08:00
|
|
|
|
}
|
2019-12-16 15:33:29 +08:00
|
|
|
|
|
|
|
|
|
public async Task OnClick(MouseEventArgs args)
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (Onclick.HasDelegate)
|
2019-12-16 15:33:29 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await Onclick.InvokeAsync(args);
|
2019-12-16 15:33:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-11 00:23:10 +08:00
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|