From 38d2648b5905f8a7d42d6387d727801236e66d2a Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Wed, 5 Sep 2018 10:18:13 +0200
Subject: [PATCH] removed dedicatedview url encoding, added niftiLayers

---
 src/atlasViewer/atlasViewer.component.ts      |   2 -
 .../atlasViewer.urlService.service.ts         | 128 +++++++++++-------
 src/plugin_examples/jugex/script.js           |   2 +-
 .../nehubaContainer.component.ts              |   9 +-
 .../nehubaViewer/nehubaViewer.component.ts    |   9 ++
 5 files changed, 93 insertions(+), 57 deletions(-)

diff --git a/src/atlasViewer/atlasViewer.component.ts b/src/atlasViewer/atlasViewer.component.ts
index 79d550840..bbde6aaac 100644
--- a/src/atlasViewer/atlasViewer.component.ts
+++ b/src/atlasViewer/atlasViewer.component.ts
@@ -379,8 +379,6 @@ export class AtlasViewer implements OnDestroy, OnInit, AfterViewInit {
 
   ngLayersChangeHandler(){
 
-    console.log('handle layer change',window['viewer'].layerManager.managedLayers)
-
     this.ngLayers = (window['viewer'].layerManager.managedLayers as any[]).map(obj => ({
       name : obj.name,
       type : obj.initialSpecification.type,
diff --git a/src/atlasViewer/atlasViewer.urlService.service.ts b/src/atlasViewer/atlasViewer.urlService.service.ts
index 910f4d33a..8a2d12d16 100644
--- a/src/atlasViewer/atlasViewer.urlService.service.ts
+++ b/src/atlasViewer/atlasViewer.urlService.service.ts
@@ -1,8 +1,9 @@
 import { Injectable } from "@angular/core";
 import { Store, select } from "@ngrx/store";
-import { ViewerStateInterface, isDefined, NEWVIEWER, getLabelIndexMap, SELECT_REGIONS, CHANGE_NAVIGATION, LOAD_DEDICATED_LAYER } from "../services/stateStore.service";
-import { Observable } from "rxjs";
+import { ViewerStateInterface, isDefined, NEWVIEWER, getLabelIndexMap, SELECT_REGIONS, CHANGE_NAVIGATION, LOAD_DEDICATED_LAYER, ADD_NG_LAYER } from "../services/stateStore.service";
+import { Observable,combineLatest } from "rxjs";
 import { filter, map, scan, take } from "rxjs/operators";
+import { getActiveColorMapFragmentMain } from "../ui/nehubaContainer/nehubaContainer.component";
 
 declare var window
 
@@ -12,6 +13,7 @@ declare var window
 
 export class AtlasViewerURLService{
   private changeQueryObservable$ : Observable<any>
+  private additionalNgLayers$ : Observable<any>
   constructor(private store : Store<ViewerStateInterface>){
 
     this.changeQueryObservable$ = this.store.pipe(
@@ -24,17 +26,32 @@ export class AtlasViewerURLService{
         isDefined(state.parcellationSelected))),
 
       /* map so that only a selection are serialized */
-      map(({templateSelected,regionsSelected,navigation,parcellationSelected,dedicatedView})=>({
+      map(({templateSelected,regionsSelected,navigation,parcellationSelected})=>({
         templateSelected,
         regionsSelected,
         navigation,
-        parcellationSelected,
-        dedicatedView
+        parcellationSelected
       }))
     ).pipe(
       scan((acc,val)=>Object.assign({},acc,val),{})
     )
 
+    this.additionalNgLayers$ = combineLatest(
+      this.changeQueryObservable$.pipe(
+        map(state => state.templateSelected)
+      ),
+      this.store.pipe(
+        select('ngViewerState'),
+        select('layers')
+      )
+    ).pipe(
+      map(([templateSelected, layers])=>{
+        const state = templateSelected.nehubaConfig.dataset.initialNgState
+        /* TODO currently only parameterise nifti layer */
+        return layers.filter(layer => /^nifti\:\/\//.test(layer.source) && Object.keys(state.layers).findIndex(layerName => layerName === layer.name) < 0)
+      })
+    )
+
     /* services has no ngOnInit lifecycle */
     this.subscriptions()
   }
@@ -93,59 +110,66 @@ export class AtlasViewerURLService{
         })
       }
 
-      /* TODO deprecated. keep for backwards compatiblity? */
-      const dedicatedView = searchparams.get('dedicatedView')
-      if(dedicatedView){
-        this.store.dispatch({
-          type : LOAD_DEDICATED_LAYER,
-          dedicatedView 
-        })
+      const niftiLayers = searchparams.get('niftiLayers')
+      if(niftiLayers){
+        const layers = niftiLayers.split('__')
+        /*  */
+        layers.forEach(layer => this.store.dispatch({
+          type : ADD_NG_LAYER, 
+          layer : {
+            name : layer,
+            source : `nifti://${layer}`,
+            mixability : 'nonmixable',
+            shader : getActiveColorMapFragmentMain()
+          }
+        }))
       }
     })
 
     /* pushing state to url */
-    this.changeQueryObservable$.pipe(
-      map(state=>{
-        let _ = {}
-        for(const key in state){
-          if(isDefined(state[key])){
-            switch(key){
-              case 'navigation':
-                if(
-                  isDefined(state[key].orientation) &&
-                  isDefined(state[key].perspectiveOrientation) &&
-                  isDefined(state[key].perspectiveZoom) &&
-                  isDefined(state[key].position) &&
-                  isDefined(state[key].zoom)
-                ){
-                  _[key] = [
-                    state[key].orientation.join('_'),
-                    state[key].perspectiveOrientation.join('_'),
-                    state[key].perspectiveZoom,
-                    state[key].position.join('_'),
-                    state[key].zoom 
-                  ].join('__')
-                }
-                break;
-              case 'regionsSelected':
-                _[key] = state[key].map(region=>region.labelIndex).join('_')
-                break;
-              case 'templateSelected':
-              case 'parcellationSelected':
-                _[key] = state[key].name
-                break;
-              default:
-                _[key] = state[key]
-            }
-          }else{
-            /* TODO dedicatedView is decprecated */
-            if(key === 'dedicatedView'){
-              _[key] = null
+    combineLatest(
+      this.changeQueryObservable$.pipe(
+        map(state=>{
+          let _ = {}
+          for(const key in state){
+            if(isDefined(state[key])){
+              switch(key){
+                case 'navigation':
+                  if(
+                    isDefined(state[key].orientation) &&
+                    isDefined(state[key].perspectiveOrientation) &&
+                    isDefined(state[key].perspectiveZoom) &&
+                    isDefined(state[key].position) &&
+                    isDefined(state[key].zoom)
+                  ){
+                    _[key] = [
+                      state[key].orientation.join('_'),
+                      state[key].perspectiveOrientation.join('_'),
+                      state[key].perspectiveZoom,
+                      state[key].position.join('_'),
+                      state[key].zoom 
+                    ].join('__')
+                  }
+                  break;
+                case 'regionsSelected':
+                  _[key] = state[key].map(region=>region.labelIndex).join('_')
+                  break;
+                case 'templateSelected':
+                case 'parcellationSelected':
+                  _[key] = state[key].name
+                  break;
+                default:
+                  _[key] = state[key]
+              }
             }
           }
-        }
-        return _
-      })
+          return _
+        })
+      ),
+      this.additionalNgLayers$
+    ).pipe(
+      /* TODO fix encoding of nifti path. if path has double underscore, this encoding will fail */
+      map(([navigationState, niftiLayers]) => Object.assign({}, navigationState, { niftiLayers : niftiLayers.length > 0 ? niftiLayers.map(layer => layer.name).join('__') : null }))
     ).subscribe(cleanedState=>{
       const url = new URL(window.location)
       const search = new URLSearchParams( window.location.search )
diff --git a/src/plugin_examples/jugex/script.js b/src/plugin_examples/jugex/script.js
index c251c6bd7..8286ab8d7 100644
--- a/src/plugin_examples/jugex/script.js
+++ b/src/plugin_examples/jugex/script.js
@@ -832,7 +832,7 @@
     }
     interactiveViewer.pluginControl.loadExternalLibraries(['webcomponentsLite'])
         .then(() => code())
-        .catch(console.warn)
+        .catch(console.warn.bind(this))
 })()
 
 
diff --git a/src/ui/nehubaContainer/nehubaContainer.component.ts b/src/ui/nehubaContainer/nehubaContainer.component.ts
index 36501ee38..d95b7166b 100644
--- a/src/ui/nehubaContainer/nehubaContainer.component.ts
+++ b/src/ui/nehubaContainer/nehubaContainer.component.ts
@@ -322,7 +322,12 @@ export class NehubaContainer implements OnInit, OnDestroy{
         if(newLayers.length > 0){
           const newLayersObj:any = {}
           newLayers.forEach(obj => newLayersObj[obj.name] = obj)
-          this.nehubaViewer.loadLayer(newLayersObj)
+
+          if(!this.nehubaViewer.nehubaViewer || !this.nehubaViewer.nehubaViewer.ngviewer){
+            this.nehubaViewer.initNiftiLayers.push(newLayersObj)
+          }else{
+            this.nehubaViewer.loadLayer(newLayersObj)
+          }
           this.ngLayersRegister.layers = this.ngLayersRegister.layers.concat(newLayers)
         }
 
@@ -331,7 +336,7 @@ export class NehubaContainer implements OnInit, OnDestroy{
             if(this.nehubaViewer.removeLayer({
               name : l.name
             }))
-              this.ngLayersRegister.layers = this.ngLayersRegister.layers.filter(rl => rl.name !== l.name)
+            this.ngLayersRegister.layers = this.ngLayersRegister.layers.filter(rl => rl.name !== l.name)
           })
         }
       })
diff --git a/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts b/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
index 82d79e510..53e26e19d 100644
--- a/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
+++ b/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
@@ -20,6 +20,9 @@ export class NehubaViewerUnit implements AfterViewInit,OnDestroy{
   /* only used to set initial navigation state */
   initNav : any
   initRegions : any[]
+  initNiftiLayers : any[] = []
+
+  /* deprecated */
   initDedicatedView : string[]
 
   config : any
@@ -325,6 +328,12 @@ export class NehubaViewerUnit implements AfterViewInit,OnDestroy{
       this.showSegs(this.initRegions)
     }
 
+    if(this.initNiftiLayers.length > 0){
+      this.initNiftiLayers.forEach(layer => this.loadLayer(layer))
+      this.hideAllSeg()
+    }
+
+    /* dedicated view is deprecated */
     if(this.initDedicatedView){
       this.hideAllSeg()
       const _ = {}
-- 
GitLab