Exemplo n.º 1
0
 public string CreatePager(int?currentPage, int itemCount, int pageSize, GeneratePageLinkDelegate urlGenerator)
 {
     return(this.CreatePager(currentPage, itemCount, pageSize, x => x.ToString(), urlGenerator));
 }
Exemplo n.º 2
0
        public string CreatePager(int?currentPage, int itemCount, int pageSize, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator)
        {
            int pageCount = PageCount(itemCount, pageSize);
            int?page      = currentPage;

            if (page.HasValue)
            {
                page = ClampPage(page, pageCount);
            }

            StringBuilder result = new StringBuilder();

            foreach (PagerElement element in this.Elements)
            {
                result.Append(element.Build(currentPage, pageCount, itemCount, itemGenerator, urlGenerator));
            }

            return(result.ToString());
        }
Exemplo n.º 3
0
            public override string Build(int?currentPage, int pageCount, int itemCount, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator)
            {
                List <NumberRange> ranges = new List <NumberRange>();

                if (firstNumber > 0)
                {
                    int min = Pager.ClampPage(1, pageCount);
                    int max = Pager.ClampPage(firstNumber - 1, pageCount);

                    ranges.Add(new NumberRange(min, max, "", max == pageCount ? "" : this.firstSeperator));
                }

                if (lastNumber > 0)
                {
                    int min = Pager.ClampPage(pageCount - (lastNumber - 1), pageCount);
                    int max = Pager.ClampPage(pageCount, pageCount);

                    ranges.Add(new NumberRange(min, max, min == 1 ? "" : this.lastSeperator, ""));
                }

                int effectiveCurrent = 1;

                if (currentPage.HasValue)
                {
                    effectiveCurrent = currentPage.Value;
                }

                {
                    int min = Pager.ClampPage(effectiveCurrent - adjacentMin, pageCount);
                    int max = Pager.ClampPage(effectiveCurrent + adjacentMin, pageCount);

                    int addMax = adjacentMin - (effectiveCurrent - min);
                    int subMin = adjacentMin - (max - effectiveCurrent);

                    if (addMax > 0)
                    {
                        max += addMax;
                        max  = Math.Min(effectiveCurrent + adjacentMax, max);
                    }

                    if (subMin > 0)
                    {
                        min -= subMin;
                        min  = Math.Max(effectiveCurrent - adjacentMax, min);
                    }

                    max = Pager.ClampPage(max, pageCount);
                    min = Pager.ClampPage(min, pageCount);

                    ranges.Add(new NumberRange(min, max, "", ""));
                }

                while (true)
                {
                    var match = from a in ranges
                                from b in ranges
                                where a != b
                                where a.Min <= b.Min
                                where (!this.collapseAdjacentElements && (a.Min < (b.Max + 1) && b.Min < (a.Max + 1))) || (this.collapseAdjacentElements && (a.Min <= (b.Max + 1) && b.Min <= (a.Max + 1)))
                                select new
                    {
                        a,
                        b
                    };

                    if (!match.Any())
                    {
                        break;
                    }

                    var firstMatch = match.First();

                    ranges.Remove(firstMatch.a);
                    ranges.Remove(firstMatch.b);

                    int    min;
                    int    max;
                    string seperatorBefore;
                    string seperatorAfter;

                    if (firstMatch.a.Min < firstMatch.b.Min)
                    {
                        min             = firstMatch.a.Min;
                        seperatorBefore = firstMatch.a.SeperatorBefore;
                    }
                    else if (firstMatch.b.Min < firstMatch.a.Min)
                    {
                        min             = firstMatch.b.Min;
                        seperatorBefore = firstMatch.b.SeperatorBefore;
                    }
                    else
                    {
                        min = firstMatch.a.Min; // == firstMatch.b.Min
                        if (string.IsNullOrEmpty(firstMatch.b.SeperatorBefore))
                        {
                            seperatorBefore = firstMatch.a.SeperatorBefore;
                        }
                        else
                        {
                            seperatorBefore = firstMatch.b.SeperatorBefore;
                        }
                    }

                    if (firstMatch.a.Max > firstMatch.b.Max)
                    {
                        max            = firstMatch.a.Max;
                        seperatorAfter = firstMatch.a.SeperatorAfter;
                    }
                    else if (firstMatch.b.Max > firstMatch.a.Max)
                    {
                        max            = firstMatch.b.Max;
                        seperatorAfter = firstMatch.b.SeperatorAfter;
                    }
                    else
                    {
                        max = firstMatch.a.Max;
                        if (string.IsNullOrEmpty(firstMatch.b.SeperatorAfter))
                        {
                            seperatorAfter = firstMatch.a.SeperatorAfter;
                        }
                        else
                        {
                            seperatorAfter = firstMatch.b.SeperatorAfter;
                        }
                    }

                    ranges.Add(new NumberRange(min, max, seperatorBefore, seperatorAfter));
                }

                StringBuilder output = new StringBuilder();

                var finalRanges = from a in ranges
                                  orderby a.Min
                                  select a;

                bool seperated = true;

                foreach (var range in finalRanges)
                {
                    if (!string.IsNullOrEmpty(range.SeperatorBefore))
                    {
                        if (!this.collapseSeperators && !seperated)
                        {
                            output.Append(this.seperatorFormat);
                        }

                        output.Append(range.SeperatorBefore);

                        if (!this.collapseSeperators)
                        {
                            output.Append(this.seperatorFormat);
                            seperated = true;
                        }
                        else
                        {
                            seperated = true;
                        }
                    }
                    else
                    {
                        if (!seperated)
                        {
                            output.Append(this.seperatorFormat);
                            seperated = true;
                        }
                    }

                    for (int i = range.Min; i <= range.Max; i++)
                    {
                        if (!seperated)
                        {
                            output.Append(this.seperatorFormat);
                            seperated = true;
                        }

                        string item = itemGenerator(i);
                        string url  = urlGenerator(i);

                        if (currentPage.HasValue && i == currentPage.Value)
                        {
                            output.Append(string.Format(this.currentFormat, item, url));
                            seperated = false;
                        }
                        else
                        {
                            output.Append(string.Format(this.otherFormat, item, url));
                            seperated = false;
                        }
                    }

                    if (!string.IsNullOrEmpty(range.SeperatorAfter))
                    {
                        if (!this.collapseSeperators && !seperated)
                        {
                            output.Append(this.seperatorFormat);
                            seperated = true;
                        }

                        output.Append(range.SeperatorAfter);

                        if (!this.collapseSeperators)
                        {
                            output.Append(this.seperatorFormat);
                            seperated = true;
                        }
                        else
                        {
                            seperated = true;
                        }
                    }
                }

                return(output.ToString());
            }
Exemplo n.º 4
0
            public override string Build(int?currentPage, int pageCount, int itemCount, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator)
            {
                string pageNumber = "?";

                if (currentPage.HasValue)
                {
                    pageNumber = currentPage.Value.ToString();
                }

                return(string.Format(this.format, pageNumber, pageCount));
            }
Exemplo n.º 5
0
 public override string Build(int?currentPage, int pageCount, int itemCount, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator)
 {
     return(this.text);
 }
Exemplo n.º 6
0
            public override string Build(int?currentPage, int pageCount, int itemCount, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator)
            {
                if (currentPage.HasValue)
                {
                    if (currentPage.Value != pageCount || !hideOnLast)
                    {
                        string link = urlGenerator(pageCount);

                        return(string.Format(this.format, link));
                    }
                }

                return("");
            }
Exemplo n.º 7
0
 public abstract string Build(int?currentPage, int pageCount, int itemCount, GenerateItemDelegate itemGenerator, GeneratePageLinkDelegate urlGenerator);