From 37439d5c63e80a9476c2c518e2328db975349c61 Mon Sep 17 00:00:00 2001
From: Daviti Gogshelidze <daviti1@mail.com>
Date: Sun, 18 Sep 2022 17:47:01 +0200
Subject: [PATCH] Fix chip layer selector

---
 .../sapiViews/core/parcellation/module.ts     |  6 +-
 .../parcellation.smartChip.component.ts       | 56 ++++++++++++++++++-
 .../parcellation.smartChip.template.html      | 51 ++---------------
 .../viewerCmp/viewerCmp.template.html         | 18 +++---
 4 files changed, 70 insertions(+), 61 deletions(-)

diff --git a/src/atlasComponents/sapiViews/core/parcellation/module.ts b/src/atlasComponents/sapiViews/core/parcellation/module.ts
index 620d55d58..9f4ba762b 100644
--- a/src/atlasComponents/sapiViews/core/parcellation/module.ts
+++ b/src/atlasComponents/sapiViews/core/parcellation/module.ts
@@ -14,7 +14,10 @@ import { ParcellationDoiPipe } from "./parcellationDoi.pipe";
 import { ParcellationIsBaseLayer } from "./parcellationIsBaseLayer.pipe";
 import { ParcellationVisibilityService } from "./parcellationVis.service";
 import { PreviewParcellationUrlPipe } from "./previewParcellationUrl.pipe";
-import { SapiViewsCoreParcellationParcellationSmartChip } from "./smartChip/parcellation.smartChip.component";
+import {
+  SapiViewsCoreParcellationParcellationSmartChip,
+  TemplateNotAvailableDialog
+} from "./smartChip/parcellation.smartChip.component";
 import { SapiViewsCoreParcellationParcellationTile } from "./tile/parcellation.tile.component";
 import {GetSpaceByIdPipe} from "src/atlasComponents/sapiViews/core/parcellation/getSpaceById.pipe";
 
@@ -37,6 +40,7 @@ import {GetSpaceByIdPipe} from "src/atlasComponents/sapiViews/core/parcellation/
     ParcellationIsBaseLayer,
     ParcellationDoiPipe,
     GetSpaceByIdPipe,
