Exemplo n.º 1
0
 private static void AddCssClasses(TagBuilder a, PagedListRenderOptionsBase options)
 {
     foreach (var c in options.PageClasses ?? Enumerable.Empty <string>())
     {
         a.AddCssClass(c);
     }
 }
Exemplo n.º 2
0
 public HtmlPagerBuilder(IHtmlHelper htmlHelper, IPagedList pagedList)
 {
     this._htmlHelper      = htmlHelper;
     this._pagedList       = pagedList;
     this._generatePageUrl = x => x.ToString();
     this._options         = new PagedListRenderOptions();
 }
Exemplo n.º 3
0
        private static TagBuilder Ellipses(PagedListRenderOptionsBase options)
        {
            var a = new TagBuilder("a");

            a.InnerHtml.AppendHtml(options.EllipsesFormat);
            return(WrapInListItem(a, options, options.EllipsesElementClass, "disabled"));
        }
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="options">The preferred Html.PagedList(...) style options.</param>
        /// <param name="ajaxOptions">The ajax options that will put into the link</param>
        /// <returns>The PagedListRenderOptionsBase value passed in, with unobtrusive AJAX attributes added to the page links.</returns>
        public static PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(PagedListRenderOptionsBase options, AjaxOptions ajaxOptions)
        {
            if (options is PagedListRenderOptions)
            {
                options.IsAjax = true;
                ((PagedListRenderOptions)options).FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) =>
                {
                    var liClass = liTagBuilder.Attributes.ContainsKey("class")
                         ? liTagBuilder.Attributes["class"] ?? ""
                         : "";
                    // && !liClass.Contains("active") 生产Html,将当前页也生成ajax
                    if (ajaxOptions != null && !liClass.Contains("disabled"))
                    {
                        foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes())
                        {
                            aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString());
                        }
                    }


                    return(aTagBuilder);
                };
            }


            return(options);
        }
Exemplo n.º 5
0
        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));

            return(WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"));
        }
Exemplo n.º 6
0
        private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptionsBase options)
        {
            var text = new TagBuilder("div");

            SetInnerText(text, string.Format(options.PageCountAndCurrentLocationFormat, list.PageNumber, list.PageCount));
            text.AddCssClass("ui label");
            return(text);
        }
Exemplo n.º 7
0
        private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptionsBase options)
        {
            var text = new TagBuilder("a");

            SetInnerText(text, string.Format(options.PageCountAndCurrentLocationFormat, list.PageNumber, list.PageCount));

            return(WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"));
        }
Exemplo n.º 8
0
        private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptionsBase options)
        {
            var text = new TagBuilder("div");

            SetInnerText(text, string.Format(options.ItemSliceAndTotalFormat, list.FirstItemOnPage, list.LastItemOnPage, list.TotalItemCount));
            text.AddCssClass("ui label");

            return(text);
        }
Exemplo n.º 9
0
        private static PagedListRenderOptionsBase GetCommonSetting()
        {
            var option = new PagedListRenderOptionsBase()
            {
                LiElementClasses = new[] { "page-item" },
                PageClasses      = new[] { "page-link" },

                // Due to no way change Ellipses element class, try setting EllipsesElementClass but fail, so disable Ellipses
                DisplayEllipsesWhenNotShowingAllPageNumbers = false
            };

            return(option);
        }
