UNPKG

@crnk/angular-ngrx

Version:

Angular helper library for ngrx-json-api and crnk:

124 lines (123 loc) 5.33 kB
import { Observable } from "rxjs/Observable"; import "rxjs/add/operator/zip"; import "rxjs/add/operator/do"; import "rxjs/add/operator/debounceTime"; import "rxjs/add/operator/distinct"; import "rxjs/add/operator/switch"; import "rxjs/add/operator/finally"; import "rxjs/add/operator/share"; import { NgForm } from "@angular/forms"; import { NgrxJsonApiService, NgrxJsonApiStoreData, NgrxJsonApiZoneService, ResourceError, StoreResource } from 'ngrx-json-api'; import { Store } from "@ngrx/store"; export interface FormBindingConfig { /** * Reference to the forFormElement instance to hook into. */ form: NgForm; /** * Reference to a query from the store to get notified about validation errors. * FormBinding implementation assumes that the query has already been executed * (typically when performing the route to a new page). */ queryId: string; /** * By default a denormalized selectOneResults is used to fetch resources. Any update of those * resources triggers an update of the FormControl states. Set this flag to true to listen to all store changes. */ mapNonResultResources?: boolean; /** * Zone to use within ngrx-json-api. */ zoneId?: string; } /** * Binding between ngrx-jsonapi and angular forms. It serves two purposes: * * <ul> * <li>Updates the JSON API store when forFormElement controls changes their values.</li> * <li>Updates the validation state of forFormElement controls in case of JSON API errors. JSON API errors that cannot be * mapped to a forFormElement control are hold in the errors property * </li> * <ul> * * The binding between resources in the store and forFormElement controls happens trough the naming of the forFormElement * controls. Two naming patterns are supported: * * <ul> * <li>basic binding for all forFormElement controls that start with "attributes." or "relationships.". A forFormElement * control with label "attributes.title" is mapped to the "title" attribute of the JSON API resource in the store. The id of the * resource is obtained from the FormBindingConfig.resource$. * </li> * <li>(not yet supported) advanced binding with the naming pattern * "resource.{type}.{id}.{attributes/relationships}.{label}". * It allows to edit multiple resources in the same forFormElement. * </li> * <ul> * * Similarly, JSON API errors are mapped back to forFormElement controls trougth the source pointer of the error. If such a * mapping is not found, the error is added to the errors attribute of this class. Usually applications show such errors above * all fields in the config. * * You may also have a look at the CrnkExpressionModule. Its ExpressionDirective provides an alternative to NgModel * that binds both a value and sets the label of forFormElement control with a single (type-safe) attribute. */ export declare class FormBinding { private config; private store; /** * Observable to the resource to be edited. The forFormElement binding is active as long as there is * at least one subscriber to this Observable. */ resource$: Observable<StoreResource>; /** * Contains all errors that cannot be assigned to a forFormElement control. Usually such errors are shown on top above * all controls. */ unmappedErrors: Array<ResourceError>; /** * the forFormElement also sends out valueChanges upon initialization, we do not want that and filter them out with this flag */ private wasDirty; /** * id of the main resource to be edited. */ private primaryResourceId; /** * list of resources edited by this binding. May include related resources next to the primary one. */ private editResourceIds; /** * Subscription to forFormElement changes. Gets automatically cancelled if there are no subscriptions anymore to * resource$. */ private formSubscription; private storeSubscription; private formControlsInitialized; private _storeDataSnapshot?; private validSubject; private dirtySubject; valid: Observable<boolean>; dirty: Observable<boolean>; readonly zone: NgrxJsonApiZoneService; constructor(ngrxJsonApiService: NgrxJsonApiService, config: FormBindingConfig, store: Store<any>); protected cancelSubscriptions(): void; protected checkSubscriptions(): void; protected mapResourceToControlErrors(data: NgrxJsonApiStoreData): void; protected updateDirtyState(data: NgrxJsonApiStoreData): void; protected toResourceFormName(resource: StoreResource, basicFormName: string): string; protected toPath(sourcePointer: string): string; save(): void; delete(): void; /** * computes type, id and field path from formName. */ private parseResourceFieldRef(formName); updateStoreFromFormValues(values: any): void; /** * Prime NG has to annoying habit of adding _$visited. Cleaned up here. Needs to be further investigated * and preferably avoided. * * FIXME move to HTTP layer or fix PrimeNG, preferably the later. */ private clearPrimeNgMarkers(resource); }