added base form component, extracted changed method from base dialog component into separated function in utils

This commit is contained in:
Valentin 2022-01-23 15:59:11 +02:00
parent 2e36f3cac7
commit 867d7b089e
3 changed files with 49 additions and 30 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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<T extends ITrackable>() {
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;
}