-
Notifications
You must be signed in to change notification settings - Fork 97
/
DataPager.cs
398 lines (338 loc) · 18.7 KB
/
DataPager.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
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using DotVVM.Framework.Binding;
using DotVVM.Framework.Binding.Expressions;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Compilation.Javascript.Ast;
using DotVVM.Framework.Compilation.Javascript;
using DotVVM.Framework.Compilation.ControlTree;
using DotVVM.Framework.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using DotVVM.Framework.Binding.Properties;
using DotVVM.Framework.Utils;
namespace DotVVM.Framework.Controls
{
/// <summary>
/// Renders the pagination control which can be integrated with the GridViewDataSet object to provide the paging capabilities.
/// </summary>
[ControlMarkupOptions(AllowContent = false)]
public class DataPager : HtmlGenericControl
{
public class CommonBindings
{
public readonly CommandBindingExpression GoToNextPageCommand;
public readonly CommandBindingExpression GoToThisPageCommand;
public readonly CommandBindingExpression GoToPrevPageCommand;
public readonly CommandBindingExpression GoToFirstPageCommand;
public readonly CommandBindingExpression GoToLastPageCommand;
public CommonBindings(BindingCompilationService service)
{
GoToNextPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet)h[0]).GoToNextPage(), "__$DataPager_GoToNextPage");
GoToThisPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet)h[1]).GoToPage((int)h[0]), "__$DataPager_GoToThisPage");
GoToPrevPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet)h[0]).GoToPreviousPage(), "__$DataPager_GoToPrevPage");
GoToFirstPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet)h[0]).GoToFirstPage(), "__$DataPager_GoToFirstPage");
GoToLastPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet)h[0]).GoToLastPage(), "__$DataPager_GoToLastPage");
}
}
private readonly CommonBindings commonBindings;
private readonly BindingCompilationService bindingService;
public DataPager(CommonBindings commonBindings, BindingCompilationService bindingService)
: base("div", false)
{
this.commonBindings = commonBindings;
this.bindingService = bindingService;
}
/// <summary>
/// Gets or sets the GridViewDataSet object in the viewmodel.
/// </summary>
[MarkupOptions(AllowHardCodedValue = false)]
public IPageableGridViewDataSet? DataSet
{
get { return (IPageableGridViewDataSet?)GetValue(DataSetProperty); }
set { SetValue(DataSetProperty, value); }
}
public static readonly DotvvmProperty DataSetProperty =
DotvvmProperty.Register<IPageableGridViewDataSet?, DataPager>(c => c.DataSet);
/// <summary>
/// Gets or sets the template of the button which moves the user to the first page.
/// </summary>
[MarkupOptions(AllowBinding = false, MappingMode = MappingMode.InnerElement)]
public ITemplate? FirstPageTemplate
{
get { return (ITemplate?)GetValue(FirstPageTemplateProperty); }
set { SetValue(FirstPageTemplateProperty, value); }
}
public static readonly DotvvmProperty FirstPageTemplateProperty =
DotvvmProperty.Register<ITemplate?, DataPager>(c => c.FirstPageTemplate, null);
/// <summary>
/// Gets or sets the template of the button which moves the user to the last page.
/// </summary>
[MarkupOptions(AllowBinding = false, MappingMode = MappingMode.InnerElement)]
public ITemplate? LastPageTemplate
{
get { return (ITemplate?)GetValue(LastPageTemplateProperty); }
set { SetValue(LastPageTemplateProperty, value); }
}
public static readonly DotvvmProperty LastPageTemplateProperty =
DotvvmProperty.Register<ITemplate?, DataPager>(c => c.LastPageTemplate, null);
/// <summary>
/// Gets or sets the template of the button which moves the user to the previous page.
/// </summary>
[MarkupOptions(AllowBinding = false, MappingMode = MappingMode.InnerElement)]
public ITemplate? PreviousPageTemplate
{
get { return (ITemplate?)GetValue(PreviousPageTemplateProperty); }
set { SetValue(PreviousPageTemplateProperty, value); }
}
public static readonly DotvvmProperty PreviousPageTemplateProperty =
DotvvmProperty.Register<ITemplate?, DataPager>(c => c.PreviousPageTemplate, null);
/// <summary>
/// Gets or sets the template of the button which moves the user to the next page.
/// </summary>
[MarkupOptions(AllowBinding = false, MappingMode = MappingMode.InnerElement)]
public ITemplate? NextPageTemplate
{
get { return (ITemplate?)GetValue(NextPageTemplateProperty); }
set { SetValue(NextPageTemplateProperty, value); }
}
public static readonly DotvvmProperty NextPageTemplateProperty =
DotvvmProperty.Register<ITemplate?, DataPager>(c => c.NextPageTemplate, null);
/// <summary>
/// Gets or sets whether a hyperlink should be rendered for the current page number. If set to false, only a plain text is rendered.
/// </summary>
[MarkupOptions(AllowBinding = false)]
public bool RenderLinkForCurrentPage
{
get { return (bool)GetValue(RenderLinkForCurrentPageProperty)!; }
set { SetValue(RenderLinkForCurrentPageProperty, value); }
}
public static readonly DotvvmProperty RenderLinkForCurrentPageProperty =
DotvvmProperty.Register<bool, DataPager>(c => c.RenderLinkForCurrentPage);
/// <summary>
/// Gets or sets whether the pager should hide automatically when there is only one page of results. Must not be set to true when using the Visible property.
/// </summary>
[MarkupOptions(AllowBinding = false)]
public bool HideWhenOnlyOnePage
{
get { return (bool)GetValue(HideWhenOnlyOnePageProperty)!; }
set { SetValue(HideWhenOnlyOnePageProperty, value); }
}
public static readonly DotvvmProperty HideWhenOnlyOnePageProperty
= DotvvmProperty.Register<bool, DataPager>(c => c.HideWhenOnlyOnePage, true);
public bool Enabled
{
get { return (bool)GetValue(EnabledProperty)!; }
set { SetValue(EnabledProperty, value); }
}
public static readonly DotvvmProperty EnabledProperty =
DotvvmPropertyWithFallback.Register<bool, DataPager>(nameof(Enabled), FormControls.EnabledProperty);
protected HtmlGenericControl? ContentWrapper { get; set; }
protected HtmlGenericControl? GoToFirstPageButton { get; set; }
protected HtmlGenericControl? GoToPreviousPageButton { get; set; }
protected PlaceHolder? NumberButtonsPlaceHolder { get; set; }
protected HtmlGenericControl? GoToNextPageButton { get; set; }
protected HtmlGenericControl? GoToLastPageButton { get; set; }
protected virtual string ActiveItemCssClass => "active";
protected virtual string DisabledItemCssClass => "disabled";
protected internal override void OnLoad(IDotvvmRequestContext context)
{
DataBind(context);
base.OnLoad(context);
}
protected internal override void OnPreRender(IDotvvmRequestContext context)
{
DataBind(context);
base.OnPreRender(context);
}
protected virtual void DataBind(Hosting.IDotvvmRequestContext context)
{
Children.Clear();
var dataContextType = DataContextStack.Create(typeof(IPageableGridViewDataSet), this.GetDataContextType());
ContentWrapper = CreateWrapperList(dataContextType);
Children.Add(ContentWrapper);
var bindings = context.Services.GetRequiredService<CommonBindings>();
object enabledValue = GetValueRaw(EnabledProperty)!;
GoToFirstPageButton = CreateNavigationButton("««", FirstPageTemplate, enabledValue, bindings.GoToFirstPageCommand,context);
ContentWrapper.Children.Add(GoToFirstPageButton);
GoToPreviousPageButton = CreateNavigationButton("«", PreviousPageTemplate, enabledValue, bindings.GoToPrevPageCommand,context);
ContentWrapper.Children.Add(GoToPreviousPageButton);
// number fields
NumberButtonsPlaceHolder = new PlaceHolder();
ContentWrapper.Children.Add(NumberButtonsPlaceHolder);
var dataSet = DataSet;
if (dataSet != null)
{
var i = 0;
foreach (var number in dataSet.PagingOptions.NearPageIndexes)
{
var li = new HtmlGenericControl("li");
li.SetBinding(DataContextProperty, GetNearIndexesBinding(context, i, dataContextType));
if (number == dataSet.PagingOptions.PageIndex)
{
li.Attributes.Set("class", ActiveItemCssClass);
}
var link = new LinkButton() { Text = (number + 1).ToString() };
link.SetBinding(ButtonBase.ClickProperty, bindings.GoToThisPageCommand);
if (!true.Equals(enabledValue)) link.SetValue(LinkButton.EnabledProperty, enabledValue);
li.Children.Add(link);
NumberButtonsPlaceHolder.Children.Add(li);
i++;
}
}
GoToNextPageButton = CreateNavigationButton("»", NextPageTemplate, enabledValue, bindings.GoToNextPageCommand, context);
ContentWrapper.Children.Add(GoToNextPageButton);
GoToLastPageButton = CreateNavigationButton("»»", LastPageTemplate, enabledValue, bindings.GoToLastPageCommand, context);
ContentWrapper.Children.Add(GoToLastPageButton);
}
protected virtual HtmlGenericControl CreateWrapperList(DataContextStack dataContext)
{
var list = new HtmlGenericControl("ul");
list.SetDataContextType(dataContext);
list.SetBinding(DataContextProperty, GetDataSetBinding());
return list;
}
protected virtual HtmlGenericControl CreateNavigationButton(string defaultText, ITemplate? userDefinedContentTemplate, object enabledValue, ICommandBinding clickCommandBindingExpression,IDotvvmRequestContext context)
{
var li = new HtmlGenericControl("li");
var link = new LinkButton();
SetButtonContent(context, link, defaultText, userDefinedContentTemplate);
link.SetBinding(ButtonBase.ClickProperty, clickCommandBindingExpression);
if (!true.Equals(enabledValue))
link.SetValue(LinkButton.EnabledProperty, enabledValue);
li.Children.Add(link);
return li;
}
protected virtual void SetButtonContent(Hosting.IDotvvmRequestContext context, LinkButton button, string text, ITemplate? contentTemplate)
{
if (contentTemplate != null)
{
contentTemplate.BuildContent(context, button);
}
else
{
button.Text = text;
}
}
private ConditionalWeakTable<DotvvmConfiguration, ConcurrentDictionary<int, ValueBindingExpression>> _nearIndexesBindingCache
= new ConditionalWeakTable<DotvvmConfiguration, ConcurrentDictionary<int, ValueBindingExpression>>();
private ValueBindingExpression GetNearIndexesBinding(IDotvvmRequestContext context, int i, DataContextStack? dataContext = null)
{
return
_nearIndexesBindingCache.GetOrCreateValue(context.Configuration)
.GetOrAdd(i, _ =>
ValueBindingExpression.CreateBinding(
bindingService.WithoutInitialization(),
h => ((IPageableGridViewDataSet)h[0]!).PagingOptions.NearPageIndexes[i],
dataContext));
}
protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
{
if (RenderOnServer)
{
throw new DotvvmControlException(this, "The DataPager control cannot be rendered in the RenderSettings.Mode='Server'.");
}
base.AddAttributesToRender(writer, context);
// If Visible property was set to something, it will be overwritten by this. TODO: is it how it should behave?
if (HideWhenOnlyOnePage)
{
if (IsPropertySet(VisibleProperty))
throw new Exception("Visible can't be set on a DataPager when HideWhenOnlyOnePage is true. You can wrap it in an element that hide that or set HideWhenOnlyOnePage to false");
writer.AddKnockoutDataBind("visible", $"({GetDataSetBinding().GetKnockoutBindingExpression(this, unwrapped: true)}).PagingOptions().PagesCount() > 1");
}
}
protected override void RenderBeginTag(IHtmlWriter writer, IDotvvmRequestContext context)
{
if (GetValueBinding(EnabledProperty) is IValueBinding enabledBinding)
{
var disabledBinding = enabledBinding.GetProperty<NegatedBindingExpression>().Binding.CastTo<IValueBinding>();
AddKnockoutDisabledCssDataBind(writer, context, disabledBinding.GetKnockoutBindingExpression(this));
}
else if (!Enabled)
{
writer.AddAttribute("class", DisabledItemCssClass, true);
}
writer.AddKnockoutDataBind("with", this, DataSetProperty, renderEvenInServerRenderingMode: true);
writer.RenderBeginTag("ul");
}
protected virtual void AddItemCssClass(IHtmlWriter writer, IDotvvmRequestContext context)
{
}
protected virtual void AddKnockoutDisabledCssDataBind(IHtmlWriter writer, IDotvvmRequestContext context, string expression)
{
writer.AddKnockoutDataBind("css", $"{{ '{DisabledItemCssClass}': {expression} }}");
}
protected virtual void AddKnockoutActiveCssDataBind(IHtmlWriter writer, IDotvvmRequestContext context, string expression)
{
writer.AddKnockoutDataBind("css", $"{{ '{ActiveItemCssClass}': {expression} }}");
}
private static ParametrizedCode currentPageTextJs = new JsBinaryExpression(new JsBinaryExpression(new JsLiteral(1), BinaryOperatorType.Plus, new JsSymbolicParameter(JavascriptTranslator.KnockoutViewModelParameter)), BinaryOperatorType.Plus, new JsLiteral("")).FormatParametrizedScript();
protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
{
AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsFirstPage()");
GoToFirstPageButton!.Render(writer, context);
AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsFirstPage()");
GoToPreviousPageButton!.Render(writer, context);
// render template
writer.WriteKnockoutForeachComment("PagingOptions().NearPageIndexes");
// render page number
NumberButtonsPlaceHolder!.Children.Clear();
RenderPageNumberButton(writer, context);
writer.WriteKnockoutDataBindEndComment();
AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsLastPage()");
GoToNextPageButton!.Render(writer, context);
AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsLastPage()");
GoToLastPageButton!.Render(writer, context);
}
protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequestContext context)
{
HtmlGenericControl li = new HtmlGenericControl("li");
var currentPageTextContext = DataContextStack.Create(typeof(int), NumberButtonsPlaceHolder!.GetDataContextType());
li.SetDataContextType(currentPageTextContext);
li.DataContext = null;
var currentPageTextBinding = ValueBindingExpression.CreateBinding(bindingService.WithoutInitialization(),
vm => ((int) vm[0]! + 1).ToString(),
currentPageTextJs,
currentPageTextContext);
if (!RenderLinkForCurrentPage)
{
writer.AddKnockoutDataBind("visible", "$data == $parent.PagingOptions().PageIndex()");
AddItemCssClass(writer, context);
writer.AddAttribute("class", ActiveItemCssClass, true);
var literal = new Literal();
literal.DataContext = 0;
literal.SetBinding(Literal.TextProperty, currentPageTextBinding);
li.Children.Add(literal);
NumberButtonsPlaceHolder!.Children.Add(li);
li.Render(writer, context);
writer.AddKnockoutDataBind("visible", "$data != $parent.PagingOptions().PageIndex()");
}
li = new HtmlGenericControl("li");
li.SetDataContextType(currentPageTextContext);
li.DataContext = null;
NumberButtonsPlaceHolder!.Children.Add(li);
AddItemCssClass(writer, context);
if (RenderLinkForCurrentPage)
AddKnockoutActiveCssDataBind(writer, context, "$data == $parent.PagingOptions().PageIndex()");
li.SetValue(Internal.PathFragmentProperty, "PagingOptions.NearPageIndexes[$index]");
var link = new LinkButton();
li.Children.Add(link);
link.SetDataContextType(currentPageTextContext);
link.SetBinding(ButtonBase.TextProperty, currentPageTextBinding);
link.SetBinding(ButtonBase.ClickProperty, commonBindings.GoToThisPageCommand);
object enabledValue = GetValueRaw(EnabledProperty)!;
if (!true.Equals(enabledValue)) link.SetValue(LinkButton.EnabledProperty, enabledValue);
li.Render(writer, context);
}
protected override void RenderEndTag(IHtmlWriter writer, IDotvvmRequestContext context)
{
writer.RenderEndTag();
}
private IValueBinding GetDataSetBinding()
=> GetValueBinding(DataSetProperty) ?? throw new DotvvmControlException(this, "The DataSet property of the dot:DataPager control must be set!");
}
}