Skip to content

Commit

Permalink
Add _dataPager.Load extension parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Nov 11, 2023
1 parent 52ead0a commit e26b66b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/Framework/Framework/Binding/HelperNamespace/DataPagerApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using DotVVM.Core.Storage;
using DotVVM.Framework.Compilation.ControlTree;
using DotVVM.Framework.Compilation.ControlTree.Resolved;
using DotVVM.Framework.Compilation.Javascript.Ast;
using DotVVM.Framework.Controls;

namespace DotVVM.Framework.Binding.HelperNamespace
{
public class DataPagerApi
{
public void Load() => throw new NotSupportedException("The _dataPager.Load method is not supported on the server, please use a staticCommand to invoke it.");




public class DataPagerExtensionParameter : BindingExtensionParameter
{
public DataPagerExtensionParameter(string identifier, bool inherit = true) : base(identifier, ResolvedTypeDescriptor.Create(typeof(DataPagerApi)), inherit)
{
}

public override JsExpression GetJsTranslation(JsExpression dataContext) =>
new JsObjectExpression();
public override Expression GetServerEquivalent(Expression controlParameter) =>
Expression.New(typeof(DataPagerApi));
}

public class AddParameterDataContextChangeAttribute: DataContextChangeAttribute
{
public AddParameterDataContextChangeAttribute(string name = "_dataPager", int order = 0)
{
Name = name;
Order = order;
}

public string Name { get; }
public override int Order { get; }

public override ITypeDescriptor? GetChildDataContextType(ITypeDescriptor dataContext, IDataContextStack controlContextStack, IAbstractControl control, IPropertyDescriptor? property = null) =>
dataContext;
public override Type? GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, DotvvmBindableObject control, DotvvmProperty? property = null) => dataContext;

public override IEnumerable<BindingExtensionParameter> GetExtensionParameters(ITypeDescriptor dataContext)
{
return new BindingExtensionParameter[] {
new DataPagerExtensionParameter(Name)
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static JsBlockStatement AsBlock(this JsStatement statement) =>
public static JsBlockStatement AsBlock(this IEnumerable<JsStatement> statements) =>
new JsBlockStatement(statements);

public static JsExpression AsSequenceOperators(this IEnumerable<JsExpression?> expressions) =>
expressions.OfType<JsExpression>().Aggregate((a, b) => new JsBinaryExpression(a, BinaryOperatorType.Sequence, b));
public static JsExpression AsSequenceOperators(params JsExpression?[] expressions) =>
expressions.AsSequenceOperators();

public static JsArrayExpression ArrayExpression(this IEnumerable<JsExpression> items) =>
new JsArrayExpression(items);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,14 +811,19 @@ JsExpression wrapInRound(JsExpression a) =>

private void AddDataSetOptionsTranslations()
{
var dataSetHelper = new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$gridViewDataSetHelper");
// GridViewDataSetBindingProvider
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))));
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("loadDataSet").Invoke(args[1].WithAnnotation(ShouldBeObservableAnnotation.Instance), dataSetHelper.Clone().Member("loadDataSet")).WithAnnotation(new ResultIsPromiseAnnotation(e => e))));

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

// _dataPager.Load()
AddMethodTranslator(() => default(DataPagerApi)!.Load(), new GenericMethodCompiler(args =>
dataSetHelper.Member("loadNextPage").Invoke().WithAnnotation(new ResultIsPromiseAnnotation(e => e))));

// PagingOptions
AddMethodTranslator(() => default(PagingOptions)!.GoToFirstPage(),new GenericMethodCompiler(args =>
new JsIdentifierExpression("dotvvm").Member("dataSet").Member("translations").Member("PagingOptions").Member("goToFirstPage")
Expand Down

0 comments on commit e26b66b

Please sign in to comment.