Skip to content

Commit

Permalink
Made DataPager work, at least with commands
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Oct 7, 2023
1 parent 3ab3461 commit ac7f0f7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,13 @@ JsExpression wrapInRound(JsExpression a) =>
private void AddDataSetOptionsTranslations()
{
// GridViewDataSetBindingProvider
AddMethodTranslator(typeof(GridViewDataSetBindingProvider), "DataSetClientSideLoad", new GenericMethodCompiler(args =>
AddMethodTranslator(() => GridViewDataSetBindingProvider.DataSetClientSideLoad(null!), new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("loadDataSet").Invoke(args[1].WithAnnotation(ShouldBeObservableAnnotation.Instance), new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$gridViewDataSetHelper").Member("loadDataSet")).WithAnnotation(new ResultIsPromiseAnnotation(e => e))));

AddMethodTranslator(() => GridViewDataSetBindingProvider.GetCurrentGridDataSet<Generic.DataSet>(), new GenericMethodCompiler(args =>
new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$gridViewDataSetHelper").Member("dataSet")
));

// PagingOptions
AddMethodTranslator(() => default(PagingOptions)!.GoToFirstPage(),new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToFirstPage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using DotVVM.Framework.Controls;

namespace DotVVM.Framework.Compilation.Javascript
{
Expand Down Expand Up @@ -154,6 +155,7 @@ public static class Generic {
public record T { }
public enum Enum { Something }
public record struct Struct { }
public class DataSet: GridViewDataSet<string> { }
}
}
}
24 changes: 16 additions & 8 deletions src/Framework/Framework/Controls/DataPager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)

if (DataSet is IPageableGridViewDataSet<IPagingPageIndexCapability> dataSet)
{
var currentPageTextContext = DataContextStack.Create(typeof(int), dataContextType);
var i = 0;
foreach (var number in dataSet.PagingOptions.NearPageIndexes)
{
var li = new HtmlGenericControl("li");
li.SetDataContextType(currentPageTextContext);
li.SetBinding(DataContextProperty, GetNearIndexesBinding(context, i, dataContextType));
if (number == dataSet.PagingOptions.PageIndex)
{
Expand Down Expand Up @@ -283,7 +285,7 @@ protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequest
var loadDataExpression = KnockoutHelper.GenerateClientPostbackLambda("LoadData", loadData, this, new PostbackScriptOptions(elementAccessor: "$element", koContext: CodeParameterAssignment.FromIdentifier("$context")));
helperBinding.Add("loadData", loadDataExpression);
}
writer.AddKnockoutDataBind("dotvvm-gridviewdataset", helperBinding);
writer.AddKnockoutDataBind("dotvvm-gridviewdataset", helperBinding.ToString());

// If Visible property was set to something, it will be overwritten by this. TODO: is it how it should behave?
if (HideWhenOnlyOnePage)
Expand All @@ -293,7 +295,7 @@ protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequest
writer.AddKnockoutDataBind("visible", $"({dataSetBinding}).PagingOptions().PagesCount() > 1");
}

writer.AddKnockoutDataBind("with", this, DataSetProperty, renderEvenInServerRenderingMode: true);
// writer.AddKnockoutDataBind("with", this, DataSetProperty, renderEvenInServerRenderingMode: true);


if (GetValueBinding(EnabledProperty) is IValueBinding enabledBinding)
Expand Down Expand Up @@ -345,11 +347,11 @@ protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext
writer.WriteKnockoutDataBindEndComment();

AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsLastPage()");
AddKnockoutDisabledCssDataBind(writer, context, "$gridViewDataSetHelper.dataSet.PagingOptions().IsLastPage()");
GoToNextPageButton!.Render(writer, context);

AddItemCssClass(writer, context);
AddKnockoutDisabledCssDataBind(writer, context, "PagingOptions().IsLastPage()");
AddKnockoutDisabledCssDataBind(writer, context, "$gridViewDataSetHelper.dataSet.PagingOptions().IsLastPage()");
GoToLastPageButton!.Render(writer, context);
}

Expand All @@ -366,7 +368,7 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest

if (!RenderLinkForCurrentPage)
{
writer.AddKnockoutDataBind("visible", "$data == $parent.PagingOptions().PageIndex()");
writer.AddKnockoutDataBind("visible", "$data == $parentContext.$gridViewDataSetHelper.dataSet.PagingOptions().PageIndex()");
AddItemCssClass(writer, context);
writer.AddAttribute("class", ActiveItemCssClass, true);
var literal = new Literal();
Expand All @@ -377,7 +379,7 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest

li.Render(writer, context);

writer.AddKnockoutDataBind("visible", "$data != $parent.PagingOptions().PageIndex()");
writer.AddKnockoutDataBind("visible", "$data != $parentContext.$gridViewDataSetHelper.dataSet.PagingOptions().PageIndex()");
}

li = new HtmlGenericControl("li");
Expand All @@ -388,9 +390,9 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest
AddItemCssClass(writer, context);

if (RenderLinkForCurrentPage)
AddKnockoutActiveCssDataBind(writer, context, "$data == $parent.PagingOptions().PageIndex()");
AddKnockoutActiveCssDataBind(writer, context, "$data == $parentContext.$gridViewDataSetHelper.dataSet.PagingOptions().PageIndex()");

li.SetValue(Internal.PathFragmentProperty, "PagingOptions.NearPageIndexes[$index]");
li.SetValue(Internal.PathFragmentProperty, "$parent.PagingOptions.NearPageIndexes[$index]");
var link = new LinkButton();
li.Children.Add(link);
link.SetDataContextType(currentPageTextContext);
Expand All @@ -402,6 +404,12 @@ protected virtual void RenderPageNumberButton(IHtmlWriter writer, IDotvvmRequest
li.Render(writer, context);
}

// protected override void RenderEndTag(IHtmlWriter writer, IDotvvmRequestContext context)
// {
// base.RenderEndTag(writer, context);
// writer.WriteKnockoutDataBindEndComment();
// }

private IValueBinding GetDataSetBinding()
=> GetValueBinding(DataSetProperty) ?? throw new DotvvmControlException(this, "The DataSet property of the dot:DataPager control must be set!");
}
Expand Down
13 changes: 13 additions & 0 deletions src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private ICommandBinding CreateCommandBinding<TDataSetInterface>(GridViewDataSetC
new object[]
{
new ParsedExpressionBindingProperty(expression),
new OriginalStringBindingProperty($"DataPager: _dataSet.{methodName}({string.Join(", ", arguments.AsEnumerable())})"), // For ID generation
dataContextStack
});
}
Expand Down Expand Up @@ -175,6 +176,12 @@ private static Expression CallClientSideLoad(Expression dataSetParam)
return Expression.Call(method, dataSetParam);
}

private static Expression CurrentGridDataSetExpression(Type datasetType)
{
var method = typeof(GridViewDataSetBindingProvider).GetMethod(nameof(GetCurrentGridDataSet))!.MakeGenericMethod(datasetType);
return Expression.Call(method);
}

/// <summary>
/// A sentinel method which is translated to load the GridViewDataSet on the client side using the Load delegate.
/// Do not call this method on the server.
Expand All @@ -184,6 +191,12 @@ public static Task DataSetClientSideLoad(IBaseGridViewDataSet dataSet)
throw new InvalidOperationException("This method cannot be called on the server!");
}

/// <summary> Returns the DataSet we currently work on from the $context.$gridViewDataSetHelper.dataSet </summary>
public static T GetCurrentGridDataSet<T>() where T : IBaseGridViewDataSet
{
throw new InvalidOperationException("This method cannot be called on the server!");
}

private static Type GetOptionsConcreteType<TDataSetInterface>(Type dataSetConcreteType, out PropertyInfo optionsProperty)
{
if (!typeof(TDataSetInterface).IsGenericType || !typeof(TDataSetInterface).IsAssignableFrom(dataSetConcreteType))
Expand Down

0 comments on commit ac7f0f7

Please sign in to comment.