Skip to content

Commit

Permalink
DataPager commands refactored and added support for static commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg committed Jan 20, 2023
1 parent badd553 commit 18b3058
Show file tree
Hide file tree
Showing 13 changed files with 552 additions and 55 deletions.
17 changes: 0 additions & 17 deletions src/Framework/Framework/Binding/CommonBindings.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public void AddMethodTranslator<T>(Expression<Func<T>> methodCall, IJavascriptMe
AddMethodTranslator(method, translator);
}

public void AddMethodTranslator(Expression<Action> methodCall, IJavascriptMethodTranslator translator)
{
var method = (MethodInfo)MethodFindingHelper.GetMethodFromExpression(methodCall);
AddMethodTranslator(method, translator);
}

public void AddMethodTranslator(Type declaringType, string methodName, IJavascriptMethodTranslator translator, int parameterCount, bool allowMultipleMethods = false, Func<ParameterInfo[], bool>? parameterFilter = null)
{
var methods = declaringType.GetMethods()
Expand Down Expand Up @@ -204,8 +210,9 @@ JsExpression dictionarySetIndexer(JsExpression[] args, MethodInfo method) =>
AddDefaultMathTranslations();
AddDefaultDateTimeTranslations();
AddDefaultConvertTranslations();
AddDataSetOptionsTranslations();
}

private void AddDefaultToStringTranslations()
{
AddMethodTranslator(typeof(object), "ToString", new PrimitiveToStringTranslator(), 0);
Expand Down Expand Up @@ -707,6 +714,58 @@ JsExpression wrapInRound(JsExpression a) =>
}
}
}

private void AddDataSetOptionsTranslations()
{
// GridViewDataSetBindingProvider
AddMethodTranslator(typeof(GridViewDataSetBindingProvider), "DataSetClientSideLoad", new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("loadDataSet").Invoke(args[1], args[2])));

// PagingOptions
AddMethodTranslator(() => default(PagingOptions)!.GoToFirstPage(),new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToFirstPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(PagingOptions)!.GoToLastPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToLastPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(PagingOptions)!.GoToPreviousPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToPreviousPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(PagingOptions)!.GoToNextPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToNextPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(PagingOptions)!.GoToPage(default(int)), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])));

// NextTokenPagingOptions
AddMethodTranslator(() => default(NextTokenPagingOptions)!.GoToFirstPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenPagingOptions").Member("goToFirstPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(NextTokenPagingOptions)!.GoToNextPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenPagingOptions").Member("goToNextPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));

// NextTokenHistoryPagingOptions
AddMethodTranslator(() => default(NextTokenHistoryPagingOptions)!.GoToFirstPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenHistoryPagingOptions").Member("goToFirstPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(NextTokenHistoryPagingOptions)!.GoToPreviousPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenHistoryPagingOptions").Member("goToPreviousPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(NextTokenHistoryPagingOptions)!.GoToNextPage(), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenHistoryPagingOptions").Member("goToNextPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))));
AddMethodTranslator(() => default(NextTokenHistoryPagingOptions)!.GoToPage(default(int)), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("NextTokenHistoryPagingOptions").Member("goToPage")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])));

// SortingOptions
AddMethodTranslator(() => default(SortingOptions)!.SetSortExpression(default(string?)), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("SortingOptions").Member("setSortExpression")
.Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])));
}

