From 599dccb965d85c18d59e3333668dcde7c28f0124 Mon Sep 17 00:00:00 2001
From: Alex Orluc <alex.orluc@maarch.org>
Date: Fri, 23 Mar 2018 21:45:49 +0100
Subject: [PATCH] FEAT #258 hide panel if no parent_entity when cancel

---
 .../entities-administration.component.html    | 28 ++++++--------
 .../entities-administration.component.ts      | 38 +++++++++----------
 2 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/apps/maarch_entreprise/Views/entities-administration.component.html b/apps/maarch_entreprise/Views/entities-administration.component.html
index 96cbb41064d..d4bfc576d06 100644
--- a/apps/maarch_entreprise/Views/entities-administration.component.html
+++ b/apps/maarch_entreprise/Views/entities-administration.component.html
@@ -15,7 +15,7 @@
             <mat-divider></mat-divider>
             <mat-nav-list>
                 <h3 mat-subheader>{{lang.actions}}</h3>
-                <a mat-list-item (click)="prepareEntityAdd()">
+                <a mat-list-item (click)="prepareEntityAdd()" *ngIf="!creationMode">
                     <mat-icon color="primary" mat-list-icon class="fa fa-plus"></mat-icon>
                     <p mat-line>
                         {{lang.add}}
@@ -57,26 +57,20 @@
 
             <mat-nav-list>
                 <mat-tab-group>
-                    <mat-tab label="{{lang.informations}}">
+                    <mat-tab [label]="creationMode ? 'Créer une nouvelle entité' : lang.informations">
                         <form class="form-horizontal" #entityForm="ngForm" style="overflow:hidden;">
                             <div class="form-group">
                                 <div class="col-sm-12">
                                     <mat-form-field>
                                         <mat-select id="parent_entity_id" name="parent_entity_id" title="{{lang.isLinkedTo}}" placeholder="{{lang.isLinkedTo}}" [(ngModel)]="currentEntity.parent_entity_id"
-                                            (change)="selectParentEntity(currentEntity.parent_entity_id)" *ngIf="creationMode">
-                                            <mat-option value="">
-                                            </mat-option>
-                                            <mat-option *ngFor="let entity of entities | sortBy : 'entity_label'" [value]="entity.entity_id">
-                                                {{entity.entity_label}}
-                                            </mat-option>
-                                        </mat-select>
-                                        <mat-select id="parent_entity_id" name="parent_entity_id" title="{{lang.isLinkedTo}}" placeholder="{{lang.isLinkedTo}}" [(ngModel)]="currentEntity.parent_entity_id"
-                                            *ngIf="!creationMode">
-                                            <mat-option value="">
-                                            </mat-option>
-                                            <mat-option *ngFor="let entity of entities | sortBy : 'entity_label'" [value]="entity.entity_id">
-                                                {{entity.entity_label}}
+                                            (change)="selectParentEntity(currentEntity.parent_entity_id)" required>
+                                            <mat-option *ngIf="entities.length == 0" value="">
                                             </mat-option>
+                                            <ng-container *ngFor="let entity of entities | sortBy : 'entity_label'">
+                                                <mat-option [value]="entity.entity_id" *ngIf="(!entity.state.disabled && entity.entity_id != currentEntity.entity_id) || currentEntity.parent_entity_id == entity.entity_id">
+                                                    {{entity.entity_label}}
+                                                </mat-option>
+                                            </ng-container>           
                                         </mat-select>
                                     </mat-form-field>
                                     <mat-form-field>
@@ -220,10 +214,10 @@
                                     <mat-autocomplete #auto="matAutocomplete">
                                         <mat-option *ngFor="let elem of filteredElements | async" [value]="elem.id" (click)="addElemListModel(elem)">
                                             <p mat-line>
