public SingleControlTests()
        {
            context = new TestDotvvmRequestContext()
            {
                Configuration = configuration
            };
            writer = new HtmlWriter(new StringWriter(output), context);
            Internal.MarkupFileNameProperty.SetValue(rootView, "some_fake_path");
            Internal.RequestContextProperty.SetValue(rootView, "some_fake_path");

            var bcs         = context.Services.GetService <BindingCompilationService>();
            var dataContext = DataContextStack.Create(typeof(TestViewModel));

            Internal.DataContextTypeProperty.SetValue(rootView, dataContext);
            DotvvmBindableObject.DataContextProperty.SetValue(rootView, viewModel);

            testValueBinding = ValueBindingExpression.CreateBinding(bcs, h => ((TestViewModel)h[0]).Property, dataContext);
            boolValueBinding = ValueBindingExpression.CreateBinding(bcs, h => ((TestViewModel)h[0]).Property == 0, dataContext);

            basicHtmlElement = new HtmlGenericControl("div");
            richHtmlElement  = new HtmlGenericControl("div");
            HtmlGenericControl.IncludeInPageProperty.SetValue(richHtmlElement, boolValueBinding);
            HtmlGenericControl.CssClassesGroupDescriptor.GetDotvvmProperty("my-class").SetValue(richHtmlElement, boolValueBinding);
            HtmlGenericControl.CssStylesGroupDescriptor.GetDotvvmProperty("width").SetValue(richHtmlElement, testValueBinding);
            richHtmlElement.Attributes.Add("data-my-attr", "HELLO");
            richHtmlElement.Attributes.Add("title", new(testValueBinding));

            Internal.UniqueIDProperty.SetValue(basicHtmlElement, "c1");
            Internal.UniqueIDProperty.SetValue(richHtmlElement, "c1");
        }
Esempio n. 2
0
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var configuration = DotvvmTestHelper.CreateConfiguration();

            configuration.RegisterApiClient(typeof(TestApiClient), "http://server/api", "./apiscript.js", "_api");
            configuration.Markup.ImportedNamespaces.Add(new NamespaceImport("DotVVM.Framework.Tests.Binding"));

            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] {
                new BindingPageInfoExtensionParameter(),
            }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = parser.ParseWithLambdaConversion(expression, context, BindingParserOptions.Create <ValueBindingExpression>(), expectedType);
            var jsExpression   =
                configuration.ServiceProvider.GetRequiredService <StaticCommandBindingCompiler>().CompileToJavascript(context, expressionTree);

            return(KnockoutHelper.GenerateClientPostBackScript(
                       "",
                       new FakeCommandBinding(BindingPropertyResolvers.FormatJavascript(jsExpression, nullChecks: false), null),
                       new Literal(),
                       new PostbackScriptOptions(
                           allowPostbackHandlers: false,
                           returnValue: null,
                           commandArgs: CodeParameterAssignment.FromIdentifier("commandArguments")
                           )));
        }
Esempio n. 3
0
 public CollectionElementDataContextBindingProperty GetCollectionElementDataContext(DataContextStack dataContext, ResultTypeBindingProperty resultType)
 {
     return(new CollectionElementDataContextBindingProperty(DataContextStack.Create(
                                                                ReflectionUtils.GetEnumerableType(resultType.Type),
                                                                parent: dataContext,
                                                                extensionParameters: new CollectionElementDataContextChangeAttribute(0).GetExtensionParameters(new ResolvedTypeDescriptor(dataContext.DataContextType)).ToArray()
                                                                )));
 }
 private DataContextStack CreateDataContextStack(DataContextStack dataContextStack, Type[] nestedDataContextTypes)
 {
     foreach (var type in nestedDataContextTypes)
     {
         dataContextStack = DataContextStack.Create(type, dataContextStack, dataContextStack.NamespaceImports, dataContextStack.ExtensionParameters);
     }
     return(dataContextStack);
 }
Esempio n. 5
0
 public DataContextStack GetDataContext()
 {
     DataContextStack c = null;
     foreach (var vm in VmTypes)
     {
         c = DataContextStack.Create(vm ?? typeof(object), c);
     }
     return c;
 }