public JsExpression? TryTranslateCall(LazyTranslatedExpression? context, LazyTranslatedExpression[] args, MethodInfo method)
{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static MethodBase GetMethodFromExpression<T>(Expression<Func<T>> expressi
{
return GetMethodFromExpression((Expression)expression);
}

public static MethodBase GetMethodFromExpression(Expression<Action> expression)
{
return GetMethodFromExpression((Expression)expression);
}

static MethodBase GetMethodFromExpression(Expression expression)
{
var originalExpression = expression;
Expand Down
49 changes: 16 additions & 33 deletions src/Framework/Framework/Controls/DataPager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,17 @@ namespace DotVVM.Framework.Controls
[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;
private readonly GridViewDataSetBindingProvider gridViewDataSetBindingProvider;
private readonly BindingCompilationService bindingCompilationService;

public CommonBindings(BindingCompilationService service)
{
GoToNextPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet<IPagingNextPageCapability>)h[0]).GoToNextPageAndRefresh(), "__$DataPager_GoToNextPage");
GoToThisPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet<IPagingPageIndexCapability>)h[1]).GoToPageAndRefresh((int)h[0]), "__$DataPager_GoToThisPage");
GoToPrevPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet<IPagingPreviousPageCapability>)h[0]).GoToPreviousPageAndRefresh(), "__$DataPager_GoToPrevPage");
GoToFirstPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet<IPagingFirstPageCapability>)h[0]).GoToFirstPageAndRefresh(), "__$DataPager_GoToFirstPage");
GoToLastPageCommand = new CommandBindingExpression(service, h => ((IPageableGridViewDataSet<IPagingLastPageCapability>)h[0]).GoToLastPageAndRefresh(), "__$DataPager_GoToLastPage");
}
}
private DataPagerCommands? pagerCommands;

private readonly CommonBindings commonBindings;
private readonly BindingCompilationService bindingService;

public DataPager(CommonBindings commonBindings, BindingCompilationService bindingService)
public DataPager(GridViewDataSetBindingProvider gridViewDataSetBindingProvider, BindingCompilationService bindingCompilationService)
: base("div", false)
{
this.commonBindings = commonBindings;
this.bindingService = bindingService;
this.gridViewDataSetBindingProvider = gridViewDataSetBindingProvider;
this.bindingCompilationService = bindingCompilationService;
}

/// <summary>
Expand Down Expand Up @@ -171,19 +156,18 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)
ContentWrapper = CreateWrapperList(dataContextType);
Children.Add(ContentWrapper);

var bindings = context.Services.GetRequiredService<CommonBindings>();

pagerCommands = gridViewDataSetBindingProvider.GetDataPagerCommands(dataContextType, GridViewDataSetCommandType.Command);
object enabledValue = GetValueRaw(EnabledProperty)!;

if (typeof(IPageableGridViewDataSet<IPagingFirstPageCapability>).IsAssignableFrom(dataContextType.DataContextType))
{
GoToFirstPageButton = CreateNavigationButton("««", FirstPageTemplate, enabledValue, bindings.GoToFirstPageCommand, context);
GoToFirstPageButton = CreateNavigationButton("««", FirstPageTemplate, enabledValue, pagerCommands.GoToFirstPage!, context);
ContentWrapper.Children.Add(GoToFirstPageButton);
}

if (typeof(IPageableGridViewDataSet<IPagingPreviousPageCapability>).IsAssignableFrom(dataContextType.DataContextType))
{
GoToPreviousPageButton = CreateNavigationButton("«", PreviousPageTemplate, enabledValue, bindings.GoToPrevPageCommand, context);
GoToPreviousPageButton = CreateNavigationButton("«", PreviousPageTemplate, enabledValue, pagerCommands.GoToPreviousPage!, context);
ContentWrapper.Children.Add(GoToPreviousPageButton);
}

Expand All @@ -205,7 +189,7 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)
li.Attributes.Set("class", ActiveItemCssClass);
}
var link = new LinkButton() { Text = (number + 1).ToString() };
link.SetBinding(ButtonBase.ClickProperty, bindings.GoToThisPageCommand);
link.SetBinding(ButtonBase.ClickProperty, pagerCommands.GoToPage!);
if (!true.Equals(enabledValue)) link.SetValue(LinkButton.EnabledProperty, enabledValue);
li.Children.Add(link);
NumberButtonsPlaceHolder.Children.Add(li);
Expand All @@ -217,13 +201,13 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)

