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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
import { Component } from '@angular/core';
import { Observable, of } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, startWith, switchMap, tap } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class HomeComponent {
containerForm: FormGroup;
filteredImages: any;
filteredImageTag: any;
images = of([
{
"name": "tomcat-image-1",
"createdDate": "2020-01-10 10:25:24"
},
{
"name": "nginx-image-2",
"createdDate": "2020-01-12 10:45:24"
}
]);
imageTag1 = of([
{
"tag": "120.5"
},
{
"tag": "119.5"
}
]);
imageTag2 = of([
{
"tag": "220.5"
},
{
"tag": "219.5"
}
]);
getImages(): any {
return this.images;
}
getImageTag(): any {
console.log('getImageTag')
if (this.containerForm.get('image').value == 'tomcat-image-1') {
return this.imageTag1;
}
return this.imageTag2;
}
constructor(private formBuilder: FormBuilder, private route: ActivatedRoute){}
ngOnInit() {
// Création du formulaire de création de conteneur
this.containerForm = this.formBuilder.group({
name: new FormControl('', Validators.compose([Validators.required, Validators.minLength(10)])),
image: new FormControl('', Validators.required),
imageTag: new FormControl('', Validators.required),
});
this.route.params.subscribe(params => {
if(params['image']){
this.containerForm.controls.image.patchValue(params['image']);
}
});
// Filtrage des images récupérées depuis l'API
this.filteredImages = this.containerForm.controls.image.valueChanges.pipe(
startWith(''),
debounceTime(400),
distinctUntilChanged(),
switchMap(val => {
return this.filterImage(val || '')
}),
tap(v => this.containerForm.controls['imageTag'].setValue(''))
)
}
// Recherche des tags d'une image auparavant selectionnée
searchTagOfImage(): void{
console.log("searchTagOfImage");
// if(this.image.value.length > 2){
this.filteredImageTag = this.containerForm.controls.imageTag.valueChanges.pipe(
startWith(''),
debounceTime(400),
distinctUntilChanged(),
switchMap((val: string) => {
return this.filterImageTag(val )
})
)
// }
}
// Récupération de toutes les images utilisables
filterImage(val: string): Observable<any[]> {
console.log("filterImage");
return this.getImages()
.pipe(
tap( v => console.log(v)),
map((response: any) => response.filter(myImage => {
console.log(JSON.stringify(myImage));
return myImage.name.toLowerCase().indexOf(val.toLowerCase()) === 0
}))
)
}
// Filtrage des tags d'une image
filterImageTag(val: string): Observable<any[]> {
console.log("filterImageTag");
// if(this.image.value.length > 2){ // do not search tag if value has been delete by clicking on delete button
return this.getImageTag()
.pipe(
map((response: any) => response.filter(myImageTag => {
return myImageTag.tag.toLowerCase().indexOf(val.toLowerCase()) === 0
}))
)
// }
}
displayFn(image: any): string {
return image?.name || '';
}
displayFn2(user: any): string {
return user?.tag || '';
}
deleteVariable(variable: string, e?, i?): void {
switch (variable) {
case 'image':
console.log("ici");
this.containerForm.controls.imageTag.patchValue("");
this.containerForm.controls.image.patchValue("");
break;
case 'imageTag':
this.containerForm.controls.imageTag.patchValue('');
break;
default:
break;
}
}
submitForm(formdataReceived) {
//event.preventDefault();
console.log("submit" + this.containerForm);
}
} |
Partager