コード例 #1
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));
        }
コード例 #2
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));
        }
コード例 #3
0
ファイル: KnockoutHelper.cs プロジェクト: wushian/dotvvm
        private static JsExpression TransformOptionValueToExpression(DotvvmBindableObject handler, object optionValue)
        {
            switch (optionValue)
            {
            case IValueBinding binding:
                return(new JsIdentifierExpression(
                           JavascriptTranslator.FormatKnockoutScript(binding.GetParametrizedKnockoutExpression(handler, unwrapped: true),
                                                                     new ParametrizedCode("c"), new ParametrizedCode("d"))));

            case IStaticValueBinding staticValueBinding:
                return(new JsLiteral(staticValueBinding.Evaluate(handler)));

            case JsExpression expression:
                return(expression.Clone());

            case IBinding _:
                throw new ArgumentException("Option value can contains only IValueBinding or IStaticValueBinding. Other bindings are not supported.");

            default:
                return(new JsLiteral(optionValue));
            }
        }
コード例 #4
0
ファイル: BindingHelper.cs プロジェクト: fgcp020/dotvvm
 public static string FormatKnockoutScript(this ParametrizedCode code, DotvvmBindableObject currentControl, IBinding currentBinding) =>
 JavascriptTranslator.FormatKnockoutScript(code, dataContextLevel: FindDataContextTarget(currentBinding, currentControl).stepsUp);
コード例 #5
0
ファイル: BindingHelper.cs プロジェクト: fgcp020/dotvvm
 public static string GetKnockoutBindingExpression(this IValueBinding binding) =>
 JavascriptTranslator.FormatKnockoutScript(binding.KnockoutExpression);
コード例 #6
0
 public static string FormatKnockoutScript(ParametrizedCode code) => JavascriptTranslator.FormatKnockoutScript(code);
コード例 #7
0
        public new string CompileToJavascript(ResolvedBinding binding, CompiledBindingExpression compiledExpression, DotvvmConfiguration config)
        {
            var vmMapper   = config.ServiceLocator.GetService <IViewModelSerializationMapper>();
            var expression = binding.GetExpression();

            var visitor      = new ExtractExpressionVisitor(ex => ex.NodeType == ExpressionType.Call);
            var rootCallback = visitor.Visit(expression);
            var js           = SouldCompileCallback(rootCallback) ? "resultPromise.resolve(" + JavascriptTranslator.FormatKnockoutScript(JavascriptTranslator.CompileToJavascript(rootCallback, binding.DataContextTypeStack, vmMapper), allowDataGlobal: false) + ")" : null;

            foreach (var param in visitor.ParameterOrder.Reverse <ParameterExpression>())
            {
                if (js == null)
                {
                    js = $"resultPromise.resolve({param.Name})";
                }
                var callback = $"function({param.Name}){{{js}}}";
                var method   = visitor.Replaced[param] as MethodCallExpression;
                js = CompileMethodCall(method, binding.DataContextTypeStack, vmMapper, callback);
            }
            return("var $context = ko.contextFor(this);var sender = this;var resultPromise = new DotvvmPromise();(function(i_pageArea){with($context){" + js + "}})");
        }
コード例 #8
0
        private static string GenerateHandlerOptions(DotvvmBindableObject handler, Dictionary <string, object> options)
        {
            JsExpression optionsExpr = new JsObjectExpression(
                options.Where(o => o.Value != null).Select(o => new JsObjectProperty(o.Key,
                                                                                     o.Value is IValueBinding b ?
                                                                                     (JsExpression) new JsIdentifierExpression(
                                                                                         JavascriptTranslator.FormatKnockoutScript(b.GetParametrizedKnockoutExpression(handler, unwrapped: true), new ParametrizedCode("c"), new ParametrizedCode("d"))) :
                                                                                     new JsLiteral(o.Value is IBinding ? ((IStaticValueBinding)o.Value).Evaluate(handler) : o.Value)))
                );

            if (options.Any(o => o.Value is IValueBinding))
            {
                optionsExpr = new JsFunctionExpression(
                    new[] { new JsIdentifier("c"), new JsIdentifier("d") },
                    new JsBlockStatement(new JsReturnStatement(optionsExpr))
                    );
            }

            optionsExpr.FixParenthesis();
            var script = new JsFormattingVisitor().ApplyAction(optionsExpr.AcceptVisitor).GetParameterlessResult();

            return(script);
        }