Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

PagerModel.cs

using Nop.Services.Localization;
using Nop.Web.Infrastructure;

namespace Nop.Web.Models.Common;

public partial record PagerModel
{
    #region Fields

    protected readonly ILocalizationService _localizationService;

    protected int _individualPagesDisplayedCount;
    protected int _pageIndex = -2;
    protected int _pageSize;

    protected bool? _showFirst;
    protected bool? _showIndividualPages;
    protected bool? _showLast;
    protected bool? _showNext;
    protected bool? _showPagerItems;
    protected bool? _showPrevious;
    protected bool? _showTotalSummary;

    #endregion Fields

    #region Ctor

    public PagerModel(ILocalizationService localizationService)
    {
        _localizationService = localizationService;
    }

    #endregion

    #region Methods

    /// 
    /// Gets the first button text
    /// 
    /// A task that represents the asynchronous operation
    public async Task GetFirstButtonTextAsync()
    {
        return await _localizationService.GetResourceAsync("Pager.First");
    }

    /// 
    /// Gets the last button text
    /// 
    /// A task that represents the asynchronous operation
    public async Task GetLastButtonTextAsync()
    {
        return await _localizationService.GetResourceAsync("Pager.Last");
    }

    /// 
    /// Gets the next button text
    /// 
    /// A task that represents the asynchronous operation
    public async Task GetNextButtonTextAsync()
    {
        return await _localizationService.GetResourceAsync("Pager.Next");
    }

    /// 
    /// Gets the previous button text
    /// 
    /// A task that represents the asynchronous operation
    public async Task GetPreviousButtonTextAsync()
    {
        return await _localizationService.GetResourceAsync("Pager.Previous");
    }

    /// 
    /// Gets or sets the current page text
    /// 
    /// A task that represents the asynchronous operation
    public async Task GetCurrentPageTextAsync()
    {
        return await _localizationService.GetResourceAsync("Pager.CurrentPage");
    }

    /// 
    /// Gets first individual page index
    /// 
    /// Page index
    public int GetFirstIndividualPageIndex()
    {
        if ((TotalPages < IndividualPagesDisplayedCount) ||
            ((PageIndex - (IndividualPagesDisplayedCount / 2)) < 0))
            return 0;

        if ((PageIndex + (IndividualPagesDisplayedCount / 2)) >= TotalPages) 
            return (TotalPages - IndividualPagesDisplayedCount);

        return (PageIndex - (IndividualPagesDisplayedCount / 2));
    }

    /// 
    /// Get last individual page index
    /// 
    /// Page index
    public int GetLastIndividualPageIndex()
    {
        var num = IndividualPagesDisplayedCount / 2;
        if ((IndividualPagesDisplayedCount % 2) == 0) 
            num--;

        if ((TotalPages < IndividualPagesDisplayedCount) ||
            ((PageIndex + num) >= TotalPages))
            return (TotalPages - 1);

        if ((PageIndex - (IndividualPagesDisplayedCount / 2)) < 0) 
            return (IndividualPagesDisplayedCount - 1);

        return PageIndex + num;
    }

    #endregion Methods

    #region Properties

    /// 
    /// Gets the current page index
    /// 
    public int CurrentPage => PageIndex + 1;

    /// 
    /// Gets or sets a count of individual pages to be displayed
    /// 
    public int IndividualPagesDisplayedCount
    {
        get
        {
            if (_individualPagesDisplayedCount <= 0)
                return 5;

            return _individualPagesDisplayedCount;
        }
        set => _individualPagesDisplayedCount = value;
    }

    /// 
    /// Gets the current page index
    /// 
    public int PageIndex
    {
        get => _pageIndex < 0 ? 0 : _pageIndex;
        set => _pageIndex = value;
    }

    /// 
    /// Gets or sets a page size
    /// 
    public int PageSize
    {
        get => (_pageSize <= 0) ? 10 : _pageSize;
        set => _pageSize = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "first"
    /// 
    public bool ShowFirst
    {
        get => _showFirst ?? true;
        set => _showFirst = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "individual pages"
    /// 
    public bool ShowIndividualPages
    {
        get => _showIndividualPages ?? true;
        set => _showIndividualPages = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "last"
    /// 
    public bool ShowLast
    {
        get => _showLast ?? true;
        set => _showLast = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "next"
    /// 
    public bool ShowNext
    {
        get => _showNext ?? true;
        set => _showNext = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show pager items
    /// 
    public bool ShowPagerItems
    {
        get => _showPagerItems ?? true;
        set => _showPagerItems = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "previous"
    /// 
    public bool ShowPrevious
    {
        get => _showPrevious ?? true;
        set => _showPrevious = value;
    }

    /// 
    /// Gets or sets a value indicating whether to show "total summary"
    /// 
    public bool ShowTotalSummary
    {
        get => _showTotalSummary ?? false;
        set => _showTotalSummary = value;
    }

    /// 
    /// Gets a total pages count
    /// 
    public int TotalPages
    {
        get
        {
            if (TotalRecords == 0 || PageSize == 0)
                return 0;

            var num = TotalRecords / PageSize;

            if ((TotalRecords % PageSize) > 0)
                num++;

            return num;
        }
    }

    /// 
    /// Gets or sets a total records count
    /// 
    public int TotalRecords { get; set; }


    /// 
    /// Gets or sets the route name or action name
    /// 
    public string RouteActionName { get; set; }

    /// 
    /// Gets or sets whether the links are created using RouteLink instead of Action Link 
    /// (for additional route values such as slugs or page numbers)
    /// 
    public bool UseRouteLinks { get; set; }

    /// 
    /// Gets or sets the RouteValues object. Allows for custom route values other than page.
    /// 
    public IRouteValues RouteValues { get; set; }

    #endregion Properties
}