From 5e19690d25162f945804e6d65fa65ed6ba17187d Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Wed, 31 May 2023 16:18:14 +0200
Subject: [PATCH] feat: atlas download when ready hint at user maint: external
 precomputed layer now fetches and sets transform

---
 deploy/csp/index.js                           |  2 +-
 docs/releases/v2.11.1.md                      |  4 +-
 .../atlas-download.directive.ts               |  4 +-
 src/atlas-download/atlas-download.module.ts   |  2 +
 .../point-assignment.component.ts             |  2 +-
 src/index.html                                |  2 +-
 .../topMenu/topMenuCmp/topMenu.components.ts  |  1 -
 .../topMenu/topMenuCmp/topMenu.template.html  |  6 +-
 src/viewerModule/nehuba/userLayers/service.ts | 63 ++++++++++++-------
 9 files changed, 55 insertions(+), 31 deletions(-)

diff --git a/deploy/csp/index.js b/deploy/csp/index.js
index 1d67da419..e976dd482 100644
--- a/deploy/csp/index.js
+++ b/deploy/csp/index.js
@@ -115,7 +115,7 @@ module.exports = {
           'https://unpkg.com/d3@6.2.0/', // required for preview component
           'https://unpkg.com/mathjax@3.1.2/', // math jax
           'https://unpkg.com/three-surfer@0.0.13/dist/bundle.js', // for threeSurfer (freesurfer support in browser)
-          'https://unpkg.com/ng-layer-tune@0.0.6/dist/ng-layer-tune/', // needed for ng layer control
+          'https://unpkg.com/ng-layer-tune@0.0.12/dist/ng-layer-tune/', // needed for ng layer control
           'https://unpkg.com/hbp-connectivity-component@0.6.6/', // needed for connectivity component
           (req, res) => res.locals.nonce ? `'nonce-${res.locals.nonce}'` : null,
           ...SCRIPT_SRC,
diff --git a/docs/releases/v2.11.1.md b/docs/releases/v2.11.1.md
index cd89866c7..7c187df69 100644
--- a/docs/releases/v2.11.1.md
+++ b/docs/releases/v2.11.1.md
@@ -4,6 +4,7 @@
 
 - Allow point assignment result to be sorted
 - Allow point assignment result to be downloaded as csv
+- Informs user when atlas download should be occurring, and check popup blocker
 
 ## Bugfixes
 
@@ -13,4 +14,5 @@
 
 - Bump siibra-api version dependency. Remove guard for feature type query restrictions
 - Removed unused components
-- Tweaked context menu, showing on hover effects
\ No newline at end of file
+- Tweaked context menu, showing on hover effects
+- now also attempts to fetch `transform.json` when external layer is populated
diff --git a/src/atlas-download/atlas-download.directive.ts b/src/atlas-download/atlas-download.directive.ts
index ce8733011..9b2ba9826 100644
--- a/src/atlas-download/atlas-download.directive.ts
+++ b/src/atlas-download/atlas-download.directive.ts
@@ -1,4 +1,5 @@
 import { Directive, HostListener } from '@angular/core';
+import { MatSnackBar } from '@angular/material/snack-bar';
 import { Store, select } from '@ngrx/store';
 import { Subject, concat, of } from 'rxjs';
 import { distinctUntilChanged, shareReplay, take } from 'rxjs/operators';
@@ -70,6 +71,7 @@ export class AtlasDownloadDirective {
        */
       window.open(`${endpoint}/atlas_download/${task_id}/download`, "_blank")
       this.#busy$.next(false)
+      this.snackbar.open(`Download starting. If it has not, please check your browser's popup blocker.`, 'Dismiss')
     } catch (e) {
       this.#busy$.next(false)
       this.#error$.next(e.toString())
@@ -89,6 +91,6 @@ export class AtlasDownloadDirective {
   #error$ = new Subject<string>()
   error$ = this.#error$.pipe()
 
-  constructor(private store: Store<MainState>) { }
+  constructor(private store: Store<MainState>, private snackbar: MatSnackBar) { }
 
 }
diff --git a/src/atlas-download/atlas-download.module.ts b/src/atlas-download/atlas-download.module.ts
index b4181a9cc..910154cb4 100644
--- a/src/atlas-download/atlas-download.module.ts
+++ b/src/atlas-download/atlas-download.module.ts
@@ -1,6 +1,7 @@
 import { NgModule } from '@angular/core';
 import { CommonModule } from '@angular/common';
 import { AtlasDownloadDirective } from './atlas-download.directive';
+import { MatSnackBarModule } from '@angular/material/snack-bar';
 
 
 @NgModule({
@@ -9,6 +10,7 @@ import { AtlasDownloadDirective } from './atlas-download.directive';
   ],
   imports: [
     CommonModule,
+    MatSnackBarModule,
   ],
   exports: [
     AtlasDownloadDirective
diff --git a/src/atlasComponents/sapiViews/volumes/point-assignment/point-assignment.component.ts b/src/atlasComponents/sapiViews/volumes/point-assignment/point-assignment.component.ts
index 46377dac6..90cf9b9ed 100644
--- a/src/atlasComponents/sapiViews/volumes/point-assignment/point-assignment.component.ts
+++ b/src/atlasComponents/sapiViews/volumes/point-assignment/point-assignment.component.ts
@@ -127,7 +127,7 @@ Data retrieved through siibra-api version \`${EXPECTED_SIIBRA_API_VERSION}\`
 Retrieval parameters:
 
 Point
-- coord: ${pt.coordinates.map(v => v.value).join(',')} mm
+- coord: ${pt.coordinates.map(v => v.value/1e6).join(',')} mm
 
 Parcellation
 - name: ${parc.name || parc.shortName}
diff --git a/src/index.html b/src/index.html
index 0f009f4f2..c1cef9374 100644
--- a/src/index.html
+++ b/src/index.html
@@ -14,7 +14,7 @@
   <script src="extra_js.js"></script>
   <script src="https://unpkg.com/kg-dataset-previewer@1.2.0/dist/kg-dataset-previewer/kg-dataset-previewer.js" defer></script>
   <script src="https://unpkg.com/three-surfer@0.0.13/dist/bundle.js" defer></script>
-  <script type="module" src="https://unpkg.com/ng-layer-tune@0.0.6/dist/ng-layer-tune/ng-layer-tune.esm.js"></script>
+  <script type="module" src="https://unpkg.com/ng-layer-tune@0.0.12/dist/ng-layer-tune/ng-layer-tune.esm.js"></script>
   <script type="module" src="https://unpkg.com/hbp-connectivity-component@0.6.6/dist/connectivity-component/connectivity-component.js" ></script>
   <script defer src="https://unpkg.com/mathjax@3.1.2/es5/tex-svg.js"></script>
   <script defer src="https://unpkg.com/d3@6.2.0/dist/d3.min.js"></script>
diff --git a/src/ui/topMenu/topMenuCmp/topMenu.components.ts b/src/ui/topMenu/topMenuCmp/topMenu.components.ts
index cf6922a35..688da2116 100644
--- a/src/ui/topMenu/topMenuCmp/topMenu.components.ts
+++ b/src/ui/topMenu/topMenuCmp/topMenu.components.ts
@@ -3,7 +3,6 @@ import {
   Component,
   Input,
   TemplateRef,
-  ViewChild,
 } from "@angular/core";
 import { Observable } from "rxjs";
 import { map } from "rxjs/operators";
diff --git a/src/ui/topMenu/topMenuCmp/topMenu.template.html b/src/ui/topMenu/topMenuCmp/topMenu.template.html
index a33377a72..c64c0dcee 100644
--- a/src/ui/topMenu/topMenuCmp/topMenu.template.html
+++ b/src/ui/topMenu/topMenuCmp/topMenu.template.html
@@ -22,7 +22,7 @@
 
     <!-- pinned dataset -->
     <div iav-fab-speed-dial-child>
-      <ng-container *ngTemplateOutlet="pinnedDatasetBtnTmpl">
+      <ng-container *ngTemplateOutlet="downloadAllTmpl">
       </ng-container>
     </div>
 
@@ -53,7 +53,7 @@
     </ng-container>
 
     <!-- pinned dataset -->
-    <ng-container *ngTemplateOutlet="pinnedDatasetBtnTmpl">
+    <ng-container *ngTemplateOutlet="downloadAllTmpl">
     </ng-container>
 
     <!-- help one pager -->
@@ -100,7 +100,7 @@
 </ng-template>
 
 <!-- pinned dataset btn -->
-<ng-template #pinnedDatasetBtnTmpl>
+<ng-template #downloadAllTmpl>
   <div class="btnWrapper"
     [matTooltip]="(atlasDlDct.busy$| async) ? busyTxt : idleTxt"
     quick-tour
diff --git a/src/viewerModule/nehuba/userLayers/service.ts b/src/viewerModule/nehuba/userLayers/service.ts
index 042cc5485..3fe9d0a7d 100644
--- a/src/viewerModule/nehuba/userLayers/service.ts
+++ b/src/viewerModule/nehuba/userLayers/service.ts
@@ -1,8 +1,8 @@
 import { Injectable, OnDestroy } from "@angular/core"
 import { MatDialog } from "@angular/material/dialog"
 import { select, Store } from "@ngrx/store"
-import { concat, of, Subscription } from "rxjs"
-import { pairwise } from "rxjs/operators"
+import { concat, from, of, Subscription } from "rxjs"
+import { catchError, map, pairwise, switchMap } from "rxjs/operators"
 import {
   linearTransform,
   TVALID_LINEAR_XFORM_DST,
@@ -198,29 +198,48 @@ export class UserLayerService implements OnDestroy {
   ) {
     this.#subscription.push(
       concat(
-        of(null),
-        this.routerSvc.customRoute$.pipe(select((v) => v[OVERLAY_LAYER_KEY]))
-      )
-        .pipe(pairwise())
-        .subscribe(([prev, curr]) => {
-          if (prev) {
-            this.removeUserLayer(prev)
+        of(null as string),
+        this.routerSvc.customRoute$.pipe(
+          select(v => v[OVERLAY_LAYER_KEY])
+        )
+      ).pipe(
+        pairwise(),
+        switchMap(([prev, curr]) => {
+          /**
+           * for precomputed sources, check if transform.json exists.
+           * if so, try to fetch it, and set it as transform
+           */
+          if (!curr) {
+            return of([prev, curr, null])
           }
-          if (curr) {
-            this.addUserLayer(
-              curr,
-              {
-                filename: curr,
-                message: `Overlay layer populated in URL`,
-              },
-              {
-                shader: getShader({
-                  colormap: EnumColorMapName.MAGMA,
-                }),
-              }
-            )
+          if (!curr.startsWith("precomputed://")) {
+            return of([prev, curr, null])
           }
+          return from(fetch(`${curr.replace('precomputed://', '')}/transform.json`).then(res => res.json())).pipe(
+            catchError(() => of([prev, curr, null])),
+            map(transform => [prev, curr, transform])
+          )
         })
+      ).subscribe(([prev, curr, transform]) => {
+        if (prev) {
+          this.removeUserLayer(prev)
+        }
+        if (curr) {
+          this.addUserLayer(
+            curr,
+            {
+              filename: curr,
+              message: `Overlay layer populated in URL`,
+            },
+            {
+              shader: getShader({
+                colormap: EnumColorMapName.MAGMA,
+              }),
+              transform
+            }
+          )
+        }
+      })
     )
   }
 
-- 
GitLab