Skip to content

Commit

Permalink
Code cleanup. Close dncuug#53
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gubskiy committed Jun 10, 2017
1 parent f12051a commit e8933ec
Showing 1 changed file with 98 additions and 14 deletions.
112 changes: 98 additions & 14 deletions src/X.PagedList.Mvc/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ namespace X.PagedList.Mvc
///</summary>
public static class HtmlHelper
{
internal static void SetInnerText(TagBuilder tagBuilder, string innerText)
private static void SetInnerText(TagBuilder tagBuilder, string innerText)
{
tagBuilder.SetInnerText(innerText);
}

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

internal static string TagBuilderToString(TagBuilder tagBuilder)
private static string TagBuilderToString(TagBuilder tagBuilder)
{
return tagBuilder.ToString();
}

internal static string TagBuilderToString(TagBuilder tagBuilder, TagRenderMode renderMode)
private static string TagBuilderToString(TagBuilder tagBuilder, TagRenderMode renderMode)
{
return tagBuilder.ToString(renderMode);
}
Expand All @@ -42,10 +42,17 @@ private static TagBuilder WrapInListItem(string text)
private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOptions options, params string[] classes)
{
var li = new TagBuilder("li");

foreach (var @class in classes)
{
li.AddCssClass(@class);
}

if (options.FunctionToTransformEachPageLink != null)
{
return options.FunctionToTransformEachPageLink(li, inner);
}

AppendHtml(li, TagBuilderToString(inner));
return li;
}
Expand All @@ -57,10 +64,14 @@ private static TagBuilder First(IPagedList list, Func<int, string> generatePageU
AppendHtml(first, string.Format(options.LinkToFirstPageFormat, targetPageNumber));

foreach (var c in options.PageClasses ?? Enumerable.Empty<string>())
{
first.AddCssClass(c);
}

if (list.IsFirstPage)
{
return WrapInListItem(first, options, "PagedList-skipToFirst", "disabled");
}

first.Attributes["href"] = generatePageUrl(targetPageNumber);
return WrapInListItem(first, options, "PagedList-skipToFirst");
Expand All @@ -74,12 +85,17 @@ private static TagBuilder Previous(IPagedList list, Func<int, string> generatePa
previous.Attributes["rel"] = "prev";

foreach (var c in options.PageClasses ?? Enumerable.Empty<string>())
{
previous.AddCssClass(c);
}

if (!list.HasPreviousPage)
{
return WrapInListItem(previous, options, options.PreviousElementClass, "disabled");
}

previous.Attributes["href"] = generatePageUrl(targetPageNumber);

return WrapInListItem(previous, options, options.PreviousElementClass);
}

Expand All @@ -92,10 +108,14 @@ private static TagBuilder Page(int i, IPagedList list, Func<int, string> generat
SetInnerText(page, format(targetPageNumber));

foreach (var c in options.PageClasses ?? Enumerable.Empty<string>())
{
page.AddCssClass(c);
}

if (i == list.PageNumber)
{
return WrapInListItem(page, options, options.ActiveLiElementClass);
}

page.Attributes["href"] = generatePageUrl(targetPageNumber);

Expand All @@ -110,10 +130,14 @@ private static TagBuilder Next(IPagedList list, Func<int, string> generatePageUr
next.Attributes["rel"] = "next";

foreach (var c in options.PageClasses ?? Enumerable.Empty<string>())
{
next.AddCssClass(c);
}

if (!list.HasNextPage)
{
return WrapInListItem(next, options, options.NextElementClass, "disabled");
}

next.Attributes["href"] = generatePageUrl(targetPageNumber);
return WrapInListItem(next, options, options.NextElementClass);
Expand All @@ -126,10 +150,14 @@ private static TagBuilder Last(IPagedList list, Func<int, string> generatePageUr
AppendHtml(last, string.Format(options.LinkToLastPageFormat, targetPageNumber));

foreach (var c in options.PageClasses ?? Enumerable.Empty<string>())
{
last.AddCssClass(c);
}

if (list.IsLastPage)
{
return WrapInListItem(last, options, "PagedList-skipToLast", "disabled");
}

last.Attributes["href"] = generatePageUrl(targetPageNumber);
return WrapInListItem(last, options, "PagedList-skipToLast");
Expand Down Expand Up @@ -174,17 +202,17 @@ public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
}

///<summary>
/// Displays a configurable paging control for instances of PagedList.
/// Displays a configurable paging control for instances of PagedList.
///</summary>
///<param name = "html">This method is meant to hook off HtmlHelper as an extension method.</param>
///<param name = "list">The PagedList to use as the data source.</param>
///<param name = "generatePageUrl">A function that takes the page number of the desired page and returns a URL-string that will load that page.</param>
///<param name = "options">Formatting options.</param>
///<returns>Outputs the paging control HTML.</returns>
public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
IPagedList list,
Func<int, string> generatePageUrl,
PagedListRenderOptions options)
IPagedList list,
Func<int, string> generatePageUrl,
PagedListRenderOptions options)
{
if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1))
return null;
Expand Down Expand Up @@ -229,53 +257,74 @@ public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
{
//if there are previous page numbers not displayed, show an ellipsis
if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && firstPageToDisplay > 1)
listItemLinks.Add(Ellipses(options));
{
listItemLinks.Add(PreviousEllipsis(list, generatePageUrl, options, firstPageToDisplay));
}

