-
-
Save INTERNALINTERFERENCE/bb4facac6d0abee6e21cc509e3280067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UISchema.Attributes; | |
using System; | |
using System.Collections.Generic; | |
using System.Xml; | |
namespace UISchema.Builder; | |
public class UISchemaBuilder | |
{ | |
private readonly string _groupName; | |
private readonly UISchemaBuilder _schemaBuilder; | |
private readonly UISchemaSpecification _uiSchemaSpecification; | |
public UISchemaBuilder() | |
: this(new UISchemaSpecification()) { } | |
private UISchemaBuilder(UISchemaSpecification schemaSpecification) | |
{ | |
_uiSchemaSpecification = schemaSpecification; | |
} | |
private UISchemaBuilder( | |
string groupName, | |
UISchemaBuilder schemaBuilder, | |
UISchemaSpecification specification) | |
: this(specification) | |
{ | |
_groupName = groupName; | |
_schemaBuilder = schemaBuilder; | |
} | |
public UISchemaBuilder StartGroup(string name) | |
{ | |
_uiSchemaSpecification.ParsecMetadata ??= new(); | |
_uiSchemaSpecification.ParsecMetadata.Groups ??= new(); | |
_uiSchemaSpecification.ParsecMetadata.Groups.Add(new(name)); | |
return new UISchemaBuilder(name, this, _uiSchemaSpecification); | |
} | |
public UISchemaBuilder EndGroup() => _schemaBuilder; | |
public UISchemaSpecification Build() => _uiSchemaSpecification; | |
public UISchemaBuilder AddTextLine( | |
NameLocalizedTitle nameTitle, | |
uint? maxLength = null) => | |
AddText(nameTitle.Name, nameTitle.Title, maxLength, false); | |
public UISchemaBuilder AddTextArea( | |
NameLocalizedTitle nameTitle, | |
uint? maxLength = null) => | |
AddText(nameTitle.Name, nameTitle.Title, maxLength, true); | |
public UISchemaBuilder AddText( | |
string fieldName, | |
string fieldTitle, | |
uint? maxLength, | |
bool? multiline, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlStringAttribute | |
{ | |
Parameters = new UISchemaStringFieldParameters | |
{ | |
MaxLength = maxLength, | |
Multiline = multiline | |
} | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddBoolean( | |
NameLocalizedTitle nameTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddBoolean(nameTitle.Name, nameTitle.Title, isReadonly, isRequired); | |
public UISchemaBuilder AddBoolean( | |
string fieldName, | |
string fieldTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlBooleanAttribute(); | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddCatalog( | |
NameLocalizedTitle nameTitle, | |
IEnumerable<(string, string)> values, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddCatalog(nameTitle.Name, nameTitle.Title, values, isReadonly, isRequired); | |
public UISchemaBuilder AddCatalog( | |
string fieldName, | |
string fieldTitle, | |
IEnumerable<(string, string)> values, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlCatalogAttribute | |
{ | |
Parameters = values, | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddCatalogEditor( | |
NameLocalizedTitle nameTitle, | |
IEnumerable<string> values, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddCatalogEditor(nameTitle.Name, nameTitle.Title, values, isReadonly, isRequired); | |
public UISchemaBuilder AddCatalogEditor( | |
string fieldName, | |
string fieldTitle, | |
IEnumerable<string> values = null, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlCatalogEditorAttribute | |
{ | |
Values = values | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddDate( | |
NameLocalizedTitle nameTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddDate(nameTitle.Name, nameTitle.Title, isReadonly, isRequired); | |
public UISchemaBuilder AddDate( | |
string fieldName, | |
string fieldTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlDateAttribute(); | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddDateTime( | |
NameLocalizedTitle nameTitle, | |
bool isLocalDateTime, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddDateTime(nameTitle.Name, nameTitle.Title, isLocalDateTime, isReadonly, isRequired); | |
public UISchemaBuilder AddDateTime( | |
string fieldName, | |
string fieldTitle, | |
bool isLocalDateTime, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlDateTimeAttribute() | |
{ | |
IsLocalDatetime = isLocalDateTime | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddRangeDateTime( | |
NameLocalizedTitle nameTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddRangeDateTime(nameTitle.Name, nameTitle.Title, isReadonly, isRequired); | |
public UISchemaBuilder AddRangeDateTime( | |
string fieldName, | |
string fieldTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlRangeDateTimeAttribute() | |
{ | |
IntervalType = UIIntervalType.Variable | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddEmail( | |
NameLocalizedTitle nameTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddEmail(nameTitle.Name, nameTitle.Title, isReadonly, isRequired); | |
public UISchemaBuilder AddEmail( | |
string fieldName, | |
string fieldTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlEmailAttribute(); | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddPhoneNumber( | |
NameLocalizedTitle nameTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddPhoneNumber(nameTitle.Name, nameTitle.Title, isReadonly, isRequired); | |
public UISchemaBuilder AddPhoneNumber( | |
string fieldName, | |
string fieldTitle, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlPhoneNumberAttribute(); | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
public UISchemaBuilder AddNumber( | |
NameLocalizedTitle nameTitle, | |
decimal? min = null, | |
decimal? max = null, | |
bool? isReadonly = null, | |
bool? isRequired = null) => | |
AddNumber(nameTitle.Name, nameTitle.Title, min, max, isReadonly, isRequired); | |
public UISchemaBuilder AddNumber( | |
string fieldName, | |
string fieldTitle, | |
decimal? min, | |
decimal? max, | |
bool? isReadonly = null, | |
bool? isRequired = null) | |
{ | |
var schemaControl = new UIControlNumberAttribute() | |
{ | |
Parameters = | |
{ | |
Min = min, | |
Max = max | |
} | |
}; | |
CreateUISchemaItem( | |
fieldName, | |
fieldTitle, | |
isRequired, | |
isReadonly, | |
schemaControl); | |
return this; | |
} | |
private void CreateUISchemaItem( | |
string fieldName, | |
string fieldTitle, | |
bool? isRequired, | |
bool? isReadonly, | |
UIControlAttribute schemaControl) | |
{ | |
var schema = new UISchemaItem | |
{ | |
Id = $"#/{fieldTitle}", | |
Title = fieldTitle | |
}; | |
if (isReadonly != null) | |
schemaControl.IsReadonly = isReadonly; | |
if (_groupName != null) | |
schemaControl.Group = _groupName; | |
if (isRequired.HasValue && isRequired.Value) | |
{ | |
_uiSchemaSpecification.Required ??= new(); | |
_uiSchemaSpecification.Required.Add(fieldName); | |
} | |
schemaControl.FillSchema(schema, null, null); | |
_uiSchemaSpecification.Properties[fieldName] = schema; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment