-
Notifications
You must be signed in to change notification settings - Fork 240
/
PagedListRenderOptions.cs
436 lines (393 loc) · 16.8 KB
/
PagedListRenderOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace PagedList.Mvc
{
///<summary>
/// Options for configuring the output of <see cref = "HtmlHelper" />.
///</summary>
public class PagedListRenderOptions
{
///<summary>
/// The default settings render all navigation links and no descriptive text.
///</summary>
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<string>();
}
///<summary>
/// CSS Classes to append to the <div> element that wraps the paging control.
///</summary>
public IEnumerable<string> ContainerDivClasses { get; set; }
///<summary>
/// CSSClasses to append to the <ul> element in the paging control.
///</summary>
public IEnumerable<string> UlElementClasses { get; set; }
///<summary>
/// CSS Classes to append to every <li> element in the paging control.
///</summary>
public IEnumerable<string> LiElementClasses { get; set; }
///<summary>
/// 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.
///</summary>
public string ClassToApplyToFirstListItemInPager { get; set; }
///<summary>
/// 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.
///</summary>
public string ClassToApplyToLastListItemInPager { get; set; }
/// <summary>
/// If set to Always, always renders the paging control. If set to IfNeeded, render the paging control when there is more than one page.
/// </summary>
public PagedListDisplayMode Display { get; set; }
///<summary>
/// 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.
///</summary>
public PagedListDisplayMode DisplayLinkToFirstPage { get; set; }
///<summary>
/// 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.
///</summary>
public PagedListDisplayMode DisplayLinkToLastPage { get; set; }
///<summary>
/// 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.
///</summary>
public PagedListDisplayMode DisplayLinkToPreviousPage { get; set; }
///<summary>
/// 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.
///</summary>
public PagedListDisplayMode DisplayLinkToNextPage { get; set; }
///<summary>
/// When true, includes hyperlinks for each page in the list.
///</summary>
public bool DisplayLinkToIndividualPages { get; set; }
///<summary>
/// When true, shows the current page number and the total number of pages in the list.
///</summary>
///<example>
/// "Page 3 of 8."
///</example>
public bool DisplayPageCountAndCurrentLocation { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "Showing items 75 through 100 of 183."
///</example>
public bool DisplayItemSliceAndTotal { get; set; }
///<summary>
/// The maximum number of page numbers to display. Null displays all page numbers.
///</summary>
public int? MaximumPageNumbersToDisplay { get; set; }
///<summary>
/// If true, adds an ellipsis where not all page numbers are being displayed.
///</summary>
///<example>
/// "1 2 3 4 5 ...",
/// "... 6 7 8 9 10 ...",
/// "... 11 12 13 14 15"
///</example>
public bool DisplayEllipsesWhenNotShowingAllPageNumbers { get; set; }
///<summary>
/// The pre-formatted text to display when not all page numbers are displayed at once.
///</summary>
///<example>
/// "..."
///</example>
public string EllipsesFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "<< First"
///</example>
public string LinkToFirstPageFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "< Previous"
///</example>
public string LinkToPreviousPageFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "{0}"
///</example>
public string LinkToIndividualPageFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "Next >"
///</example>
public string LinkToNextPageFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "Last >>"
///</example>
public string LinkToLastPageFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "Page {0} of {1}."
///</example>
public string PageCountAndCurrentLocationFormat { get; set; }
///<summary>
/// 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.
///</summary>
///<example>
/// "Showing items {0} through {1} of {2}."
///</example>
public string ItemSliceAndTotalFormat { get; set; }
/// <summary>
/// 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.
/// </summary>
public Func<int, string> FunctionToDisplayEachPageNumber { get; set; }
/// <summary>
/// Text that will appear between each page number. If null or whitespace is specified, no delimiter will be shown.
/// </summary>
public string DelimiterBetweenPageNumbers { get; set; }
/// <summary>
/// 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.
/// </summary>
public Func<TagBuilder, TagBuilder, TagBuilder> FunctionToTransformEachPageLink { get; set; }
/// <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 PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links.</returns>
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;
}
/// <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="id">The element ID ("my_id") of the element whose innerHtml should be replaced, if # is included at the start this will be removed.</param>
/// <returns>A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links.</returns>
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);
}
/// <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="ajaxOptions">Ajax options that will be used to generate the unobstrusive tags on the link</param>
/// <returns>A default instance of PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links.</returns>
public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(AjaxOptions ajaxOptions)
{
return EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions(), ajaxOptions);
}
///<summary>
/// Also includes links to First and Last pages.
///</summary>
public static PagedListRenderOptions Classic
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always
};
}
}
///<summary>
/// Also includes links to First and Last pages.
///</summary>
public static PagedListRenderOptions ClassicPlusFirstAndLast
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Always,
DisplayLinkToLastPage = PagedListDisplayMode.Always,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always
};
}
}
///<summary>
/// Shows only the Previous and Next links.
///</summary>
public static PagedListRenderOptions Minimal
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always,
DisplayLinkToIndividualPages = false
};
}
}
///<summary>
/// Shows Previous and Next links along with current page number and page count.
///</summary>
public static PagedListRenderOptions MinimalWithPageCountText
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always,
DisplayLinkToIndividualPages = false,
DisplayPageCountAndCurrentLocation = true
};
}
}
///<summary>
/// Shows Previous and Next links along with index of first and last items on page and total number of items across all pages.
///</summary>
public static PagedListRenderOptions MinimalWithItemCountText
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always,
DisplayLinkToIndividualPages = false,
DisplayItemSliceAndTotal = true
};
}
}
///<summary>
/// Shows only links to each individual page.
///</summary>
public static PagedListRenderOptions PageNumbersOnly
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Never,
DisplayLinkToNextPage = PagedListDisplayMode.Never,
DisplayEllipsesWhenNotShowingAllPageNumbers = false
};
}
}
///<summary>
/// Shows Next and Previous while limiting to a max of 5 page numbers at a time.
///</summary>
public static PagedListRenderOptions OnlyShowFivePagesAtATime
{
get
{
return new PagedListRenderOptions
{
DisplayLinkToFirstPage = PagedListDisplayMode.Never,
DisplayLinkToLastPage = PagedListDisplayMode.Never,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always,
MaximumPageNumbersToDisplay = 5
};
}
}
///<summary>
/// Twitter Bootstrap 2's basic pager format (just Previous and Next links).
///</summary>
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"
};
}
}
///<summary>
/// Twitter Bootstrap 2's basic pager format (just Previous and Next links), with aligned links.
///</summary>
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 →"
};
}
}
}
}