Files
WatchIt/WatchIt.Website/Components/Subcomponents/Common/Image.razor.cs

95 lines
2.4 KiB
C#
Raw Normal View History

2024-10-19 21:14:38 +02:00
using Microsoft.AspNetCore.Components;
using WatchIt.DTO.Models.Generics.Image;
2024-10-19 21:14:38 +02:00
namespace WatchIt.Website.Components.Subcomponents.Common;
2024-10-19 21:14:38 +02:00
public partial class Image : Component
2024-10-19 21:14:38 +02:00
{
#region PARAMETERS
[Parameter] public ImageBase? Content { get; set; }
2024-10-19 21:14:38 +02:00
[Parameter] public required string Placeholder { get; set; }
[Parameter] public ImageComponentAspectRatio AspectRatio { get; set; } = ImageComponentAspectRatio.Default;
2024-10-19 21:14:38 +02:00
[Parameter] public string AlternativeText { get; set; } = "picture";
[Parameter] public string Class { get; set; } = string.Empty;
2024-10-19 22:05:53 +02:00
[Parameter] public int? Height { get; set; }
[Parameter] public int? Width { get; set; }
[Parameter] public bool Circle { get; set; }
[Parameter] public bool Shadow { get; set; } = true;
2024-10-19 22:05:53 +02:00
#endregion
#region FIELDS
private Dictionary<string, object> _attributes = [];
#endregion
#region PRIVATE METHODS
protected override void OnParametersSet()
{
_attributes.Clear();
if (Height.HasValue)
{
_attributes.Add("height", Height.Value);
}
else if (Width.HasValue)
{
_attributes.Add("width", Width.Value);
}
if (Circle)
{
AspectRatio = ImageComponentAspectRatio.Square;
}
2024-10-19 22:05:53 +02:00
}
2024-10-19 21:14:38 +02:00
#endregion
#region STRUCTS
public struct ImageComponentAspectRatio
2024-10-19 21:14:38 +02:00
{
#region Properties
public int Vertical { get; set; }
public int Horizontal { get; set; }
#endregion
#region Constructors
public ImageComponentAspectRatio() : this(3, 5) {}
2024-10-19 21:14:38 +02:00
public ImageComponentAspectRatio(int horizontal, int vertical)
2024-10-19 21:14:38 +02:00
{
Horizontal = horizontal;
Vertical = vertical;
}
public static readonly ImageComponentAspectRatio Default = new ImageComponentAspectRatio();
public static readonly ImageComponentAspectRatio Photo = new ImageComponentAspectRatio(16, 9);
public static readonly ImageComponentAspectRatio Square = new ImageComponentAspectRatio(1, 1);
2024-10-19 21:14:38 +02:00
#endregion
#region Public methods
public override string ToString() => $"{Horizontal}/{Vertical}";
#endregion
}
#endregion
}