+    TemplateNotAvailableDialog
   ],
   exports: [
     SapiViewsCoreParcellationParcellationTile,
diff --git a/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.component.ts b/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.component.ts
index 406484d30..2f7c8ed60 100644
--- a/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.component.ts
+++ b/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.component.ts
@@ -1,10 +1,11 @@
-import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from "@angular/core";
+import {Component, EventEmitter, Inject, Input, OnChanges, Output, SimpleChanges} from "@angular/core";
 import { BehaviorSubject, concat, Observable, of, timer } from "rxjs";
 import {SapiParcellationModel, SapiSpaceModel} from "src/atlasComponents/sapi/type";
 import { ParcellationVisibilityService } from "../parcellationVis.service";
 import { ARIA_LABELS } from "common/constants"
 import { getTraverseFunctions } from "../parcellationVersion.pipe";
-import { mapTo, shareReplay, switchMap } from "rxjs/operators";
+import {filter, mapTo, shareReplay, switchMap, take} from "rxjs/operators";
+import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from "@angular/material/dialog";
 
 @Component({
   selector: `sxplr-sapiviews-core-parcellation-smartchip`,
@@ -21,6 +22,9 @@ export class SapiViewsCoreParcellationParcellationSmartChip implements OnChanges
   @Input('sxplr-sapiviews-core-parcellation-smartchip-selected-space')
   selectedSpace: SapiSpaceModel
 
+  @Input('sxplr-sapiviews-core-parcellation-smartchip-selected-all-spaces')
+  allAvailableSpaces: SapiSpaceModel[]
+
   @Input('sxplr-sapiviews-core-parcellation-smartchip-parcellation')
   parcellation: SapiParcellationModel
 
@@ -40,11 +44,14 @@ export class SapiViewsCoreParcellationParcellationSmartChip implements OnChanges
   onSelectSpaceParcellation = new EventEmitter<{space: SapiSpaceModel, parcellation: SapiParcellationModel}>()
 
   constructor(
-    private svc: ParcellationVisibilityService
+    private svc: ParcellationVisibilityService,
+    public dialog: MatDialog
   ){
 
   }
 
+  private templateNotAvailableDialog: MatDialogRef<TemplateNotAvailableDialog>
+
   otherVersions: SapiParcellationModel[]
 
   ngOnChanges(changes: SimpleChanges) {
@@ -116,5 +123,48 @@ export class SapiViewsCoreParcellationParcellationSmartChip implements OnChanges
     this.onSelectSpaceParcellation.emit({space, parcellation})
   }
 
+  openChangeTemplateModal(supportedSpaces, parc) {
+    const spaces = this.allAvailableSpaces.filter(s => supportedSpaces.includes(s['@id']))
+    this.templateNotAvailableDialog = this.dialog.open(TemplateNotAvailableDialog, {
+      data: spaces
+    })
+
+    this.templateNotAvailableDialog.afterClosed().pipe(
+      take(1),
+      filter(r => !!r)
+    ).subscribe(res => {
+      this.selectSpaceAndParcellation(res, parc)
+    })
+  }
+
   onDismissClicked$ = new BehaviorSubject<boolean>(false)
 }
+
+
+@Component({
+  selector: 'template-not-available-dialog',
+  template: `
+      <div class="d-flex align-items-start">
+
+          <div class="flex-grow-1">
+              <p>Parcellation is not available for the current template space. Please select template space to explore
+                  the parcellation:</p>
+              <div class="d-flex align-items-center">
+                  <button mat-button *ngFor="let space of availableSpaces"
+                  (click)="selectTemplate(space)">{{space.fullName}}</button>
+              </div>
+          </div>
+          <button class="flex-grow-0" mat-icon-button (click)="dialogRef.close()"><i class="fas fa-times"></i></button>
+      </div>
+  `,
+})
+export class TemplateNotAvailableDialog {
+  constructor(
+    public dialogRef: MatDialogRef<TemplateNotAvailableDialog>,
+    @Inject(MAT_DIALOG_DATA) public availableSpaces: SapiSpaceModel[],
+  ) {}
+
+  selectTemplate(space) {
+    this.dialogRef.close(space)
+  }
+}
\ No newline at end of file
diff --git a/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.template.html b/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.template.html
index b58cbc0e6..fdf7b4d94 100644
--- a/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.template.html
+++ b/src/atlasComponents/sapiViews/core/parcellation/smartChip/parcellation.smartChip.template.html
@@ -18,19 +18,12 @@
              [ngClass]="{'loading': (loadingParc$ | async) === parc}"
              class="d-flex align-items-center justify-content-space-between">
             <ng-container *ngIf="(parc | parcellationSupportedInCurrentSpace | async) as sup;">
-                <button [disabled]="!sup.supported"
-                        mat-menu-item (click)="selectParcellation(parc)"
+                <button mat-menu-item
+                        (click)="sup.supported? selectParcellation(parc) : openChangeTemplateModal(sup.spaces, parc)"
                         [style.background-color]="(parcellation | equality : parc : trackByFn) ? 'grey' : ''">
                     {{parc.name}}
                 </button>
-                <button *ngIf="sup.spaces"
-                        [matMenuTriggerFor]="otherSpacesMenu"
-                        [matMenuTriggerData]="{parc: parc, spaces: sup.spaces}"
-                        mat-menu-item class="w-0 d-flex align-items-center justify-content-center">
-                    <i class="fas fa-sync"></i>
-                </button>
             </ng-container>
-<!--            <ng-container [ngTemplateOutlet]="selectParcButton" [ngTemplateOutletContext]="{parc:parc}"></ng-container>-->
             <ng-container [ngTemplateOutlet]="menuItemSuffix" [ngTemplateOutletContext]="{parc: parc}"></ng-container>
         </div>
 
@@ -42,37 +35,17 @@
                  [ngClass]="{'loading': (loadingParc$ | async) === parc}"
                  class="d-flex align-items-center justify-content-space-between">
                 <ng-container *ngIf="(parc | parcellationSupportedInCurrentSpace | async) as sup;">
-                    <button [disabled]="!sup.supported"
-                            mat-menu-item (click)="selectParcellation(parc)"
+                    <button mat-menu-item
+                            (click)="sup.supported? selectParcellation(parc) : openChangeTemplateModal(sup.spaces, parc)"
                             [style.background-color]="(parcellation | equality : parc : trackByFn) ? 'grey' : ''">
                         {{parc.name}}
                     </button>
-                    <button *ngIf="sup.spaces && !(sup.spaces.length === 1 && sup.supported)"
-                            [matMenuTriggerFor]="otherSpacesMenu"
-                            [matMenuTriggerData]="{parc: parc, templates: sup.spaces}"
-                            mat-menu-item class="w-0 d-flex align-items-center justify-content-center">
-                        <i class="fas fa-sync"></i>
-                    </button>
                 </ng-container>
-<!--                <ng-container [ngTemplateOutlet]="selectParcButton" [ngTemplateOutletContext]="{parc:parc}"></ng-container>-->
                 <ng-container [ngTemplateOutlet]="menuItemSuffix" [ngTemplateOutletContext]="{parc: parc}"></ng-container>
             </div>
         </ng-template>
     </mat-menu>
 
-    <mat-menu #otherSpacesMenu="matMenu">
-        <ng-template matMenuContent let-parc="parc" >
-            <div *ngFor="let spaceId of (parc | parcellationSupportedInCurrentSpace | async).spaces">
-                <button *ngIf="spaceId | getSpaceById | async as space"
-                        mat-menu-item
-                        (click)="selectSpaceAndParcellation(space, parc)"
-                        [disabled]="(selectedSpace | equality : space : trackByFn) && (parcellation | equality : parc : trackByFn)">
-                    {{space.fullName}}
-                </button>
-            </div>
-        </ng-template>
-    </mat-menu>
-
     <div [style.background-color]="customColor && customColor"
          [matMenuTriggerFor]="otherParcMenu"
          #menuTrigger="matMenuTrigger"
@@ -86,22 +59,6 @@
 
 </div>
 
-<!--<ng-template #selectParcButton let-parc="parc">-->
-<!--    <ng-container *ngIf="(parc | parcellationSupportedInCurrentSpace | async) as sup;">-->
-<!--        <button [disabled]="!sup.supported"-->
-<!--                mat-menu-item (click)="selectParcellation(parc)"-->
-<!--                [style.background-color]="(parcellation | equality : parc : trackByFn) ? 'grey' : ''">-->
-<!--            {{parc.name}}-->
-<!--        </button>-->
-<!--        <button *ngIf="sup.spaces"-->
-<!--                [matMenuTriggerFor]="otherSpacesMenu"-->
-<!--                [matMenuTriggerData]="{templates: sup.spaces}"-->
-<!--                mat-menu-item class="w-0 d-flex align-items-center justify-content-center">-->
-<!--            <i class="fas fa-sync"></i>-->
-<!--        </button>-->
-<!--    </ng-container>-->
-<!--</ng-template>-->
-
 <!-- parcellation description template -->
 <ng-template #parcDescTmpl let-parc="parcellation">
     <h1 mat-dialog-title>
diff --git a/src/viewerModule/viewerCmp/viewerCmp.template.html b/src/viewerModule/viewerCmp/viewerCmp.template.html
index fac8463af..b17d590f4 100644
--- a/src/viewerModule/viewerCmp/viewerCmp.template.html
+++ b/src/viewerModule/viewerCmp/viewerCmp.template.html
@@ -416,21 +416,19 @@
 
 
     <!-- selected space chip -->
-    <ng-container *ngIf="(allAvailableSpaces$ | async) as spaces">
-      <sxplr-sapiviews-core-space-smartchip
-              *ngIf="spaces.length > 1"
-              class="sxplr-z-3"
-              [sxplr-sapiviews-core-space-smartchip-custom-color]="'grey'"
-              [sxplr-sapiviews-core-space-smartchip-space]="templateSelected$ | async"
-              [sxplr-sapiviews-core-space-smartchip-all-spaces]="allAvailableSpaces$ | async"
-              (sxplr-sapiviews-core-space-smartchip-select-space)="onSelectSpace($event)">
-      </sxplr-sapiviews-core-space-smartchip>
-    </ng-container>
+    <sxplr-sapiviews-core-space-smartchip
+            class="sxplr-z-3"
+            [sxplr-sapiviews-core-space-smartchip-custom-color]="'grey'"
+            [sxplr-sapiviews-core-space-smartchip-space]="templateSelected$ | async"
+            [sxplr-sapiviews-core-space-smartchip-all-spaces]="allAvailableSpaces$ | async"
+            (sxplr-sapiviews-core-space-smartchip-select-space)="onSelectSpace($event)">
+    </sxplr-sapiviews-core-space-smartchip>
 
     <!-- selected parcellation chip -->
     <sxplr-sapiviews-core-parcellation-smartchip
       class="sxplr-z-2 sxplr-mr-1"
       [sxplr-sapiviews-core-parcellation-smartchip-selected-space]="templateSelected$ | async"
+      [sxplr-sapiviews-core-parcellation-smartchip-selected-all-spaces]="allAvailableSpaces$ | async"
       [sxplr-sapiviews-core-parcellation-smartchip-parcellation]="parcellationSelected$ | async"
       [sxplr-sapiviews-core-parcellation-smartchip-all-parcellations]="allAvailableParcellations$ | async"
       (sxplr-sapiviews-core-parcellation-smartchip-dismiss-nonbase-layer)="onDismissNonbaseLayer()"
-- 
GitLab