From de58872d258f4fc9629766387c3043ea1c213fea Mon Sep 17 00:00:00 2001 From: Andrew Gubskiy Date: Wed, 8 Nov 2017 10:52:04 +0200 Subject: [PATCH] Move main part of PagedListRenderOptions to PagedListRenderOptionsBase in X.PagedList.Mvc.Common --- .../PagedListRenderOptionsBase.cs | 410 ++++++++++++++ src/X.PagedList.Mvc.Core/HtmlHelper.cs | 28 +- .../PagedListRenderOptions.cs | 436 +-------------- src/X.PagedList.Mvc/HtmlHelper.cs | 31 +- src/X.PagedList.Mvc/PagedListRenderOptions.cs | 516 +++--------------- 5 files changed, 526 insertions(+), 895 deletions(-) create mode 100644 src/X.PagedList.Mvc.Common/PagedListRenderOptionsBase.cs diff --git a/src/X.PagedList.Mvc.Common/PagedListRenderOptionsBase.cs b/src/X.PagedList.Mvc.Common/PagedListRenderOptionsBase.cs new file mode 100644 index 00000000..418f2f21 --- /dev/null +++ b/src/X.PagedList.Mvc.Common/PagedListRenderOptionsBase.cs @@ -0,0 +1,410 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace X.PagedList.Mvc.Common +{ + public class PagedListRenderOptionsBase + { + /// + /// The default settings render all navigation links and no descriptive text. + /// + public PagedListRenderOptionsBase() + { + DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded; + DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded; + DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded; + DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded; + DisplayLinkToIndividualPages = true; + DisplayPageCountAndCurrentLocation = false; + MaximumPageNumbersToDisplay = 10; + DisplayEllipsesWhenNotShowingAllPageNumbers = true; + EllipsesFormat = "…"; + LinkToFirstPageFormat = "<<"; + LinkToPreviousPageFormat = "<"; + LinkToIndividualPageFormat = "{0}"; + LinkToNextPageFormat = ">"; + LinkToLastPageFormat = ">>"; + PageCountAndCurrentLocationFormat = "Page {0} of {1}."; + ItemSliceAndTotalFormat = "Showing items {0} through {1} of {2}."; + FunctionToDisplayEachPageNumber = null; + ClassToApplyToFirstListItemInPager = null; + ClassToApplyToLastListItemInPager = null; + ContainerDivClasses = new[] { "pagination-container" }; + UlElementClasses = new[] { "pagination" }; + LiElementClasses = Enumerable.Empty(); + PageClasses = Enumerable.Empty(); + UlElementattributes = null; + ActiveLiElementClass = "active"; + EllipsesElementClass = "PagedList-ellipses"; + PreviousElementClass = "PagedList-skipToPrevious"; + NextElementClass = "PagedList-skipToNext"; + } + + /// + /// CSS Classes to append to the <div> element that wraps the paging control. + /// + public IEnumerable ContainerDivClasses { get; set; } + + /// + /// CSSClasses to append to the <ul> element in the paging control. + /// + public IEnumerable UlElementClasses { get; set; } + + /// + /// Attrinutes to appendto the <ul> element in the paging control + /// + public IDictionary UlElementattributes { get; set; } + + /// + /// CSS Classes to append to every <li> element in the paging control. + /// + public IEnumerable LiElementClasses { get; set; } + + /// + /// CSS Classes to appent to active <li> element in the paging control. + /// + public string ActiveLiElementClass { get; set; } + + /// + /// CSS Classes to append to every <a> or <span> element that represent each page in the paging control. + /// + public IEnumerable PageClasses { get; set; } + + /// + /// CSS Classes to append to previous element in the paging control. + /// + public string PreviousElementClass { get; set; } + + /// + /// CSS Classes to append to next element in the paging control. + /// + public string NextElementClass { get; set; } + + /// + /// CSS Classes to append to Ellipses element in the paging control. + /// + public string EllipsesElementClass { get; set; } + + /// + /// Specifies a CSS class to append to the first list item in the pager. If null or whitespace is defined, no additional class is added to first list item in list. + /// + public string ClassToApplyToFirstListItemInPager { get; set; } + + /// + /// Specifies a CSS class to append to the last list item in the pager. If null or whitespace is defined, no additional class is added to last list item in list. + /// + public string ClassToApplyToLastListItemInPager { get; set; } + + /// + /// If set to Always, always renders the paging control. If set to IfNeeded, render the paging control when there is more than one page. + /// + public PagedListDisplayMode Display { get; set; } + + /// + /// If set to Always, render a hyperlink to the first page in the list. If set to IfNeeded, render the hyperlink only when the first page isn't visible in the paging control. + /// + public PagedListDisplayMode DisplayLinkToFirstPage { get; set; } + + /// + /// If set to Always, render a hyperlink to the last page in the list. If set to IfNeeded, render the hyperlink only when the last page isn't visible in the paging control. + /// + public PagedListDisplayMode DisplayLinkToLastPage { get; set; } + + /// + /// If set to Always, render a hyperlink to the previous page of the list. If set to IfNeeded, render the hyperlink only when there is a previous page in the list. + /// + public PagedListDisplayMode DisplayLinkToPreviousPage { get; set; } + + /// + /// If set to Always, render a hyperlink to the next page of the list. If set to IfNeeded, render the hyperlink only when there is a next page in the list. + /// + public PagedListDisplayMode DisplayLinkToNextPage { get; set; } + + /// + /// When true, includes hyperlinks for each page in the list. + /// + public bool DisplayLinkToIndividualPages { get; set; } + + /// + /// When true, shows the current page number and the total number of pages in the list. + /// + /// + /// "Page 3 of 8." + /// + public bool DisplayPageCountAndCurrentLocation { get; set; } + + /// + /// When true, shows the one-based index of the first and last items on the page, and the total number of items in the list. + /// + /// + /// "Showing items 75 through 100 of 183." + /// + public bool DisplayItemSliceAndTotal { get; set; } + + /// + /// The maximum number of page numbers to display. Null displays all page numbers. + /// + public int? MaximumPageNumbersToDisplay { get; set; } + + /// + /// If true, adds an ellipsis where not all page numbers are being displayed. + /// + /// + /// "1 2 3 4 5 ...", + /// "... 6 7 8 9 10 ...", + /// "... 11 12 13 14 15" + /// + public bool DisplayEllipsesWhenNotShowingAllPageNumbers { get; set; } + + /// + /// The pre-formatted text to display when not all page numbers are displayed at once. + /// + /// + /// "..." + /// + public string EllipsesFormat { get; set; } + + /// + /// The pre-formatted text to display inside the hyperlink to the first page. The one-based index of the page (always 1 in this case) is passed into the formatting function - use {0} to reference it. + /// + /// + /// "<< First" + /// + public string LinkToFirstPageFormat { get; set; } + + /// + /// The pre-formatted text to display inside the hyperlink to the previous page. The one-based index of the page is passed into the formatting function - use {0} to reference it. + /// + /// + /// "< Previous" + /// + public string LinkToPreviousPageFormat { get; set; } + + /// + /// The pre-formatted text to display inside the hyperlink to each individual page. The one-based index of the page is passed into the formatting function - use {0} to reference it. + /// + /// + /// "{0}" + /// + public string LinkToIndividualPageFormat { get; set; } + + /// + /// The pre-formatted text to display inside the hyperlink to the next page. The one-based index of the page is passed into the formatting function - use {0} to reference it. + /// + /// + /// "Next >" + /// + public string LinkToNextPageFormat { get; set; } + + /// + /// The pre-formatted text to display inside the hyperlink to the last page. The one-based index of the page is passed into the formatting function - use {0} to reference it. + /// + /// + /// "Last >>" + /// + public string LinkToLastPageFormat { get; set; } + + /// + /// The pre-formatted text to display when DisplayPageCountAndCurrentLocation is true. Use {0} to reference the current page and {1} to reference the total number of pages. + /// + /// + /// "Page {0} of {1}." + /// + public string PageCountAndCurrentLocationFormat { get; set; } + + /// + /// The pre-formatted text to display when DisplayItemSliceAndTotal is true. Use {0} to reference the first item on the page, {1} for the last item on the page, and {2} for the total number of items across all pages. + /// + /// + /// "Showing items {0} through {1} of {2}." + /// + public string ItemSliceAndTotalFormat { get; set; } + + /// + /// A function that will render each page number when specified (and DisplayLinkToIndividualPages is true). If no function is specified, the LinkToIndividualPageFormat value will be used instead. + /// + public Func FunctionToDisplayEachPageNumber { get; set; } + + /// + /// Text that will appear between each page number. If null or whitespace is specified, no delimiter will be shown. + /// + public string DelimiterBetweenPageNumbers { get; set; } + + + + /// + /// Also includes links to First and Last pages. + /// + public static PagedListRenderOptionsBase Classic + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always + }; + } + } + + /// + /// Also includes links to First and Last pages. + /// + public static PagedListRenderOptionsBase ClassicPlusFirstAndLast + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Always, + DisplayLinkToLastPage = PagedListDisplayMode.Always, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always + }; + } + } + + /// + /// Shows only the Previous and Next links. + /// + public static PagedListRenderOptionsBase Minimal + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + DisplayLinkToIndividualPages = false + }; + } + } + + /// + /// Shows Previous and Next links along with current page number and page count. + /// + public static PagedListRenderOptionsBase MinimalWithPageCountText + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + DisplayLinkToIndividualPages = false, + DisplayPageCountAndCurrentLocation = true + }; + } + } + + /// + /// Shows Previous and Next links along with index of first and last items on page and total number of items across all pages. + /// + public static PagedListRenderOptionsBase MinimalWithItemCountText + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + DisplayLinkToIndividualPages = false, + DisplayItemSliceAndTotal = true + }; + } + } + + /// + /// Shows only links to each individual page. + /// + public static PagedListRenderOptionsBase PageNumbersOnly + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Never, + DisplayLinkToNextPage = PagedListDisplayMode.Never, + DisplayEllipsesWhenNotShowingAllPageNumbers = false + }; + } + } + + /// + /// Shows Next and Previous while limiting to a max of 5 page numbers at a time. + /// + public static PagedListRenderOptionsBase OnlyShowFivePagesAtATime + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + MaximumPageNumbersToDisplay = 5 + }; + } + } + + /// + /// Twitter Bootstrap 2's basic pager format (just Previous and Next links). + /// + public static PagedListRenderOptionsBase TwitterBootstrapPager + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + DisplayLinkToIndividualPages = false, + ContainerDivClasses = null, + UlElementClasses = new[] { "pager" }, + ClassToApplyToFirstListItemInPager = null, + ClassToApplyToLastListItemInPager = null, + LinkToPreviousPageFormat = "Previous", + LinkToNextPageFormat = "Next" + }; + } + } + + /// + /// Twitter Bootstrap 2's basic pager format (just Previous and Next links), with aligned links. + /// + public static PagedListRenderOptionsBase TwitterBootstrapPagerAligned + { + get + { + return new PagedListRenderOptionsBase + { + DisplayLinkToFirstPage = PagedListDisplayMode.Never, + DisplayLinkToLastPage = PagedListDisplayMode.Never, + DisplayLinkToPreviousPage = PagedListDisplayMode.Always, + DisplayLinkToNextPage = PagedListDisplayMode.Always, + DisplayLinkToIndividualPages = false, + ContainerDivClasses = null, + UlElementClasses = new[] { "pager" }, + ClassToApplyToFirstListItemInPager = "previous", + ClassToApplyToLastListItemInPager = "next", + LinkToPreviousPageFormat = "← Older", + LinkToNextPageFormat = "Newer →" + }; + } + } + } +} diff --git a/src/X.PagedList.Mvc.Core/HtmlHelper.cs b/src/X.PagedList.Mvc.Core/HtmlHelper.cs index 4c2427b0..9ce9e3ac 100644 --- a/src/X.PagedList.Mvc.Core/HtmlHelper.cs +++ b/src/X.PagedList.Mvc.Core/HtmlHelper.cs @@ -40,21 +40,25 @@ private static TagBuilder WrapInListItem(string text) return li; } - private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOptions options, params string[] classes) + private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOptionsBase options, params string[] classes) { var li = new TagBuilder("li"); foreach (var @class in classes) li.AddCssClass(@class); - if (options.FunctionToTransformEachPageLink != null) + + if (options is PagedListRenderOptions) { - inner = options.FunctionToTransformEachPageLink(li, inner); + if (((PagedListRenderOptions)options).FunctionToTransformEachPageLink != null) + { + inner = ((PagedListRenderOptions)options).FunctionToTransformEachPageLink(li, inner); + } } AppendHtml(li, TagBuilderToString(inner)); return li; } - private static TagBuilder First(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder First(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { const int targetPageNumber = 1; var first = new TagBuilder("a"); @@ -70,7 +74,7 @@ private static TagBuilder First(IPagedList list, Func generatePageU return WrapInListItem(first, options, "PagedList-skipToFirst"); } - private static TagBuilder Previous(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Previous(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageNumber - 1; var previous = new TagBuilder("a"); @@ -87,7 +91,7 @@ private static TagBuilder Previous(IPagedList list, Func generatePa return WrapInListItem(previous, options, options.PreviousElementClass); } - private static TagBuilder Page(int i, IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Page(int i, IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var format = options.FunctionToDisplayEachPageNumber ?? (pageNumber => string.Format(options.LinkToIndividualPageFormat, pageNumber)); @@ -106,7 +110,7 @@ private static TagBuilder Page(int i, IPagedList list, Func generat return WrapInListItem(page, options); } - private static TagBuilder Next(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Next(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageNumber + 1; var next = new TagBuilder("a"); @@ -123,7 +127,7 @@ private static TagBuilder Next(IPagedList list, Func generatePageUr return WrapInListItem(next, options, options.NextElementClass); } - private static TagBuilder Last(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Last(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageCount; var last = new TagBuilder("a"); @@ -139,7 +143,7 @@ private static TagBuilder Last(IPagedList list, Func generatePageUr return WrapInListItem(last, options, "PagedList-skipToLast"); } - private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptions options) + private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptionsBase options) { var text = new TagBuilder("a"); SetInnerText(text, string.Format(options.PageCountAndCurrentLocationFormat, list.PageNumber, list.PageCount)); @@ -147,7 +151,7 @@ private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRen return WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"); } - private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptions options) + private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptionsBase options) { var text = new TagBuilder("a"); SetInnerText(text, string.Format(options.ItemSliceAndTotalFormat, list.FirstItemOnPage, list.LastItemOnPage, list.TotalItemCount)); @@ -155,7 +159,7 @@ private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRender return WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"); } - private static TagBuilder Ellipses(PagedListRenderOptions options) + private static TagBuilder Ellipses(PagedListRenderOptionsBase options) { var a = new TagBuilder("a"); AppendHtml(a, options.EllipsesFormat); @@ -188,7 +192,7 @@ public static HtmlString PagedListPager(this IHtmlHelper html, public static HtmlString PagedListPager(this IHtmlHelper html, IPagedList list, Func generatePageUrl, - PagedListRenderOptions options) + PagedListRenderOptionsBase options) { if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1)) return null; diff --git a/src/X.PagedList.Mvc.Core/PagedListRenderOptions.cs b/src/X.PagedList.Mvc.Core/PagedListRenderOptions.cs index 00212637..c26f7617 100644 --- a/src/X.PagedList.Mvc.Core/PagedListRenderOptions.cs +++ b/src/X.PagedList.Mvc.Core/PagedListRenderOptions.cs @@ -10,232 +10,8 @@ namespace X.PagedList.Mvc.Core /// /// Options for configuring the output of . /// - public class PagedListRenderOptions + public class PagedListRenderOptions : PagedListRenderOptionsBase { - /// - /// The default settings render all navigation links and no descriptive text. - /// - public PagedListRenderOptions() - { - DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToIndividualPages = true; - DisplayPageCountAndCurrentLocation = false; - MaximumPageNumbersToDisplay = 10; - DisplayEllipsesWhenNotShowingAllPageNumbers = true; - EllipsesFormat = "…"; - LinkToFirstPageFormat = "<<"; - LinkToPreviousPageFormat = "<"; - LinkToIndividualPageFormat = "{0}"; - LinkToNextPageFormat = ">"; - LinkToLastPageFormat = ">>"; - PageCountAndCurrentLocationFormat = "Page {0} of {1}."; - ItemSliceAndTotalFormat = "Showing items {0} through {1} of {2}."; - FunctionToDisplayEachPageNumber = null; - ClassToApplyToFirstListItemInPager = null; - ClassToApplyToLastListItemInPager = null; - ContainerDivClasses = new[] { "pagination-container" }; - UlElementClasses = new[] { "pagination" }; - LiElementClasses = Enumerable.Empty(); - PageClasses = Enumerable.Empty(); - UlElementattributes = null; - ActiveLiElementClass = "active"; - EllipsesElementClass = "PagedList-ellipses"; - PreviousElementClass = "PagedList-skipToPrevious"; - NextElementClass = "PagedList-skipToNext"; - } - - /// - /// CSS Classes to append to the <div> element that wraps the paging control. - /// - public IEnumerable ContainerDivClasses { get; set; } - - /// - /// CSSClasses to append to the <ul> element in the paging control. - /// - public IEnumerable UlElementClasses { get; set; } - - /// - /// Attrinutes to appendto the <ul> element in the paging control - /// - public IDictionary UlElementattributes { get; set; } - - /// - /// CSS Classes to append to every <li> element in the paging control. - /// - public IEnumerable LiElementClasses { get; set; } - - /// - /// CSS Classes to appent to active <li> element in the paging control. - /// - public string ActiveLiElementClass { get; set; } - - /// - /// CSS Classes to append to every <a> or <span> element that represent each page in the paging control. - /// - public IEnumerable PageClasses { get; set; } - - /// - /// CSS Classes to append to previous element in the paging control. - /// - public string PreviousElementClass { get; set; } - - /// - /// CSS Classes to append to next element in the paging control. - /// - public string NextElementClass { get; set; } - - /// - /// CSS Classes to append to Ellipses element in the paging control. - /// - public string EllipsesElementClass { get; set; } - - /// - /// Specifies a CSS class to append to the first list item in the pager. If null or whitespace is defined, no additional class is added to first list item in list. - /// - public string ClassToApplyToFirstListItemInPager { get; set; } - - /// - /// Specifies a CSS class to append to the last list item in the pager. If null or whitespace is defined, no additional class is added to last list item in list. - /// - public string ClassToApplyToLastListItemInPager { get; set; } - - /// - /// If set to Always, always renders the paging control. If set to IfNeeded, render the paging control when there is more than one page. - /// - public PagedListDisplayMode Display { get; set; } - - /// - /// If set to Always, render a hyperlink to the first page in the list. If set to IfNeeded, render the hyperlink only when the first page isn't visible in the paging control. - /// - public PagedListDisplayMode DisplayLinkToFirstPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the last page in the list. If set to IfNeeded, render the hyperlink only when the last page isn't visible in the paging control. - /// - public PagedListDisplayMode DisplayLinkToLastPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the previous page of the list. If set to IfNeeded, render the hyperlink only when there is a previous page in the list. - /// - public PagedListDisplayMode DisplayLinkToPreviousPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the next page of the list. If set to IfNeeded, render the hyperlink only when there is a next page in the list. - /// - public PagedListDisplayMode DisplayLinkToNextPage { get; set; } - - /// - /// When true, includes hyperlinks for each page in the list. - /// - public bool DisplayLinkToIndividualPages { get; set; } - - /// - /// When true, shows the current page number and the total number of pages in the list. - /// - /// - /// "Page 3 of 8." - /// - public bool DisplayPageCountAndCurrentLocation { get; set; } - - /// - /// When true, shows the one-based index of the first and last items on the page, and the total number of items in the list. - /// - /// - /// "Showing items 75 through 100 of 183." - /// - public bool DisplayItemSliceAndTotal { get; set; } - - /// - /// The maximum number of page numbers to display. Null displays all page numbers. - /// - public int? MaximumPageNumbersToDisplay { get; set; } - - /// - /// If true, adds an ellipsis where not all page numbers are being displayed. - /// - /// - /// "1 2 3 4 5 ...", - /// "... 6 7 8 9 10 ...", - /// "... 11 12 13 14 15" - /// - public bool DisplayEllipsesWhenNotShowingAllPageNumbers { get; set; } - - /// - /// The pre-formatted text to display when not all page numbers are displayed at once. - /// - /// - /// "..." - /// - public string EllipsesFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the first page. The one-based index of the page (always 1 in this case) is passed into the formatting function - use {0} to reference it. - /// - /// - /// "<< First" - /// - public string LinkToFirstPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the previous page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "< Previous" - /// - public string LinkToPreviousPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to each individual page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "{0}" - /// - public string LinkToIndividualPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the next page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "Next >" - /// - public string LinkToNextPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the last page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "Last >>" - /// - public string LinkToLastPageFormat { get; set; } - - /// - /// The pre-formatted text to display when DisplayPageCountAndCurrentLocation is true. Use {0} to reference the current page and {1} to reference the total number of pages. - /// - /// - /// "Page {0} of {1}." - /// - public string PageCountAndCurrentLocationFormat { get; set; } - - /// - /// The pre-formatted text to display when DisplayItemSliceAndTotal is true. Use {0} to reference the first item on the page, {1} for the last item on the page, and {2} for the total number of items across all pages. - /// - /// - /// "Showing items {0} through {1} of {2}." - /// - public string ItemSliceAndTotalFormat { get; set; } - - /// - /// A function that will render each page number when specified (and DisplayLinkToIndividualPages is true). If no function is specified, the LinkToIndividualPageFormat value will be used instead. - /// - public Func FunctionToDisplayEachPageNumber { get; set; } - - /// - /// Text that will appear between each page number. If null or whitespace is specified, no delimiter will be shown. - /// - public string DelimiterBetweenPageNumbers { get; set; } /// /// An extension point which allows you to fully customize the anchor tags used for clickable pages, as well as navigation features such as Next, Last, etc. @@ -247,19 +23,24 @@ public PagedListRenderOptions() /// /// The preferred Html.PagedList(...) style options. /// The ajax options that will put into the link - /// The PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions options, AjaxOptions ajaxOptions) + /// The PagedListRenderOptionsBase value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(PagedListRenderOptionsBase options, AjaxOptions ajaxOptions) { - options.FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => - { - var liClass = liTagBuilder.Attributes.ContainsKey("class") ? liTagBuilder.Attributes["class"] ?? "" : ""; - if (ajaxOptions != null && !liClass.Contains("disabled") && !liClass.Contains("active")) - { - foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) - aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); - } - return aTagBuilder; - }; + if (options is PagedListRenderOptions) + { + ((PagedListRenderOptions)options).FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => + { + var liClass = liTagBuilder.Attributes.ContainsKey("class") + ? liTagBuilder.Attributes["class"] ?? "" + : ""; + if (ajaxOptions != null && !liClass.Contains("disabled") && !liClass.Contains("active")) + { + foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) + aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); + } + return aTagBuilder; + }; + } return options; } @@ -267,8 +48,8 @@ public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(PagedListRen /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. /// /// The element ID ("my_id") of the element whose innerHtml should be replaced, if # is included at the start this will be removed. - /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(string id) + /// A default instance of PagedListRenderOptionsBase value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(string id) { if (id.StartsWith("#")) @@ -288,184 +69,11 @@ public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(string id) /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. /// /// Ajax options that will be used to generate the unobstrusive tags on the link - /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(AjaxOptions ajaxOptions) + /// A default instance of PagedListRenderOptionsBase value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(AjaxOptions ajaxOptions) { return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions); } - /// - /// Also includes links to First and Last pages. - /// - public static PagedListRenderOptions Classic - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always - }; - } - } - - /// - /// Also includes links to First and Last pages. - /// - public static PagedListRenderOptions ClassicPlusFirstAndLast - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Always, - DisplayLinkToLastPage = PagedListDisplayMode.Always, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always - }; - } - } - - /// - /// Shows only the Previous and Next links. - /// - public static PagedListRenderOptions Minimal - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false - }; - } - } - - /// - /// Shows Previous and Next links along with current page number and page count. - /// - public static PagedListRenderOptions MinimalWithPageCountText - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - DisplayPageCountAndCurrentLocation = true - }; - } - } - - /// - /// Shows Previous and Next links along with index of first and last items on page and total number of items across all pages. - /// - public static PagedListRenderOptions MinimalWithItemCountText - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - DisplayItemSliceAndTotal = true - }; - } - } - - /// - /// Shows only links to each individual page. - /// - public static PagedListRenderOptions PageNumbersOnly - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Never, - DisplayLinkToNextPage = PagedListDisplayMode.Never, - DisplayEllipsesWhenNotShowingAllPageNumbers = false - }; - } - } - - /// - /// Shows Next and Previous while limiting to a max of 5 page numbers at a time. - /// - public static PagedListRenderOptions OnlyShowFivePagesAtATime - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - MaximumPageNumbersToDisplay = 5 - }; - } - } - - /// - /// Twitter Bootstrap 2's basic pager format (just Previous and Next links). - /// - public static PagedListRenderOptions TwitterBootstrapPager - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - ContainerDivClasses = null, - UlElementClasses = new[] { "pager" }, - ClassToApplyToFirstListItemInPager = null, - ClassToApplyToLastListItemInPager = null, - LinkToPreviousPageFormat = "Previous", - LinkToNextPageFormat = "Next" - }; - } - } - - /// - /// Twitter Bootstrap 2's basic pager format (just Previous and Next links), with aligned links. - /// - public static PagedListRenderOptions TwitterBootstrapPagerAligned - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - ContainerDivClasses = null, - UlElementClasses = new[] { "pager" }, - ClassToApplyToFirstListItemInPager = "previous", - ClassToApplyToLastListItemInPager = "next", - LinkToPreviousPageFormat = "← Older", - LinkToNextPageFormat = "Newer →" - }; - } - } } } \ No newline at end of file diff --git a/src/X.PagedList.Mvc/HtmlHelper.cs b/src/X.PagedList.Mvc/HtmlHelper.cs index 9218fc37..07aa28e4 100644 --- a/src/X.PagedList.Mvc/HtmlHelper.cs +++ b/src/X.PagedList.Mvc/HtmlHelper.cs @@ -40,7 +40,7 @@ private static TagBuilder WrapInListItem(string text) return li; } - private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOptions options, params string[] classes) + private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOptionsBase options, params string[] classes) { var li = new TagBuilder("li"); @@ -49,16 +49,19 @@ private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOption li.AddCssClass(@class); } - if (options.FunctionToTransformEachPageLink != null) + if (options is PagedListRenderOptions) { - return options.FunctionToTransformEachPageLink(li, inner); + if (((PagedListRenderOptions)options).FunctionToTransformEachPageLink != null) + { + return ((PagedListRenderOptions)options).FunctionToTransformEachPageLink(li, inner); + } } AppendHtml(li, TagBuilderToString(inner)); return li; } - private static TagBuilder First(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder First(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { const int targetPageNumber = 1; var first = new TagBuilder("a"); @@ -78,7 +81,7 @@ private static TagBuilder First(IPagedList list, Func generatePageU return WrapInListItem(first, options, "PagedList-skipToFirst"); } - private static TagBuilder Previous(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Previous(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageNumber - 1; var previous = new TagBuilder("a"); @@ -100,7 +103,7 @@ private static TagBuilder Previous(IPagedList list, Func generatePa return WrapInListItem(previous, options, options.PreviousElementClass); } - private static TagBuilder Page(int i, IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Page(int i, IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var format = options.FunctionToDisplayEachPageNumber ?? (pageNumber => string.Format(options.LinkToIndividualPageFormat, pageNumber)); @@ -123,7 +126,7 @@ private static TagBuilder Page(int i, IPagedList list, Func generat return WrapInListItem(page, options); } - private static TagBuilder Next(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Next(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageNumber + 1; var next = new TagBuilder("a"); @@ -144,7 +147,7 @@ private static TagBuilder Next(IPagedList list, Func generatePageUr return WrapInListItem(next, options, options.NextElementClass); } - private static TagBuilder Last(IPagedList list, Func generatePageUrl, PagedListRenderOptions options) + private static TagBuilder Last(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options) { var targetPageNumber = list.PageCount; var last = new TagBuilder("a"); @@ -164,7 +167,7 @@ private static TagBuilder Last(IPagedList list, Func generatePageUr return WrapInListItem(last, options, "PagedList-skipToLast"); } - private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptions options) + private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptionsBase options) { var text = new TagBuilder("a"); SetInnerText(text, string.Format(options.PageCountAndCurrentLocationFormat, list.PageNumber, list.PageCount)); @@ -172,7 +175,7 @@ private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRen return WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"); } - private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptions options) + private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptionsBase options) { var text = new TagBuilder("a"); SetInnerText(text, string.Format(options.ItemSliceAndTotalFormat, list.FirstItemOnPage, list.LastItemOnPage, list.TotalItemCount)); @@ -180,7 +183,7 @@ private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRender return WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"); } - private static TagBuilder Ellipses(PagedListRenderOptions options) + private static TagBuilder Ellipses(PagedListRenderOptionsBase options) { var a = new TagBuilder("a"); AppendHtml(a, options.EllipsesFormat); @@ -213,7 +216,7 @@ public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html, public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html, IPagedList list, Func generatePageUrl, - PagedListRenderOptions options) + PagedListRenderOptionsBase options) { if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1)) return null; @@ -346,7 +349,7 @@ public static HtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html, return new HtmlString(TagBuilderToString(outerDiv)); } - private static TagBuilder PreviousEllipsis(IPagedList list, Func generatePageUrl, PagedListRenderOptions options, int firstPageToDisplay) + private static TagBuilder PreviousEllipsis(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options, int firstPageToDisplay) { var targetPageNumber = firstPageToDisplay - 1; var previous = new TagBuilder("a") @@ -362,7 +365,7 @@ private static TagBuilder PreviousEllipsis(IPagedList list, Func ge return WrapInListItem(previous, options, "PagedList-skipToPrevious"); } - private static TagBuilder NextEllipsis(IPagedList list, Func generatePageUrl, PagedListRenderOptions options, int lastPageToDisplay) + private static TagBuilder NextEllipsis(IPagedList list, Func generatePageUrl, PagedListRenderOptionsBase options, int lastPageToDisplay) { var targetPageNumber = lastPageToDisplay + 1; diff --git a/src/X.PagedList.Mvc/PagedListRenderOptions.cs b/src/X.PagedList.Mvc/PagedListRenderOptions.cs index 1da0ca25..76bf1557 100644 --- a/src/X.PagedList.Mvc/PagedListRenderOptions.cs +++ b/src/X.PagedList.Mvc/PagedListRenderOptions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Web.Mvc; using System.Web.Mvc.Ajax; using X.PagedList.Mvc.Common; @@ -10,464 +8,72 @@ namespace X.PagedList.Mvc /// /// Options for configuring the output of . /// - public class PagedListRenderOptions - { - /// - /// The default settings render all navigation links and no descriptive text. - /// - public PagedListRenderOptions() - { - DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded; - DisplayLinkToIndividualPages = true; - DisplayPageCountAndCurrentLocation = false; - MaximumPageNumbersToDisplay = 10; - DisplayEllipsesWhenNotShowingAllPageNumbers = true; - EllipsesFormat = "…"; - LinkToFirstPageFormat = "<<"; - LinkToPreviousPageFormat = "<"; - LinkToIndividualPageFormat = "{0}"; - LinkToNextPageFormat = ">"; - LinkToLastPageFormat = ">>"; - PageCountAndCurrentLocationFormat = "Page {0} of {1}."; - ItemSliceAndTotalFormat = "Showing items {0} through {1} of {2}."; - FunctionToDisplayEachPageNumber = null; - ClassToApplyToFirstListItemInPager = null; - ClassToApplyToLastListItemInPager = null; - ContainerDivClasses = new[] { "pagination-container" }; - UlElementClasses = new[] { "pagination" }; - LiElementClasses = Enumerable.Empty(); - PageClasses = Enumerable.Empty(); - UlElementattributes = null; - ActiveLiElementClass = "active"; - EllipsesElementClass = "PagedList-ellipses"; - PreviousElementClass = "PagedList-skipToPrevious"; - NextElementClass = "PagedList-skipToNext"; - } - - /// - /// CSS Classes to append to the <div> element that wraps the paging control. - /// - public IEnumerable ContainerDivClasses { get; set; } - - /// - /// CSSClasses to append to the <ul> element in the paging control. - /// - public IEnumerable UlElementClasses { get; set; } - - /// - /// Attrinutes to appendto the <ul> element in the paging control - /// - public IDictionary UlElementattributes { get; set; } - - /// - /// CSS Classes to append to every <li> element in the paging control. - /// - public IEnumerable LiElementClasses { get; set; } - - /// - /// CSS Classes to appent to active <li> element in the paging control. - /// - public string ActiveLiElementClass { get; set; } - - /// - /// CSS Classes to append to every <a> or <span> element that represent each page in the paging control. - /// - public IEnumerable PageClasses { get; set; } - - /// - /// CSS Classes to append to previous element in the paging control. - /// - public string PreviousElementClass { get; set; } - - /// - /// CSS Classes to append to next element in the paging control. - /// - public string NextElementClass { get; set; } - - /// - /// CSS Classes to append to Ellipses element in the paging control. - /// - public string EllipsesElementClass { get; set; } - - /// - /// Specifies a CSS class to append to the first list item in the pager. If null or whitespace is defined, no additional class is added to first list item in list. - /// - public string ClassToApplyToFirstListItemInPager { get; set; } - - /// - /// Specifies a CSS class to append to the last list item in the pager. If null or whitespace is defined, no additional class is added to last list item in list. - /// - public string ClassToApplyToLastListItemInPager { get; set; } - - /// - /// If set to Always, always renders the paging control. If set to IfNeeded, render the paging control when there is more than one page. - /// - public PagedListDisplayMode Display { get; set; } - - /// - /// If set to Always, render a hyperlink to the first page in the list. If set to IfNeeded, render the hyperlink only when the first page isn't visible in the paging control. - /// - public PagedListDisplayMode DisplayLinkToFirstPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the last page in the list. If set to IfNeeded, render the hyperlink only when the last page isn't visible in the paging control. - /// - public PagedListDisplayMode DisplayLinkToLastPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the previous page of the list. If set to IfNeeded, render the hyperlink only when there is a previous page in the list. - /// - public PagedListDisplayMode DisplayLinkToPreviousPage { get; set; } - - /// - /// If set to Always, render a hyperlink to the next page of the list. If set to IfNeeded, render the hyperlink only when there is a next page in the list. - /// - public PagedListDisplayMode DisplayLinkToNextPage { get; set; } - - /// - /// When true, includes hyperlinks for each page in the list. - /// - public bool DisplayLinkToIndividualPages { get; set; } - - /// - /// When true, shows the current page number and the total number of pages in the list. - /// - /// - /// "Page 3 of 8." - /// - public bool DisplayPageCountAndCurrentLocation { get; set; } - - /// - /// When true, shows the one-based index of the first and last items on the page, and the total number of items in the list. - /// - /// - /// "Showing items 75 through 100 of 183." - /// - public bool DisplayItemSliceAndTotal { get; set; } - - /// - /// The maximum number of page numbers to display. Null displays all page numbers. - /// - public int? MaximumPageNumbersToDisplay { get; set; } - - /// - /// If true, adds an ellipsis where not all page numbers are being displayed. - /// - /// - /// "1 2 3 4 5 ...", - /// "... 6 7 8 9 10 ...", - /// "... 11 12 13 14 15" - /// - public bool DisplayEllipsesWhenNotShowingAllPageNumbers { get; set; } - - /// - /// The pre-formatted text to display when not all page numbers are displayed at once. - /// - /// - /// "..." - /// - public string EllipsesFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the first page. The one-based index of the page (always 1 in this case) is passed into the formatting function - use {0} to reference it. - /// - /// - /// "<< First" - /// - public string LinkToFirstPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the previous page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "< Previous" - /// - public string LinkToPreviousPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to each individual page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "{0}" - /// - public string LinkToIndividualPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the next page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "Next >" - /// - public string LinkToNextPageFormat { get; set; } - - /// - /// The pre-formatted text to display inside the hyperlink to the last page. The one-based index of the page is passed into the formatting function - use {0} to reference it. - /// - /// - /// "Last >>" - /// - public string LinkToLastPageFormat { get; set; } - - /// - /// The pre-formatted text to display when DisplayPageCountAndCurrentLocation is true. Use {0} to reference the current page and {1} to reference the total number of pages. - /// - /// - /// "Page {0} of {1}." - /// - public string PageCountAndCurrentLocationFormat { get; set; } - - /// - /// The pre-formatted text to display when DisplayItemSliceAndTotal is true. Use {0} to reference the first item on the page, {1} for the last item on the page, and {2} for the total number of items across all pages. - /// - /// - /// "Showing items {0} through {1} of {2}." - /// - public string ItemSliceAndTotalFormat { get; set; } - - /// - /// A function that will render each page number when specified (and DisplayLinkToIndividualPages is true). If no function is specified, the LinkToIndividualPageFormat value will be used instead. - /// - public Func FunctionToDisplayEachPageNumber { get; set; } - - /// - /// Text that will appear between each page number. If null or whitespace is specified, no delimiter will be shown. - /// - public string DelimiterBetweenPageNumbers { get; set; } - - /// - /// An extension point which allows you to fully customize the anchor tags used for clickable pages, as well as navigation features such as Next, Last, etc. - /// - public Func FunctionToTransformEachPageLink { get; set; } - - /// - /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. - /// - /// The preferred Html.PagedList(...) style options. - /// The ajax options that will put into the link - /// The PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions options, AjaxOptions ajaxOptions) - { - options.FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => - { - var liClass = liTagBuilder.Attributes.ContainsKey("class") ? liTagBuilder.Attributes["class"] ?? "" : ""; - if (ajaxOptions != null && !liClass.Contains("disabled") && !liClass.Contains("active")) - { - foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) - aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); - } - - liTagBuilder.InnerHtml = aTagBuilder.ToString(); - return liTagBuilder; - }; - return options; - } - - /// - /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. - /// - /// The element ID ("my_id") of the element whose innerHtml should be replaced, if # is included at the start this will be removed. - /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(string id) - { - - if (id.StartsWith("#")) - id = id.Substring(1); - - var ajaxOptions = new AjaxOptions() - { - HttpMethod = "GET", - InsertionMode = InsertionMode.Replace, - UpdateTargetId = id - }; - - return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions); - } - - /// - /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. - /// - /// Ajax options that will be used to generate the unobstrusive tags on the link - /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. - public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(AjaxOptions ajaxOptions) - { - return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions); - } - - /// - /// Also includes links to First and Last pages. - /// - public static PagedListRenderOptions Classic - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always - }; - } - } - - /// - /// Also includes links to First and Last pages. - /// - public static PagedListRenderOptions ClassicPlusFirstAndLast - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Always, - DisplayLinkToLastPage = PagedListDisplayMode.Always, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always - }; - } - } - - /// - /// Shows only the Previous and Next links. - /// - public static PagedListRenderOptions Minimal + public class PagedListRenderOptions : PagedListRenderOptionsBase { - get - { - return new PagedListRenderOptions + /// + /// An extension point which allows you to fully customize the anchor tags used for clickable pages, as well as navigation features such as Next, Last, etc. + /// + public Func FunctionToTransformEachPageLink { get; set; } + + /// + /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. + /// + /// The preferred Html.PagedList(...) style options. + /// The ajax options that will put into the link + /// The PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(PagedListRenderOptionsBase options, AjaxOptions ajaxOptions) { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false - }; - } - } - - /// - /// Shows Previous and Next links along with current page number and page count. - /// - public static PagedListRenderOptions MinimalWithPageCountText - { - get - { - return new PagedListRenderOptions + if (options is PagedListRenderOptions) + { + ((PagedListRenderOptions) options).FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => + { + var liClass = liTagBuilder.Attributes.ContainsKey("class") + ? liTagBuilder.Attributes["class"] ?? "" + : ""; + if (ajaxOptions != null && !liClass.Contains("disabled") && !liClass.Contains("active")) + { + foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) + aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); + } + + liTagBuilder.InnerHtml = aTagBuilder.ToString(); + return liTagBuilder; + }; + } + + return options; + } + + /// + /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. + /// + /// The element ID ("my_id") of the element whose innerHtml should be replaced, if # is included at the start this will be removed. + /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(string id) { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - DisplayPageCountAndCurrentLocation = true - }; - } - } - /// - /// Shows Previous and Next links along with index of first and last items on page and total number of items across all pages. - /// - public static PagedListRenderOptions MinimalWithItemCountText - { - get - { - return new PagedListRenderOptions + if (id.StartsWith("#")) + id = id.Substring(1); + + var ajaxOptions = new AjaxOptions() + { + HttpMethod = "GET", + InsertionMode = InsertionMode.Replace, + UpdateTargetId = id + }; + + return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions); + } + + /// + /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. + /// + /// Ajax options that will be used to generate the unobstrusive tags on the link + /// A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links. + public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(AjaxOptions ajaxOptions) { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - DisplayItemSliceAndTotal = true - }; - } - } + return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions); + } - /// - /// Shows only links to each individual page. - /// - public static PagedListRenderOptions PageNumbersOnly - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Never, - DisplayLinkToNextPage = PagedListDisplayMode.Never, - DisplayEllipsesWhenNotShowingAllPageNumbers = false - }; - } - } - - /// - /// Shows Next and Previous while limiting to a max of 5 page numbers at a time. - /// - public static PagedListRenderOptions OnlyShowFivePagesAtATime - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - MaximumPageNumbersToDisplay = 5 - }; - } - } - - /// - /// Twitter Bootstrap 2's basic pager format (just Previous and Next links). - /// - public static PagedListRenderOptions TwitterBootstrapPager - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - ContainerDivClasses = null, - UlElementClasses = new[] { "pager" }, - ClassToApplyToFirstListItemInPager = null, - ClassToApplyToLastListItemInPager = null, - LinkToPreviousPageFormat = "Previous", - LinkToNextPageFormat = "Next" - }; - } - } - - /// - /// Twitter Bootstrap 2's basic pager format (just Previous and Next links), with aligned links. - /// - public static PagedListRenderOptions TwitterBootstrapPagerAligned - { - get - { - return new PagedListRenderOptions - { - DisplayLinkToFirstPage = PagedListDisplayMode.Never, - DisplayLinkToLastPage = PagedListDisplayMode.Never, - DisplayLinkToPreviousPage = PagedListDisplayMode.Always, - DisplayLinkToNextPage = PagedListDisplayMode.Always, - DisplayLinkToIndividualPages = false, - ContainerDivClasses = null, - UlElementClasses = new[] { "pager" }, - ClassToApplyToFirstListItemInPager = "previous", - ClassToApplyToLastListItemInPager = "next", - LinkToPreviousPageFormat = "← Older", - LinkToNextPageFormat = "Newer →" - }; - } } - } } \ No newline at end of file