Ejemplo n.º 1
0
 public ComplexFieldClass(string ns, Func <string, Stream> createStream, IComplexField template, string baseClassName)
 {
     this.ns            = ns;
     this.createStream  = createStream;
     this.template      = template;
     this.baseClassName = baseClassName;
 }
Ejemplo n.º 2
0
 private void UpdateComplexField(IComplexField complexField, string data)
 {
     string[] fields = data.Split(CurrentSeperators.ComponentDataElement);
     foreach (PropertyInfo p in complexField.GetType().GetProperties())
     {
         PositionIndexAttribute pos = p.GetCustomAttributes().Where(a => a is PositionIndexAttribute).FirstOrDefault() as PositionIndexAttribute;
         if (pos != null && pos.Index < fields.Length)
         {
             p.SetValue(complexField, fields[pos.Index]);
         }
     }
 }
Ejemplo n.º 3
0
        private SegmentBase ParseUsingPosition(Type type, string[] fields)
        {
            SegmentBase ret = Activator.CreateInstance(type) as SegmentBase;

            foreach (PropertyInfo p in ret.GetType().GetProperties())
            {
                PositionIndexAttribute pos = p.GetCustomAttributes().Where(a => a is PositionIndexAttribute).FirstOrDefault() as PositionIndexAttribute;
                if (pos != null && pos.Index < fields.Length)
                {
                    if (p.PropertyType.GetInterfaces().Contains(typeof(IComplexField)))
                    {
                        IComplexField complexField = Activator.CreateInstance(p.PropertyType) as IComplexField;
                        this.UpdateComplexField(complexField, fields[pos.Index]);
                        p.SetValue(ret, complexField);
                    }
                    else
                    {
                        p.SetValue(ret, fields[pos.Index]);
                    }
                }
            }
            return(ret);
        }
Ejemplo n.º 4
0
    private Task generateComplexField(IComplexField complexField, bool isForm)
    {
        var className = complexField.TypeName;
        var tsFile    = new TypeScriptFile(className, createStream);

        if (isForm)
        {
            tsFile.AddLine("import { BaseForm } from '@jasonbenfield/sharedwebapp/Forms/BaseForm';");
        }
        else
        {
            tsFile.AddLine("import { ComplexFieldFormGroup } from '@jasonbenfield/sharedwebapp/Forms/ComplexFieldFormGroup';");
        }
        tsFile.AddLine($"import {{ {className}View }} from './{className}View';");
        var fields = complexField.Fields;

        if (fields.OfType <DropDownFieldModel>().Any())
        {
            tsFile.AddLine("import { DropDownFieldItem } from \"@jasonbenfield/sharedwebapp/Forms/DropDownFieldItem\";");
        }
        foreach (var field in fields.OfType <ComplexFieldModel>().Select(f => f.TypeName).Distinct())
        {
            tsFile.AddLine($"import {{ {field} }} from './{field}';");
        }
        tsFile.AddLine();
        var baseClass = isForm ? "BaseForm" : "ComplexFieldFormGroup";

        tsFile.AddLine($"export class {className} extends {baseClass} {{");
        tsFile.Indent();
        tsFile.AddLine($"protected readonly view: {className}View;");
        tsFile.AddLine();
        var ctorArgs    = isForm ? "" : "prefix: string, name: string, ";
        var vmClassName = isForm ? "FormComponentViewModel" : "BlockViewModel";

        tsFile.AddLine($"constructor({ctorArgs}view: {className}View) {{");
        tsFile.Indent();
        var superArgs = isForm ? $"'{className}'" : "prefix, name";

        tsFile.AddLine($"super({superArgs}, view);");
        foreach (var field in fields)
        {
            if (!string.IsNullOrWhiteSpace(field.Caption))
            {
                tsFile.AddLine($"this.{field.Name}.setCaption('{field.Caption}');");
            }
            if (field is SimpleFieldModel simpleField)
            {
                if (!simpleField.IsNullAllowed)
                {
                    tsFile.AddLine($"this.{field.Name}.constraints.mustNotBeNull();");
                }
                foreach (var constraint in simpleField.Constraints)
                {
                    if (constraint is NotWhitespaceConstraintModel)
                    {
                        tsFile.AddLine($"this.{field.Name}.constraints.mustNotBeWhitespace('{constraint.FailureMessage}');");
                    }
                    else if (constraint is LowerRangeConstraintModel lowerRange)
                    {
                        var lowerValue = valueToJsLiteral(lowerRange.Value);
                        if (lowerRange.IsIncluded)
                        {
                            tsFile.AddLine($"this.{field.Name}.constraints.mustBeOnOrAbove({lowerRange.Value}, '{constraint.FailureMessage}');");
                        }
                        else
                        {
                            tsFile.AddLine($"this.{field.Name}.constraints.mustBeAbove({lowerRange.Value}, '{constraint.FailureMessage}');");
                        }
                    }
                    else if (constraint is UpperRangeConstraintModel upperRange)
                    {
                        var upperValue = valueToJsLiteral(upperRange.Value);
                        if (upperRange.IsIncluded)
                        {
                            tsFile.AddLine($"this.{field.Name}.constraints.mustBeOnOrBelow({upperRange.Value}, '{constraint.FailureMessage}');");
                        }
                        else
                        {
                            tsFile.AddLine($"this.{field.Name}.constraints.mustBeBelow({upperRange.Value}, '{constraint.FailureMessage}');");
                        }
                    }
                }
            }
            if (field is InputFieldModel inputField)
            {
                if (inputField.MaxLength.HasValue)
                {
                    tsFile.AddLine($"this.{field.Name}.setMaxLength({inputField.MaxLength});");
                }
                if (inputField.IsProtected)
                {
                    tsFile.AddLine($"this.{field.Name}.protect();");
                }
            }
            else if (field is DropDownFieldModel dropDownField)
            {
                if (!string.IsNullOrWhiteSpace(dropDownField.ItemCaption))
                {
                    tsFile.AddLine($"this.{field.Name}.setItemCaption('{dropDownField.ItemCaption}');");
                }
                var dropDownItems = dropDownField.Items;
                if (dropDownItems?.Any() == true)
                {
                    tsFile.AddLine($"this.{field.Name}.setItems(");
                    tsFile.Indent();
                    var inputType = dropDownField.InputDataType;
                    var lastItem  = dropDownItems.Last();
                    foreach (var dropDownItem in dropDownItems)
                    {
                        var itemValue = valueToJsLiteral(dropDownItem.Value);
                        tsFile.AddLine($"new DropDownFieldItem({itemValue}, '{dropDownItem.DisplayText}')");
                        if (dropDownItem != lastItem)
                        {
                            tsFile.Append(",");
                        }
                    }
                    tsFile.Outdent();
                    tsFile.AddLine(");");
                }
            }
        }
        tsFile.Outdent();
        tsFile.AddLine("}");
        foreach (var field in fields)
        {
            tsFile.AddLine("");
            tsFile.Append($"readonly {field.Name} = ");
            if (field is IComplexField complex)
            {
                tsFile.Append($"this.addFormGroup(new {complex.TypeName}(this.getName(), '{field.Name}', this.view.{field.Name}))");
            }
            else
            {
                string addFormGroup;
                if (field is InputFieldModel inputField)
                {
                    var inputType = inputField.InputDataType;
                    if (inputType == typeof(int?) || inputType == typeof(decimal?))
                    {
                        addFormGroup = "addNumberInputFormGroup";
                    }
                    else if (inputType == typeof(DateTimeOffset?))
                    {
                        addFormGroup = "addDateInputFormGroup";
                    }
                    else if (inputType == typeof(string))
                    {
                        addFormGroup = "addTextInputFormGroup";
                    }
                    else
                    {
                        throw new NotSupportedException($"Simple field with input type {inputType} is not supported");
                    }
                }
                else if (field is DropDownFieldModel dropDownField)
                {
                    var inputType   = dropDownField.InputDataType;
                    var tsInputType = getTsType(inputType);
                    if (inputType == typeof(int?) || inputType == typeof(decimal?))
                    {
                        addFormGroup = "addNumberDropDownFormGroup";
                    }
                    else if (inputType == typeof(DateTimeOffset?))
                    {
                        addFormGroup = "addDateDropDownFormGroup";
                    }
                    else if (inputType == typeof(string))
                    {
                        addFormGroup = "addTextDropDownFormGroup";
                    }
                    else if (inputType == typeof(bool?))
                    {
                        addFormGroup = "addBooleanDropDownFormGroup";
                    }
                    else
                    {
                        throw new NotSupportedException($"DropDown field with input type {inputType} is not supported");
                    }
                }
                else if (field is SimpleFieldModel hiddenField)
                {
                    var inputType = hiddenField.InputDataType;
                    if (inputType == typeof(int?) || inputType == typeof(decimal?))
                    {
                        addFormGroup = "addHiddenNumberFormGroup";
                    }
                    else if (inputType == typeof(DateTimeOffset?))
                    {
                        addFormGroup = "addHiddenDateFormGroup";
                    }
                    else if (inputType == typeof(string))
                    {
                        addFormGroup = "addHiddenTextFormGroup";
                    }
                    else
                    {
                        throw new NotSupportedException($"Simple field with input type {inputType} is not supported");
                    }
                }
                else
                {
                    throw new NotSupportedException($"Simple field of type {field.GetType()} is not supported");
                }
                tsFile.Append($"this.{addFormGroup}('{field.Name}', this.view.{field.Name})");
            }
            tsFile.Append(";");
        }
        tsFile.Outdent();
        tsFile.AddLine("}");
        return(tsFile.Output());
    }
