2021-01-25 02:05:18 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Image : AntDomComponentBase
|
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Alt { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Fallback { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Height { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Width { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment Placeholder { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Preview { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2021-01-30 22:39:56 +08:00
|
|
|
|
public string Src
|
|
|
|
|
{
|
|
|
|
|
get => _src;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_src != value)
|
|
|
|
|
{
|
|
|
|
|
_loaded = false;
|
|
|
|
|
_isError = false;
|
|
|
|
|
_src = value;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(PreviewSrc))
|
|
|
|
|
{
|
|
|
|
|
PreviewSrc = _src;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string PreviewSrc { get; set; }
|
2021-01-25 02:05:18 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public ImageLocale Locale { get; set; } = LocaleProvider.CurrentLocale.Image;
|
|
|
|
|
|
2021-01-30 22:39:56 +08:00
|
|
|
|
[CascadingParameter]
|
|
|
|
|
private ImagePreviewGroup Group { get; set; }
|
|
|
|
|
|
2021-01-25 02:05:18 +08:00
|
|
|
|
[Inject]
|
|
|
|
|
private ImageService ImageService { get; set; }
|
|
|
|
|
|
|
|
|
|
private bool _isError;
|
|
|
|
|
|
|
|
|
|
private string _imgStyle;
|
|
|
|
|
|
|
|
|
|
private bool _loaded;
|
2021-01-30 22:39:56 +08:00
|
|
|
|
private string _src;
|
|
|
|
|
private ImageRef _imageRef;
|
2021-01-25 02:05:18 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Width))
|
|
|
|
|
{
|
|
|
|
|
Style = $"width:{(CssSizeLength)Width};" + Style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Height))
|
|
|
|
|
{
|
|
|
|
|
Style = $"height:{(CssSizeLength)Height};" + Style;
|
|
|
|
|
_imgStyle = $"height:{(CssSizeLength)Height};" + _imgStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 22:39:56 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(PreviewSrc))
|
|
|
|
|
{
|
|
|
|
|
PreviewSrc = _src;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 02:05:18 +08:00
|
|
|
|
ClassMapper.Add("ant-image")
|
|
|
|
|
.If("ant-image-error", () => _isError);
|
2021-01-30 22:39:56 +08:00
|
|
|
|
|
|
|
|
|
Group?.AddImage(this);
|
2021-01-25 02:05:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleOnError()
|
|
|
|
|
{
|
2021-01-30 23:39:40 +08:00
|
|
|
|
Src = Fallback;
|
|
|
|
|
|
2021-01-30 22:39:56 +08:00
|
|
|
|
_loaded = true;
|
2021-01-25 02:05:18 +08:00
|
|
|
|
_isError = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleOnLoad()
|
|
|
|
|
{
|
|
|
|
|
_loaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleOnLoadStart()
|
|
|
|
|
{
|
|
|
|
|
_loaded = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPreview()
|
|
|
|
|
{
|
2021-01-30 22:39:56 +08:00
|
|
|
|
var images = Group?.Images ?? new List<Image>() { this };
|
|
|
|
|
var index = images.IndexOf(this);
|
|
|
|
|
|
|
|
|
|
_imageRef = ImageService.OpenImages(images);
|
|
|
|
|
|
|
|
|
|
_imageRef.SwitchTo(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
Group?.Remove(this);
|
|
|
|
|
base.Dispose(disposing);
|
2021-01-25 02:05:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|