private static void AddActionScript(StringBuilder script, string controllerName, ActionApiDescriptionModel action, string normalizedActionName)
        {
            var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams");

            script.AppendLine($"    {controllerName}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(normalizedActionName.RemovePostFix("Async").ToCamelCase())} = function({parameterList}) {{");

            var versionParam = action.Parameters.FirstOrDefault(p => p.Name == "apiVersion" && p.BindingSourceId == ParameterBindingSources.Path) ??
                               action.Parameters.FirstOrDefault(p => p.Name == "api-version" && p.BindingSourceId == ParameterBindingSources.Query);

            if (versionParam != null)
            {
                var version = FindBestApiVersion(action);
                script.AppendLine($"      var {ProxyScriptingJsFuncHelper.NormalizeJsVariableName(versionParam.Name)} = '{version}';");
            }

            script.AppendLine("      return Plus.ajax($.extend(true, {");

            AddAjaxCallParameters(script, action);

            var ajaxParamsIsFromForm = action.Parameters.Any(x => x.BindingSourceId == ParameterBindingSources.Form);

            script.AppendLine(ajaxParamsIsFromForm
                ? "      }, $.extend(true, {}, ajaxParams, { contentType: 'application/x-www-form-urlencoded; charset=UTF-8' })));"
                : "      }, ajaxParams));");

            script.AppendLine("    };");
        }
        public virtual string GenerateMethod()
        {
            var jsMethodName          = _actionInfo.ActionName.ToCamelCase();
            var jsMethodParameterList = ActionScriptingHelper.GenerateJsMethodParameterList(_actionInfo.Method, "ajaxParams");

            var jsMethod = JsMethodTemplate
                           .Replace("{jsMethodName}", ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(jsMethodName))
                           .Replace("{jsMethodParameterList}", jsMethodParameterList)
                           .Replace("{ajaxCallParameters}", GenerateAjaxCallParameters());

            return(jsMethod);
        }
        private static void AddActionScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action)
        {
            var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams");

            script.AppendLine($"    // action '{action.Name.ToCamelCase()}'");
            script.AppendLine($"    abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.Name.ToCamelCase())} = function({parameterList}) {{");
            script.AppendLine("      return abp.ajax($.extend(true, {");

            AddAjaxCallParameters(script, controller, action);

            script.AppendLine("      }, ajaxParams));;");
            script.AppendLine("    };");
        }
예제 #4
0
    public static string GenerateFormPostData(ActionApiDescriptionModel action, int indent = 0)
    {
        var parameters = action
                         .Parameters
                         .Where(p => p.BindingSourceId == ParameterBindingSources.Form)
                         .ToArray();

        if (!parameters.Any())
        {
            return(null);
        }

        return(ProxyScriptingJsFuncHelper.CreateJsFormPostData(parameters, indent));
    }
예제 #5
0
    private static string ReplacePathVariables(string url, IList <ParameterApiDescriptionModel> actionParameters)
    {
        var pathParameters = actionParameters
                             .Where(p => p.BindingSourceId == ParameterBindingSources.Path)
                             .ToArray();

        if (!pathParameters.Any())
        {
            return(url);
        }

        foreach (var pathParameter in pathParameters)
        {
            url = url.Replace($"{{{pathParameter.Name}}}", $"' + {ProxyScriptingJsFuncHelper.GetParamNameInJsFunc(pathParameter)} + '");
        }

        return(url);
    }
        public virtual void WriteTo(StringBuilder script)
        {
            script.AppendLine("                this" + ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(_actionInfo.ActionName.ToCamelCase()) + " = function (" + ActionScriptingHelper.GenerateJsMethodParameterList(_actionInfo.Method, "httpParams") + ") {");
            script.AppendLine("                    return $http(angular.extend({");
            script.AppendLine("                        url: CodeZero.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, _actionInfo) + "',");
            script.AppendLine("                        method: '" + _actionInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',");

            if (_actionInfo.Verb == HttpVerb.Get)
            {
                script.AppendLine("                        params: " + ActionScriptingHelper.GenerateBody(_actionInfo));
            }
            else
            {
                script.AppendLine("                        data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(_actionInfo) + ")");
            }

            script.AppendLine("                    }, httpParams));");
            script.AppendLine("                };");
            script.AppendLine("                ");
        }
예제 #7
0
    public static string GenerateBody(ActionApiDescriptionModel action)
    {
        var parameters = action
                         .Parameters
                         .Where(p => p.BindingSourceId == ParameterBindingSources.Body)
                         .ToArray();

        if (parameters.Length <= 0)
        {
            return(null);
        }

        if (parameters.Length > 1)
        {
            throw new AbpException(
                      $"Only one complex type allowed as argument to a controller action that's binding source is 'Body'. But {action.Name} ({action.Url}) contains more than one!"
                      );
        }

        return(ProxyScriptingJsFuncHelper.GetParamNameInJsFunc(parameters[0]));
    }
        public string Generate()
        {
            var script = new StringBuilder();

            script.AppendLine("(function(){");
            script.AppendLine();
            script.AppendLine("    var serviceNamespace = CodeZero.utils.createNamespace(CodeZero, 'services." + _controllerInfo.ServiceName.Replace("/", ".") + "');");
            script.AppendLine();

            //generate all methods
            foreach (var methodInfo in _controllerInfo.Actions.Values)
            {
                AppendMethod(script, _controllerInfo, methodInfo);
                script.AppendLine();
            }

            //generate amd module definition
            if (_defineAmdModule)
            {
                script.AppendLine("    if(typeof define === 'function' && define.amd){");
                script.AppendLine("        define(function (require, exports, module) {");
                script.AppendLine("            return {");

                var methodNo = 0;
                foreach (var methodInfo in _controllerInfo.Actions.Values)
                {
                    script.AppendLine("                '" + methodInfo.ActionName.ToCamelCase() + "' : serviceNamespace" + ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(methodInfo.ActionName.ToCamelCase()) + ((methodNo++) < (_controllerInfo.Actions.Count - 1) ? "," : ""));
                }

                script.AppendLine("            };");
                script.AppendLine("        });");
                script.AppendLine("    }");
            }

            script.AppendLine();
            script.AppendLine("})();");

            return(script.ToString());
        }
예제 #9
0
    private static string AddQueryStringParameters(string url, IList <ParameterApiDescriptionModel> actionParameters)
    {
        var queryStringParameters = actionParameters
                                    .Where(p => p.BindingSourceId.IsIn(ParameterBindingSources.ModelBinding, ParameterBindingSources.Query))
                                    .ToArray();

        if (!queryStringParameters.Any())
        {
            return(url);
        }

        var qsBuilderParams = queryStringParameters
                              .Select(p => $"{{ name: '{p.Name.ToCamelCase()}', value: {ProxyScriptingJsFuncHelper.GetParamNameInJsFunc(p)} }}")
                              .JoinAsString(", ");

        return(url + $"' + abp.utils.buildQueryString([{qsBuilderParams}]) + '");
    }