-                                                <span class="col-sm-1">
+                                                <span class="col-xm-1">
                                                     <mat-icon color="primary" [class]="elem.type == 'entity' ? 'fa fa-sitemap fa-2x' : 'fa fa-user fa-2x'" style="margin-right:0px;"></mat-icon>
                                                 </span>
-                                                <span class="col-sm-11">
+                                                <span class="col-xm-11">
                                                     {{ elem.idToDisplay }}
                                                     <small>{{ elem.otherInfo }}</small>
                                                 </span>
diff --git a/apps/maarch_entreprise/js/angular/app/administration/entities-administration.component.ts b/apps/maarch_entreprise/js/angular/app/administration/entities-administration.component.ts
index 1e4598f98b2..3a1c87a4d27 100644
--- a/apps/maarch_entreprise/js/angular/app/administration/entities-administration.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/administration/entities-administration.component.ts
@@ -373,17 +373,23 @@ export class EntitiesAdministrationComponent extends AutoCompletePlugin implemen
         this.creationMode = false;
         this.isDraggable = true;
         $j('#jstree').jstree('deselect_all');
-        for (let i = 0; i < this.entities.length; i++) {
-            if (this.entities[i].entity_id == this.currentEntity.parent_entity_id) {
-                $j('#jstree').jstree('select_node', this.entities[i]);
-                break;
+        if(this.currentEntity.parent_entity_id) {
+            for (let i = 0; i < this.entities.length; i++) {
+                if (this.entities[i].entity_id == this.currentEntity.parent_entity_id) {
+                    $j('#jstree').jstree('select_node', this.entities[i]);
+                    break;
+                }
             }
+        } else {
+            this.sidenav.close();
         }
     }
 
     selectParentEntity(entity_id:any) {
-        $j('#jstree').jstree('deselect_all');
-        $j('#jstree').jstree('select_node', entity_id);
+        if (this.creationMode) {
+            $j('#jstree').jstree('deselect_all');
+            $j('#jstree').jstree('select_node', entity_id);
+        }
     }
 
     removeEntity() {
@@ -398,13 +404,9 @@ export class EntitiesAdministrationComponent extends AutoCompletePlugin implemen
                             this.entities = data['entities'];
                             $j('#jstree').jstree(true).settings.core.data = this.entities;
                             $j('#jstree').jstree("refresh");
+                            this.sidenav.close();
                             this.notify.success(this.lang.entityDeleted);
-                            for (let i = 0; i < this.entities.length; i++) {
-                                if (this.entities[i].allowed == true) {
-                                    $j('#jstree').jstree('select_node', this.entities[i]);
-                                    break;
-                                }
-                            }
+                            
                         }, (err) => {
                             this.notify.error(err.error.errors);
                         });
@@ -420,13 +422,8 @@ export class EntitiesAdministrationComponent extends AutoCompletePlugin implemen
                         this.entities = data['entities'];
                         $j('#jstree').jstree(true).settings.core.data = this.entities;
                         $j('#jstree').jstree("refresh");
+                        this.sidenav.close();
                         this.notify.success(this.lang.entityDeleted);
-                        for (let i = 0; i < this.entities.length; i++) {
-                            if (this.entities[i].allowed == true) {
-                                $j('#jstree').jstree('select_node', this.entities[i]);
-                                break;
-                            }
-                        }
                     }, (err) => {
                         this.notify.error(err.error.errors);
                     });
@@ -449,12 +446,13 @@ export class EntitiesAdministrationComponent extends AutoCompletePlugin implemen
         } else {
             this.currentEntity = { "entity_type": this.entityTypeList[0].id };
             $j('#jstree').jstree('deselect_all');
-            for (let i = 0; i < this.entities.length; i++) {
+            this.sidenav.open();
+            /*for (let i = 0; i < this.entities.length; i++) {
                 if (this.entities[i].allowed == true) {
                     $j('#jstree').jstree('select_node', this.entities[i]);
                     break;
                 }
-            }
+            }*/
         }
     }
 
-- 
GitLab