diff --git a/projects/common-form-elements/src/lib/dynamic-form/dynamic-form.component.ts b/projects/common-form-elements/src/lib/dynamic-form/dynamic-form.component.ts index 7b5828fa850b248b3b7aa3c437937623ec7c2e81..15083c4a446e9c7c8d7309cd760a2d2f8a1318f5 100644 --- a/projects/common-form-elements/src/lib/dynamic-form/dynamic-form.component.ts +++ b/projects/common-form-elements/src/lib/dynamic-form/dynamic-form.component.ts @@ -282,10 +282,10 @@ export class DynamicFormComponent implements OnInit, OnChanges, OnDestroy { validationList.push(this.validateTime.bind(this, element.validations[i].value, element)); break; case 'maxTime': - validationList.push(this.validateMaxTime.bind(this, element.validations[i].value, element)); + validationList.push(this.compareTime.bind(this, element.validations[i].value, element.validations[i].type)); break; case 'minTime': - validationList.push(this.validateMinTime.bind(this, element.validations[i].value, element)); + validationList.push(this.compareTime.bind(this, element.validations[i].value, element.validations[i].type)); break; case 'compare': validationList.push(this.compareFields.bind(this, element.validations[i].criteria)); @@ -357,33 +357,23 @@ export class DynamicFormComponent implements OnInit, OnChanges, OnDestroy { // return moment(control.value, pattern, true).isValid() && control.touched ? null : {time: true}; } - validateMaxTime(maxTimeValue, field, control: AbstractControl): ValidationErrors | null { - if (control.value && maxTimeValue) { - const maxTimeInputTime = control.value.split(':'); - const maxTimeAllowed = maxTimeValue.split(':'); - const maxTimeAllowedInSeconds = (_.parseInt(maxTimeAllowed[0]) * 3600) + - (_.parseInt(maxTimeAllowed[1]) * 60); - const maxTimeInputInSeconds = (_.parseInt(maxTimeInputTime[0]) * 3600) + - (_.parseInt(maxTimeInputTime[1]) * 60); - if (maxTimeInputInSeconds > maxTimeAllowedInSeconds) { - return { maxtime: true }; - } - return null; - } else { - return null; - } - } - - validateMinTime(minTimeValue, field, control: AbstractControl): ValidationErrors | null { - if (control.value && minTimeValue) { + compareTime(timeValue, type, control: AbstractControl): ValidationErrors | null { + if (control.value && timeValue) { const inputTime = control.value.split(':'); - const minTimeRequired = minTimeValue.split(':'); - const minTimeRequiredInSeconds = (_.parseInt(minTimeRequired[0]) * 3600) + - (_.parseInt(minTimeRequired[1]) * 60); + const timeRequired = timeValue.split(':'); + const timeRequiredInSeconds = (_.parseInt(timeRequired[0]) * 3600) + + (_.parseInt(timeRequired[1]) * 60); const inputTimeInSeconds = (_.parseInt(inputTime[0]) * 3600) + (_.parseInt(inputTime[1]) * 60); - if (inputTimeInSeconds < minTimeRequiredInSeconds) { - return { mintime: true }; + if (type === 'maxTime') { + if (inputTimeInSeconds > timeRequiredInSeconds) { + return { maxtime: true }; + } + } + if (type === 'minTime') { + if (inputTimeInSeconds < timeRequiredInSeconds) { + return { mintime: true }; + } } return null; } else { diff --git a/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.html b/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.html index 915d3f84c301ac83b7fa918068b884174245a9c6..1d3b0267b64fa151d2d8facbea1d6dd8ce14d474 100644 --- a/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.html +++ b/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.html @@ -1,26 +1,9 @@ -<!-- <div class="sb-input"> - <label *ngIf="label" [attr.data-title]="field.description ? field.description : null"> {{label}}</label> - - <input (keyup)="onChangeEvent($event)" [class.valid]="formControlRef.valid && - (formControlRef.dirty || formControlRef.touched)" - [class.invalid]="formControlRef.invalid && - (formControlRef.dirty || formControlRef.touched)" class="sb-textbox {{disabled}}" placeholder={{placeholder}} type="text" - [attr.disabled]="disabled ? true : ( depends ? (isDependsInvalid ? true : null) : null )" [(ngModel)]="value"> - <ng-container *ngFor="let validation of validations"> - <div class="cf-error" - *ngIf="(validation.type && (validation.type).toLowerCase() && validation.message && formControlRef.errors && formControlRef.errors[(validation.type).toLowerCase()] && - (formControlRef.dirty || formControlRef.touched)) "> - {{ validation.message }} - </div> - </ng-container> -</div> --> - <div class="sb-input"> <label *ngIf="label" [attr.data-title]="field.description ? field.description : null"> {{label}}</label> <ng-container> <div class="d-flex"> <div> - <input class="p-10 mr-20" list="hours" #hourField name="hour" id="hour" maxlength="2" + <input class="p-10 mr-20" list="hours" #hourField name="hour" maxlength="2" placeholder="{{placeholders[0]}}" (change)="onChangeTimer('hr', $event.target.value)" (keyup)="onChangeTimer('hr', $event.target.value);" @@ -31,7 +14,7 @@ </datalist> </div> <div> - <input class="p-10 mr-20" list="minutes" #minField name="minute" id="minute" maxlength="2" + <input class="p-10 mr-20" list="minutes" #minField name="minute" maxlength="2" placeholder="{{placeholders[1]}}" (change)="onChangeTimer('min', $event.target.value)" (keyup)="onChangeTimer('min', $event.target.value)" diff --git a/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.ts b/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.ts index 8c25df4c8cd70dc096d68a99778a327985da1be7..5f962f169a126e21aab88ed8f8b79b79c7667c3d 100644 --- a/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.ts +++ b/projects/common-form-elements/src/lib/dynamic-timer/dynamic-timer.component.ts @@ -41,8 +41,6 @@ export class DynamicTimerComponent implements OnInit, OnDestroy { @ViewChild('minField') minField: ElementRef; public isDependsInvalid: any; contextValueChangesSubscription?: Subscription; - options$?: Observable<FieldConfigOption<any>[]>; - value: any = null; maxValue: any = []; hourOptions = []; minuteOptions = []; @@ -55,20 +53,6 @@ export class DynamicTimerComponent implements OnInit, OnDestroy { this.getPlaceHolder(); this.findMaxValue(); this.getHourAndMinuteOptions(); - if (!this.options) { - this.options = _.isEmpty(this.field.options) ? this.isOptionsClosure(this.field.options) && this.field.options : []; - } - - if (this.isOptionsClosure(this.options) && !_.isEmpty(this.depends)) { - // tslint:disable-next-line:max-line-length - this.options$ = (this.options as DynamicFieldConfigOptionsBuilder<any>)(this.formControlRef, this.depends, this.formGroup, () => this.dataLoadStatusDelegate.next('LOADING'), () => this.dataLoadStatusDelegate.next('LOADED')) as any; - this.options$.subscribe( - (response) => { - this.dependencyTerms = response; - }, - ); - } - if (!_.isEmpty(this.depends)) { this.contextValueChangesSubscription = merge(..._.map(this.depends, depend => depend.valueChanges)).pipe( tap((value: any) => { @@ -76,15 +60,12 @@ export class DynamicTimerComponent implements OnInit, OnDestroy { this.defaultMin = null; this.hourField.nativeElement.value = null; this.minField.nativeElement.value = null; - // this.value = null; this.formControlRef.patchValue(null); this.isDependsInvalid = _.includes(_.map(this.depends, depend => depend.invalid), true); }) ).subscribe(); this.isDependsInvalid = _.includes(_.map(this.depends, depend => depend.invalid), true); } - - // this.setDefaultValue(); } ngAfterViewInit() { @@ -128,100 +109,19 @@ export class DynamicTimerComponent implements OnInit, OnDestroy { this.defaultMin = defaultTime[1]; this.hourField.nativeElement.value = defaultTime[0]; this.minField.nativeElement.value = defaultTime[1]; - // this.value = this.default; - this.setFormFieldValue(); + this.formControlRef.markAsTouched(); + this.formControlRef.patchValue(this.defaultHr + ':' + this.defaultMin); } } - setFormFieldValue() { - this.formControlRef.markAsTouched(); - this.formControlRef.patchValue(this.defaultHr + ':' + this.defaultMin); - } - - /* - checkValue(str, max) { - if (str.charAt(0) !== '0') { - let num = _.parseInt(str); - if (isNaN(num) || num <= 0 || num > max) { - num = max; - } - console.log(num, max, _.parseInt(max.toString().charAt(0)), num.toString().length); - if (num > _.parseInt(max.toString().charAt(0)) && num.toString().length === 1) { - str = '0' + num; - } else if (num === _.parseInt(max.toString()) && num.toString().length === 1) { - str = '0' + num; - } else { - str = num.toString(); - } - // str = num > _.parseInt(max.toString().charAt(0)) && num.toString().length === 1 ? '0' + num : num.toString(); - } else if (str.charAt(0) === '0' && str.toString().length === 2 && str !== '00' ) { - let num = _.parseInt(str); - if (isNaN(num) || num <= 0 || num > max) { - num = str; - } - console.log(num, max); - str = num > _.parseInt(max.toString().charAt(0)) && num.toString().length === 2 ? '0' + max : '0' + num.toString(); - } else if (str.toString().length > 2) { - let num = _.parseInt(str); - if (isNaN(num) || num <= 0 || num > max) { - num = str; - } - str = num > _.parseInt(max.toString().charAt(0)) && num.toString().length === 2 ? '0' + max : '0' + num.toString(); - } - return str; - } */ - - /* - onChangeEvent(event?: any, type?) { - type = 'text'; - let input = event.target.value; - if (/\D\/$/.test(input)) { input = input.substr(0, input.length - 3); } - - - const values = input.split(':').map((v) => { - return v.replace(/\D/g, ''); - }); - - if (!_.isEmpty(this.maxValue)) { - _.forEach(values, (val, index) => { - if (values[index]) { - values[index] = this.checkValue(val, this.maxValue[index]); - } - }); - const output = values.map((v, i) => { - if (this.maxValue[i] && this.maxValue[i].length && v.length === this.maxValue[i].length && - i <= this.maxValue.length) { - return v + ':'; - } else if (this.maxValue[i] && this.maxValue[i].length && this.maxValue[i].length < v.length) { - return v.substr(0, this.maxValue[i].length); - } else { - return v; - } - }); - this.value = output.join('').substr(0, this.getMaxValueLength()); - } - this.formControlRef.markAsTouched(); - this.formControlRef.patchValue(this.value); - } */ - findMaxValue() { - let maxObj = _.find(this.validations, {type: 'maxtimevalue'}); + let maxObj = _.find(this.validations, {type: 'maxTime'}); maxObj = maxObj && maxObj.value ? maxObj : !_.isEmpty(_.compact(this.dependencyTerms)) ? {type: 'max', value: this.dependencyTerms} : {}; this.maxValue = maxObj && maxObj.value ? maxObj.value.split(':').map((v) => v.replace(/\D/g, '')) : []; } - /* - getMaxValueLength() { - const flattenedArray = this.maxValue.join(':'); - return flattenedArray && flattenedArray.length; - } */ - - isOptionsClosure(options: any) { - return typeof options === 'function'; - } - ngOnDestroy(): void { if (this.contextValueChangesSubscription) { this.contextValueChangesSubscription.unsubscribe();