Skip to content
Snippets Groups Projects
Commit 8a74608b authored by francois.grand's avatar francois.grand
Browse files

ticket #37 : réécriture de BaseParamInputComponent avec GenericInputComponent

parent f27e8821
No related branches found
No related tags found
1 merge request!6Resolve "Amélioration mise en forme"
<div class="md-form form-sm">
<input mdbActive type="text" id="form1" class="form-control" [ngModel]="_uiValue.uncheckedValueString" (ngModelChange)="setValue($event)">
<label for="form1">{{_title}}</label>
<small class="text-danger">{{_message}}</small>
</div>
\ No newline at end of file
...@@ -7,6 +7,7 @@ import { BaseParam, NumericalString, Message, ParamDomain, ParamDomainValue } fr ...@@ -7,6 +7,7 @@ import { BaseParam, NumericalString, Message, ParamDomain, ParamDomainValue } fr
import { InternationalisationService, LanguageCode } from "../../services/internationalisation/internationalisation.service"; import { InternationalisationService, LanguageCode } from "../../services/internationalisation/internationalisation.service";
import { Observable } from "../../services/observer"; import { Observable } from "../../services/observer";
import { GenericInputComponent } from "../generic-input/generic-input.component";
export class NgBaseParam extends Observable { export class NgBaseParam extends Observable {
private _param: BaseParam; private _param: BaseParam;
...@@ -40,179 +41,68 @@ export class NgBaseParam extends Observable { ...@@ -40,179 +41,68 @@ export class NgBaseParam extends Observable {
@Component({ @Component({
selector: "base-param-input", selector: "base-param-input",
templateUrl: "./base-param-input.component.html", templateUrl: "../generic-input/generic-input.component.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => BaseParamInputComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => BaseParamInputComponent),
multi: true
}
]
}) })
export class BaseParamInputComponent implements ControlValueAccessor, DoCheck { export class BaseParamInputComponent extends GenericInputComponent {
/**
* enable/disable input field
*/
@Input('inputDisabled')
private _inputDisabled: boolean;
/** /**
* managed parameter * managed parameter
*/ */
@Input('param') @Input('param')
private _paramDef: NgBaseParam; private _paramDef: NgBaseParam;
@Input('title') constructor(private intlService: InternationalisationService) {
private _title: string; super();
private _message: string;
/**
* flag d'affichage du titre
*/
public displayTitle: boolean = false;
/**
* true si la modification du paramètre géré vient de l'interface utilisateur
* false si la modification du paramètre géré vient d'un appel du code
*/
private _fromUI: boolean;
/**
* valeur dans le contrôle (saisie par l'utilisateur)
*/
private _uiValue: NumericalString;
constructor(private changeDetector: ChangeDetectorRef, private intlService: InternationalisationService) {
this._uiValue = new NumericalString();
}
/**
* fonction appelée lorsque l'utilisateur fait une saisie
* @param event valeur du contrôle
*/
private setValue(event: any) {
this._fromUI = true;
this._uiValue.value = event;
// this.log(this._uiValue.toString());
return this.validateUIValue();
} }
/** protected getModelValue(): any {
* fonction appelée lors d'un rafraîchissement de l'UI return this._paramDef.getValue();
*/
ngDoCheck(): void {
// this.log("ngDoCheck start : " + this.getSParam() + this.getSUIvalue() + this.getSfromUI());
if (this._fromUI)
this.updateMessage(this._uiValue);
else {
if (this._paramDef.isDefined) {
this.updateMessage(new NumericalString(this._paramDef.getValue()));
this._uiValue.value = String(this._paramDef.getValue());
}
else
this.updateMessage(this._uiValue);
}
// this.log("ngDoCheck end : " + this.getSParam() + this.getSUIvalue());
this._fromUI = false;
} }
private updateMessage(v: NumericalString) { protected setModelValue(v: any) {
// this.log("updateMessage start :" + this.getSParam() + this.getSfromUI() + this.getSUIvalue(v) + " message=" + this._message); try {
this._paramDef.setValue(v);
if (v.isNumerical) {
this._message = undefined;
try {
this._paramDef.checkValue(v.numericalValue);
}
catch (e) {
if (e instanceof Message)
this._message = this.intlService.localizeMessage(e);
else
this._message = "invalid value";
}
} }
else { catch (e) {
switch (this.intlService.currentLanguage.code) { // géré par validateModelValue()
case LanguageCode.FRENCH:
this._message = "Veuillez entrer une valeur numérique";
break;
default:
this._message = "Please enter a numerical value";
}
} }
// this.log("updateMessage end :" + this.getSParam() + this.getSfromUI() + this.getSUIvalue(v) + " message=" + this._message);
} }
private validateUIValue() { protected validateModelValue(v: any): { isValid: boolean, message: string } {
// this.log(""); let msg = undefined;
// this.log("validateValue start : val '" + this._uiValue.toString() + "'" + this.getSParam() + this.getSfromUI()); let valid = false;
let ok: boolean = this._uiValue.isNumerical;
if (ok) {
try {
if (!this._paramDef.isDefined || this._paramDef.getValue() != this._uiValue.numericalValue) {
this._paramDef.setValue(this._uiValue.numericalValue);
this.changeDetector.detectChanges(); // provoque une détection des changements dans les contrôles
}
}
catch (e) {
ok = false;
}
}
if (!ok) { try {
// this.log("validateValue end : " + this.getSParam()); this._paramDef.checkValue(v);
valid = true;
let err = { }
rangeError: { catch (e) {
// given: val, if (e instanceof Message)
given: this._uiValue.toString(), msg = this.intlService.localizeMessage(e);
max: 4, else
min: 0 msg = "invalid value";
}
};
return err;
} }
// this.log("validateValue end : " + this.getSParam()); return { isValid: valid, message: msg };
return null;
} }
// private log(m: string) { protected modelToUI(v: any): string {
// console.log("ParamInputComponent(" + this._id + ") : " + m); return String(v);
// } }
// ControlValueAccessor interface protected validateUIValue(ui: string): { isValid: boolean, message: string } {
let valid: boolean = false;
let msg: string = undefined;
propagateChange = (_: any) => { }; let v: NumericalString = new NumericalString(ui);
if (!v.isNumerical)
msg = "Veuillez entrer une valeur numérique";
else
valid = true;
/* return { isValid: valid, message: msg };
//From ControlValueAccessor interface
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
*/
writeValue(value: any) {
// this.log("writeValue " + value);
} }
registerOnChange(fn: any) { protected uiToModel(ui: string) {
this.propagateChange = fn; return +ui;
} }
registerOnTouched() { }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment