From e8b7ec54c9c88522cb4abc8e1d04890020e1dba3 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 2 Oct 2020 14:50:36 +0200
Subject: [PATCH] FEAT #13271 TIME 1 fix tag input

---
 .../app/tag/indexing/tag-input.component.ts   | 41 ++++++++++++++-----
 src/lang/lang-fr.json                         |  2 +-
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/frontend/app/tag/indexing/tag-input.component.ts b/src/frontend/app/tag/indexing/tag-input.component.ts
index d72ef749a4f..1aa019a3ef1 100644
--- a/src/frontend/app/tag/indexing/tag-input.component.ts
+++ b/src/frontend/app/tag/indexing/tag-input.component.ts
@@ -16,7 +16,7 @@ import { ThesaurusModalComponent } from './thesaurus/thesaurus-modal.component';
 
 @Component({
     selector: 'app-tag-input',
-    templateUrl: "tag-input.component.html",
+    templateUrl: 'tag-input.component.html',
     styleUrls: [
         'tag-input.component.scss',
         '../../indexation/indexing-form/indexing-form.component.scss'
@@ -42,6 +42,7 @@ export class TagInputComponent implements OnInit {
 
     tags: any[] = [];
 
+    tmpObject: any = null;
 
     /**
      * FormControl used when autocomplete is used in form and must be catched in a form control.
@@ -67,9 +68,30 @@ export class TagInputComponent implements OnInit {
     }
 
     ngOnInit() {
+        this.controlAutocomplete.valueChanges
+            .pipe(
+                tap((data: any) => {
+                    console.log('change!', this.returnValue, this.tmpObject, this.functionsService.empty(this.tmpObject));
+
+                    if (this.returnValue === 'object') {
+                        this.valuesToDisplay = {};
+                        data.forEach((item: any) => {
+                            this.valuesToDisplay[item.id] = item.label;
+                        });
+                    } else {
+                        if (!this.functionsService.empty(this.tmpObject)) {
+                            this.valuesToDisplay[this.tmpObject['id']] = this.tmpObject[this.key];
+                            this.tmpObject = null;
+                        } else {
+                            console.log('initFormValue');
+                            this.initFormValue();
+                        }
+
+                    }
+                })
+            ).subscribe();
         this.controlAutocomplete.setValue(this.controlAutocomplete.value === null || this.controlAutocomplete.value === '' ? [] : this.controlAutocomplete.value);
         this.canAdd = this.privilegeService.hasCurrentUserPrivilege('manage_tags_application');
-        this.initFormValue();
         this.initAutocompleteRoute();
     }
 
@@ -78,7 +100,6 @@ export class TagInputComponent implements OnInit {
         this.options = [];
         this.myControl.valueChanges
             .pipe(
-                //tap((value) => this.canAdd = value.length === 0 ? false : true),
                 debounceTime(300),
                 filter(value => value.length > 2),
                 distinctUntilChanged(),
@@ -98,7 +119,7 @@ export class TagInputComponent implements OnInit {
     }
 
     getDatas(data: string) {
-        return this.http.get('../rest/autocomplete/tags', { params: { "search": data } });
+        return this.http.get('../rest/autocomplete/tags', { params: { 'search': data } });
     }
 
     selectOpt(ev: any) {
@@ -132,7 +153,6 @@ export class TagInputComponent implements OnInit {
                     label: item['idToDisplay']
                 });
             }
-            this.valuesToDisplay[item['id']] = item[this.key];
             this.controlAutocomplete.setValue(arrvalue);
         }
     }
@@ -199,25 +219,26 @@ export class TagInputComponent implements OnInit {
         ).subscribe();
     }
 
-    openThesaurus(tagId: number = null) {
+    openThesaurus(tag: any = null) {
+        tag = this.returnValue === 'id' ? tag : tag.id;
         const dialogRef = this.dialog.open(ThesaurusModalComponent, {
             panelClass: 'maarch-modal',
             width: '600px',
             data: {
-                id : tagId
+                id: tag
             }
         });
         dialogRef.afterClosed().pipe(
             filter((data: any) => !this.functionsService.empty(data)),
             map((data: any) => {
                 return {
-                    id : data.id,
-                    idToDisplay : data.label
+                    id: data.id,
+                    idToDisplay: data.label
                 };
             }),
             tap((tagItem: any) => {
                 console.log(tagItem);
-
+                this.tmpObject = tagItem;
                 this.setFormValue(tagItem);
             }),
             catchError((err: any) => {
diff --git a/src/lang/lang-fr.json b/src/lang/lang-fr.json
index 9044149cfc4..7a7607d88c4 100644
--- a/src/lang/lang-fr.json
+++ b/src/lang/lang-fr.json
@@ -1253,7 +1253,7 @@
     "qmail": "Serveur mail local (Qmail)",
     "qrcode": "QR code",
     "quickSearchInfo": "Cible de la recherche",
-    "quickSearchTarget": "Recherche sur le sujet (courrier / pièce jointe), numéro chrono (courrier / pièce jointe), contenu d'annotations, code à barre, numéro GED (courrier)",
+    "quickSearchTarget": "Recherche sur le sujet (courrier / pièce jointe), numéro chrono (courrier / pièce jointe), code à barre, numéro GED (courrier)",
     "quota": "Quota",
     "quotaExceeded": "Quota dépassé",
     "radioInput": "Liste à choix unique",
-- 
GitLab