1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import { Component, forwardRef, OnDestroy, ChangeDetectionStrategy } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormBuilder, FormGroup, Validators, FormControl, NG_VALIDATORS } from '@angular/forms';
import { Subscription } from 'rxjs';
import { Router } from '@angular/router';
import { AuthenticationService } from 'src/services/authentification.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NavigationService } from 'src/services/navigation.service';
@Component({
selector: 'app-form-matiere',
templateUrl: './form-matiere.component.html',
styleUrls: ['./form-matiere.component.less'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => FormMatiereComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => FormMatiereComponent),
multi: true,
}
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormMatiereComponent implements ControlValueAccessor, OnDestroy {
FGmatiere: FormGroup;
subscriptions: Subscription[] = [];
public titre = "Ajouter une nouvelle matière";
private idSommaireSelected: number;
public allowSelectedCatego = false;
public televerser = false;
public menu_hz = JSON.parse(localStorage.getItem("menu_hz"));
public category;
public recupIdMongo;
private matiere;
public cacheWait = true;
constructor(
private formBuilder: FormBuilder, private router: Router, public authService: AuthenticationService, private http: HttpClient, private navService: NavigationService
) {
this.FGmatiere = this.formBuilder.group({
listMenuSelected: ['', Validators.required],
});
this.subscriptions.push(
this.FGmatiere.valueChanges.subscribe(value => {
this.onChange(value);
this.onTouched();
})
);
}
/**uniquement matiere */
changeIdSommaire(event) {
this.cacheWait = false;
if (event.isUserInput) {
//let urlSommaire = event.source.value._links.self.href;
this.idSommaireSelected = parseInt(event.source.id.split('-')[2], 10);
this.idSommaireSelected = this.idSommaireSelected + 1// +1 car on commence à 1
this.FGmatiere.controls['listMenuSelected'].setValue(this.idSommaireSelected);
// console.log(JSON.stringify(this.idSommaireSelected));
localStorage.setItem('listMenuSelected',JSON.stringify(this.idSommaireSelected));
}
}
get value() {
return this.FGmatiere.value;
}
set value(value) {
this.FGmatiere.setValue(value);
this.onChange(value);
this.onTouched();
}
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}
onChange: any = () => { };
onTouched: any = () => { };
registerOnChange(fn) {
this.onChange = fn;
}
writeValue(value) {
if (value) {
this.FGmatiere = value;
}
if (value === null) {
this.FGmatiere.reset();
}
}
registerOnTouched(fn) {
this.onTouched = fn;
}
validate(_: FormControl) {
console.log(this.FGmatiere.valid);
return this.FGmatiere.valid ? null : { imbriqueForm: { valid: false, }, };
}
} |
Partager