Пример #1
0
        private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute)
        {
            var codeNamespace = GetNamespace(type);

            cw.Indented("export class ");

            var identifier = type.Name;

            if (identifier.EndsWith(requestSuffix) &&
                type.IsSubclassOf(typeof(ServiceRequest)))
            {
                identifier = identifier.Substring(0,
                                                  identifier.Length - requestSuffix.Length) + "Form";
                this.fileIdentifier = identifier;
            }

            sb.Append(identifier);

            generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + identifier);

            sb.Append(" extends Serenity.PrefixedContext");
            cw.InBrace(delegate
            {
                cw.Indented("static formKey = '");
                sb.Append(formScriptAttribute.Key);
                sb.AppendLine("';");
                sb.AppendLine();
            });

            sb.AppendLine();

            cw.Indented("export interface ");
            sb.Append(identifier);

            StringBuilder initializer = new StringBuilder("[");

            cw.InBrace(delegate
            {
                int j = 0;
                foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                {
                    var editorType = item.EditorType ?? "String";

                    ExternalType scriptType = null;

                    foreach (var rootNamespace in RootNamespaces)
                    {
                        string wn = rootNamespace + "." + editorType;
                        if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null)
                        {
                            break;
                        }
                    }

                    if (scriptType == null &&
                        (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null)
                    {
                        continue;
                    }

                    var fullName = ShortenFullName(scriptType, codeNamespace);

                    if (j++ > 0)
                    {
                        initializer.Append(", ");
                    }

                    initializer.Append("['");
                    initializer.Append(item.Name);
                    initializer.Append("', () => ");
                    initializer.Append(fullName);
                    initializer.Append("]");

                    cw.Indented(item.Name);
                    sb.Append(": ");
                    sb.Append(fullName);
                    sb.AppendLine(";");
                }
            });

            initializer.Append("].forEach(x => Object.defineProperty(");
            initializer.Append(identifier);
            initializer.Append(".prototype, <string>x[0], { get: function () { return this.w(x[0], (x[1] as any)()); }, enumerable: true, configurable: true }));");

            sb.AppendLine();
            cw.IndentedLine(initializer.ToString());
        }
Пример #2
0
        private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute)
        {
            var codeNamespace = GetNamespace(type);

            cw.Indented("public partial class ");
            var generatedName = MakeFriendlyName(type, codeNamespace);

            generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + generatedName);
            sb.AppendLine(" : PrefixedContext");

            cw.InBrace(delegate
            {
                cw.Indented("[InlineConstant] public const string FormKey = \"");
                sb.Append(formScriptAttribute.Key);
                sb.AppendLine("\";");
                sb.AppendLine();

                cw.Indented("public ");
                sb.Append(generatedName);
                sb.AppendLine("(string idPrefix) : base(idPrefix) {}");
                sb.AppendLine();

                foreach (var item in PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                {
                    var editorType = item.EditorType ?? "String";

                    ExternalType scriptType = null;

                    foreach (var rootNamespace in RootNamespaces)
                    {
                        string wn = rootNamespace + "." + editorType;
                        if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null)
                        {
                            break;
                        }
                    }

                    if (scriptType == null &&
                        (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null)
                    {
                        continue;
                    }

                    var fullTypeName = scriptType.FullName;
                    if (type.FullName == "Serenity.Widget")
                    {
                        fullTypeName = "Serenity.Widget<any>";
                    }

                    var shortTypeName = ShortenFullName(scriptType, codeNamespace);

                    cw.Indented("public ");
                    sb.Append(shortTypeName);
                    sb.Append(" ");
                    sb.Append(item.Name);
                    sb.Append(" { ");
                    sb.Append("[InlineCode(\"{this}.w('");
                    sb.Append(item.Name);
                    sb.Append("', ");
                    sb.Append(fullTypeName);
                    sb.AppendLine(")\")] get; private set; }");
                }
            });
        }
