[DEV] setting form enable require mode

This commit is contained in:
Edouard DUPIN 2023-02-10 00:13:37 +01:00
parent 1e5f3541bf
commit 62c92ebe5d
2 changed files with 29 additions and 17 deletions

View File

@ -12,7 +12,7 @@
<div class="elem-description">{{elem.description}}</div>
</div>
<div *ngIf="elem.type === 'STRING'">
<div class="elem-title">{{elem.title}}:</div>
<div class="elem-title">{{elem.title}}</div>
<div class="elem-description">{{elem.description}}</div>
<div class="elem-input">
<app-entry

View File

@ -3,8 +3,8 @@
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { isBoolean, isInArray, isNullOrUndefined, isNumber, isObject, isOptionalArrayOf, isOptionalOf, isString, isUndefined } from 'common/utils';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { isBoolean, isInArray, isNullOrUndefined, isNumber, isObject, isOptionalOf, isString, isUndefined } from 'common/utils';
export type ReturnFunction = (a: boolean|string) => void;
@ -42,6 +42,8 @@ export interface SettingsItem {
checker?: CheckerParameter
// result of the checker (must be set @ undefined):
state?: boolean | string;
// The element is require to have a valid form.
require?: boolean
}
export function isSettingsItem(data: any): data is SettingsItem {
@ -76,7 +78,10 @@ export function isSettingsItem(data: any): data is SettingsItem {
if (!isUndefined(data.newValue)) {
return false;
}
if (!isUndefined(data.state)) {
if (!isOptionalOf(data.state, isString)) {
return false;
}
if (!isOptionalOf(data.require, isBoolean)) {
return false;
}
return true;
@ -88,7 +93,7 @@ export function isSettingsItem(data: any): data is SettingsItem {
templateUrl: 'render-settings.html',
styleUrls: ['render-settings.less'],
})
export class RenderSettingsComponent implements OnInit {
export class RenderSettingsComponent {
/// Value of the password
@Input() values: SettingsItem[] = [];
/// Number of error detected (undefined: No change or not ready, else the number of error)
@ -96,14 +101,6 @@ export class RenderSettingsComponent implements OnInit {
/// event with the changed values
@Output() deltaValues: EventEmitter<any> = new EventEmitter();
constructor(
) {
}
ngOnInit(): void {
//if (value)
}
createOutputValues(): any {
let out = {};
//console.log(" Create values ... out ... ");
@ -123,13 +120,23 @@ export class RenderSettingsComponent implements OnInit {
countErrors(): number | undefined {
let out = 0;
this.values.forEach( (value) => {
if (value.state !== undefined) {
if (!isUndefined(value.state)) {
out++;
}
});
return out;
}
checkMissing(): boolean | undefined {
let error = 0;
this.values.forEach( (value) => {
if (value.require === true && value.value === "" && isUndefined(value.newValue) ) {
error++;
}
});
return error !== 0;
}
checkParameter(newValue: string, item: SettingsItem): void {
if (!isNullOrUndefined(item.checker)) {
item.state = item.checker(newValue);
@ -139,6 +146,7 @@ export class RenderSettingsComponent implements OnInit {
} else {
item.newValue = newValue;
}
const missing = this.checkMissing();
const outValue = this.createOutputValues();
const nbError = this.countErrors();
const nbValuesChanges = Object.keys(outValue).length;
@ -146,10 +154,14 @@ export class RenderSettingsComponent implements OnInit {
//console.log(`nbError=${nbError} nbValuesChanges=${nbValuesChanges}`);
if (nbValuesChanges === 0) {
this.changeState.emit(undefined);
} else {
this.changeState.emit(nbError);
return;
}
if (missing === true) {
this.changeState.emit(undefined);
return;
}
if (nbError === 0 && nbValuesChanges !== 0) {
this.changeState.emit(nbError);
if (nbError === 0) {
this.deltaValues.emit(outValue);
}
}