private Task generateNumericValue(NumericValueTemplate numericValueTemplate) { var tsFile = new TypeScriptFile($"{numericValueTemplate.DataType.Name}.ts", createStream); var className = numericValueTemplate.DataType.Name; tsFile.AddLine("import { NumericValue } from '@jasonbenfield/sharedwebapp/NumericValue';"); tsFile.AddLine("import { NumericValues } from '@jasonbenfield/sharedwebapp/NumericValues';"); tsFile.AddLine(); tsFile.AddLine($"export class {className}s extends NumericValues<{className}> {{"); tsFile.Indent(); tsFile.AddLine("constructor("); tsFile.Indent(); var valueNames = numericValueTemplate.Values.Select(v => whitespaceRegex.Replace(v.DisplayText, "")); var lastValueName = valueNames.Last(); foreach (var valueName in valueNames) { tsFile.AddLine($"public readonly {valueName}: {className}"); if (valueName != lastValueName) { tsFile.Append(","); } } tsFile.Outdent(); tsFile.AddLine(") {"); tsFile.Indent(); var joinedValueNames = string.Join(",", valueNames); tsFile.AddLine($"super([{joinedValueNames}]);"); tsFile.Outdent(); tsFile.AddLine("}"); tsFile.Outdent(); tsFile.AddLine("}"); tsFile.AddLine(); tsFile.AddLine($"export class {className} extends NumericValue implements I{className} {{"); tsFile.Indent(); tsFile.AddLine($"public static readonly values = new {className}s("); tsFile.Indent(); var lastValue = numericValueTemplate.Values.Last(); foreach (var numericValue in numericValueTemplate.Values) { tsFile.AddLine($"new {className}({numericValue.Value}, '{numericValue.DisplayText.Replace("'", "\'")}')"); if (numericValue != lastValue) { tsFile.Append(","); } } tsFile.Outdent(); tsFile.AddLine(");"); tsFile.AddLine(); tsFile.AddLine("private constructor(Value: number, DisplayText: string) {"); tsFile.Indent(); tsFile.AddLine("super(Value, DisplayText);"); tsFile.Outdent(); tsFile.AddLine("}"); tsFile.Outdent(); tsFile.AddLine("}"); return(tsFile.Output()); }
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()); }
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()); }