if (typeof(IPageableGridViewDataSet<IPagingNextPageCapability>).IsAssignableFrom(dataContextType.DataContextType))
{
GoToNextPageButton = CreateNavigationButton("»", NextPageTemplate, enabledValue, bindings.GoToNextPageCommand, context);
GoToNextPageButton = CreateNavigationButton("»", NextPageTemplate, enabledValue, pagerCommands.GoToNextPage!, context);
ContentWrapper.Children.Add(GoToNextPageButton);
}

if (typeof(IPageableGridViewDataSet<IPagingLastPageCapability>).IsAssignableFrom(dataContextType.DataContextType))
{
GoToLastPageButton = CreateNavigationButton("»»", LastPageTemplate, enabledValue, bindings.GoToLastPageCommand, context);
GoToLastPageButton = CreateNavigationButton("»»", LastPageTemplate, enabledValue, pagerCommands.GoToLastPage!, context);
ContentWrapper.Children.Add(GoToLastPageButton);
}
}
Expand Down Expand Up @@ -269,7 +253,7 @@ private ValueBindingExpression GetNearIndexesBinding(IDotvvmRequestContext conte
_nearIndexesBindingCache.GetOrCreateValue(context.Configuration)
.GetOrAdd(i, _ =>
ValueBindingExpression.CreateBinding(
bindingService.WithoutInitialization(),
bindingCompilationService.WithoutInitialization(),
h => ((IPageableGridViewDataSet<IPagingPageIndexCapability>)h[0]!).PagingOptions.NearPageIndexes[i],
dataContext));
}
Expand Down Expand Up @@ -358,7 +342,7 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest
var currentPageTextContext = DataContextStack.Create(typeof(int), NumberButtonsPlaceHolder!.GetDataContextType());
li.SetDataContextType(currentPageTextContext);
li.DataContext = null;
var currentPageTextBinding = ValueBindingExpression.CreateBinding(bindingService.WithoutInitialization(),
var currentPageTextBinding = ValueBindingExpression.CreateBinding(bindingCompilationService.WithoutInitialization(),
vm => ((int) vm[0]! + 1).ToString(),
currentPageTextJs,
currentPageTextContext);
Expand Down Expand Up @@ -394,7 +378,7 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest
li.Children.Add(link);
link.SetDataContextType(currentPageTextContext);
link.SetBinding(ButtonBase.TextProperty, currentPageTextBinding);
link.SetBinding(ButtonBase.ClickProperty, commonBindings.GoToThisPageCommand);
link.SetBinding(ButtonBase.ClickProperty, pagerCommands.GoToPage!);
object enabledValue = GetValueRaw(EnabledProperty)!;
if (!true.Equals(enabledValue)) link.SetValue(LinkButton.EnabledProperty, enabledValue);

Expand All @@ -409,5 +393,4 @@ protected override void RenderEndTag(IHtmlWriter writer, IDotvvmRequestContext c
private IValueBinding GetDataSetBinding()
=> GetValueBinding(DataSetProperty) ?? throw new DotvvmControlException(this, "The DataSet property of the dot:DataPager control must be set!");
}

}
13 changes: 13 additions & 0 deletions src/Framework/Framework/Controls/DataPagerCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DotVVM.Framework.Binding.Expressions;

namespace DotVVM.Framework.Controls
{
public class DataPagerCommands
{
public ICommandBinding? GoToFirstPage { get; init; }
public ICommandBinding? GoToPreviousPage { get; init; }
public ICommandBinding? GoToNextPage { get; init; }
public ICommandBinding? GoToLastPage { get; init; }
public ICommandBinding? GoToPage { get; init; }
}
}
9 changes: 9 additions & 0 deletions src/Framework/Framework/Controls/GridViewCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using DotVVM.Framework.Binding.Expressions;

namespace DotVVM.Framework.Controls
{
public class GridViewCommands
{
public ICommandBinding? SetSortExpression { get; init; }
}
}
Loading

0 comments on commit 18b3058

Please sign in to comment.