-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement formFieldInstanceRegistry
Related to #1142
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
packages/form-js-viewer/src/core/FormFieldInstanceRegistry.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export class FormFieldInstanceRegistry { | ||
constructor(eventBus, formFieldRegistry, formFields) { | ||
this._eventBus = eventBus; | ||
this._formFieldRegistry = formFieldRegistry; | ||
this._formFields = formFields; | ||
|
||
this._formFieldInstances = {}; | ||
|
||
eventBus.on('form.clear', () => this.clear()); | ||
} | ||
|
||
add(instance) { | ||
|
||
const { | ||
id, | ||
expressionContextInfo, | ||
valuePath, | ||
indexes | ||
} = instance; | ||
|
||
const instanceId = [ id, ...Object.values(indexes || {}) ].join('_'); | ||
|
||
if (this._formFieldInstances[ instanceId ]) { | ||
throw new Error('this form field instance is already registered'); | ||
} | ||
|
||
this._formFieldInstances[ instanceId ] = { | ||
id, | ||
instanceId, | ||
expressionContextInfo, | ||
valuePath, | ||
indexes | ||
}; | ||
|
||
return instanceId; | ||
} | ||
|
||
remove(instanceId) { | ||
|
||
if (!this._formFieldInstances[ instanceId ]) { | ||
return; | ||
} | ||
|
||
delete this._formFieldInstances[ instanceId ]; | ||
} | ||
|
||
getAll() { | ||
return Object.values(this._formFieldInstances); | ||
} | ||
|
||
getAllKeyed() { | ||
return this.getAll().filter(({ id }) => { | ||
const { type } = this._formFieldRegistry.get(id); | ||
const { config } = this._formFields.get(type); | ||
|
||
return config.keyed; | ||
}); | ||
} | ||
|
||
clear() { | ||
this._formFieldInstances = {}; | ||
} | ||
|
||
} | ||
|
||
FormFieldInstanceRegistry.$inject = [ 'eventBus', 'formFieldRegistry', 'formFields' ]; |
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
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