diff --git a/src/frontend/app/indexation/indexation.component.html b/src/frontend/app/indexation/indexation.component.html index ede95eabc95e6952c9f38f4432d3710010aa6730..2a1371a93324b00e794e5199fb95e79ab5334cd9 100644 --- a/src/frontend/app/indexation/indexation.component.html +++ b/src/frontend/app/indexation/indexation.component.html @@ -4,21 +4,22 @@ autoFocus="false" style="overflow-x:hidden;" [ngStyle]="{'width': appService.getViewMode() ? '95%' : '600px'}"> <header-panel [snavLeft]="snav"></header-panel> <div style="background: #F9F9F9;display: flex;padding: 10px;padding-left: 40px;padding-right: 40px;"> - <button mat-button class="button-form-primary" [matMenuTriggerFor]="menu" style="flex:1;margin-right:20px;"> + <button mat-button class="button-form-primary" [matMenuTriggerFor]="IndexingModelsMenu" style="flex:1;margin-right:20px;"> <span class="menu-label"> - Mon modèle (par défaut) + {{currentIndexingModel.label}} {{currentIndexingModel.default ? '(par défaut)': ''}} </span> <i class="fa fa-chevron-down menu-icon"></i></button> - <mat-menu #menu="matMenu" [class]="'menuForm'"> - <button mat-menu-item>Item 1</button> - <button mat-menu-item>Item 2</button> + <mat-menu #IndexingModelsMenu="matMenu" [class]="'menuForm'"> + <ng-container *ngFor="let indexingModel of indexingModels"> + <button *ngIf="indexingModel.id !== currentIndexingModel.id" mat-menu-item (click)="loadIndexingModel(indexingModel)">{{indexingModel.label}} {{indexingModel.default ? '(par défaut)': ''}}</button> + </ng-container> </mat-menu> <button mat-button class="button-form-primary" style="flex:1;align-items: center;justify-content: center;text-align: center;display: flex;">Enregistrer comme modèle</button> </div> <div class="indexing-form-container"> - <app-indexing-form #indexingForm [indexingFormId]="1"></app-indexing-form> + <app-indexing-form *ngIf="currentIndexingModel.id !== undefined" #indexingForm [indexingFormId]="currentIndexingModel.id"></app-indexing-form> </div> <div class="actions-indexing-form"> <button mat-button class="button-form-primary" [matMenuTriggerFor]="menu" style="flex:1;margin-right:20px;"> diff --git a/src/frontend/app/indexation/indexation.component.ts b/src/frontend/app/indexation/indexation.component.ts index 9b54ef106ace99a924cee90a5dfb2c5f1bdf39d0..674c5c60f09385f69e77b94935ca215d01164e2f 100644 --- a/src/frontend/app/indexation/indexation.component.ts +++ b/src/frontend/app/indexation/indexation.component.ts @@ -13,6 +13,8 @@ import { FiltersListService } from '../../service/filtersList.service'; import { Overlay } from '@angular/cdk/overlay'; import { AppService } from '../../service/app.service'; import { IndexingFormComponent } from './indexing-form/indexing-form.component'; +import { tap, finalize, catchError } from 'rxjs/operators'; +import { of } from 'rxjs'; @Component({ templateUrl: "indexation.component.html", @@ -34,7 +36,8 @@ export class IndexationComponent implements OnInit { @ViewChild('indexingForm', { static: false }) indexingForm: IndexingFormComponent; - currentSelectedChrono: string = ''; + indexingModels: any[] = []; + currentIndexingModel: any = {}; constructor( private route: ActivatedRoute, @@ -50,10 +53,21 @@ export class IndexationComponent implements OnInit { ngOnInit(): void { this.loading = false; - this.headerService.setHeader("Enregistrement d'un courrer"); + this.headerService.setHeader("Enregistrement d'un courrier"); this.route.params.subscribe(params => { - + this.http.get("../../rest/indexingModels").pipe( + tap((data: any) => { + // + this.indexingModels = data.indexingModels; + this.currentIndexingModel = this.indexingModels.filter(model => model.default === true)[0]; + }), + finalize(() => this.loading = false), + catchError((err: any) => { + this.notify.handleErrors(err); + return of(false); + }) + ).subscribe(); }, (err: any) => { this.notify.handleErrors(err); @@ -66,4 +80,9 @@ export class IndexationComponent implements OnInit { } } + + loadIndexingModel(indexingModel: any) { + this.currentIndexingModel = indexingModel; + this.indexingForm.loadForm(indexingModel.id); + } } \ No newline at end of file diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts index 4c5a2ce8696df63f37a7c6fd4ffdf2d6e3ed42dc..5c20abed3d8ae418422a8f550fc5f62884708d13 100644 --- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts +++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts @@ -172,7 +172,7 @@ export class IndexingFormComponent implements OnInit { this['indexingModels_' + category] = []; }); - if (this.indexingFormId <= 0) { + if (this.indexingFormId <= 0 || this.indexingFormId === undefined) { this.http.get("../../rest/customFields").pipe( tap((data: any) => { this.availableCustomFields = data.customFields.map((info: any) => { @@ -182,77 +182,10 @@ export class IndexingFormComponent implements OnInit { }); this.fieldCategories.forEach(element => { this['indexingModels_' + element] = this.indexingModelsCore.filter((x: any, i: any, a: any) => x.unit === element); - }); - }), - finalize(() => this.loading = false), - catchError((err: any) => { - this.notify.handleErrors(err); - return of(false); - }) - ).subscribe(); - } else { - this.http.get("../../rest/customFields").pipe( - tap((data: any) => { - this.availableCustomFields = data.customFields.map((info: any) => { - info.identifier = 'indexingCustomField_' + info.id; - info.system = false; - return info; - }); - }), - exhaustMap((data) => this.http.get("../../rest/indexingModels/" + this.indexingFormId)), - tap((data: any) => { - let fieldExist: boolean; - if (data.indexingModel.fields.length === 0) { - this.fieldCategories.forEach(element => { - this['indexingModels_' + element] = this.indexingModelsCore.filter((x: any, i: any, a: any) => x.unit === element); + this['indexingModels_' + element].forEach((field: any) => { + this.arrFormControl[field.identifier] = new FormControl({ value: field.default_value, disabled: (field.system && this.adminMode) }, field.mandatory ? [Validators.required] : []); }); - this.notify.error("Champs introuvables! les données de base ont été chargés"); - } else { - data.indexingModel.fields.forEach((field: any) => { - fieldExist = false; - field.label = this.lang[field.identifier]; - field.system = false; - field.values = []; - - let indexFound = this.availableFields.map(avField => avField.identifier).indexOf(field.identifier); - - if (indexFound > -1) { - field.label = this.availableFields[indexFound].label; - field.values = this.availableFields[indexFound].values; - field.type = this.availableFields[indexFound].type; - this.availableFields.splice(indexFound, 1); - fieldExist = true; - } - - indexFound = this.availableCustomFields.map(avField => avField.identifier).indexOf(field.identifier); - - if (indexFound > -1) { - field.label = this.availableCustomFields[indexFound].label; - field.values = this.availableCustomFields[indexFound].values; - field.type = this.availableCustomFields[indexFound].type; - this.availableCustomFields.splice(indexFound, 1); - fieldExist = true; - } - if (this.indexingModelsCore.map(info => info.identifier).indexOf(field.identifier) > -1) { - fieldExist = true; - field.system = true; - } - - if (fieldExist) { - this['indexingModels_' + field.unit].push(field); - this.arrFormControl[field.identifier] = new FormControl({ value: field.default_value, disabled: (field.system && this.adminMode) }, field.mandatory ? [Validators.required] : []); - } else { - this.notify.error("Le champ " + field.identifier + " n'existe pas !"); - } - - }); - } - this.fieldCategories.forEach(element => { - this['indexingModels_' + element + 'Clone'] = JSON.parse(JSON.stringify(this['indexingModels_' + element])); }); - - this.initElemForm(); - this.createForm(); }), finalize(() => this.loading = false), catchError((err: any) => { @@ -260,6 +193,9 @@ export class IndexingFormComponent implements OnInit { return of(false); }) ).subscribe(); + } else { + console.log(this.indexingFormId) + this.loadForm(this.indexingFormId); } } @@ -447,7 +383,79 @@ export class IndexingFormComponent implements OnInit { createForm() { this.indexingFormGroup = new FormGroup(this.arrFormControl); - console.log(this.arrFormControl['indexingCustomField_1'].value) + } + + loadForm(indexModelId: number) { + this.fieldCategories.forEach(category => { + this['indexingModels_' + category] = []; + }); + + this.http.get("../../rest/customFields").pipe( + tap((data: any) => { + this.availableCustomFields = data.customFields.map((info: any) => { + info.identifier = 'indexingCustomField_' + info.id; + info.system = false; + return info; + }); + }), + exhaustMap((data) => this.http.get("../../rest/indexingModels/" + indexModelId)), + tap((data: any) => { + let fieldExist: boolean; + if (data.indexingModel.fields.length === 0) { + this.fieldCategories.forEach(element => { + this['indexingModels_' + element] = this.indexingModelsCore.filter((x: any, i: any, a: any) => x.unit === element); + }); + this.notify.error("Champs introuvables! les données de base ont été chargés"); + } else { + data.indexingModel.fields.forEach((field: any) => { + fieldExist = false; + field.label = this.lang[field.identifier]; + field.system = false; + field.values = []; + + let indexFound = this.availableFields.map(avField => avField.identifier).indexOf(field.identifier); + + if (indexFound > -1) { + field.label = this.availableFields[indexFound].label; + field.values = this.availableFields[indexFound].values; + field.type = this.availableFields[indexFound].type; + this.availableFields.splice(indexFound, 1); + fieldExist = true; + } + + indexFound = this.availableCustomFields.map(avField => avField.identifier).indexOf(field.identifier); + + if (indexFound > -1) { + field.label = this.availableCustomFields[indexFound].label; + field.values = this.availableCustomFields[indexFound].values; + field.type = this.availableCustomFields[indexFound].type; + this.availableCustomFields.splice(indexFound, 1); + fieldExist = true; + } + if (this.indexingModelsCore.map(info => info.identifier).indexOf(field.identifier) > -1) { + fieldExist = true; + field.system = true; + } + + if (fieldExist) { + this['indexingModels_' + field.unit].push(field); + this.arrFormControl[field.identifier] = new FormControl({ value: field.default_value, disabled: (field.system && this.adminMode) }, field.mandatory ? [Validators.required] : []); + } else { + this.notify.error("Le champ " + field.identifier + " n'existe pas !"); + } + + }); + } + + this.initElemForm(); + this.createForm(); + }), + finalize(() => this.loading = false), + catchError((err: any) => { + this.notify.handleErrors(err); + return of(false); + }) + ).subscribe(); } isValidForm() {