Exemplo n.º 10
0
        private static TagBuilder ItemSliceAndTotalText(IPagedList list, PagedListRenderOptionsBase options)
        {
            var text = new TagBuilder("a");

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

            SetInnerText(text, string.Format(options.ItemSliceAndTotalFormat, list.FirstItemOnPage, list.LastItemOnPage, list.TotalItemCount));

            return(WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"));
        }
Exemplo n.º 11
0
        private static TagBuilder Ellipses(PagedListRenderOptionsBase options)
        {
            var a = new TagBuilder("a");

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

            AppendHtml(a, options.EllipsesFormat);

            return(WrapInListItem(a, options, options.EllipsesElementClass, "disabled"));
        }
Exemplo n.º 12
0
        private static TagBuilder PageCountAndLocationText(IPagedList list, PagedListRenderOptionsBase options)
        {
            var text = new TagBuilder("a");

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

            SetInnerText(text, string.Format(options.PageCountAndCurrentLocationFormat, list.PageNumber, list.PageCount));

            return(WrapInListItem(text, options, "PagedList-pageCountAndLocation", "disabled"));
        }
Exemplo n.º 13
0
        private static TagBuilder Last(IPagedList list, string urlFormat, PagedListRenderOptionsBase options)
        {
            var targetPageNumber = list.PageCount;
            var last             = new TagBuilder("a");

            last.InnerHtml.AppendHtml(string.Format(options.LinkToLastPageFormat, targetPageNumber));
            AddCssClasses(last, options);

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

            last.Attributes["href"] = string.Format(urlFormat, targetPageNumber);
            return(WrapInListItem(last, options, "PagedList-skipToLast"));
        }
Exemplo n.º 14
0
        private static TagBuilder First(IPagedList list, string urlFormat, PagedListRenderOptionsBase options)
        {
            const int targetPageNumber = 1;
            var       first            = new TagBuilder("a");

            first.InnerHtml.AppendHtml(string.Format(options.LinkToFirstPageFormat, targetPageNumber));
            AddCssClasses(first, options);

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

            first.Attributes["href"] = string.Format(urlFormat, targetPageNumber);
            return(WrapInListItem(first, options, "PagedList-skipToFirst"));
        }
Exemplo n.º 15
0
        private static TagBuilder Next(IPagedList list, string urlFormat, PagedListRenderOptionsBase options)
        {
            var targetPageNumber = list.PageNumber + 1;
            var next             = new TagBuilder("a");

            next.InnerHtml.AppendHtml(string.Format(options.LinkToNextPageFormat, targetPageNumber));
            next.Attributes["rel"] = "next";

            AddCssClasses(next, options);

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

            next.Attributes["href"] = string.Format(urlFormat, targetPageNumber);
            return(WrapInListItem(next, options, options.NextElementClass));
        }
Exemplo n.º 16
0
        private static TagBuilder Previous(IPagedList list, string urlFormat, PagedListRenderOptionsBase options)
        {
            var targetPageNumber = list.PageNumber - 1;
            var previous         = new TagBuilder("a");

            previous.InnerHtml.AppendHtml(string.Format(options.LinkToPreviousPageFormat, targetPageNumber));
            previous.Attributes["rel"] = "prev";

            AddCssClasses(previous, options);

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

            previous.Attributes["href"] = string.Format(urlFormat, targetPageNumber);
            return(WrapInListItem(previous, options, options.PreviousElementClass));
        }
Exemplo n.º 17
0
        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 is PagedListRenderOptions)
            {
                if (((PagedListRenderOptions)options).FunctionToTransformEachPageLink != null)
                {
                    inner = ((PagedListRenderOptions)options).FunctionToTransformEachPageLink(li, inner);
                }
            }

            AppendHtml(li, TagBuilderToString(inner));
            return(li);
        }
Exemplo n.º 18
0
        private static TagBuilder Page(int i, IPagedList list, string urlFormat, PagedListRenderOptionsBase options)
        {
            var format = options.FunctionToDisplayEachPageNumber
                         ?? (pageNumber => string.Format(options.LinkToIndividualPageFormat, pageNumber));
            var targetPageNumber = i;
            var page             = i == list.PageNumber ? new TagBuilder("span") : new TagBuilder("a");

            SetInnerText(page, format(targetPageNumber));

            AddCssClasses(page, options);

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

            page.Attributes["href"] = string.Format(urlFormat, targetPageNumber);

            return(WrapInListItem(page, options));
        }
Exemplo n.º 19
0
        public IHtmlContent Minimal()
        {
            this._options = PagedListRenderOptionsBase.Minimal;

            return(Build());
        }
Exemplo n.º 20
0
        private static TagBuilder First(IPagedList list, Func <int, string> generatePageUrl, PagedListRenderOptionsBase options)
        {
            const int targetPageNumber = 1;
            var       first            = new TagBuilder("a");

            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"));
        }
