Files
2024-09-24 16:19:36 +02:00

332 lines
7.7 KiB
C#

using Microsoft.AspNetCore.Components;
namespace EgwCoreLib.Razor
{
public partial class DataPager : ComponentBase
{
#region Public Enums
public enum ObjSize
{
small,
standard,
large
}
#endregion Public Enums
#region Public Properties
/// <summary>
/// Num pagina correntemente selezioanta
/// </summary>
[Parameter]
public int currPage
{
get
{
return _numPage;
}
set
{
bool doReport = !_numPage.Equals(value);
if (doReport)
{
_numPage = value;
reportChangePage();
}
}
}
/// <summary>
/// Dimensione controlli x resize
/// </summary>
[Parameter]
public ObjSize DisplSize { get; set; } = ObjSize.standard;
/// <summary>
/// Export (csv) abilitato
/// </summary>
[Parameter]
public bool exportEnabled { get; set; } = false;
/// <summary>
/// Evento richiesta export
/// </summary>
[Parameter]
public EventCallback<int> exportRequested { get; set; }
/// <summary>
/// Filename x export
/// </summary>
[Parameter]
public string fileName { get; set; } = "";
/// <summary>
/// Evento cambio pagina nel paginatore
/// </summary>
[Parameter]
public EventCallback<int> numPageChanged { get; set; }
/// <summary>
/// Massimo numero di pagine da mostrare (x display piccoli)
/// </summary>
[Parameter]
public int NumPages { get; set; } = 10;
/// <summary>
/// Test btn vai a inizio
/// </summary>
[Parameter]
public string TxtStart { get; set; } = "Start";
/// <summary>
/// Test btn vai a fine
/// </summary>
[Parameter]
public string TxtEnd { get; set; } = "End";
/// <summary>
/// Test btn vai a PrevBlock
/// </summary>
[Parameter]
public string TxtPrev { get; set; } = "Prev";
/// <summary>
/// Test btn vai a NextBlock
/// </summary>
[Parameter]
public string TxtNext{ get; set; } = "Next";
/// <summary>
/// PageSize = Num record da mostrare cambiato
/// </summary>
[Parameter]
public EventCallback<int> numRecordChanged { get; set; }
/// <summary>
/// Attuale PageSize
/// </summary>
[Parameter]
public int PageSize
{
get
{
return _numRecord;
}
set
{
bool doReport = !_numRecord.Equals(value);
if (doReport)
{
_numRecord = value;
reportChange();
}
}
}
/// <summary>
/// Elenco dei PageSize ammessi
/// </summary>
[Parameter]
public List<int> PageSizeList { get; set; } = new List<int>()
{
5,10,25,50,100
};
/// <summary>
/// Fase di loading
/// </summary>
[Parameter]
public bool showLoading
{
get
{
return _showLoading;
}
set
{
if (value)
{
Random random = new Random();
percLoading = random.Next(30, 90);
}
else
{
percLoading = 5;
}
_showLoading = value;
}
}
/// <summary>
/// Totale record da mostrare
/// </summary>
[Parameter]
public int totalCount { get; set; } = 0;
#endregion Public Properties
#region Public Methods
public void resetCurrPage()
{
//await Task.Delay(1);
currPage = 1;
}
#endregion Public Methods
#region Protected Fields
protected bool _showLoading = false;
protected string exportDir = $"{Directory.GetCurrentDirectory()}\\temp";
#endregion Protected Fields
#region Protected Properties
protected int _numPage { get; set; } = 1;
protected int _numRecord { get; set; } = 10;
protected bool fileExist
{
get
{
return File.Exists(fullPath);
}
}
protected string fullPath
{
get => $"{exportDir}\\{fileName}";
}
private string cssSize
{
get
{
string answ = "";
switch (DisplSize)
{
case ObjSize.small:
answ = "small";
break;
case ObjSize.standard:
break;
case ObjSize.large:
answ = "large";
break;
default:
break;
}
return answ;
}
}
protected int percLoading { get; set; } = 0;
#endregion Protected Properties
#region Protected Methods
protected string cssActive(int numPage)
{
string answ = "";
if (numPage == currPage)
{
answ = "active";
}
return answ;
}
protected override async Task OnInitializedAsync()
{
await Task.Run(() => showLoading = false);
}
protected void PaginationItemClick(int page)
{
currPage = page;
}
#endregion Protected Methods
#region Private Properties
private int endPage
{
get
{
int answ = startPage + NumPages -1;
answ = answ < LastPage ? answ : LastPage;
return answ;
}
}
private int LastPage
{
get
{
return Math.Max((int)Math.Ceiling(totalCount / (double)PageSize), 1);
}
}
private int nextBlock
{
get
{
int answ = currPage + NumPages;
answ = answ < LastPage ? answ : LastPage;
return answ;
}
}
private int prevBlock
{
get
{
int answ = currPage - NumPages;
answ = answ > 0 ? answ : 1;
return answ;
}
}
/// <summary>
/// calcola un set 1 .. NumPages centrato sulla pagina corrente...
/// </summary>
private int startPage
{
get
{
int answ = ((int)((currPage -1) / NumPages) ) * NumPages + 1;
answ = answ > 0 ? answ : 1;
return answ;
}
}
#endregion Private Properties
#region Private Methods
private void reportChange()
{
numRecordChanged.InvokeAsync(PageSize);
}
private void reportChangePage()
{
numPageChanged.InvokeAsync(currPage);
}
private async Task requestSave()
{
showLoading = true;
await Task.Delay(1);
await exportRequested.InvokeAsync(currPage);
showLoading = false;
}
#endregion Private Methods
}
}