Line data Source code
1 : part of apptive_grid_model; 2 : 3 : /// Data Object that represents a entry in a Form 4 : /// 5 : /// [T] is the [DataEntity] type of [data] 6 : abstract class FormComponent<T extends DataEntity> { 7 : /// Name of the Component 8 : String get property; 9 : 10 : /// Value of the Component 11 : T get data; 12 : 13 : /// Additional options of a component 14 : FormComponentOptions get options; 15 : 16 : /// Shows if the field can be [null] 17 : /// 18 : /// For [DataType.checkbox] this will make only `true` values valid 19 : bool get required; 20 : 21 : /// Id of this FormComponent 22 : String get fieldId; 23 : 24 : /// Saves this into a [Map] that can be encoded using [json.encode] 25 4 : Map<String, dynamic> toJson() => { 26 2 : 'property': property, 27 4 : 'value': data.schemaValue, 28 4 : 'options': options.toJson(), 29 2 : 'required': required, 30 2 : 'fieldId': fieldId, 31 : }; 32 : 33 3 : @override 34 : String toString() { 35 21 : return '$runtimeType(property: $property, fieldId: $fieldId data: $data, options: $options, required: $required)'; 36 : } 37 : 38 5 : @override 39 : bool operator ==(Object other) { 40 15 : return runtimeType == other.runtimeType && 41 5 : other is FormComponent<T> && 42 15 : fieldId == other.fieldId && 43 15 : property == other.property && 44 15 : data == other.data && 45 15 : options == other.options && 46 15 : required == other.required; 47 : } 48 : 49 3 : @override 50 6 : int get hashCode => toString().hashCode; 51 : 52 : /// Mapping to a concrete implementation based on [json] and [schema] 53 : /// 54 : /// Throws an [ArgumentError] if not matching implementation is found. 55 6 : static FormComponent fromJson(dynamic json, dynamic schema) { 56 18 : final properties = schema['properties'][json['fieldId']]; 57 : if (properties == null) { 58 1 : throw ArgumentError( 59 3 : 'No Schema Entry found for ${json['property']} with id ${json['fieldId']}', 60 : ); 61 : } 62 6 : final dataType = dataTypeFromSchemaProperty(schemaProperty: properties); 63 : switch (dataType) { 64 6 : case DataType.text: 65 3 : return StringFormComponent.fromJson(json); 66 5 : case DataType.dateTime: 67 2 : return DateTimeFormComponent.fromJson(json); 68 5 : case DataType.date: 69 2 : return DateFormComponent.fromJson(json); 70 5 : case DataType.integer: 71 2 : return IntegerFormComponent.fromJson(json); 72 5 : case DataType.checkbox: 73 2 : return BooleanFormComponent.fromJson(json); 74 5 : case DataType.selectionBox: 75 2 : return EnumFormComponent.fromJson(json, properties); 76 4 : case DataType.crossReference: 77 2 : return CrossReferenceFormComponent.fromJson(json, properties); 78 2 : case DataType.decimal: 79 2 : return DecimalFormComponent.fromJson(json); 80 : } 81 : } 82 : }