diff --git a/src/lib/dialog/base-dialog.component.ts b/src/lib/dialog/base-dialog.component.ts index a361d0f..783c223 100644 --- a/src/lib/dialog/base-dialog.component.ts +++ b/src/lib/dialog/base-dialog.component.ts @@ -1,8 +1,7 @@ import { Directive, HostListener, Injector, OnInit } from '@angular/core'; import { MatDialogRef } from '@angular/material/dialog'; import { FormGroup } from '@angular/forms'; -import * as moment from 'moment'; -import { AutoUnsubscribe, IqserEventTarget } from '../utils'; +import { AutoUnsubscribe, hasFormChanged, IqserEventTarget } from '../utils'; import { ConfirmOptions } from '../misc'; import { ConfirmationDialogService } from './confirmation-dialog.service'; @@ -47,34 +46,7 @@ export abstract class BaseDialogComponent extends AutoUnsubscribe implements OnI } get changed(): boolean { - for (const key of Object.keys(this.form.getRawValue())) { - const initialValue = this.initialFormValue[key]; - const updatedValue = this.form.get(key).value; - - if (initialValue == null && updatedValue != null) { - const updatedValueType = typeof updatedValue; - if (updatedValueType !== 'string' && updatedValueType !== 'boolean') { - return true; - } else if (updatedValueType === 'string' && updatedValue.length > 0) { - return true; - } else if (updatedValueType === 'boolean' && updatedValue === true) { - return true; - } - } else if (initialValue !== updatedValue) { - if (Array.isArray(updatedValue)) { - if (JSON.stringify(initialValue) !== JSON.stringify(updatedValue)) { - return true; - } - } else if (updatedValue instanceof moment) { - if (!moment(updatedValue).isSame(moment(initialValue))) { - return true; - } - } else { - return true; - } - } - } - return false; + return hasFormChanged(this.form, this.initialFormValue); } get disabled(): boolean { diff --git a/src/lib/form/base-form.component.ts b/src/lib/form/base-form.component.ts new file mode 100644 index 0000000..4ae779e --- /dev/null +++ b/src/lib/form/base-form.component.ts @@ -0,0 +1,14 @@ +import { Directive } from '@angular/core'; +import { FormGroup } from '@angular/forms'; +import { hasFormChanged } from '@iqser/common-ui'; + +@Directive() +export class BaseFormComponent { + + form!: FormGroup; + initialFormValue; + + get changed() { + return hasFormChanged(this.form, this.initialFormValue); + } +} diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index 302c4d7..133feb4 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -2,6 +2,7 @@ import { tap } from 'rxjs/operators'; import { ITrackable } from '../listing/models/trackable'; import { MonoTypeOperatorFunction } from 'rxjs'; import moment from 'moment'; +import { FormGroup } from '@angular/forms'; export function capitalize(value: string): string { if (!value) { @@ -34,3 +35,35 @@ export function toNumber(str: string): number { export function trackBy() { return (index: number, item: T): string => item.id; } + +export function hasFormChanged(form: FormGroup, initialFormValue): boolean { + + for (const key of Object.keys(form.getRawValue())) { + const initialValue = initialFormValue[key]; + const updatedValue = form.get(key).value; + + if (initialValue == null && updatedValue != null) { + const updatedValueType = typeof updatedValue; + if (updatedValueType !== 'string' && updatedValueType !== 'boolean') { + return true; + } else if (updatedValueType === 'string' && updatedValue.length > 0) { + return true; + } else if (updatedValueType === 'boolean' && updatedValue === true) { + return true; + } + } else if (initialValue !== updatedValue) { + if (Array.isArray(updatedValue)) { + if (JSON.stringify(initialValue) !== JSON.stringify(updatedValue)) { + return true; + } + } else if (updatedValue instanceof moment) { + if (!moment(updatedValue).isSame(moment(initialValue))) { + return true; + } + } else { + return true; + } + } + } + return false; +}