Skip to content

Commit

Permalink
Merge pull request #757 from EdiWang/pagelist
Browse files Browse the repository at this point in the history
Merge X.PagedList lib into the code
  • Loading branch information
EdiWang committed Oct 12, 2023
2 parents f05da1b + af62916 commit f1ea72d
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 120 deletions.
1 change: 0 additions & 1 deletion src/Moonglade.Web/Moonglade.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="WilderMinds.MetaWeblog" Version="5.1.1" />
<PackageReference Include="Spectre.Console" Version="0.47.0" />
<PackageReference Include="X.PagedList" Version="8.4.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Moonglade.Auth\Moonglade.Auth.csproj" />
Expand Down
112 changes: 112 additions & 0 deletions src/Moonglade.Web/PagedList/BasePagedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Collections;

namespace Moonglade.Web.PagedList;

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name = "T">The type of object the collection should contain.</typeparam>
/// <seealso cref = "IPagedList{T}" />
/// <seealso cref = "List{T}" />
public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T>
{
protected readonly List<T> Subset = new();

/// <summary>
/// Parameterless constructor.
/// </summary>
protected internal BasePagedList()
{
}

/// <summary>
/// Initializes a new instance of a type deriving from <see cref = "BasePagedList{T}" /> and sets properties
/// needed to calculate position and size data on the subset and superset.
/// </summary>
/// <param name = "pageNumber">The one-based index of the subset of objects contained by this instance.</param>
/// <param name = "pageSize">The maximum size of any individual subset.</param>
/// <param name = "totalItemCount">The size of the superset.</param>
protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCount)
{
if (pageNumber < 1)
{
throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1.");
}

if (pageSize < 1)
{
throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1.");
}

if (totalItemCount < 0)
{
throw new ArgumentOutOfRangeException($"totalItemCount = {totalItemCount}. TotalItemCount cannot be less than 0.");
}

// set source to blank list if superset is null to prevent exceptions
TotalItemCount = totalItemCount;

PageSize = pageSize;
PageNumber = pageNumber;
PageCount = TotalItemCount > 0
? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
: 0;

var pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount;

HasPreviousPage = pageNumberIsGood && PageNumber > 1;
HasNextPage = pageNumberIsGood && PageNumber < PageCount;
IsFirstPage = pageNumberIsGood && PageNumber == 1;
IsLastPage = pageNumberIsGood && PageNumber == PageCount;

var numberOfFirstItemOnPage = (PageNumber - 1) * PageSize + 1;

FirstItemOnPage = pageNumberIsGood ? numberOfFirstItemOnPage : 0;

var numberOfLastItemOnPage = numberOfFirstItemOnPage + PageSize - 1;

LastItemOnPage = pageNumberIsGood
? numberOfLastItemOnPage > TotalItemCount
? TotalItemCount
: numberOfLastItemOnPage
: 0;
}

#region IPagedList<T> Members

/// <summary>
/// Returns an enumerator that iterates through the BasePagedList&lt;T&gt;.
/// </summary>
/// <returns>A BasePagedList&lt;T&gt;.Enumerator for the BasePagedList&lt;T&gt;.</returns>
public IEnumerator<T> GetEnumerator()
{
return Subset.GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates through the BasePagedList&lt;T&gt;.
/// </summary>
/// <returns>A BasePagedList&lt;T&gt;.Enumerator for the BasePagedList&lt;T&gt;.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

///<summary>
/// Gets the element at the specified index.
///</summary>
///<param name = "index">The zero-based index of the element to get.</param>
public T this[int index] => Subset[index];

/// <summary>
/// Gets the number of elements contained on this page.
/// </summary>
public virtual int Count => Subset.Count;

#endregion
}
57 changes: 0 additions & 57 deletions src/Moonglade.Web/PagedList/GoToFormRenderOptions.cs

This file was deleted.

19 changes: 12 additions & 7 deletions src/Moonglade.Web/PagedList/HtmlHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Immutable;
using X.PagedList;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;

namespace Moonglade.Web.PagedList;

public class HtmlHelper
{
private readonly ITagBuilderFactory _tagBuilderFactory;
private readonly TagBuilderFactory _tagBuilderFactory;

public HtmlHelper(ITagBuilderFactory tagBuilderFactory)
public HtmlHelper(TagBuilderFactory tagBuilderFactory)
{
_tagBuilderFactory = tagBuilderFactory;
}
Expand All @@ -17,18 +18,22 @@ public HtmlHelper(ITagBuilderFactory tagBuilderFactory)

private static void SetInnerText(TagBuilder tagBuilder, string innerText)
{
tagBuilder.SetInnerText(innerText);
tagBuilder.InnerHtml.SetContent(innerText);
}

private static void AppendHtml(TagBuilder tagBuilder, string innerHtml)
{
tagBuilder.AppendHtml(innerHtml);
tagBuilder.InnerHtml.AppendHtml(innerHtml);
}

private static string TagBuilderToString(TagBuilder tagBuilder)
{
return tagBuilder
.ToString(TagRenderMode.Normal);
var encoder = HtmlEncoder.Create(new TextEncoderSettings());

using var writer = new StringWriter() as TextWriter;
tagBuilder.WriteTo(writer, encoder);

return writer.ToString();
}

private TagBuilder WrapInListItem(string text)
Expand Down
1 change: 0 additions & 1 deletion src/Moonglade.Web/PagedList/HtmlHelperExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Web;
using X.PagedList;

namespace Moonglade.Web.PagedList;

Expand Down
119 changes: 119 additions & 0 deletions src/Moonglade.Web/PagedList/IPagedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
namespace Moonglade.Web.PagedList;

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
/// <seealso cref="IEnumerable{T}"/>
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T>
{

}

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
public interface IPagedList
{
/// <summary>
/// Total number of subsets within the superset.
/// </summary>
/// <value>
/// Total number of subsets within the superset.
/// </value>
int PageCount { get; }

/// <summary>
/// Total number of objects contained within the superset.
/// </summary>
/// <value>
/// Total number of objects contained within the superset.
/// </value>
int TotalItemCount { get; }

/// <summary>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </summary>
/// <value>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </value>
int PageNumber { get; }

/// <summary>
/// Maximum size any individual subset.
/// </summary>
/// <value>
/// Maximum size any individual subset.
/// </value>
int PageSize { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the first subset within the superset.
/// </value>
bool HasPreviousPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the last subset within the superset.
/// </value>
bool HasNextPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the first subset within the superset.
/// </value>
bool IsFirstPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the last subset within the superset.
/// </value>
bool IsLastPage { get; }

/// <summary>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </value>
int FirstItemOnPage { get; }

/// <summary>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </value>
int LastItemOnPage { get; }
}
13 changes: 1 addition & 12 deletions src/Moonglade.Web/PagedList/PagedListDisplayMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@
/// </summary>
public enum PagedListDisplayMode
{
/// <summary>
/// Always render.
/// </summary>
Always,

/// <summary>
/// Never render.
/// </summary>
Never,

/// <summary>
/// Only render when there is data that makes sense to show (context sensitive).
/// </summary>
IfNeeded
}
}
Loading

0 comments on commit f1ea72d

Please sign in to comment.