Ejemplo n.º 5
0
    private Task generateComplexFieldView(IComplexField complexField, bool isForm)
    {
        var className = $"{complexField.TypeName}View";
        var tsFile    = new TypeScriptFile(className, createStream);

        if (isForm)
        {
            tsFile.AddLine("import { BaseFormView } from '@jasonbenfield/sharedwebapp/Forms/BaseFormView';");
        }
        else
        {
            tsFile.AddLine("import { ComplexFieldFormGroupView } from '@jasonbenfield/sharedwebapp/Forms/ComplexFieldFormGroupView';");
        }
        var fields = complexField.Fields;

        foreach (var field in fields.OfType <ComplexFieldModel>().Select(f => f.TypeName).Distinct())
        {
            tsFile.AddLine($"import {{ {field}View }} from './{field}View';");
        }
        tsFile.AddLine();
        var baseClass = isForm ? "BaseFormView" : "ComplexFieldFormGroupView";

        tsFile.AddLine($"export class {className} extends {baseClass} {{");
        tsFile.Indent();
        foreach (var field in fields)
        {
            tsFile.AddLine("");
            tsFile.Append($"readonly {field.Name} = ");
            if (field is IComplexField complex)
            {
                tsFile.Append($"this.addFormGroup(new {complex.TypeName}View())");
            }
            else
            {
                string addFormGroup;
                if (field is InputFieldModel inputField)
                {
                    addFormGroup = "addInputFormGroup";
                }
                else if (field is DropDownFieldModel dropDownField)
                {
                    var inputType   = dropDownField.InputDataType;
                    var tsInputType = getTsType(inputType);
                    addFormGroup = $"addDropDownFormGroup<{tsInputType}>";
                }
                else if (field is SimpleFieldModel hiddenField)
                {
                    addFormGroup = "addInputFormGroup";
                }
                else
                {
                    throw new NotSupportedException($"Simple field of type {field.GetType()} is not supported");
                }
                tsFile.Append($"this.{addFormGroup}()");
            }
            tsFile.Append(";");
        }
        tsFile.Outdent();
        tsFile.AddLine("}");
        return(tsFile.Output());
    }
Ejemplo n.º 6
0
 private void UpdateComplexField(IComplexField complexField, string data)
 {
     string[] fields = data.Split(CurrentSeperators.ComponentDataElement);
     foreach (PropertyInfo p in complexField.GetType().GetProperties())
     {
         PositionIndexAttribute pos = p.GetCustomAttributes().Where(a => a is PositionIndexAttribute).FirstOrDefault() as PositionIndexAttribute;
         if (pos != null && pos.Index < fields.Length)
         {
             p.SetValue(complexField, fields[pos.Index]);
         }
     }
 }