From f71800c8f248a2a2648ec800c0645495fb708054 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 6 Jan 2021 09:29:04 +0100
Subject: [PATCH] FIX #15550 TIME 1  fix current filters + do not use jquery

---
 src/frontend/app/search/search.component.html |  8 +--
 src/frontend/app/search/search.component.ts   | 52 ++++++++++++-------
 2 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/src/frontend/app/search/search.component.html b/src/frontend/app/search/search.component.html
index 5fdc95cbab..d23c2d88ed 100644
--- a/src/frontend/app/search/search.component.html
+++ b/src/frontend/app/search/search.component.html
@@ -36,9 +36,9 @@
                     <div *ngIf="currentFilter.id === 'workflowStates' && currentFilter.val.length > 0">
                         <ion-chip outline *ngFor="let item of currentFilter.val" color="primary"
                             style="background: white;" [title]="'lang.workflowStatesSearch' | translate"
-                            (click)="removeFilter(currentFilter, item)">
+                            (click)="removeFilter(currentFilter, item.id)">
                             <ion-icon name="options-outline"></ion-icon>
-                            <ion-label>{{ getLabel(item) | translate}}</ion-label>
+                            <ion-label>{{ item.label | translate}}</ion-label>
                             <ion-icon name="close-circle"></ion-icon>
                         </ion-chip>
                     </div>
@@ -174,8 +174,8 @@
                 </ion-list-header>
                 <ion-item *ngFor="let val of filter.values;let i=index;">
                     <ion-label>{{val.label | translate}}</ion-label>
-                    <ion-checkbox (ionChange)="toggleItem(filter, val, $event.detail.checked)"
-                        [checked]="filter.val.indexOf(val.id) > -1" slot="end" [value]="val.id" class="workflowStates">
+                    <ion-checkbox (ngModelChange)="toggleItem(filter, val, !val.selected)" [(ngModel)]="val.selected" [checked]="val.selected"
+                        slot="end" [value]="val.id" class="workflowStates">
                     </ion-checkbox>
                 </ion-item>
             </ng-container>
diff --git a/src/frontend/app/search/search.component.ts b/src/frontend/app/search/search.component.ts
index fdf1378342..426e039ad5 100644
--- a/src/frontend/app/search/search.component.ts
+++ b/src/frontend/app/search/search.component.ts
@@ -43,19 +43,23 @@ export class SearchComponent implements OnInit {
             values: [
                 {
                     id: 'PROG',
-                    label: 'lang.inprogress'
+                    label: 'lang.inprogress',
+                    selected: false
                 },
                 {
                     id: 'STOP',
-                    label: 'lang.interrupt'
+                    label: 'lang.interrupt',
+                    selected: false
                 },
                 {
                     id: 'VAL',
-                    label: 'lang.end'
+                    label: 'lang.end',
+                    selected: false
                 },
                 {
                     id: 'REF',
-                    label: 'lang.refused'
+                    label: 'lang.refused',
+                    selected: false
                 }
             ]
         },
@@ -148,7 +152,7 @@ export class SearchComponent implements OnInit {
         this.menu.close('right-menu');
     }
 
-    toggleItem(filter: any, item: any, state: boolean) {
+    toggleItem(filter: any, item: any, state: any) {
         if (!state) {
             const index = filter.val.indexOf(item.id);
             filter.val.splice(index, 1);
@@ -159,11 +163,13 @@ export class SearchComponent implements OnInit {
 
     formatDatas() {
         const objToSend: any = {};
-        const tmpArr = this.filters.filter((filter: any) => (filter.type === 'text' && filter.val !== '') || (filter.type !== 'text' && filter.val.length > 0));
+        const tmpArr = JSON.parse(JSON.stringify(this.filters.filter((filter: any) => (filter.type === 'text' && filter.val !== '') || (filter.type !== 'text' && filter.val.length > 0))));
 
         tmpArr.forEach((filter: any) => {
             if (filter.id === 'workflowUsers') {
                 objToSend[filter.id] = filter.val.map((item: any) => item.id);
+            } else if (filter.id === 'workflowStates') {
+                objToSend[filter.id] = filter.values.filter((item: any) => item.selected).map((item: any) => item.id);
             } else {
                 objToSend[filter.id] = filter.val;
             }
@@ -239,11 +245,12 @@ export class SearchComponent implements OnInit {
     launchSearch() {
         this.ressources = [];
         this.offset = 0;
+        this.refreshCurrentFilter();
+
         return new Promise((resolve) => {
             this.http.post(`../rest/search/documents?limit=10&offset=0`, this.formatDatas())
                 .pipe(
                     tap((data: any) => {
-                        this.currentFilters = JSON.parse(JSON.stringify(this.filters.filter((item: any) => !this.functionsService.empty(item.val))));
                         this.ressources = this.formatListDatas(data.documents);
                         this.count = data.count;
                         this.infiniteScroll.disabled = false;
@@ -258,6 +265,17 @@ export class SearchComponent implements OnInit {
         });
     }
 
+    refreshCurrentFilter() {
+        this.currentFilters = JSON.parse(JSON.stringify(this.filters.filter((item: any) => !this.functionsService.empty(item.val))));
+
+        if (this.currentFilters.filter((item: any) => item.id === 'workflowStates').length > 0) {
+            this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].val = this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].values.filter((item: any) => item.selected);
+            if (this.currentFilters.filter((item: any) => item.id === 'workflowStates')[0].val.length === 0) {
+                this.currentFilters = this.currentFilters.filter((item: any) => item.id !== 'workflowStates');
+            }
+        }
+    }
+
     loadData(event: any) {
         if (this.count <= this.limit) {
             event.target.complete();
@@ -412,16 +430,19 @@ export class SearchComponent implements OnInit {
     }
 
     clearFilters() {
-        $(".workflowStates").each(function () {
-            $(this).prop("checked", false);
-        });
         for (let index = 0; index < this.filters.length; index++) {
             if (!Array.isArray(this.filters[index].val) && this.filters[index].val !== '') {
                 this.filters[index].val = '';
             }
 
-            if (Array.isArray(this.filters[index].val) && this.filters[index].val.length > 0 && (this.filters[index].id === 'workflowUsers')) {
+            if (Array.isArray(this.filters[index].val)) {
                 this.filters[index].val = [];
+                this.filters[index].values = this.filters[index].values.map((item: any) => {
+                    return {
+                        ...item,
+                        selected : false
+                    };
+                });
             }
         }
         if (this.ressources.length > 0) {
@@ -434,15 +455,10 @@ export class SearchComponent implements OnInit {
             this.filters.find((element: any) => element.id === filter.id).val = '';
         } else {
             if (filter.id === 'workflowStates') {
-                $(".workflowStates").each(function () {
-                    if ($(this).val() === item) {
-                        $(this).prop("checked", false);
-                        return false;
-                    }
-                });
+                this.filters.find((element: any) => element.id === filter.id).values.filter((element: any) => element.id === item)[0].selected = false;
             } else {
                 const index = filter.val.indexOf(item);
-                filter.val.splice(index, 1);
+                this.filters.filter((element: any) => element.id === filter.id)[0].val.splice(index, 1);
             }
         }
         this.launchSearch();
-- 
GitLab