foreach (var i in Enumerable.Range(firstPageToDisplay, pageNumbersToDisplay))
{
//show delimiter between page numbers
if (i > firstPageToDisplay && !string.IsNullOrWhiteSpace(options.DelimiterBetweenPageNumbers))
{
listItemLinks.Add(WrapInListItem(options.DelimiterBetweenPageNumbers));
}

//show page number link
listItemLinks.Add(Page(i, list, generatePageUrl, options));
}

//if there are subsequent page numbers not displayed, show an ellipsis
if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && (firstPageToDisplay + pageNumbersToDisplay - 1) < list.PageCount)
listItemLinks.Add(Ellipses(options));
if (options.DisplayEllipsesWhenNotShowingAllPageNumbers &&
(firstPageToDisplay + pageNumbersToDisplay - 1) < list.PageCount)
{
listItemLinks.Add(NextEllipsis(list, generatePageUrl, options, lastPageToDisplay));
}
}

//next
if (options.DisplayLinkToNextPage == PagedListDisplayMode.Always || (options.DisplayLinkToNextPage == PagedListDisplayMode.IfNeeded && !list.IsLastPage))
if (options.DisplayLinkToNextPage == PagedListDisplayMode.Always ||
(options.DisplayLinkToNextPage == PagedListDisplayMode.IfNeeded && !list.IsLastPage))
{
listItemLinks.Add(Next(list, generatePageUrl, options));
}

//last
if (options.DisplayLinkToLastPage == PagedListDisplayMode.Always || (options.DisplayLinkToLastPage == PagedListDisplayMode.IfNeeded && lastPageToDisplay < list.PageCount))
if (options.DisplayLinkToLastPage == PagedListDisplayMode.Always ||
(options.DisplayLinkToLastPage == PagedListDisplayMode.IfNeeded && lastPageToDisplay < list.PageCount))
{
listItemLinks.Add(Last(list, generatePageUrl, options));
}

if (listItemLinks.Any())
{
//append class to first item in list?
if (!string.IsNullOrWhiteSpace(options.ClassToApplyToFirstListItemInPager))
{
listItemLinks.First().AddCssClass(options.ClassToApplyToFirstListItemInPager);
}

//append class to last item in list?
if (!string.IsNullOrWhiteSpace(options.ClassToApplyToLastListItemInPager))
{
listItemLinks.Last().AddCssClass(options.ClassToApplyToLastListItemInPager);
}

//append classes to all list item links
foreach (var li in listItemLinks)
{
foreach (var c in options.LiElementClasses ?? Enumerable.Empty<string>())
{
li.AddCssClass(c);
}
}
}

//collapse all of the list items into one big string
var listItemLinksString = listItemLinks.Aggregate(
new StringBuilder(),
(sb, listItem) => sb.Append(TagBuilderToString(listItem)),
sb => sb.ToString()
);
);

var ul = new TagBuilder("ul");
AppendHtml(ul, listItemLinksString);
Expand All @@ -296,6 +345,40 @@ public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
return new HtmlString(TagBuilderToString(outerDiv));
}

private static TagBuilder PreviousEllipsis(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options, int firstPageToDisplay)
{
var targetPageNumber = firstPageToDisplay - 1;
var previous = new TagBuilder("a")
{
InnerHtml = string.Format(options.EllipsesFormat, targetPageNumber)
};
previous.Attributes["rel"] = "prev";

if (!list.HasPreviousPage)
return WrapInListItem(previous, options, "PagedList-skipToPrevious", "disabled");

previous.Attributes["href"] = generatePageUrl(targetPageNumber);
return WrapInListItem(previous, options, "PagedList-skipToPrevious");
}

private static TagBuilder NextEllipsis(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options, int lastPageToDisplay)
{
var targetPageNumber = lastPageToDisplay + 1;

var next = new TagBuilder("a")
{
InnerHtml = string.Format(options.EllipsesFormat, targetPageNumber)
};

next.Attributes["rel"] = "next";

if (!list.HasNextPage)
return WrapInListItem(next, options, "PagedList-skipToNext", "disabled");

next.Attributes["href"] = generatePageUrl(targetPageNumber);
return WrapInListItem(next, options, "PagedList-skipToNext");
}

///<summary>
/// Displays a configurable "Go To Page:" form for instances of PagedList.
///</summary>
Expand Down Expand Up @@ -368,6 +451,7 @@ public static HtmlString PagedListGoToPageForm(this System.Web.Mvc.HtmlHelper ht
AppendHtml(fieldset, TagBuilderToString(submit, TagRenderMode.SelfClosing));

AppendHtml(form, TagBuilderToString(fieldset));

return new HtmlString(TagBuilderToString(form));
}
}
Expand Down

0 comments on commit e8933ec

Please sign in to comment.