Exemplo n.º 21
0
        ///<summary>
        ///	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 IHtmlHelper html,
                                                IPagedList list,
                                                Func <int, string> generatePageUrl,
                                                PagedListRenderOptionsBase options)
        {
            if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1))
            {
                return(null);
            }

            var listItemLinks = new List <TagBuilder>();

            //calculate start and end of range of page numbers
            var firstPageToDisplay   = 1;
            var lastPageToDisplay    = list.PageCount;
            var pageNumbersToDisplay = lastPageToDisplay;

            if (options.MaximumPageNumbersToDisplay.HasValue && list.PageCount > options.MaximumPageNumbersToDisplay)
            {
                // cannot fit all pages into pager
                var maxPageNumbersToDisplay = options.MaximumPageNumbersToDisplay.Value;
                firstPageToDisplay = list.PageNumber - maxPageNumbersToDisplay / 2;
                if (firstPageToDisplay < 1)
                {
                    firstPageToDisplay = 1;
                }
                pageNumbersToDisplay = maxPageNumbersToDisplay;
                lastPageToDisplay    = firstPageToDisplay + pageNumbersToDisplay - 1;
                if (lastPageToDisplay > list.PageCount)
                {
                    firstPageToDisplay = list.PageCount - maxPageNumbersToDisplay + 1;
                }
            }

            //first
            if (options.DisplayLinkToFirstPage == PagedListDisplayMode.Always || (options.DisplayLinkToFirstPage == PagedListDisplayMode.IfNeeded && firstPageToDisplay > 1))
            {
                listItemLinks.Add(First(list, generatePageUrl, options));
            }

            //previous
            if (options.DisplayLinkToPreviousPage == PagedListDisplayMode.Always || (options.DisplayLinkToPreviousPage == PagedListDisplayMode.IfNeeded && !list.IsFirstPage))
            {
                listItemLinks.Add(Previous(list, generatePageUrl, options));
            }

            //text
            if (options.DisplayPageCountAndCurrentLocation)
            {
                listItemLinks.Add(PageCountAndLocationText(list, options));
            }

            //text
            if (options.DisplayItemSliceAndTotal)
            {
                listItemLinks.Add(ItemSliceAndTotalText(list, options));
            }

            //page
            if (options.DisplayLinkToIndividualPages)
            {
                //if there are previous page numbers not displayed, show an ellipsis
                if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && firstPageToDisplay > 1)
                {
                    listItemLinks.Add(Ellipses(options));
                }

                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));
                }
            }

            //next
            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))
            {
                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);
            foreach (var c in options.UlElementClasses ?? Enumerable.Empty <string>())
            {
                ul.AddCssClass(c);
            }

            if (options.UlElementattributes != null)
            {
                foreach (var c in options.UlElementattributes)
                {
                    ul.MergeAttribute(c.Key, c.Value);
                }
            }

            var outerDiv = new TagBuilder("div");

            foreach (var c in options.ContainerDivClasses ?? Enumerable.Empty <string>())
            {
                outerDiv.AddCssClass(c);
            }
            AppendHtml(outerDiv, TagBuilderToString(ul));

            return(new HtmlString(TagBuilderToString(outerDiv)));
        }
Exemplo n.º 22
0
        private static TagBuilder Next(IPagedList list, Func <int, string> generatePageUrl, PagedListRenderOptionsBase options)
        {
            var targetPageNumber = list.PageNumber + 1;
            var next             = new TagBuilder("a");

            AppendHtml(next, string.Format(options.LinkToNextPageFormat, targetPageNumber));
            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));
        }
Exemplo n.º 23
0
        public IHtmlContent PageNumbersOnly()
        {
            this._options = PagedListRenderOptionsBase.PageNumbersOnly;

            return(Build());
        }
Exemplo n.º 24
0
        private static TagBuilder Page(int i, IPagedList list, Func <int, string> generatePageUrl, PagedListRenderOptionsBase options)
        {
            var format = options.FunctionToDisplayEachPageNumber
                         ?? (pageNumber => string.Format(options.LinkToIndividualPageFormat, pageNumber));
            var targetPageNumber = i;
            var page             = i == list.PageNumber ? new TagBuilder("span") : new TagBuilder("a");

            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);

            return(WrapInListItem(page, options));
        }
Exemplo n.º 25
0
        public IHtmlContent ClassicPlusFirstAndLast()
        {
            this._options = PagedListRenderOptionsBase.ClassicPlusFirstAndLast;

            return(Build());
        }
Exemplo n.º 26
0
        public IHtmlContent Classic()
        {
            this._options = PagedListRenderOptionsBase.Classic;

            return(Build());
        }
Exemplo n.º 27
0
        public IHtmlContent OnlyShowFivePagesAtATime()
        {
            this._options = PagedListRenderOptionsBase.OnlyShowFivePagesAtATime;

            return(Build());
        }
Exemplo n.º 28
0
        public IHtmlContent Build(PagedListRenderOptions options)
        {
            this._options = options ?? this._options;

            return(Build());
        }
Exemplo n.º 29
0
        private static TagBuilder Previous(IPagedList list, Func <int, string> generatePageUrl, PagedListRenderOptionsBase options)
        {
            var targetPageNumber = list.PageNumber - 1;
            var previous         = new TagBuilder("a");

            AppendHtml(previous, string.Format(options.LinkToPreviousPageFormat, targetPageNumber));
            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));
        }
Exemplo n.º 30
0
        public IHtmlContent MinimalWithItemCountText()
        {
            this._options = PagedListRenderOptionsBase.MinimalWithItemCountText;

            return(Build());
        }