@ngxs/form-plugin 3.7.3-dev.master-10b0883 → 3.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,606 +4,616 @@ import { FormGroupDirective, ReactiveFormsModule } from '@angular/forms';
4
4
  import { Subject } from 'rxjs';
5
5
  import { filter, takeUntil, distinctUntilChanged, debounceTime } from 'rxjs/operators';
6
6
 
7
- /**
8
- * @fileoverview added by tsickle
9
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
10
- */
11
- class UpdateFormStatus {
12
- /**
13
- * @param {?} payload
14
- */
15
- constructor(payload) {
16
- this.payload = payload;
17
- }
18
- /**
19
- * @return {?}
20
- */
21
- static get type() {
22
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
23
- return '[Forms] Update Form Status';
24
- }
25
- }
26
- if (false) {
27
- /** @type {?} */
28
- UpdateFormStatus.prototype.payload;
29
- }
30
- class UpdateFormValue {
31
- /**
32
- * @param {?} payload
33
- */
34
- constructor(payload) {
35
- this.payload = payload;
36
- }
37
- /**
38
- * @return {?}
39
- */
40
- static get type() {
41
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
42
- return '[Forms] Update Form Value';
43
- }
44
- }
45
- if (false) {
46
- /** @type {?} */
47
- UpdateFormValue.prototype.payload;
48
- }
49
- class UpdateForm {
50
- /**
51
- * @param {?} payload
52
- */
53
- constructor(payload) {
54
- this.payload = payload;
55
- }
56
- /**
57
- * @return {?}
58
- */
59
- static get type() {
60
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
61
- return '[Forms] Update Form';
62
- }
63
- }
64
- if (false) {
65
- /** @type {?} */
66
- UpdateForm.prototype.payload;
67
- }
68
- class UpdateFormDirty {
69
- /**
70
- * @param {?} payload
71
- */
72
- constructor(payload) {
73
- this.payload = payload;
74
- }
75
- /**
76
- * @return {?}
77
- */
78
- static get type() {
79
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
80
- return '[Forms] Update Form Dirty';
81
- }
82
- }
83
- if (false) {
84
- /** @type {?} */
85
- UpdateFormDirty.prototype.payload;
86
- }
87
- class SetFormDirty {
88
- /**
89
- * @param {?} payload
90
- */
91
- constructor(payload) {
92
- this.payload = payload;
93
- }
94
- /**
95
- * @return {?}
96
- */
97
- static get type() {
98
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
99
- return '[Forms] Set Form Dirty';
100
- }
101
- }
102
- if (false) {
103
- /** @type {?} */
104
- SetFormDirty.prototype.payload;
105
- }
106
- class SetFormPristine {
107
- /**
108
- * @param {?} payload
109
- */
110
- constructor(payload) {
111
- this.payload = payload;
112
- }
113
- /**
114
- * @return {?}
115
- */
116
- static get type() {
117
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
118
- return '[Forms] Set Form Pristine';
119
- }
120
- }
121
- if (false) {
122
- /** @type {?} */
123
- SetFormPristine.prototype.payload;
124
- }
125
- class UpdateFormErrors {
126
- /**
127
- * @param {?} payload
128
- */
129
- constructor(payload) {
130
- this.payload = payload;
131
- }
132
- /**
133
- * @return {?}
134
- */
135
- static get type() {
136
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
137
- return '[Forms] Update Form Errors';
138
- }
139
- }
140
- if (false) {
141
- /** @type {?} */
142
- UpdateFormErrors.prototype.payload;
143
- }
144
- class SetFormDisabled {
145
- /**
146
- * @param {?} payload
147
- */
148
- constructor(payload) {
149
- this.payload = payload;
150
- }
151
- /**
152
- * @return {?}
153
- */
154
- static get type() {
155
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
156
- return '[Forms] Set Form Disabled';
157
- }
158
- }
159
- if (false) {
160
- /** @type {?} */
161
- SetFormDisabled.prototype.payload;
162
- }
163
- class SetFormEnabled {
164
- /**
165
- * @param {?} payload
166
- */
167
- constructor(payload) {
168
- this.payload = payload;
169
- }
170
- /**
171
- * @return {?}
172
- */
173
- static get type() {
174
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
175
- return '[Forms] Set Form Enabled';
176
- }
177
- }
178
- if (false) {
179
- /** @type {?} */
180
- SetFormEnabled.prototype.payload;
181
- }
182
- class ResetForm {
183
- /**
184
- * @param {?} payload
185
- */
186
- constructor(payload) {
187
- this.payload = payload;
188
- }
189
- /**
190
- * @return {?}
191
- */
192
- static get type() {
193
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
194
- return '[Forms] Reset Form';
195
- }
196
- }
197
- if (false) {
198
- /** @type {?} */
199
- ResetForm.prototype.payload;
7
+ /**
8
+ * @fileoverview added by tsickle
9
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
10
+ */
11
+ class UpdateFormStatus {
12
+ /**
13
+ * @param {?} payload
14
+ */
15
+ constructor(payload) {
16
+ this.payload = payload;
17
+ }
18
+ /**
19
+ * @return {?}
20
+ */
21
+ static get type() {
22
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
23
+ return '[Forms] Update Form Status';
24
+ }
25
+ }
26
+ if (false) {
27
+ /** @type {?} */
28
+ UpdateFormStatus.prototype.payload;
29
+ }
30
+ class UpdateFormValue {
31
+ /**
32
+ * @param {?} payload
33
+ */
34
+ constructor(payload) {
35
+ this.payload = payload;
36
+ }
37
+ /**
38
+ * @return {?}
39
+ */
40
+ static get type() {
41
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
42
+ return '[Forms] Update Form Value';
43
+ }
44
+ }
45
+ if (false) {
46
+ /** @type {?} */
47
+ UpdateFormValue.prototype.payload;
48
+ }
49
+ class UpdateForm {
50
+ /**
51
+ * @param {?} payload
52
+ */
53
+ constructor(payload) {
54
+ this.payload = payload;
55
+ }
56
+ /**
57
+ * @return {?}
58
+ */
59
+ static get type() {
60
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
61
+ return '[Forms] Update Form';
62
+ }
63
+ }
64
+ if (false) {
65
+ /** @type {?} */
66
+ UpdateForm.prototype.payload;
67
+ }
68
+ class UpdateFormDirty {
69
+ /**
70
+ * @param {?} payload
71
+ */
72
+ constructor(payload) {
73
+ this.payload = payload;
74
+ }
75
+ /**
76
+ * @return {?}
77
+ */
78
+ static get type() {
79
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
80
+ return '[Forms] Update Form Dirty';
81
+ }
82
+ }
83
+ if (false) {
84
+ /** @type {?} */
85
+ UpdateFormDirty.prototype.payload;
86
+ }
87
+ class SetFormDirty {
88
+ /**
89
+ * @param {?} payload
90
+ */
91
+ constructor(payload) {
92
+ this.payload = payload;
93
+ }
94
+ /**
95
+ * @return {?}
96
+ */
97
+ static get type() {
98
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
99
+ return '[Forms] Set Form Dirty';
100
+ }
101
+ }
102
+ if (false) {
103
+ /** @type {?} */
104
+ SetFormDirty.prototype.payload;
105
+ }
106
+ class SetFormPristine {
107
+ /**
108
+ * @param {?} payload
109
+ */
110
+ constructor(payload) {
111
+ this.payload = payload;
112
+ }
113
+ /**
114
+ * @return {?}
115
+ */
116
+ static get type() {
117
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
118
+ return '[Forms] Set Form Pristine';
119
+ }
120
+ }
121
+ if (false) {
122
+ /** @type {?} */
123
+ SetFormPristine.prototype.payload;
124
+ }
125
+ class UpdateFormErrors {
126
+ /**
127
+ * @param {?} payload
128
+ */
129
+ constructor(payload) {
130
+ this.payload = payload;
131
+ }
132
+ /**
133
+ * @return {?}
134
+ */
135
+ static get type() {
136
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
137
+ return '[Forms] Update Form Errors';
138
+ }
139
+ }
140
+ if (false) {
141
+ /** @type {?} */
142
+ UpdateFormErrors.prototype.payload;
143
+ }
144
+ class SetFormDisabled {
145
+ /**
146
+ * @param {?} payload
147
+ */
148
+ constructor(payload) {
149
+ this.payload = payload;
150
+ }
151
+ /**
152
+ * @return {?}
153
+ */
154
+ static get type() {
155
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
156
+ return '[Forms] Set Form Disabled';
157
+ }
158
+ }
159
+ if (false) {
160
+ /** @type {?} */
161
+ SetFormDisabled.prototype.payload;
162
+ }
163
+ class SetFormEnabled {
164
+ /**
165
+ * @param {?} payload
166
+ */
167
+ constructor(payload) {
168
+ this.payload = payload;
169
+ }
170
+ /**
171
+ * @return {?}
172
+ */
173
+ static get type() {
174
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
175
+ return '[Forms] Set Form Enabled';
176
+ }
177
+ }
178
+ if (false) {
179
+ /** @type {?} */
180
+ SetFormEnabled.prototype.payload;
181
+ }
182
+ class ResetForm {
183
+ /**
184
+ * @param {?} payload
185
+ */
186
+ constructor(payload) {
187
+ this.payload = payload;
188
+ }
189
+ /**
190
+ * @return {?}
191
+ */
192
+ static get type() {
193
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
194
+ return '[Forms] Reset Form';
195
+ }
196
+ }
197
+ if (false) {
198
+ /** @type {?} */
199
+ ResetForm.prototype.payload;
200
200
  }
201
201
 
202
- /**
203
- * @fileoverview added by tsickle
204
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
205
- */
206
- class NgxsFormPlugin {
207
- /**
208
- * @param {?} state
209
- * @param {?} event
210
- * @param {?} next
211
- * @return {?}
212
- */
213
- handle(state, event, next) {
214
- /** @type {?} */
215
- const type = getActionTypeFromInstance(event);
216
- /** @type {?} */
217
- let nextState = state;
218
- if (type === UpdateFormValue.type || type === UpdateForm.type || type === ResetForm.type) {
219
- const { value } = event.payload;
220
- /** @type {?} */
221
- const payloadValue = Array.isArray(value) ? value.slice() : Object.assign({}, value);
222
- /** @type {?} */
223
- const path = this.joinPathWithPropertyPath(event);
224
- nextState = setValue(nextState, path, payloadValue);
225
- }
226
- if (type === ResetForm.type) {
227
- /** @type {?} */
228
- const model = getValue(nextState, `${event.payload.path}.model`);
229
- nextState = setValue(nextState, `${event.payload.path}`, { model: model });
230
- }
231
- if (type === UpdateFormStatus.type || type === UpdateForm.type) {
232
- nextState = setValue(nextState, `${event.payload.path}.status`, event.payload.status);
233
- }
234
- if (type === UpdateFormErrors.type || type === UpdateForm.type) {
235
- nextState = setValue(nextState, `${event.payload.path}.errors`, Object.assign({}, event.payload.errors));
236
- }
237
- if (type === UpdateFormDirty.type || type === UpdateForm.type) {
238
- nextState = setValue(nextState, `${event.payload.path}.dirty`, event.payload.dirty);
239
- }
240
- if (type === SetFormDirty.type) {
241
- nextState = setValue(nextState, `${event.payload}.dirty`, true);
242
- }
243
- if (type === SetFormPristine.type) {
244
- nextState = setValue(nextState, `${event.payload}.dirty`, false);
245
- }
246
- if (type === SetFormDisabled.type) {
247
- nextState = setValue(nextState, `${event.payload}.disabled`, true);
248
- }
249
- if (type === SetFormEnabled.type) {
250
- nextState = setValue(nextState, `${event.payload}.disabled`, false);
251
- }
252
- return next(nextState, event);
253
- }
254
- /**
255
- * @private
256
- * @param {?} __0
257
- * @return {?}
258
- */
259
- joinPathWithPropertyPath({ payload }) {
260
- /** @type {?} */
261
- let path = `${payload.path}.model`;
262
- if (payload.propertyPath) {
263
- path += `.${payload.propertyPath}`;
264
- }
265
- return path;
266
- }
202
+ /**
203
+ * @fileoverview added by tsickle
204
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
205
+ */
206
+ class NgxsFormPlugin {
207
+ /**
208
+ * @param {?} state
209
+ * @param {?} event
210
+ * @param {?} next
211
+ * @return {?}
212
+ */
213
+ handle(state, event, next) {
214
+ /** @type {?} */
215
+ const type = getActionTypeFromInstance(event);
216
+ /** @type {?} */
217
+ let nextState = state;
218
+ if (type === UpdateFormValue.type || type === UpdateForm.type || type === ResetForm.type) {
219
+ const { value } = event.payload;
220
+ /** @type {?} */
221
+ const payloadValue = Array.isArray(value)
222
+ ? value.slice()
223
+ : isObjectLike(value)
224
+ ? Object.assign({}, value) : value;
225
+ /** @type {?} */
226
+ const path = this.joinPathWithPropertyPath(event);
227
+ nextState = setValue(nextState, path, payloadValue);
228
+ }
229
+ if (type === ResetForm.type) {
230
+ /** @type {?} */
231
+ const model = getValue(nextState, `${event.payload.path}.model`);
232
+ nextState = setValue(nextState, `${event.payload.path}`, { model: model });
233
+ }
234
+ if (type === UpdateFormStatus.type || type === UpdateForm.type) {
235
+ nextState = setValue(nextState, `${event.payload.path}.status`, event.payload.status);
236
+ }
237
+ if (type === UpdateFormErrors.type || type === UpdateForm.type) {
238
+ nextState = setValue(nextState, `${event.payload.path}.errors`, Object.assign({}, event.payload.errors));
239
+ }
240
+ if (type === UpdateFormDirty.type || type === UpdateForm.type) {
241
+ nextState = setValue(nextState, `${event.payload.path}.dirty`, event.payload.dirty);
242
+ }
243
+ if (type === SetFormDirty.type) {
244
+ nextState = setValue(nextState, `${event.payload}.dirty`, true);
245
+ }
246
+ if (type === SetFormPristine.type) {
247
+ nextState = setValue(nextState, `${event.payload}.dirty`, false);
248
+ }
249
+ if (type === SetFormDisabled.type) {
250
+ nextState = setValue(nextState, `${event.payload}.disabled`, true);
251
+ }
252
+ if (type === SetFormEnabled.type) {
253
+ nextState = setValue(nextState, `${event.payload}.disabled`, false);
254
+ }
255
+ return next(nextState, event);
256
+ }
257
+ /**
258
+ * @private
259
+ * @param {?} __0
260
+ * @return {?}
261
+ */
262
+ joinPathWithPropertyPath({ payload }) {
263
+ /** @type {?} */
264
+ let path = `${payload.path}.model`;
265
+ if (payload.propertyPath) {
266
+ path += `.${payload.propertyPath}`;
267
+ }
268
+ return path;
269
+ }
270
+ }
271
+ NgxsFormPlugin.decorators = [
272
+ { type: Injectable }
273
+ ];
274
+ /**
275
+ * @param {?} target
276
+ * @return {?}
277
+ */
278
+ function isObjectLike(target) {
279
+ return target !== null && typeof target === 'object';
267
280
  }
268
- NgxsFormPlugin.decorators = [
269
- { type: Injectable }
270
- ];
271
281
 
272
- /**
273
- * @fileoverview added by tsickle
274
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
275
- */
276
- class FormDirective {
277
- /**
278
- * @param {?} _actions$
279
- * @param {?} _store
280
- * @param {?} _formGroupDirective
281
- * @param {?} _cd
282
- */
283
- constructor(_actions$, _store, _formGroupDirective, _cd) {
284
- this._actions$ = _actions$;
285
- this._store = _store;
286
- this._formGroupDirective = _formGroupDirective;
287
- this._cd = _cd;
288
- this.path = (/** @type {?} */ (null));
289
- this.debounce = 100;
290
- this._clearDestroy = false;
291
- this._destroy$ = new Subject();
292
- this._updating = false;
293
- }
294
- /**
295
- * @param {?} val
296
- * @return {?}
297
- */
298
- set clearDestroy(val) {
299
- this._clearDestroy = val != null && `${val}` !== 'false';
300
- }
301
- /**
302
- * @return {?}
303
- */
304
- get clearDestroy() {
305
- return this._clearDestroy;
306
- }
307
- /**
308
- * @return {?}
309
- */
310
- ngOnInit() {
311
- this._actions$
312
- .pipe(ofActionDispatched(ResetForm), filter((/**
313
- * @param {?} action
314
- * @return {?}
315
- */
316
- (action) => action.payload.path === this.path)), takeUntil(this._destroy$))
317
- .subscribe((/**
318
- * @param {?} __0
319
- * @return {?}
320
- */
321
- ({ payload: { value } }) => {
322
- this.form.reset(value);
323
- this.updateFormStateWithRawValue(true);
324
- this._cd.markForCheck();
325
- }));
326
- this.getStateStream(`${this.path}.model`).subscribe((/**
327
- * @param {?} model
328
- * @return {?}
329
- */
330
- model => {
331
- if (this._updating || !model) {
332
- return;
333
- }
334
- this.form.patchValue(model);
335
- this._cd.markForCheck();
336
- }));
337
- this.getStateStream(`${this.path}.dirty`).subscribe((/**
338
- * @param {?} dirty
339
- * @return {?}
340
- */
341
- dirty => {
342
- if (this.form.dirty === dirty || typeof dirty !== 'boolean') {
343
- return;
344
- }
345
- if (dirty) {
346
- this.form.markAsDirty();
347
- }
348
- else {
349
- this.form.markAsPristine();
350
- }
351
- this._cd.markForCheck();
352
- }));
353
- // On first state change, sync form model, status and dirty with state
354
- this._store
355
- .selectOnce((/**
356
- * @param {?} state
357
- * @return {?}
358
- */
359
- state => getValue(state, this.path)))
360
- .subscribe((/**
361
- * @return {?}
362
- */
363
- () => {
364
- this._store.dispatch([
365
- new UpdateFormValue({
366
- path: this.path,
367
- value: this.form.getRawValue()
368
- }),
369
- new UpdateFormStatus({
370
- path: this.path,
371
- status: this.form.status
372
- }),
373
- new UpdateFormDirty({
374
- path: this.path,
375
- dirty: this.form.dirty
376
- })
377
- ]);
378
- }));
379
- this.getStateStream(`${this.path}.disabled`).subscribe((/**
380
- * @param {?} disabled
381
- * @return {?}
382
- */
383
- disabled => {
384
- if (this.form.disabled === disabled || typeof disabled !== 'boolean') {
385
- return;
386
- }
387
- if (disabled) {
388
- this.form.disable();
389
- }
390
- else {
391
- this.form.enable();
392
- }
393
- this._cd.markForCheck();
394
- }));
395
- (/** @type {?} */ (this._formGroupDirective.valueChanges)).pipe(this.debounceChange()).subscribe((/**
396
- * @return {?}
397
- */
398
- () => {
399
- this.updateFormStateWithRawValue();
400
- }));
401
- (/** @type {?} */ (this._formGroupDirective
402
- .statusChanges)).pipe(distinctUntilChanged(), this.debounceChange())
403
- .subscribe((/**
404
- * @param {?} status
405
- * @return {?}
406
- */
407
- (status) => {
408
- this._store.dispatch(new UpdateFormStatus({
409
- status,
410
- path: this.path
411
- }));
412
- }));
413
- }
414
- /**
415
- * @param {?=} withFormStatus
416
- * @return {?}
417
- */
418
- updateFormStateWithRawValue(withFormStatus) {
419
- if (this._updating)
420
- return;
421
- /** @type {?} */
422
- const value = this._formGroupDirective.control.getRawValue();
423
- /** @type {?} */
424
- const actions = [
425
- new UpdateFormValue({
426
- path: this.path,
427
- value
428
- }),
429
- new UpdateFormDirty({
430
- path: this.path,
431
- dirty: this._formGroupDirective.dirty
432
- }),
433
- new UpdateFormErrors({
434
- path: this.path,
435
- errors: this._formGroupDirective.errors
436
- })
437
- ];
438
- if (withFormStatus) {
439
- actions.push(new UpdateFormStatus({
440
- path: this.path,
441
- status: this._formGroupDirective.status
442
- }));
443
- }
444
- this._updating = true;
445
- this._store.dispatch(actions).subscribe({
446
- error: (/**
447
- * @return {?}
448
- */
449
- () => (this._updating = false)),
450
- complete: (/**
451
- * @return {?}
452
- */
453
- () => (this._updating = false))
454
- });
455
- }
456
- /**
457
- * @return {?}
458
- */
459
- ngOnDestroy() {
460
- this._destroy$.next();
461
- this._destroy$.complete();
462
- if (this.clearDestroy) {
463
- this._store.dispatch(new UpdateForm({
464
- path: this.path,
465
- value: null,
466
- dirty: null,
467
- status: null,
468
- errors: null
469
- }));
470
- }
471
- }
472
- /**
473
- * @private
474
- * @return {?}
475
- */
476
- debounceChange() {
477
- /** @type {?} */
478
- const skipDebounceTime = this._formGroupDirective.control.updateOn !== 'change' || this.debounce < 0;
479
- return skipDebounceTime
480
- ? (/**
481
- * @param {?} change
482
- * @return {?}
483
- */
484
- (change) => change.pipe(takeUntil(this._destroy$)))
485
- : (/**
486
- * @param {?} change
487
- * @return {?}
488
- */
489
- (change) => change.pipe(debounceTime(this.debounce), takeUntil(this._destroy$)));
490
- }
491
- /**
492
- * @private
493
- * @return {?}
494
- */
495
- get form() {
496
- return this._formGroupDirective.form;
497
- }
498
- /**
499
- * @private
500
- * @param {?} path
501
- * @return {?}
502
- */
503
- getStateStream(path) {
504
- return this._store.select((/**
505
- * @param {?} state
506
- * @return {?}
507
- */
508
- state => getValue(state, path))).pipe(takeUntil(this._destroy$));
509
- }
510
- }
511
- FormDirective.decorators = [
512
- { type: Directive, args: [{ selector: '[ngxsForm]' },] }
513
- ];
514
- /** @nocollapse */
515
- FormDirective.ctorParameters = () => [
516
- { type: Actions },
517
- { type: Store },
518
- { type: FormGroupDirective },
519
- { type: ChangeDetectorRef }
520
- ];
521
- FormDirective.propDecorators = {
522
- path: [{ type: Input, args: ['ngxsForm',] }],
523
- debounce: [{ type: Input, args: ['ngxsFormDebounce',] }],
524
- clearDestroy: [{ type: Input, args: ['ngxsFormClearOnDestroy',] }]
525
- };
526
- if (false) {
527
- /** @type {?} */
528
- FormDirective.prototype.path;
529
- /** @type {?} */
530
- FormDirective.prototype.debounce;
531
- /** @type {?} */
532
- FormDirective.prototype._clearDestroy;
533
- /**
534
- * @type {?}
535
- * @private
536
- */
537
- FormDirective.prototype._destroy$;
538
- /**
539
- * @type {?}
540
- * @private
541
- */
542
- FormDirective.prototype._updating;
543
- /**
544
- * @type {?}
545
- * @private
546
- */
547
- FormDirective.prototype._actions$;
548
- /**
549
- * @type {?}
550
- * @private
551
- */
552
- FormDirective.prototype._store;
553
- /**
554
- * @type {?}
555
- * @private
556
- */
557
- FormDirective.prototype._formGroupDirective;
558
- /**
559
- * @type {?}
560
- * @private
561
- */
562
- FormDirective.prototype._cd;
282
+ /**
283
+ * @fileoverview added by tsickle
284
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
285
+ */
286
+ class FormDirective {
287
+ /**
288
+ * @param {?} _actions$
289
+ * @param {?} _store
290
+ * @param {?} _formGroupDirective
291
+ * @param {?} _cd
292
+ */
293
+ constructor(_actions$, _store, _formGroupDirective, _cd) {
294
+ this._actions$ = _actions$;
295
+ this._store = _store;
296
+ this._formGroupDirective = _formGroupDirective;
297
+ this._cd = _cd;
298
+ this.path = (/** @type {?} */ (null));
299
+ this.debounce = 100;
300
+ this._clearDestroy = false;
301
+ this._destroy$ = new Subject();
302
+ this._updating = false;
303
+ }
304
+ /**
305
+ * @param {?} val
306
+ * @return {?}
307
+ */
308
+ set clearDestroy(val) {
309
+ this._clearDestroy = val != null && `${val}` !== 'false';
310
+ }
311
+ /**
312
+ * @return {?}
313
+ */
314
+ get clearDestroy() {
315
+ return this._clearDestroy;
316
+ }
317
+ /**
318
+ * @return {?}
319
+ */
320
+ ngOnInit() {
321
+ this._actions$
322
+ .pipe(ofActionDispatched(ResetForm), filter((/**
323
+ * @param {?} action
324
+ * @return {?}
325
+ */
326
+ (action) => action.payload.path === this.path)), takeUntil(this._destroy$))
327
+ .subscribe((/**
328
+ * @param {?} __0
329
+ * @return {?}
330
+ */
331
+ ({ payload: { value } }) => {
332
+ this.form.reset(value);
333
+ this.updateFormStateWithRawValue(true);
334
+ this._cd.markForCheck();
335
+ }));
336
+ this.getStateStream(`${this.path}.model`).subscribe((/**
337
+ * @param {?} model
338
+ * @return {?}
339
+ */
340
+ model => {
341
+ if (this._updating || !model) {
342
+ return;
343
+ }
344
+ this.form.patchValue(model);
345
+ this._cd.markForCheck();
346
+ }));
347
+ this.getStateStream(`${this.path}.dirty`).subscribe((/**
348
+ * @param {?} dirty
349
+ * @return {?}
350
+ */
351
+ dirty => {
352
+ if (this.form.dirty === dirty || typeof dirty !== 'boolean') {
353
+ return;
354
+ }
355
+ if (dirty) {
356
+ this.form.markAsDirty();
357
+ }
358
+ else {
359
+ this.form.markAsPristine();
360
+ }
361
+ this._cd.markForCheck();
362
+ }));
363
+ // On first state change, sync form model, status and dirty with state
364
+ this._store
365
+ .selectOnce((/**
366
+ * @param {?} state
367
+ * @return {?}
368
+ */
369
+ state => getValue(state, this.path)))
370
+ .subscribe((/**
371
+ * @return {?}
372
+ */
373
+ () => {
374
+ this._store.dispatch([
375
+ new UpdateFormValue({
376
+ path: this.path,
377
+ value: this.form.getRawValue()
378
+ }),
379
+ new UpdateFormStatus({
380
+ path: this.path,
381
+ status: this.form.status
382
+ }),
383
+ new UpdateFormDirty({
384
+ path: this.path,
385
+ dirty: this.form.dirty
386
+ })
387
+ ]);
388
+ }));
389
+ this.getStateStream(`${this.path}.disabled`).subscribe((/**
390
+ * @param {?} disabled
391
+ * @return {?}
392
+ */
393
+ disabled => {
394
+ if (this.form.disabled === disabled || typeof disabled !== 'boolean') {
395
+ return;
396
+ }
397
+ if (disabled) {
398
+ this.form.disable();
399
+ }
400
+ else {
401
+ this.form.enable();
402
+ }
403
+ this._cd.markForCheck();
404
+ }));
405
+ (/** @type {?} */ (this._formGroupDirective.valueChanges)).pipe(this.debounceChange()).subscribe((/**
406
+ * @return {?}
407
+ */
408
+ () => {
409
+ this.updateFormStateWithRawValue();
410
+ }));
411
+ (/** @type {?} */ (this._formGroupDirective
412
+ .statusChanges)).pipe(distinctUntilChanged(), this.debounceChange())
413
+ .subscribe((/**
414
+ * @param {?} status
415
+ * @return {?}
416
+ */
417
+ (status) => {
418
+ this._store.dispatch(new UpdateFormStatus({
419
+ status,
420
+ path: this.path
421
+ }));
422
+ }));
423
+ }
424
+ /**
425
+ * @param {?=} withFormStatus
426
+ * @return {?}
427
+ */
428
+ updateFormStateWithRawValue(withFormStatus) {
429
+ if (this._updating)
430
+ return;
431
+ /** @type {?} */
432
+ const value = this._formGroupDirective.control.getRawValue();
433
+ /** @type {?} */
434
+ const actions = [
435
+ new UpdateFormValue({
436
+ path: this.path,
437
+ value
438
+ }),
439
+ new UpdateFormDirty({
440
+ path: this.path,
441
+ dirty: this._formGroupDirective.dirty
442
+ }),
443
+ new UpdateFormErrors({
444
+ path: this.path,
445
+ errors: this._formGroupDirective.errors
446
+ })
447
+ ];
448
+ if (withFormStatus) {
449
+ actions.push(new UpdateFormStatus({
450
+ path: this.path,
451
+ status: this._formGroupDirective.status
452
+ }));
453
+ }
454
+ this._updating = true;
455
+ this._store.dispatch(actions).subscribe({
456
+ error: (/**
457
+ * @return {?}
458
+ */
459
+ () => (this._updating = false)),
460
+ complete: (/**
461
+ * @return {?}
462
+ */
463
+ () => (this._updating = false))
464
+ });
465
+ }
466
+ /**
467
+ * @return {?}
468
+ */
469
+ ngOnDestroy() {
470
+ this._destroy$.next();
471
+ this._destroy$.complete();
472
+ if (this.clearDestroy) {
473
+ this._store.dispatch(new UpdateForm({
474
+ path: this.path,
475
+ value: null,
476
+ dirty: null,
477
+ status: null,
478
+ errors: null
479
+ }));
480
+ }
481
+ }
482
+ /**
483
+ * @private
484
+ * @return {?}
485
+ */
486
+ debounceChange() {
487
+ /** @type {?} */
488
+ const skipDebounceTime = this._formGroupDirective.control.updateOn !== 'change' || this.debounce < 0;
489
+ return skipDebounceTime
490
+ ? (/**
491
+ * @param {?} change
492
+ * @return {?}
493
+ */
494
+ (change) => change.pipe(takeUntil(this._destroy$)))
495
+ : (/**
496
+ * @param {?} change
497
+ * @return {?}
498
+ */
499
+ (change) => change.pipe(debounceTime(this.debounce), takeUntil(this._destroy$)));
500
+ }
501
+ /**
502
+ * @private
503
+ * @return {?}
504
+ */
505
+ get form() {
506
+ return this._formGroupDirective.form;
507
+ }
508
+ /**
509
+ * @private
510
+ * @param {?} path
511
+ * @return {?}
512
+ */
513
+ getStateStream(path) {
514
+ return this._store.select((/**
515
+ * @param {?} state
516
+ * @return {?}
517
+ */
518
+ state => getValue(state, path))).pipe(takeUntil(this._destroy$));
519
+ }
520
+ }
521
+ FormDirective.decorators = [
522
+ { type: Directive, args: [{ selector: '[ngxsForm]' },] }
523
+ ];
524
+ /** @nocollapse */
525
+ FormDirective.ctorParameters = () => [
526
+ { type: Actions },
527
+ { type: Store },
528
+ { type: FormGroupDirective },
529
+ { type: ChangeDetectorRef }
530
+ ];
531
+ FormDirective.propDecorators = {
532
+ path: [{ type: Input, args: ['ngxsForm',] }],
533
+ debounce: [{ type: Input, args: ['ngxsFormDebounce',] }],
534
+ clearDestroy: [{ type: Input, args: ['ngxsFormClearOnDestroy',] }]
535
+ };
536
+ if (false) {
537
+ /** @type {?} */
538
+ FormDirective.prototype.path;
539
+ /** @type {?} */
540
+ FormDirective.prototype.debounce;
541
+ /** @type {?} */
542
+ FormDirective.prototype._clearDestroy;
543
+ /**
544
+ * @type {?}
545
+ * @private
546
+ */
547
+ FormDirective.prototype._destroy$;
548
+ /**
549
+ * @type {?}
550
+ * @private
551
+ */
552
+ FormDirective.prototype._updating;
553
+ /**
554
+ * @type {?}
555
+ * @private
556
+ */
557
+ FormDirective.prototype._actions$;
558
+ /**
559
+ * @type {?}
560
+ * @private
561
+ */
562
+ FormDirective.prototype._store;
563
+ /**
564
+ * @type {?}
565
+ * @private
566
+ */
567
+ FormDirective.prototype._formGroupDirective;
568
+ /**
569
+ * @type {?}
570
+ * @private
571
+ */
572
+ FormDirective.prototype._cd;
563
573
  }
564
574
 
565
- /**
566
- * @fileoverview added by tsickle
567
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
568
- */
569
- class NgxsFormPluginModule {
570
- /**
571
- * @return {?}
572
- */
573
- static forRoot() {
574
- return {
575
- ngModule: NgxsFormPluginModule,
576
- providers: [
577
- {
578
- provide: NGXS_PLUGINS,
579
- useClass: NgxsFormPlugin,
580
- multi: true
581
- }
582
- ]
583
- };
584
- }
585
- }
586
- NgxsFormPluginModule.decorators = [
587
- { type: NgModule, args: [{
588
- imports: [ReactiveFormsModule],
589
- declarations: [FormDirective],
590
- exports: [FormDirective]
591
- },] }
575
+ /**
576
+ * @fileoverview added by tsickle
577
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
578
+ */
579
+ class NgxsFormPluginModule {
580
+ /**
581
+ * @return {?}
582
+ */
583
+ static forRoot() {
584
+ return {
585
+ ngModule: NgxsFormPluginModule,
586
+ providers: [
587
+ {
588
+ provide: NGXS_PLUGINS,
589
+ useClass: NgxsFormPlugin,
590
+ multi: true
591
+ }
592
+ ]
593
+ };
594
+ }
595
+ }
596
+ NgxsFormPluginModule.decorators = [
597
+ { type: NgModule, args: [{
598
+ imports: [ReactiveFormsModule],
599
+ declarations: [FormDirective],
600
+ exports: [FormDirective]
601
+ },] }
592
602
  ];
593
603
 
594
- /**
595
- * @fileoverview added by tsickle
596
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
604
+ /**
605
+ * @fileoverview added by tsickle
606
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
597
607
  */
598
608
 
599
- /**
600
- * @fileoverview added by tsickle
601
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
609
+ /**
610
+ * @fileoverview added by tsickle
611
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
602
612
  */
603
613
 
604
- /**
605
- * @fileoverview added by tsickle
606
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
614
+ /**
615
+ * @fileoverview added by tsickle
616
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
607
617
  */
608
618
 
609
619
  export { NgxsFormPlugin, NgxsFormPluginModule, ResetForm, SetFormDirty, SetFormDisabled, SetFormEnabled, SetFormPristine, UpdateForm, UpdateFormDirty, UpdateFormErrors, UpdateFormStatus, UpdateFormValue, FormDirective as ɵa };