From 39e60a219fcc33085538c440fcdb3c9f2874eb7d Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Thu, 26 Sep 2019 12:24:04 +0200
Subject: [PATCH] FIX #11271 TIME 0:05 add calc process limit date mecanics

---
 .../indexing-form.component.html              |  2 +-
 .../indexing-form/indexing-form.component.ts  | 25 ++++++++++++++++-
 .../select-search.component.html              |  2 +-
 .../select-search/select-search.component.ts  | 27 ++++++++++++++-----
 4 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.html b/src/frontend/app/indexation/indexing-form/indexing-form.component.html
index d63b2850bd5..995ef917cae 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.html
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.html
@@ -61,7 +61,7 @@
                                 <ng-container *ngIf="field.type === 'select'">
                                     <plugin-select-search [showResetOption]="adminMode"
                                         [placeholderLabel]="!adminMode ? lang.chooseValue : lang.defaultValue"
-                                        [formControlSelect]="arrFormControl[field.identifier]" [datas]="field.values"
+                                        [formControlSelect]="arrFormControl[field.identifier]" [datas]="field.values" (afterSelected)="launchEvent($event, field)"
                                         style="width:100%;"></plugin-select-search>
                                 </ng-container>
                                 <ng-container *ngIf="field.type === 'date'">
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 bc497c92704..5925cda3033 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
@@ -402,6 +402,7 @@ export class IndexingFormComponent implements OnInit {
                                 }
                             });
                             elem.values = arrValues;
+                            elem.event = 'calcLimitDate';
                         }
                     });
                 });
@@ -483,7 +484,7 @@ export class IndexingFormComponent implements OnInit {
             }),
             exhaustMap((data) => this.http.get("../../rest/indexingModels/" + indexModelId)),
             tap((data: any) => {
-                
+
                 this.currentCategory = data.indexingModel.category;
                 let fieldExist: boolean;
                 if (data.indexingModel.fields.length === 0) {
@@ -601,4 +602,26 @@ export class IndexingFormComponent implements OnInit {
             this.arrFormControl[field.identifier].enable();
         }
     }
+
+    launchEvent(value: any, field: any) {
+        this[field.event](field.identifier, value);
+    }
+
+    calcLimitDate(identifier: string, value: any) {
+
+        // TO DO: REMOVE AFTER BACK
+        if (this.arrFormControl['processLimitDate'] !== undefined) {
+            this.arrFormControl['processLimitDate'].setValue(new Date());
+        }
+        
+        /*this.http.get("../../rest/doctypes/types/" + value + '/getLimitDate').pipe(
+            tap((data: any) => {
+                this.arrFormControl['processLimitDate'].setValue(data);
+            }),
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();*/
+    }
 }
\ No newline at end of file
diff --git a/src/frontend/plugins/select-search/select-search.component.html b/src/frontend/plugins/select-search/select-search.component.html
index 8f5030464c6..8b9a728c7c7 100644
--- a/src/frontend/plugins/select-search/select-search.component.html
+++ b/src/frontend/plugins/select-search/select-search.component.html
@@ -1,5 +1,5 @@
 <mat-form-field class="input-form" floatLabel="never">
-  <mat-select [formControl]="formControlSelect" [placeholder]="placeholderLabel" #test>
+  <mat-select [formControl]="formControlSelect" [placeholder]="placeholderLabel" #test (selectionChange)="launchEvent($event)">
     <input *ngIf="datas.length > 5" matInput class="mat-select-search-input mat-select-search-hidden" />
 
     <div *ngIf="datas.length > 5" class="mat-select-search-inner" [ngClass]="{'mat-select-search-inner-multiple': matSelect.multiple}">
diff --git a/src/frontend/plugins/select-search/select-search.component.ts b/src/frontend/plugins/select-search/select-search.component.ts
index 376e0c801d6..2fa8e623983 100644
--- a/src/frontend/plugins/select-search/select-search.component.ts
+++ b/src/frontend/plugins/select-search/select-search.component.ts
@@ -2,7 +2,8 @@ import {
     AfterViewInit, ChangeDetectorRef,
     Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, QueryList,
     ViewChild,
-    Renderer2
+    Renderer2,
+    Output
 } from '@angular/core';
 import { ControlValueAccessor, FormControl } from '@angular/forms';
 import { MatOption, MatSelect } from '@angular/material';
@@ -27,6 +28,11 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
 
     @Input('showResetOption') showResetOption: boolean;
 
+    /**
+     * Catch external event after select an element in autocomplete
+     */
+    @Output('afterSelected') afterSelected = new EventEmitter();
+
     /** Reference to the search input field */
     @ViewChild('searchSelectInput', { read: ElementRef, static: true }) searchSelectInput: ElementRef;
 
@@ -121,10 +127,10 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
             
         }, 800);*/
         this.filteredDatas = this.formControlSearch.valueChanges
-        .pipe(
-            startWith(''),
-            map(value => this._filter(value))
-        );
+            .pipe(
+                startWith(''),
+                map(value => this._filter(value))
+            );
 
 
         // this.initMultipleHandling();
@@ -231,8 +237,10 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
             .pipe(takeUntil(this._onDestroy))
             .subscribe(() => {
                 // note: this is hacky, but currently there is no better way to do this
-                this.searchSelectInput.nativeElement.parentElement.parentElement
-                    .parentElement.parentElement.parentElement.classList.add(overlayClass);
+                if (this.searchSelectInput !== undefined) {
+                    this.searchSelectInput.nativeElement.parentElement.parentElement
+                        .parentElement.parentElement.parentElement.classList.add(overlayClass);
+                }
             });
 
         this.overlayClassSet = true;
@@ -286,4 +294,9 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
         }
     }
 
+    launchEvent(ev: any) {
+        if (this.afterSelected !== undefined) {
+            this.afterSelected.emit(ev.value);
+        }
+    }
 }
-- 
GitLab