Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | 1x 89x 89x 89x 89x 89x 89x 22x 22x 22x 33x 33x 32x 4x 3x 14x 13x 6x 5x 7x 6x 4x 4x 4x 4x 6x 5x 4x 4x 4x 4x 5x 4x 4x 4x 4x 25x 25x 25x 7x 7x 7x 5x 4x 1x 3x 19x 18x 1x 17x 15x 2x 1x 7x 6x 1x 5x 42x 8x 8x 8x 1x 4x 1x 2x 1x 7x 92x 2x 2x 2x 30x 26x 4x 4x 4x 1x 1x 2x 1x 27x 11x 12x 27x 9x 4x 59x 7x 5x 5x 3x 4x 4x 4x 2x 4x 12x 12x 37x 5x 4x 11x 3x 1x 8x 8x 9x 9x 61x 61x 12x 12x 9x 82x | import { Inject, Injectable, Optional } from '@angular/core'; import { DynamicFormAction } from '../dynamic-form-action/dynamic-form-action'; import { DynamicFormActionDefinition } from '../dynamic-form-action/dynamic-form-action-definition'; import { DynamicFormActionExpressions } from '../dynamic-form-action/dynamic-form-action-expressions'; import { DynamicFormActionType } from '../dynamic-form-action/dynamic-form-action-type'; import { DynamicFormArray } from '../dynamic-form-array/dynamic-form-array'; import { DynamicFormArrayDefinition } from '../dynamic-form-array/dynamic-form-array-definition'; import { DynamicFormArrayAsyncValidator, DynamicFormArrayValidator } from '../dynamic-form-array/dynamic-form-array-validator'; import { DynamicFormConfigService } from '../dynamic-form-config/dynamic-form-config.service'; import { DynamicFormControl } from '../dynamic-form-control/dynamic-form-control'; import { DynamicFormControlAddOn, DynamicFormControlAddOnDefinition, DynamicFormControlDefinition } from '../dynamic-form-control/dynamic-form-control-definition'; import { DynamicFormControlEvaluator } from '../dynamic-form-control/dynamic-form-control-evaluator'; import { DynamicFormControlAsyncValidator, DynamicFormControlValidator } from '../dynamic-form-control/dynamic-form-control-validator'; import { DynamicFormDictionary } from '../dynamic-form-dictionary/dynamic-form-dictionary'; import { DynamicFormDictionaryDefinition } from '../dynamic-form-dictionary/dynamic-form-dictionary-definition'; import { DynamicFormDictionaryAsyncValidator,DynamicFormDictionaryValidator, } from '../dynamic-form-dictionary/dynamic-form-dictionary-validator'; import { DynamicFormElement } from '../dynamic-form-element/dynamic-form-element'; import { DynamicFormElementDefinition } from '../dynamic-form-element/dynamic-form-element-definition'; import { DynamicFormElementExpressions } from '../dynamic-form-element/dynamic-form-element-expressions'; import { DynamicFormElementType } from '../dynamic-form-element/dynamic-form-element-type'; import { DynamicFormError } from '../dynamic-form-error/dynamic-form-error'; import { DynamicFormErrorType } from '../dynamic-form-error/dynamic-form-error-type'; import { DynamicFormErrorHandler } from '../dynamic-form-error/dynamic-form-error.handler'; import { DynamicFormEvaluationBuilder } from '../dynamic-form-evaluation/dynamic-form-evaluation.builder'; import { DynamicFormExpressionBuilder } from '../dynamic-form-expression/dynamic-form-expression.builder'; import { DynamicFormField } from '../dynamic-form-field/dynamic-form-field'; import { DynamicFormFieldDefinition } from '../dynamic-form-field/dynamic-form-field-definition'; import { DynamicFormFieldExpressions } from '../dynamic-form-field/dynamic-form-field-expressions'; import { DynamicFormFieldType } from '../dynamic-form-field/dynamic-form-field-type'; import { DynamicFormGroup } from '../dynamic-form-group/dynamic-form-group'; import { DynamicFormGroupDefinition } from '../dynamic-form-group/dynamic-form-group-definition'; import { DynamicFormGroupAsyncValidator, DynamicFormGroupValidator } from '../dynamic-form-group/dynamic-form-group-validator'; import { DynamicFormValidationBuilder } from '../dynamic-form-validation/dynamic-form-validation.builder'; import { DynamicForm } from './dynamic-form'; import { DynamicFormDefinition } from './dynamic-form-definition'; import { cloneObject, mergeObject } from './dynamic-form-helpers'; import { DynamicFormIdBuilder, DYNAMIC_FORM_ID_BUILDER } from './dynamic-form-id.builder'; export interface DynamicFormFieldConstructor<Field extends DynamicFormField> { // eslint-disable-next-line @typescript-eslint/prefer-function-type new( builder: DynamicFormBuilder, root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormFieldDefinition, type: DynamicFormFieldType ): Field; } @Injectable() export class DynamicFormBuilder { constructor( private configService: DynamicFormConfigService, private expressionBuilder: DynamicFormExpressionBuilder, private evaluationBuilder: DynamicFormEvaluationBuilder, private validationBuilder: DynamicFormValidationBuilder, private errorHandler: DynamicFormErrorHandler, @Optional() @Inject(DYNAMIC_FORM_ID_BUILDER) private idBuilder: DynamicFormIdBuilder, ) {} initForm<Value extends { [key: string]: any } = any, Model extends Value = Value>( definition: DynamicFormDefinition, model: Model, ): DynamicForm<Value, Model> { const field = this.createForm<Value, Model>(definition, model); field.check(); return field; } createForm<Value extends { [key: string]: any } = any, Model extends Value = Value>( definition: DynamicFormDefinition, model: Model, ): DynamicForm<Value, Model> { const field = new DynamicForm<Value, Model>(this, definition, model); field.init(); return field; } createFormElement( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormElementDefinition, ): DynamicFormElement | undefined { const elementType = this.getElementType(definition); return elementType ? this.createFormElementForType(root, parent, definition, elementType) : undefined; } createFormControl( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormControlDefinition, ): DynamicFormControl | undefined { const fieldType = this.getFieldType(definition); return fieldType ? this.createFormFieldForType(DynamicFormControl, root, parent, definition, fieldType) : undefined; } createFormGroup( root: DynamicForm, parent: DynamicFormField, definition: DynamicFormGroupDefinition, ): DynamicFormGroup | undefined { const fieldType = this.getFieldType(definition); return fieldType ? this.createFormFieldForType(DynamicFormGroup, root, parent, definition, fieldType) : undefined; } createFormArray( root: DynamicForm, parent: DynamicFormField, definition: DynamicFormArrayDefinition, ): DynamicFormArray | undefined { const fieldType = this.getFieldType(definition); return fieldType ? this.createFormFieldForType(DynamicFormArray, root, parent, definition, fieldType) : undefined; } createFormArrayField(field: DynamicFormArray, index: number): DynamicFormField | undefined { const definitionTemplate = field.definition.definitionTemplate || {}; const definitionBase = this.getDefinitionClone(definitionTemplate, field.root); const definition = { ...definitionBase, key: `${index}`, index }; return this.createFormFieldForFactory(field.root, field, definition); } createFormDictionary( root: DynamicForm, parent: DynamicFormField, definition: DynamicFormDictionaryDefinition, ): DynamicFormDictionary | undefined { const fieldType = this.getFieldType(definition); return fieldType ? this.createFormFieldForType(DynamicFormDictionary, root, parent, definition, fieldType) : undefined; } createFormDictionaryField(field: DynamicFormDictionary, key: string): DynamicFormField | undefined { const definitionTemplate = field.definition.definitionTemplate || {}; const definitionBase = this.getDefinitionClone(definitionTemplate, field.root); const definition = { ...definitionBase, key }; return this.createFormFieldForFactory(field.root, field, definition); } createFormAction( root: DynamicForm, parent: DynamicFormElement | DynamicFormField, definition: DynamicFormActionDefinition, ): DynamicFormAction | undefined { const actionType = this.getActionType(definition); return actionType ? this.createFormActionForType(root, parent, definition, actionType) : undefined; } createFormElementForType( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormElementDefinition, type: DynamicFormElementType, ): DynamicFormElement { const element = new DynamicFormElement(this, root, parent, definition, type); element.init(); return element; } createFormFieldForType<Field extends DynamicFormField>( fieldConstructor: DynamicFormFieldConstructor<Field>, root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormFieldDefinition, type: DynamicFormFieldType, ): Field { const field = new fieldConstructor(this, root, parent, definition, type); field.init(); return field; } createFormActionForType( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormActionDefinition, type: DynamicFormActionType, ): DynamicFormAction { const action = new DynamicFormAction(this, root, parent, definition, type); action.init(); return action; } createFormElementForFactory( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormElementDefinition, ): DynamicFormElement | undefined { const elementType = this.getElementType(definition); if (!elementType) { return undefined; } return !elementType.factory ? this.createFormElementForType(root, parent, definition, elementType) : elementType.factory(this, root, parent, definition, elementType); } createFormFieldForFactory( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormFieldDefinition, ): DynamicFormField | undefined { const fieldType = this.getFieldType(definition); if (!fieldType) { return undefined; } if (fieldType.factory) { return fieldType.factory(this, root, parent, definition, fieldType); } this.handleError(DynamicFormErrorType.FieldType, `Field type ${ fieldType.type } does not provide a factory`); return undefined; } createFormActionForFactory( root: DynamicForm, parent: DynamicFormElement, definition: DynamicFormActionDefinition, ): DynamicFormAction | undefined { const actionType = this.getActionType(definition); if (!actionType) { return undefined; } return !actionType.factory ? this.createFormActionForType(root, parent, definition, actionType) : actionType.factory(this, root, parent, definition, actionType); } createFormElements( root: DynamicForm, parent: DynamicFormElement, definitions: DynamicFormElementDefinition[], ): DynamicFormElement[] { return (definitions || []) .map((definition) => { const elementDefintion = this.getDefinition(definition, root); const classType = this.configService.getClassType(elementDefintion.type); switch (classType) { case 'element': return this.createFormElementForFactory(root, parent, elementDefintion); case 'field': return this.createFormFieldForFactory(root, parent, elementDefintion as DynamicFormFieldDefinition); case 'action': return this.createFormActionForFactory(root, parent, elementDefintion as DynamicFormActionDefinition); default: this.handleError(DynamicFormErrorType.ClassType, `Class type ${ classType } is not defined`); return undefined; } }) .filter(element => !!element); } createFormActions( root: DynamicForm, parent: DynamicFormElement | DynamicFormField, definitions: DynamicFormActionDefinition[], ): DynamicFormAction[] { return (definitions || []) .map(definition => { const actionDefinition = this.getDefinition(definition, root); return this.createFormActionForFactory(root, parent, actionDefinition); }) .filter(element => !!element); } createFormControlAddOn( root: DynamicForm, parent: DynamicFormControl, definition: DynamicFormControlAddOnDefinition, ): DynamicFormControlAddOn | undefined { if (!definition) { return undefined; } const addOnDefinition = this.getDefinition(definition, root); const classType = this.configService.getClassType(addOnDefinition.type); switch (classType) { case 'element': return this.createFormElementForFactory(root, parent, addOnDefinition); case 'action': return this.createFormActionForFactory(root, parent, addOnDefinition as DynamicFormActionDefinition); default: this.handleError(DynamicFormErrorType.ClassType, `Class type ${ classType } is not defined or not supported for add-on`); return undefined; } } getDefinition<TDefinition extends DynamicFormElementDefinition>(definition: TDefinition, root: DynamicForm): TDefinition { return definition.reference ? this.mergeDefinition(definition, root) : definition; } getDefinitionClone<TDefinition extends DynamicFormElementDefinition>(definition: TDefinition, root: DynamicForm): TDefinition { return definition.reference ? this.mergeDefinition(definition, root) : cloneObject(definition); } createId(): string { return this.idBuilder?.createId ? this.idBuilder?.createId() : `${Date.now()}${Math.random().toString().slice(2)}`; } getFieldId(field: DynamicFormField): string { return field.settings.autoGeneratedId && !field.id ? this.createId() : field.id; } getActionId(action: DynamicFormAction): string { return !action.id ? `${action.template.action}-${this.createId()}` : action.id; } createElementExpressions(element: DynamicFormElement): DynamicFormElementExpressions { return this.expressionBuilder.createElementExpressions(element); } createFieldExpressions(field: DynamicFormField): DynamicFormFieldExpressions { return this.expressionBuilder.createFieldExpressions(field); } createActionExpressions(action: DynamicFormAction): DynamicFormActionExpressions { return this.expressionBuilder.createActionExpressions(action); } createFormArrayElements(array: DynamicFormArray): DynamicFormField[] | undefined { const type = this.getFieldType(this.getDefinition(array.definition.definitionTemplate || {}, array.root)); if (!type) { return undefined; } return array.model.map((_item, index) => this.createFormArrayField(array , index)); } createFormDictionaryElements(dictionary: DynamicFormDictionary): DynamicFormField[] { const type = this.getFieldType(this.getDefinition(dictionary.definition.definitionTemplate || {}, dictionary.root)); if (!type) { return undefined; } return Object.keys(dictionary.model).map((key, _index) => this.createFormDictionaryField(dictionary, key)); } createControlEvaluators(control: DynamicFormControl): DynamicFormControlEvaluator[] { return this.evaluationBuilder.createControlEvaluators(control); } createControlValidators(control: DynamicFormControl): (DynamicFormControlValidator | DynamicFormControlAsyncValidator)[] { return this.validationBuilder.createControlValidators(control); } createGroupValidators(group: DynamicFormGroup): (DynamicFormGroupValidator | DynamicFormGroupAsyncValidator)[] { return this.validationBuilder.createGroupValidators(group); } createArrayValidators(array: DynamicFormArray): (DynamicFormArrayValidator | DynamicFormArrayAsyncValidator)[] { return this.validationBuilder.createArrayValidators(array); } createDictionaryValidators(dictionary: DynamicFormDictionary): (DynamicFormDictionaryValidator | DynamicFormDictionaryAsyncValidator)[] { return this.validationBuilder.createDictionaryValidators(dictionary); } private mergeDefinition<TDefinition extends DynamicFormElementDefinition>(definition: TDefinition, root: DynamicForm): TDefinition { if (!root.definition.references?.[definition.reference]) { this.handleError(DynamicFormErrorType.DefinitionReference, `Definition reference ${ definition.reference } is not defined`); return definition; } const reference = cloneObject(root.definition.references[definition.reference]); return mergeObject(reference, definition); } private getElementType(definition: DynamicFormElementDefinition): DynamicFormElementType | undefined { const type = definition.type ? this.configService.getElementType(definition.type) : undefined; return this.handleUndefined(type, DynamicFormErrorType.ElementType, () => `Element type ${ definition.type } is not defined`); } private getFieldType(definition: DynamicFormFieldDefinition): DynamicFormFieldType | undefined { const type = definition.type ? this.configService.getFieldType(definition.type) : undefined; return this.handleUndefined(type, DynamicFormErrorType.FieldType, () => `Field type ${ definition.type } is not defined`); } private getActionType(definition: DynamicFormActionDefinition): DynamicFormActionType | undefined { const type = definition.type ? this.configService.getActionType(definition.type) : undefined; return this.handleUndefined(type, DynamicFormErrorType.ActionType, () => `Action type ${ definition.type } is not defined`); } private handleError<ErrorType extends DynamicFormErrorType = DynamicFormErrorType>(type: ErrorType, message: string): void { this.errorHandler.handle(new DynamicFormError<ErrorType>(type, message)); } private handleUndefined<Value, ErrorType extends DynamicFormErrorType = DynamicFormErrorType>( value: Value | undefined, type: ErrorType, createMessage: () => string, ): Value | undefined { return this.errorHandler.handleUndefined(value, () => new DynamicFormError<ErrorType>(type, createMessage())); } } |