From e26b66b665a12d1862eb17096a3b9c454eefb1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Sat, 11 Nov 2023 16:46:38 +0100 Subject: [PATCH] Add _dataPager.Load extension parameter --- .../Binding/HelperNamespace/DataPagerApi.cs | 55 +++++++++++++++++++ .../Javascript/Ast/JsAstHelpers.cs | 5 ++ .../JavascriptTranslatableMethodCollection.cs | 9 ++- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/Framework/Framework/Binding/HelperNamespace/DataPagerApi.cs diff --git a/src/Framework/Framework/Binding/HelperNamespace/DataPagerApi.cs b/src/Framework/Framework/Binding/HelperNamespace/DataPagerApi.cs new file mode 100644 index 0000000000..4c538ffba1 --- /dev/null +++ b/src/Framework/Framework/Binding/HelperNamespace/DataPagerApi.cs @@ -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 GetExtensionParameters(ITypeDescriptor dataContext) + { + return new BindingExtensionParameter[] { + new DataPagerExtensionParameter(Name) + }; + } + } + } +} diff --git a/src/Framework/Framework/Compilation/Javascript/Ast/JsAstHelpers.cs b/src/Framework/Framework/Compilation/Javascript/Ast/JsAstHelpers.cs index 53e710a8a8..b1f9593e3f 100644 --- a/src/Framework/Framework/Compilation/Javascript/Ast/JsAstHelpers.cs +++ b/src/Framework/Framework/Compilation/Javascript/Ast/JsAstHelpers.cs @@ -46,6 +46,11 @@ public static JsBlockStatement AsBlock(this JsStatement statement) => public static JsBlockStatement AsBlock(this IEnumerable statements) => new JsBlockStatement(statements); + public static JsExpression AsSequenceOperators(this IEnumerable expressions) => + expressions.OfType().Aggregate((a, b) => new JsBinaryExpression(a, BinaryOperatorType.Sequence, b)); + public static JsExpression AsSequenceOperators(params JsExpression?[] expressions) => + expressions.AsSequenceOperators(); + public static JsArrayExpression ArrayExpression(this IEnumerable items) => new JsArrayExpression(items); diff --git a/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs b/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs index f1996cbcc6..b5c5a3ed53 100644 --- a/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs +++ b/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs @@ -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(), 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")