Esempio n. 6
0
 public static DataContextStack CreateChildStack(this DataContextStack dataContextStack, Type viewModelType)
 {
     return(DataContextStack.Create(
                viewModelType,
                dataContextStack,
                dataContextStack.NamespaceImports,
                dataContextStack.ExtensionParameters,
                dataContextStack.BindingPropertyResolvers));
 }
Esempio n. 7
0
        public static DataContextStack CreateChildStack(this DotvvmBindableObject bindableObject, Type viewModelType)
        {
            var dataContextTypeStack = bindableObject.GetDataContextType();

            return(DataContextStack.Create(
                       viewModelType,
                       dataContextTypeStack,
                       dataContextTypeStack.NamespaceImports,
                       dataContextTypeStack.ExtensionParameters,
                       dataContextTypeStack.BindingPropertyResolvers));
        }
Esempio n. 8
0
        public static string BuildPath <TValidationTarget>(Expression <Func <TValidationTarget, object> > propertyAccessor, DotvvmConfiguration configuration)
        {
            var context = DataContextStack.Create(typeof(TValidationTarget));
            var js      = configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(propertyAccessor, context);

            js.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
            var propertyPathExtractingVisitor = new PropertyPathExtractingVisitor();

            js.AcceptVisitor(propertyPathExtractingVisitor);
            return(propertyPathExtractingVisitor.ExtractedPropertyPath);
        }
Esempio n. 9
0
        protected virtual HtmlGenericControl CreatePageNumberButton(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            HtmlGenericControl li;
            var currentPageTextContext = DataContextStack.Create(typeof(int), NumberButtonsPlaceHolder.GetDataContextType());
            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);
                AddKnockoutActiveCssDataBind(writer, context, "$data == $parent.PagingOptions().PageIndex()");
                li = new HtmlGenericControl("li");
                var literal = new Literal();
                literal.DataContext = 0;
                literal.SetDataContextType(currentPageTextContext);

                literal.SetBinding(Literal.TextProperty, currentPageTextBinding);
                li.Children.Add(literal);
                NumberButtonsPlaceHolder.Children.Add(li);
                li.Render(writer, context);

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

            AddItemCssClass(writer, context);
            AddKnockoutActiveCssDataBind(writer, context, "$data == $parent.PagingOptions().PageIndex()");
            li = new HtmlGenericControl("li");
            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 = HasValueBinding(EnabledProperty)
                ? (object)ValueBindingExpression.CreateBinding(bindingService.WithoutInitialization(),
                                                               h => GetValueBinding(EnabledProperty).Evaluate(this),
                                                               new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$pagerEnabled"))
                : Enabled;

            if (!true.Equals(enabledValue))
            {
                link.SetValue(LinkButton.EnabledProperty, enabledValue);
            }
            NumberButtonsPlaceHolder.Children.Add(li);
            return(li);
        }
Esempio n. 10
0
 public override DataContextStack ChangeStackForChildren(DataContextStack original, DotvvmBindableObject obj, DotvvmProperty property, Func <DataContextStack, Type, DataContextStack> createNewFrame)
 {
     return(DataContextStack.Create(original.DataContextType, original.Parent,
                                    bindingPropertyResolvers: new Delegate[] {
         new Func <ParsedExpressionBindingProperty, ParsedExpressionBindingProperty>(e => {
             if (e.Expression.NodeType == ExpressionType.Constant && (string)((ConstantExpression)e.Expression).Value == "abc")
             {
                 return new ParsedExpressionBindingProperty(Expression.Constant("def"));
             }
             else
             {
                 return e;
             }
         })
     }));
 }
Esempio n. 11
0
 public string CompileBinding(Func<Dictionary<string, Expression>, Expression> expr, Type[] contexts)
 {
     var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[]{
         new BindingPageInfoExtensionParameter()
         });
     for (int i = 1; i < contexts.Length; i++)
     {
         context = DataContextStack.Create(contexts[i], context);
     }
     var expressionTree = expr(BindingExpressionBuilder.GetParameters(context).ToDictionary(e => e.Name, e => (Expression)e));
     var configuration = DotvvmTestHelper.CreateConfiguration();
     var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService<JavascriptTranslator>().CompileToJavascript(expressionTree, context));
     jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
     JsTemporaryVariableResolver.ResolveVariables(jsExpression);
     return JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression);
 }
