Example #1
0
        public static string GenerateClientPostBackExpression(string propertyName, ICommandBinding expression, DotvvmBindableObject control, PostbackScriptOptions options)
        {
            var target          = (DotvvmControl?)control.GetClosestControlBindingTarget();
            var uniqueControlId = target?.GetDotvvmUniqueId();

            string getContextPath(DotvvmBindableObject?current)
            {
                var result = new List <string>();

                while (current != null)
                {
                    var pathFragment = current.GetDataContextPathFragment();
                    if (pathFragment != null)
                    {
                        result.Add(JsonConvert.ToString(pathFragment));
                    }
                    current = current.Parent;
                }
                result.Reverse();
                return("[" + string.Join(",", result) + "]");
            }

            string getHandlerScript()
            {
                if (!options.AllowPostbackHandlers)
                {
                    return("[]");
                }
                // turn validation off for static commands
                var validationPath = expression is StaticCommandBindingExpression ? null : GetValidationTargetExpression(control);

                return(GetPostBackHandlersScript(control, propertyName,
                                                 // validation handler
                                                 validationPath == null ? null :
                                                 validationPath == RootValidationTargetExpression ? "\"validate-root\"" :
                                                 validationPath == "$data" ? "\"validate-this\"" :
                                                 $"[\"validate\", {{path:{JsonConvert.ToString(validationPath)}}}]",

                                                 // use window.setTimeout
                                                 options.UseWindowSetTimeout ? "\"timeout\"" : null,

                                                 options.IsOnChange ? "\"suppressOnUpdating\"" : null,

                                                 GenerateConcurrencyModeHandler(propertyName, control)
                                                 ));
            }

            string?generatedPostbackHandlers = null;

            var adjustedExpression = expression.GetParametrizedCommandJavascript(control);
            // when the expression changes the dataContext, we need to override the default knockout context fo the command binding.
            var knockoutContext = options.KoContext ?? (
                // adjustedExpression != expression.CommandJavascript ?
                new CodeParameterAssignment(new ParametrizedCode.Builder {
                "ko.contextFor(", options.ElementAccessor.Code !, ")"
            }.Build(OperatorPrecedence.Max))
Example #2
0
        public static string GenerateClientPostBackExpression(string propertyName, ICommandBinding expression, DotvvmBindableObject control, PostbackScriptOptions options)
        {
            var target          = (DotvvmControl)control.GetClosestControlBindingTarget();
            var uniqueControlId = target?.GetDotvvmUniqueId();

            string getHandlerScript()
            {
                // turn validation off for static commands
                var validationPath = expression is StaticCommandBindingExpression ? null : GetValidationTargetExpression(control);

                return(GetPostBackHandlersScript(control, propertyName,
                                                 // validation handler
                                                 validationPath == null ? null :
                                                 validationPath == RootValidationTargetExpression ? "\"validate-root\"" :
                                                 validationPath == "$data" ? "\"validate-this\"" :
                                                 $"[\"validate\", {{path:{JsonConvert.ToString(validationPath)}}}]",

                                                 // use window.setTimeout
                                                 options.UseWindowSetTimeout ? "\"timeout\"" : null,

                                                 options.IsOnChange ? "\"suppressOnUpdating\"" : null,

                                                 GenerateConcurrencyModeHandler(control)
                                                 ));
            }

            string generatedPostbackHandlers = null;

            var call = expression.GetParametrizedCommandJavascript(control).ToString(p =>
                                                                                     p == CommandBindingExpression.ViewModelNameParameter ? new CodeParameterAssignment("\"root\"", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.SenderElementParameter ? options.ElementAccessor :
                                                                                     p == CommandBindingExpression.CurrentPathParameter ? new CodeParameterAssignment(
                                                                                         "[" + String.Join(", ", GetContextPath(control).Reverse().Select(JavascriptCompilationHelper.CompileConstant)) + "]",
                                                                                         OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.ControlUniqueIdParameter ? new CodeParameterAssignment(
                                                                                         (uniqueControlId is IValueBinding ? "{ expr: " + JsonConvert.ToString(((IValueBinding)uniqueControlId).GetKnockoutBindingExpression(control)) + "}" : '"' + (string)uniqueControlId + '"'), OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.OptionalKnockoutContextParameter ? options.KoContext ?? new CodeParameterAssignment("null", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.CommandArgumentsParameter ? options.CommandArgs ?? new CodeParameterAssignment("undefined", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.PostbackHandlersParameter ? new CodeParameterAssignment(generatedPostbackHandlers ?? (generatedPostbackHandlers = getHandlerScript()), OperatorPrecedence.Max) :
                                                                                     default(CodeParameterAssignment)
                                                                                     );

            if (generatedPostbackHandlers == null)
            {
                return($"dotvvm.applyPostbackHandlers(function(){{return {call}}}.bind(this),{options.ElementAccessor.Code.ToString(e => default(CodeParameterAssignment))},{getHandlerScript()})");
            }
            else
            {
                return(call);
            }
        }
Example #3
0
        public static string GenerateClientPostBackScript(string propertyName, ICommandBinding expression, DotvvmBindableObject control, PostbackScriptOptions options)
        {
            var target          = (DotvvmControl)control.GetClosestControlBindingTarget();
            var uniqueControlId = target?.GetDotvvmUniqueId();

            // return the script
            string returnStatement;

            if (options.ReturnValue == false)
            {
                returnStatement = ";event.stopPropagation();return false;";
            }
            else
            {
                returnStatement = "";
            }

            string generatedPostbackHanlders = null;

            var call = expression.GetParametrizedCommandJavascript(control).ToString(p =>
                                                                                     p == CommandBindingExpression.ViewModelNameParameter ? new CodeParameterAssignment("'root'", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.SenderElementParameter ? options.ElementAccessor :
                                                                                     p == CommandBindingExpression.CurrentPathParameter ? new CodeParameterAssignment(
                                                                                         "[" + String.Join(", ", GetContextPath(control).Reverse().Select(JavascriptCompilationHelper.CompileConstant)) + "]",
                                                                                         OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.ControlUniqueIdParameter ? new CodeParameterAssignment(
                                                                                         (uniqueControlId is IValueBinding ? "{ expr: " + JsonConvert.ToString(((IValueBinding)uniqueControlId).GetKnockoutBindingExpression(control), '\'', StringEscapeHandling.Default) + "}" : "'" + (string)uniqueControlId + "'"), OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.UseObjectSetTimeoutParameter ? new CodeParameterAssignment(options.UseWindowSetTimeout ? "true" : "false", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.ValidationPathParameter ? CodeParameterAssignment.FromExpression(new JsLiteral(GetValidationTargetExpression(control))) :
                                                                                     p == CommandBindingExpression.OptionalKnockoutContextParameter ? options.KoContext ?? new CodeParameterAssignment("null", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.CommandArgumentsParameter ? options.CommandArgs ?? new CodeParameterAssignment("undefined", OperatorPrecedence.Max) :
                                                                                     p == CommandBindingExpression.PostbackHandlersParameter ? new CodeParameterAssignment(generatedPostbackHanlders ?? (generatedPostbackHanlders = GetPostBackHandlersScript(control, propertyName)), OperatorPrecedence.Max) :
                                                                                     default(CodeParameterAssignment)
                                                                                     );

            if (generatedPostbackHanlders == null)
            {
                call = $"dotvvm.applyPostbackHandlers(function(){{return {call}}}.bind(this),{options.ElementAccessor.Code.ToString(e => default(CodeParameterAssignment))},{GetPostBackHandlersScript(control, propertyName)})";
            }
            if (options.IsOnChange)
            {
                call = "if(!dotvvm.isViewModelUpdating){" + call + "}";
            }
            return(call + returnStatement);
        }
Example #4
0
        public static string GenerateClientPostBackExpression(string propertyName, ICommandBinding expression, DotvvmBindableObject control, PostbackScriptOptions options)
        {
            var target          = (DotvvmControl)control.GetClosestControlBindingTarget();
            var uniqueControlId = target?.GetDotvvmUniqueId();

            string getContextPath(DotvvmBindableObject current)
            {
                var result = new List <string>();

                while (current != null)
                {
                    var pathFragment = current.GetDataContextPathFragment();
                    if (pathFragment != null)
                    {
                        result.Add(JsonConvert.ToString(pathFragment));
                    }
                    current = current.Parent;
                }
                result.Reverse();
                return("[" + string.Join(",", result) + "]");
            }

            string getHandlerScript()
            {
                if (!options.AllowPostbackHandlers)
                {
                    return("[]");
                }
                // turn validation off for static commands
                var validationPath = expression is StaticCommandBindingExpression ? null : GetValidationTargetExpression(control);

                return(GetPostBackHandlersScript(control, propertyName,
                                                 // validation handler
                                                 validationPath == null ? null :
                                                 validationPath == RootValidationTargetExpression ? "\"validate-root\"" :
                                                 validationPath == "$data" ? "\"validate-this\"" :
                                                 $"[\"validate\", {{path:{JsonConvert.ToString(validationPath)}}}]",

                                                 // use window.setTimeout
                                                 options.UseWindowSetTimeout ? "\"timeout\"" : null,

                                                 options.IsOnChange ? "\"suppressOnUpdating\"" : null,

                                                 GenerateConcurrencyModeHandler(control)
                                                 ));
            }

            string generatedPostbackHandlers = null;

            var adjustedExpression = expression.GetParametrizedCommandJavascript(control);
            // when the expression changes the dataContext, we need to override the default knockout context fo the command binding.
            var knockoutContext = options.KoContext ?? (
                adjustedExpression != expression.CommandJavascript ?
                new CodeParameterAssignment(new ParametrizedCode.Builder {
                "ko.contextFor(", options.ElementAccessor.Code, ")"
            }.Build(OperatorPrecedence.Max)) :
                default);

            var call = adjustedExpression.ToString(p =>
                                                   p == CommandBindingExpression.ViewModelNameParameter ? new CodeParameterAssignment("\"root\"", OperatorPrecedence.Max) :
                                                   p == CommandBindingExpression.SenderElementParameter ? options.ElementAccessor :
                                                   p == CommandBindingExpression.CurrentPathParameter ? new CodeParameterAssignment(
                                                       getContextPath(control),
                                                       OperatorPrecedence.Max) :
                                                   p == CommandBindingExpression.ControlUniqueIdParameter ? new CodeParameterAssignment(
                                                       (uniqueControlId is IValueBinding ? "{ expr: " + JsonConvert.ToString(((IValueBinding)uniqueControlId).GetKnockoutBindingExpression(control)) + "}" : '"' + (string)uniqueControlId + '"'), OperatorPrecedence.Max) :
                                                   p == JavascriptTranslator.KnockoutContextParameter ? knockoutContext :
                                                   p == CommandBindingExpression.CommandArgumentsParameter ? options.CommandArgs ?? default :
                                                   p == CommandBindingExpression.PostbackHandlersParameter ? new CodeParameterAssignment(generatedPostbackHandlers ?? (generatedPostbackHandlers = getHandlerScript()), OperatorPrecedence.Max) :
                                                   default(CodeParameterAssignment)
                                                   );

            if (generatedPostbackHandlers == null && options.AllowPostbackHandlers)
            {
                return($"dotvvm.applyPostbackHandlers(function(){{return {call}}}.bind(this),{options.ElementAccessor.Code.ToString(e => default(CodeParameterAssignment))},{getHandlerScript()})");
            }
            else
            {
                return(call);
            }
        }