Пример #3
0
        private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute)
        {
            var codeNamespace = GetNamespace(type);

            var identifier = type.Name;

            if (identifier.EndsWith(requestSuffix) &&
                type.IsSubclassOf(typeof(ServiceRequest)))
            {
                identifier = identifier.Substring(0,
                                                  identifier.Length - requestSuffix.Length) + "Form";
                this.fileIdentifier = identifier;
            }

            cw.Indented("export interface ");
            sb.Append(identifier);

            var propertyNames = new List <string>();
            var propertyTypes = new List <string>();

            cw.InBrace(delegate
            {
                foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                {
                    var editorType = item.EditorType ?? "String";

                    ExternalType scriptType = null;

                    foreach (var rootNamespace in RootNamespaces)
                    {
                        string wn = rootNamespace + "." + editorType;
                        if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null)
                        {
                            break;
                        }
                    }

                    if (scriptType == null &&
                        (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null)
                    {
                        continue;
                    }

                    var fullName  = ShortenFullName(scriptType, codeNamespace);
                    var shortName = fullName;
                    if (fullName.StartsWith("Serenity."))
                    {
                        shortName = "s." + fullName.Substring("Serenity.".Length);
                    }

                    propertyNames.Add(item.Name);
                    propertyTypes.Add(shortName);

                    cw.Indented(item.Name);
                    sb.Append(": ");
                    sb.Append(fullName);
                    sb.AppendLine(";");
                }
            });

            sb.AppendLine();
            cw.Indented("export class ");
            sb.Append(identifier);

            sb.Append(" extends Serenity.PrefixedContext");
            cw.InBrace(delegate
            {
                cw.Indented("static formKey = '");
                sb.Append(formScriptAttribute.Key);
                sb.AppendLine("';");

                if (propertyNames.Count > 0)
                {
                    cw.IndentedLine("private static init: boolean;");
                    sb.AppendLine();
                    cw.Indented("constructor(prefix: string)");
                    cw.InBrace(delegate
                    {
                        cw.IndentedLine("super(prefix);");
                        sb.AppendLine();
                        cw.Indented("if (!");
                        sb.Append(identifier);
                        sb.Append(".init) ");


                        cw.InBrace(delegate
                        {
                            cw.Indented(identifier);
                            sb.AppendLine(".init = true;");
                            sb.AppendLine();

                            cw.IndentedLine("var s = Serenity;");
                            var typeNumber = new Dictionary <string, int>();
                            foreach (var s in propertyTypes)
                            {
                                if (!typeNumber.ContainsKey(s))
                                {
                                    cw.Indented("var w");
                                    sb.Append(typeNumber.Count);
                                    sb.Append(" = ");
                                    sb.Append(s);
                                    sb.AppendLine(";");
                                    typeNumber[s] = typeNumber.Count;
                                }
                            }
                            sb.AppendLine();

                            cw.Indented("Q.initFormType(");
                            sb.Append(identifier);
                            sb.AppendLine(", [");
                            cw.Block(delegate
                            {
                                for (var i = 0; i < propertyNames.Count; i++)
                                {
                                    if (i > 0)
                                    {
                                        sb.AppendLine(",");
                                    }

                                    cw.Indented("'");
                                    sb.Append(propertyNames[i]);
                                    sb.Append("', w");
                                    sb.Append(typeNumber[propertyTypes[i]]);
                                    sb.Append("");
                                }

                                sb.AppendLine();
                            });
                            cw.IndentedLine("]);");
                        });
                    });
                }
            });

            generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + identifier);
        }
        private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute)
        {
            var codeNamespace = GetNamespace(type);

            cw.Indented("export class ");
            var generatedName = MakeFriendlyName(type, codeNamespace);

            generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + generatedName);

            sb.Append(" extends Serenity.PrefixedContext");
            cw.InBrace(delegate
            {
                cw.Indented("static formKey = '");
                sb.Append(formScriptAttribute.Key);
                sb.AppendLine("';");
                sb.AppendLine();
            });

            sb.AppendLine();

            cw.Indented("export interface ");
            MakeFriendlyName(type, codeNamespace);

            StringBuilder initializer = new StringBuilder("[");

            cw.InBrace(delegate
            {
                int j = 0;
                foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                {
                    var editorType = item.EditorType ?? "String";

                    ExternalType scriptType = null;

                    foreach (var rootNamespace in RootNamespaces)
                    {
                        string wn = rootNamespace + "." + editorType;
                        if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null)
                        {
                            break;
                        }
                    }

                    if (scriptType == null &&
                        (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null)
                    {
                        continue;
                    }

                    var fullName = ShortenFullName(scriptType, codeNamespace);

                    if (j++ > 0)
                    {
                        initializer.Append(", ");
                    }

                    initializer.Append("['");
                    initializer.Append(item.Name);
                    initializer.Append("', ");
                    initializer.Append(fullName);
                    initializer.Append("]");

                    cw.Indented(item.Name);
                    sb.Append("(): ");
                    sb.Append(fullName);
                    sb.AppendLine(";");
                }
            });

            initializer.Append("].forEach(x => ");
            MakeFriendlyName(type, codeNamespace, initializer);
            initializer.Append(".prototype[<string>x[0]] = function() { return this.w(x[0], x[1]); });");

            sb.AppendLine();
            cw.IndentedLine(initializer.ToString());
        }
        private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute)
        {
            var codeNamespace = GetNamespace(type);

            cw.Indented("public partial class ");

            var identifier = type.Name;

            if (identifier.EndsWith(requestSuffix) &&
                type.IsSubclassOf(typeof(ServiceRequest)))
            {
                identifier = identifier.Substring(0,
                                                  identifier.Length - requestSuffix.Length) + "Form";
                this.fileIdentifier = identifier;
            }

            sb.Append(identifier);

            sb.AppendLine(" : PrefixedContext");

            cw.InBrace(delegate
            {
                cw.Indented("[InlineConstant] public const string FormKey = \"");
                sb.Append(formScriptAttribute.Key);
                sb.AppendLine("\";");
                sb.AppendLine();

                cw.Indented("public ");
                sb.Append(identifier);
                sb.AppendLine("(string idPrefix) : base(idPrefix) {}");
                sb.AppendLine();

                foreach (var item in PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                {
                    var editorType = item.EditorType ?? "String";

                    ExternalType scriptType = null;

                    foreach (var rootNamespace in RootNamespaces)
                    {
                        string wn = rootNamespace + "." + editorType;
                        if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null)
                        {
                            break;
                        }
                    }

                    if (scriptType == null &&
                        (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null)
                    {
                        continue;
                    }

                    var fullTypeName = scriptType.FullName;
                    if (type.FullName == "Serenity.Widget")
                    {
                        fullTypeName = "Serenity.Widget<any>";
                    }

                    var shortTypeName = ShortenFullName(scriptType, codeNamespace);

                    cw.Indented("public ");
                    sb.Append(shortTypeName);
                    sb.Append(" ");
                    sb.Append(item.Name);
                    sb.Append(" { ");
                    sb.Append("[InlineCode(\"{this}.w('");
                    sb.Append(item.Name);
                    sb.Append("', ");
                    sb.Append(fullTypeName);
                    sb.AppendLine(")\")] get; private set; }");
                }
            });
        }