Esempio n. 12
0
 public override IDataContextStack ChangeStackForChildren(IDataContextStack original, IAbstractControl control, IPropertyDescriptor property, Func <IDataContextStack, ITypeDescriptor, IDataContextStack> createNewFrame)
 {
     return(DataContextStack.Create(ResolvedTypeDescriptor.ToSystemType(original.DataContextType), (DataContextStack)original.Parent,
                                    bindingPropertyResolvers: new Delegate[] {
         new Func <ParsedExpressionBindingProperty, ParsedExpressionBindingProperty>(e => {
             if (e.Expression.NodeType == ExpressionType.Constant && (string)((ConstantExpression)e.Expression).Value == "abc")
             {
                 return new ParsedExpressionBindingProperty(Expression.Constant("def"));
             }
             else
             {
                 return e;
             }
         })
     }));
 }
Esempio n. 13
0
        public void ThisBinding()
        {
            var dataContext = DataContextStack.Create(typeof(string));

            var binding1 = ValueBindingExpression.CreateThisBinding <string>(service, dataContext);
            var binding2 = ValueBindingExpression.CreateThisBinding <string>(service, dataContext);
            var binding3 = ValueBindingExpression.CreateThisBinding <object>(service, dataContext);
            var binding4 = ValueBindingExpression.CreateThisBinding <string>(service, DataContextStack.Create(typeof(string), dataContext));
            var binding5 = ValueBindingExpression.CreateThisBinding <string>(service, DataContextStack.Create(typeof(string), dataContext));

            Assert.AreEqual(binding1, binding2);
            Assert.AreNotEqual(binding1, binding3);
            Assert.AreNotEqual(binding1, binding4);
            Assert.AreNotEqual(binding2, binding3);
            Assert.AreNotEqual(binding2, binding4);
            Assert.AreNotEqual(binding3, binding4);
            Assert.AreEqual(binding4, binding5);
        }
Esempio n. 14
0
        public ValueBindingExpression CompileValueBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[]{
                new BindingPageInfoExtensionParameter(),
                }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());
            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }

            var valueBinding = new ValueBindingExpression(bindingService, new object[] {
                context,
                new OriginalStringBindingProperty(expression),
                new BindingParserOptions(typeof(ValueBindingExpression)).AddImports(configuration.Markup.ImportedNamespaces),
                new ExpectedTypeBindingProperty(expectedType ?? typeof(object))
            });
            return valueBinding;
        }
Esempio n. 15
0
        public static void OnCompilation(ResolvedControl control, BindingCompilationService bindingService)
        {
            // ComboBoxed does not have to have the DataSource property and then they don't use the CurrentIndexBindingProperty
            if (!control.HasProperty(DataSourceProperty))
            {
                return;
            }

            var dcChange = ControlTreeResolverBase.ApplyContextChange(control.DataContextTypeStack,
                                                                      new DataContextChangeAttribute[] { new ControlPropertyBindingDataContextChangeAttribute(nameof(DataSource)), new CollectionElementDataContextChangeAttribute(0) },
                                                                      control, null);
            var dataContext = DataContextStack.Create(ResolvedTypeDescriptor.ToSystemType(dcChange.type), control.DataContextTypeStack, extenstionParameters: dcChange.extensionParameters);

            control.SetProperty(new ResolvedPropertyBinding(Internal.CurrentIndexBindingProperty,
                                                            new ResolvedBinding(bindingService, new Compilation.BindingParserOptions(typeof(ValueBindingExpression)), dataContext,
                                                                                parsedExpression: Expression.Parameter(typeof(int), "_index").AddParameterAnnotation(
                                                                                    new BindingParameterAnnotation(dataContext, new CurrentCollectionIndexExtensionParameter())))));
        }
Esempio n. 16
0
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] {
                new BindingPageInfoExtensionParameter(),
            }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true);
            var jsExpression   = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expressionTree, context));

            jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
            JsTemporaryVariableResolver.ResolveVariables(jsExpression);
            return(JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression));
        }
