/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Everest.CmsServices.MvcHelper{
/// <summary>
///
/// </summary>
public class ContentPagedList : List<IDictionary<string, object>>
{
/// <summary>
/// Initializes a new instance of the <see cref="ContentPagedList"/> class.
/// </summary>
/// <param name="contents">The contents.</param>
/// <param name="totalItemCount">The total item count.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="pageIndex">Index of the page.</param>
public ContentPagedList(IEnumerable<IDictionary<string, object>> contents, int totalItemCount, int pageSize, int pageIndex)
{
Initialize(contents, pageIndex, pageSize, totalItemCount);
}
/// <summary>
/// Gets or sets the page count.
/// </summary>
/// <value>The page count.</value>
public int TotalPages { get; private set; }
/// <summary>
/// Gets or sets the total item count.
/// </summary>
/// <value>The total item count.</value>
public int TotalItemCount { get; private set; }
/// <summary>
/// Gets or sets the index of the page.
/// </summary>
/// <value>The index of the page.</value>
public int PageIndex { get; private set; }
/// <summary>
/// Gets or sets the size of the page.
/// </summary>
/// <value>The size of the page.</value>
public int PageSize { get; private set; }
/// <summary>
/// Gets or sets the start index of the next.
/// </summary>
/// <value>The start index of the next.</value>
public int NextStartRowIndex { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this instance has previous page.
/// </summary>
/// <value>
/// <c>true</c> if this instance has previous page; otherwise, <c>false</c>.
/// </value>
public bool HasPreviousPage { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this instance has next page.
/// </summary>
/// <value>
/// <c>true</c> if this instance has next page; otherwise, <c>false</c>.
/// </value>
public bool HasNextPage { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is first page.
/// </summary>
/// <value>
/// <c>true</c> if this instance is first page; otherwise, <c>false</c>.
/// </value>
public bool IsFirstPage { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is last page.
/// </summary>
/// <value>
/// <c>true</c> if this instance is last page; otherwise, <c>false</c>.
/// </value>
public bool IsLastPage { get; private set; }
/// <summary>
/// Initializes the specified contents.
/// </summary>
/// <param name="contents">The contents.</param>
/// <param name="index">The index.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="totalCount">The total count.</param>
protected void Initialize(IEnumerable<IDictionary<string, object>> contents, int index, int pageSize, int totalCount)
{
//### argument checking
if (index < 1)
{
index = 1;
}
if (pageSize < 1)
{
throw new ArgumentOutOfRangeException("PageSize cannot be less than 1.");
}
//### set source to blank list if source is null to prevent exceptions
if (contents != null)
{
AddRange(contents);
}
TotalItemCount = totalCount;
PageSize = pageSize;
PageIndex = index;
if (TotalItemCount > 0)
{
TotalPages = (int)Math.Ceiling(TotalItemCount / (double)PageSize);
}
else
{
TotalPages = 0;
}
HasPreviousPage = (PageIndex - 1 > 0);
HasNextPage = (PageIndex < TotalPages);
IsFirstPage = (PageIndex <= 1);
IsLastPage = (PageIndex >= TotalPages);
//the next page start row index
NextStartRowIndex = PageSize * PageIndex;
}
}
}
|