@firestitch/form 18.0.13 → 18.0.15

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.
@@ -11,8 +11,8 @@ import * as i1 from '@angular/material/dialog';
11
11
  import { MAT_DIALOG_DATA, MatDialogModule, MatDialog, MatDialogRef } from '@angular/material/dialog';
12
12
  import * as i2 from '@firestitch/dialog';
13
13
  import { FsDialogModule } from '@firestitch/dialog';
14
- import { Subject, of, filter, BehaviorSubject, throwError, fromEvent, iif, defer, from, isObservable } from 'rxjs';
15
- import { switchMap, map, catchError, takeUntil, tap, take, mergeMap, filter as filter$1, delay, first, startWith, mapTo } from 'rxjs/operators';
14
+ import { Subject, of, BehaviorSubject, throwError, fromEvent, iif, defer, from, isObservable } from 'rxjs';
15
+ import { switchMap, map, catchError, takeUntil, filter, delay, tap, take, mergeMap, first, startWith, mapTo } from 'rxjs/operators';
16
16
  import * as i2$1 from '@angular/router';
17
17
  import { guid, email, isEmpty, isNumeric, phone, url } from '@firestitch/common';
18
18
  import * as i7 from '@firestitch/drawer';
@@ -77,11 +77,13 @@ var ConfirmResult;
77
77
  ConfirmResult["Discard"] = "discard";
78
78
  ConfirmResult["Review"] = "review";
79
79
  ConfirmResult["Invalid"] = "invalid";
80
- ConfirmResult["Pristine"] = "pristine";
80
+ ConfirmResult["NoChanges"] = "noChanges";
81
81
  })(ConfirmResult || (ConfirmResult = {}));
82
82
 
83
83
  function confirmResultContinue(result) {
84
- return result === ConfirmResult.Discard || result === ConfirmResult.Save || result === ConfirmResult.Pristine;
84
+ return (result === ConfirmResult.Discard ||
85
+ result === ConfirmResult.Save ||
86
+ result === ConfirmResult.NoChanges);
85
87
  }
86
88
 
87
89
  function getActiveRoute(route) {
@@ -118,15 +120,24 @@ class FsForm {
118
120
  removeFormDirective(routeComponent) {
119
121
  this._formDirectiveStore.delete(routeComponent);
120
122
  }
123
+ hasChanges(form) {
124
+ return Object.keys(form.ngForm.control.controls)
125
+ .some((key) => {
126
+ const control = form.ngForm.control.controls[key];
127
+ return (control.dirty ||
128
+ (control.errors.required &&
129
+ (control.value === '' || control.value === null || control.value === undefined)));
130
+ });
131
+ }
121
132
  confirmUnsaved(form) {
122
- if (!form.confirm || !form.ngForm.dirty) {
123
- return of(ConfirmResult.Pristine);
133
+ if (!form.confirm || !this.hasChanges(form)) {
134
+ return of(ConfirmResult.NoChanges);
124
135
  }
125
- let title = 'You Have Unsaved Changes';
136
+ let title = 'You have unsaved changes';
126
137
  let message = 'What would you like to do with your changes?';
127
- let saveLabel = 'Save & Continue';
128
- let discardLabel = 'Discard Changes & Continue';
129
- let cancelLabel = 'Review Changes';
138
+ let saveLabel = 'Save and continue';
139
+ let discardLabel = 'Discard changes and continue';
140
+ let cancelLabel = 'Review changes';
130
141
  if (typeof form.confirm === 'object') {
131
142
  title = form.confirm.title || title;
132
143
  message = form.confirm.message || message;
@@ -134,7 +145,8 @@ class FsForm {
134
145
  discardLabel = form.confirm.discardLabel || discardLabel;
135
146
  cancelLabel = form.confirm.cancelLabel || cancelLabel;
136
147
  }
137
- return this._dialog.open(ConfirmUnsavedComponent, {
148
+ return this._dialog
149
+ .open(ConfirmUnsavedComponent, {
138
150
  data: {
139
151
  title,
140
152
  message,
@@ -156,7 +168,12 @@ class FsForm {
156
168
  if (result === 'save') {
157
169
  form.ngForm.control.markAsPristine();
158
170
  return form.submit$({ confirmed: true })
159
- .pipe(map(() => ConfirmResult.Save), catchError(() => {
171
+ .pipe(map((submitEvent) => {
172
+ if (submitEvent.error) {
173
+ return ConfirmResult.Invalid;
174
+ }
175
+ return ConfirmResult.Save;
176
+ }), catchError(() => {
160
177
  return of(ConfirmResult.Invalid);
161
178
  }));
162
179
  }
@@ -237,6 +254,8 @@ class FsFormBaseDirective {
237
254
  _tabGroups = new QueryList();
238
255
  _buttons = new QueryList();
239
256
  _activeSubmitButton;
257
+ _currentTabIndex;
258
+ _tabConfirming;
240
259
  ngAfterContentInit() {
241
260
  this._registerConfirmTabs();
242
261
  }
@@ -286,31 +305,28 @@ class FsFormBaseDirective {
286
305
  });
287
306
  }
288
307
  _registerConfirmTabGroup(tabGroup) {
289
- const confirmTabGroup = tabGroup;
290
- if (!confirmTabGroup._originalHandleClick) {
291
- confirmTabGroup._originalHandleClick = tabGroup._handleClick;
292
- confirmTabGroup._handlClick$ = new Subject();
293
- confirmTabGroup._handleClick = (tab, tabHeader, idx) => {
294
- if (confirmTabGroup._handlClick$.observers.length) {
295
- confirmTabGroup._handlClick$.next({ tab, tabHeader, idx });
296
- }
297
- else {
298
- confirmTabGroup._originalHandleClick(tab, tabHeader, idx);
299
- }
300
- };
301
- }
302
- confirmTabGroup._handlClick$
303
- .pipe(filter(() => !this.submitting), switchMap((event) => {
308
+ this._currentTabIndex = tabGroup.selectedIndex;
309
+ tabGroup.selectedIndexChange
310
+ .pipe(filter(() => !this._tabConfirming), switchMap((selectedIndex) => {
304
311
  if (this.confirm && this.confirmTabs) {
312
+ tabGroup.selectedIndex = this._currentTabIndex;
313
+ this._tabConfirming = true;
305
314
  return this.triggerConfirm()
306
- .pipe(tap((result) => {
315
+ .pipe(map((result) => {
307
316
  if (confirmResultContinue(result)) {
308
- confirmTabGroup.selectedIndex = event.idx;
317
+ tabGroup.selectedIndex = selectedIndex;
318
+ return selectedIndex;
309
319
  }
320
+ return null;
321
+ }), delay(50), tap(() => {
322
+ this._tabConfirming = false;
310
323
  }));
311
324
  }
312
- confirmTabGroup._originalHandleClick(event.tab, event.tabHeader, event.idx);
313
- return of(null);
325
+ return of(selectedIndex);
326
+ }), tap((selectedIndex) => {
327
+ if (selectedIndex !== null) {
328
+ this._currentTabIndex = selectedIndex;
329
+ }
314
330
  }), takeUntil(this._destroy$))
315
331
  .subscribe();
316
332
  }
@@ -571,9 +587,12 @@ class FsFormDirective extends FsFormBaseDirective {
571
587
  return this._formValidState$;
572
588
  }), catchError((e) => {
573
589
  this._handleError(e);
574
- return of(null);
590
+ return of({
591
+ ...this._submitEvent,
592
+ error: e.message,
593
+ });
575
594
  }), tap((submittedEvent) => {
576
- if (submittedEvent) {
595
+ if (!submittedEvent.error) {
577
596
  this._completeSubmit(true, submittedEvent);
578
597
  }
579
598
  }));
@@ -584,7 +603,7 @@ class FsFormDirective extends FsFormBaseDirective {
584
603
  .ngSubmit
585
604
  .pipe(tap((event) => {
586
605
  event?.preventDefault();
587
- }), filter$1(() => {
606
+ }), filter(() => {
588
607
  return [FormStatus.Valid, FormStatus.Invalid]
589
608
  .includes(this._status$.getValue());
590
609
  }), switchMap(() => this.submit$(this._submitEvent)), takeUntil(this.destroy$))
@@ -650,8 +669,8 @@ class FsFormDirective extends FsFormBaseDirective {
650
669
  _formClose() {
651
670
  if (this.confirm && this.confirmDialog) {
652
671
  this.triggerConfirm()
653
- .pipe(filter$1((result) => confirmResultContinue(result)), switchMap((result) => {
654
- return result === ConfirmResult.Pristine || result === ConfirmResult.Discard
672
+ .pipe(filter((result) => confirmResultContinue(result)), switchMap((result) => {
673
+ return result === ConfirmResult.NoChanges || result === ConfirmResult.Discard
655
674
  ? of(null)
656
675
  : this.submitted.asObservable();
657
676
  }), takeUntil(this.destroy$))
@@ -754,7 +773,7 @@ class FsFormDirective extends FsFormBaseDirective {
754
773
  .subscribe((subscriber) => {
755
774
  if (this.submitting) {
756
775
  this._status$
757
- .pipe(filter$1((status) => status === FormStatus.Success || status === FormStatus.Error), takeUntil(this.destroy$))
776
+ .pipe(filter((status) => status === FormStatus.Success || status === FormStatus.Error), takeUntil(this.destroy$))
758
777
  .subscribe((status) => {
759
778
  if (status === FormStatus.Success) {
760
779
  subscriber.next(null);
@@ -991,7 +1010,7 @@ class FsFormDialogCloseDirective {
991
1010
  closeClick() {
992
1011
  if (this._form) {
993
1012
  this._form.triggerConfirm()
994
- .pipe(filter$1((confirmResult) => (confirmResult !== ConfirmResult.Review)), takeUntil(this._destroy$))
1013
+ .pipe(filter((confirmResult) => (confirmResult !== ConfirmResult.Review)), takeUntil(this._destroy$))
995
1014
  .subscribe(() => {
996
1015
  this._dialogRef.close(this.closeData);
997
1016
  });
@@ -1182,7 +1201,7 @@ class FsFormDialogActionsComponent {
1182
1201
  ngOnInit() {
1183
1202
  if (this._form) {
1184
1203
  this._form.ngForm.valueChanges
1185
- .pipe(filter$1(() => (!this.dirty)), takeUntil(this._destroy$))
1204
+ .pipe(filter(() => (!this.dirty)), takeUntil(this._destroy$))
1186
1205
  .subscribe(() => {
1187
1206
  this.dirty = this._form.ngForm.dirty;
1188
1207
  this._cdRef.markForCheck();