Esempio n. 17
0
        public object ExecuteBinding(string expression, object[] contexts, DotvvmControl control, NamespaceImport[] imports = null, Type expectedType = null)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault()?.GetType() ?? typeof(object), extensionParameters: new[] {
                new CurrentMarkupControlExtensionParameter(new ResolvedTypeDescriptor(control?.GetType() ?? typeof(DotvvmControl)))
            });

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i].GetType(), context);
            }
            Array.Reverse(contexts);
            var binding = new ResourceBindingExpression(bindingService, new object[] {
                context,
                new OriginalStringBindingProperty(expression),
                new BindingParserOptions(typeof(ResourceBindingExpression), importNamespaces: imports?.ToImmutableList()),
                new ExpectedTypeBindingProperty(expectedType ?? typeof(object))
            });

            return(binding.BindingDelegate.Invoke(contexts, control));
        }
Esempio n. 18
0
        private void BuildLevel(IDotvvmRequestContext context, HtmlGenericControl container, IEnumerable <IMenuItem> menuItems)
        {
            var stack = container.GetDataContextType();

            foreach (var item in menuItems)
            {
                // create <li>
                var li = new HtmlGenericControl("li");
                li.SetDataContextType(DataContextStack.Create(item.GetType(), stack));
                li.DataContext = item;
                container.Children.Add(li);

                // create <a>
                var link = new HtmlGenericControl("a");
                link.Attributes["href"] = item.NavigateUrl;
                if (item.IsActive)
                {
                    link.Attributes["class"] = "nav-active";
                }
                li.Children.Add(link);

                // create the content
                if (ItemTemplate != null)
                {
                    ItemTemplate.BuildContent(context, link);
                }
                else
                {
                    link.InnerText = item.Text;
                }

                // create child item list
                if (item.ChildItems?.Any() == true)
                {
                    var ul = new HtmlGenericControl("ul");
                    li.Children.Add(ul);
                    BuildLevel(context, ul, item.ChildItems);
                }
            }
        }
Esempio n. 19
0
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            writer.AddKnockoutDataBind("css", "{ 'disabled': PagingOptions().IsFirstPage() }");
            firstLi.Render(writer, context);

            writer.AddKnockoutDataBind("css", "{ 'disabled': PagingOptions().IsFirstPage() }");
            previousLi.Render(writer, context);

            // render template
            writer.WriteKnockoutForeachComment("PagingOptions().NearPageIndexes");

            // render page number
            numbersPlaceHolder.Children.Clear();
            HtmlGenericControl li;
            var currentPageTextContext = DataContextStack.Create(typeof(int), numbersPlaceHolder.GetDataContextType());
            var currentPageTextBinding = ValueBindingExpression.CreateBinding(bindingService.WithoutInitialization(),
                                                                              vm => ((int)vm[0] + 1).ToString(),
                                                                              currentPageTextJs,
                                                                              currentPageTextContext);

            if (!RenderLinkForCurrentPage)
            {
                writer.AddKnockoutDataBind("visible", "$data == $parent.PagingOptions().PageIndex()");
                writer.AddKnockoutDataBind("css", "{'active': $data == $parent.PagingOptions().PageIndex()}");
                li = new HtmlGenericControl("li");
                var literal = new Literal();
                literal.DataContext = 0;
                literal.SetDataContextType(currentPageTextContext);

                literal.SetBinding(Literal.TextProperty, currentPageTextBinding);
                li.Children.Add(literal);
                numbersPlaceHolder.Children.Add(li);
                li.Render(writer, context);

                writer.AddKnockoutDataBind("visible", "$data != $parent.PagingOptions().PageIndex()");
            }
            writer.AddKnockoutDataBind("css", "{ 'active': $data == $parent.PagingOptions().PageIndex()}");
            li = new HtmlGenericControl("li");
            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 = HasValueBinding(EnabledProperty) ?
                                  (object)ValueBindingExpression.CreateBinding(bindingService.WithoutInitialization(),
                                                                               h => GetValueBinding(EnabledProperty).Evaluate(this),
                                                                               new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$pagerEnabled")) :
                                  Enabled;

            if (!true.Equals(enabledValue))
            {
                link.SetValue(LinkButton.EnabledProperty, enabledValue);
            }
            numbersPlaceHolder.Children.Add(li);
            li.Render(writer, context);

            writer.WriteKnockoutDataBindEndComment();

            writer.AddKnockoutDataBind("css", "{ 'disabled': PagingOptions().IsLastPage() }");
            nextLi.Render(writer, context);

            writer.AddKnockoutDataBind("css", "{ 'disabled': PagingOptions().IsLastPage() }");
            lastLi.Render(writer, context);
        }
