using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using GWMS.UI.Components; using GWMS.UI.Data; using System.IO; namespace GWMS.UI.Components { public partial class DataPager : ComponentBase { #region Public Properties /// /// Num pagina correntemente selezioanta /// [Parameter] public int currPage { get { return _numPage; } set { bool doReport = !_numPage.Equals(value); if (doReport) { _numPage = value; reportChangePage(); } } } /// /// Export (csv) abilitato /// [Parameter] public bool exportEnabled { get; set; } = false; /// /// Evento richiesta export /// [Parameter] public EventCallback exportRequested { get; set; } /// /// Filename x export /// [Parameter] public string fileName { get; set; } = ""; /// /// Evento cambio pagina nel paginatore /// [Parameter] public EventCallback numPageChanged { get; set; } /// /// PageSize = Num record da mostrare cambiato /// [Parameter] public EventCallback numRecordChanged { get; set; } /// /// Attuale PageSize /// [Parameter] public int PageSize { get { return _numRecord; } set { bool doReport = !_numRecord.Equals(value); if (doReport) { _numRecord = value; reportChange(); } } } /// /// Fase di loading /// [Parameter] public bool showLoading { get { return _showLoading; } set { if (value) { Random random = new Random(); percLoading = random.Next(30, 90); } else { percLoading = 5; } _showLoading = value; } } /// /// Totale record da mostrare /// [Parameter] public int totalCount { get; set; } = 0; /// /// Elenco dei PageSize ammessi /// [Parameter] public List PageSizeList { get; set; } = new List() { 5,10,25,50,100 }; #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}"; } 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 = (int)(currPage / numPages) * numPages + numPages; 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 numPages { get; set; } = 10; private int prevBlock { get { int answ = currPage - numPages; answ = answ > 0 ? answ : 1; return answ; } } // calcola un set 1 .. numPages centrato sulla pagina corrente... private int startPage { get { int answ = (int)(currPage / numPages) * numPages; 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 } }