Esempio n. 20
0
        private void DataBind(Hosting.IDotvvmRequestContext context)
        {
            if (DataSet is IRefreshableGridViewDataSet refreshableDataSet)
            {
                CallGridViewDataSetRefreshRequest(refreshableDataSet);
            }

            Children.Clear();

            content = new HtmlGenericControl("ul");
            var dataContextType = DataContextStack.Create(typeof(IPageableGridViewDataSet), this.GetDataContextType());

            content.SetDataContextType(dataContextType);
            content.SetBinding(DataContextProperty, GetDataSetBinding());
            Children.Add(content);

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

            var dataSet = DataSet;

            if (dataSet != null)
            {
                object enabledValue = HasValueBinding(EnabledProperty) ?
                                      (object)ValueBindingExpression.CreateBinding <bool>(
                    bindingService.WithoutInitialization(),
                    h => (bool)GetValueBinding(EnabledProperty).Evaluate(this),
                    new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$pagerEnabled")) :
                                      Enabled;

                // first button
                firstLi = new HtmlGenericControl("li");
                var firstLink = new LinkButton();
                SetButtonContent(context, firstLink, "««", FirstPageTemplate);
                firstLink.SetBinding(ButtonBase.ClickProperty, bindings.GoToFirstPageCommand);
                if (!true.Equals(enabledValue))
                {
                    firstLink.SetValue(LinkButton.EnabledProperty, enabledValue);
                }
                firstLi.Children.Add(firstLink);
                content.Children.Add(firstLi);

                // previous button
                previousLi = new HtmlGenericControl("li");
                var previousLink = new LinkButton();
                SetButtonContent(context, previousLink, "«", PreviousPageTemplate);
                previousLink.SetBinding(ButtonBase.ClickProperty, bindings.GoToPrevPageCommand);
                if (!true.Equals(enabledValue))
                {
                    previousLink.SetValue(LinkButton.EnabledProperty, enabledValue);
                }
                previousLi.Children.Add(previousLink);
                content.Children.Add(previousLi);

                // number fields
                numbersPlaceHolder = new PlaceHolder();
                content.Children.Add(numbersPlaceHolder);

                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["class"] = "active";
                    }
                    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);
                    numbersPlaceHolder.Children.Add(li);

                    i++;
                }

                // next button
                nextLi = new HtmlGenericControl("li");
                var nextLink = new LinkButton();
                SetButtonContent(context, nextLink, "»", NextPageTemplate);
                nextLink.SetBinding(ButtonBase.ClickProperty, bindings.GoToNextPageCommand);
                if (!true.Equals(enabledValue))
                {
                    nextLink.SetValue(LinkButton.EnabledProperty, enabledValue);
                }
                nextLi.Children.Add(nextLink);
                content.Children.Add(nextLi);

                // last button
                lastLi = new HtmlGenericControl("li");
                var lastLink = new LinkButton();
                SetButtonContent(context, lastLink, "»»", LastPageTemplate);
                if (!true.Equals(enabledValue))
                {
                    lastLink.SetValue(LinkButton.EnabledProperty, enabledValue);
                }
                lastLink.SetBinding(ButtonBase.ClickProperty, bindings.GoToLastPageCommand);
                lastLi.Children.Add(lastLink);
                content.Children.Add(lastLi);
            }
        }
        public void JsTranslator_StaticFieldInCSharpExpression()
        {
            Expression <Func <string, string> > expr = _ => string.Empty;
            var tree = configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expr, DataContextStack.Create(typeof(object)));

            Assert.AreEqual("function(_){return \"\";}", tree.ToString());
        }
        public void JsTranslator_SimpleCSharpExpression()
        {
            Expression <Func <string, string> > expr = abc => abc + "def";
            var tree = configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expr, DataContextStack.Create(typeof(object)));

            Assert.AreEqual("function(abc){return abc+\"def\";}", tree.ToString());
        }
Esempio n. 23
0
        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 = (HasValueBinding(EnabledProperty) ?
                                   (object)ValueBindingExpression.CreateBinding <bool>(
                                       bindingService.WithoutInitialization(),
                                       h => (bool)GetValueBinding(EnabledProperty).Evaluate(this),
                                       new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$pagerEnabled")) :
                                   Enabled);


            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["class"] = "active";
                    }
                    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);
        }