From 18fecd16f1fe07e47e6429b2711a002055ea89c9 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Wed, 13 Oct 2021 18:33:34 +0200
Subject: [PATCH] chore: provide better nav when not provided

---
 src/routerModule/util.spec.ts    | 49 ++++++++++++++++++++++++++++++++
 src/routerModule/util.ts         | 21 ++++++++++++--
 src/util/pureConstant.service.ts |  2 +-
 3 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/src/routerModule/util.spec.ts b/src/routerModule/util.spec.ts
index ac15ff598..b2ed55ea8 100644
--- a/src/routerModule/util.spec.ts
+++ b/src/routerModule/util.spec.ts
@@ -6,6 +6,8 @@ import { cvtFullRouteToState, cvtStateToHashedRoutes, DummyCmp, routes } from '.
 import { encodeNumber } from './cipher'
 import { Router } from '@angular/router'
 import { RouterTestingModule } from '@angular/router/testing'
+import * as parsedRoute from './parseRouteToTmplParcReg'
+import { spaceMiscInfoMap } from 'src/util/pureConstant.service'
 
 describe('> util.ts', () => {
   describe('> cvtFullRouteToState', () => {
@@ -61,6 +63,53 @@ describe('> util.ts', () => {
       })
     })
 
+    describe('> navigation', () => {
+      let parseSpy: jasmine.Spy
+      let mapGetSpy: jasmine.Spy
+      beforeEach(() => {
+        parseSpy = spyOnProperty(parsedRoute, 'parseSearchParamForTemplateParcellationRegion')
+        mapGetSpy = spyOn(spaceMiscInfoMap, 'get')
+      })
+      it('> if not present, should show something palatable', () => {
+        parseSpy.and.returnValue(() => ({
+          parcellationSelected: {
+            id: 'dummpy-id-parc'
+          },
+          regionsSelected: [],
+          templateSelected: {
+            id: 'dummpy-id-tmpl-sel'
+          },
+        }))
+
+        const scale = 0.25
+
+        mapGetSpy.and.returnValue({ scale })
+
+        const router = TestBed.inject(Router)
+        const route = `/a:juelich:iav:atlas:v1.0.0:1/t:minds:core:referencespace:v1.0.0:dafcffc5-4826-4bf1-8ff6-46b8a31ff8e2/p:minds:core:parcellationatlas:v1.0.0:94c1125b-b87e-45e4-901c-00daee7f2579-290`
+        const parsedUrl = router.parseUrl(route)
+        const { viewerState = {} } = cvtFullRouteToState(parsedUrl, {}) || {}
+        const { navigation } = viewerState
+        const {
+          orientation,
+          perspectiveOrientation,
+          position,
+          zoom,
+          perspectiveZoom,
+        } = navigation
+
+        expect(orientation).toEqual([0,0,0,1])
+        expect(perspectiveOrientation).toEqual([
+          0.3140767216682434,
+          -0.7418519854545593,
+          0.4988985061645508,
+          -0.3195493221282959
+        ])
+        expect(position).toEqual([0,0,0])
+        expect(zoom).toEqual(350000 * scale)
+        expect(perspectiveZoom).toEqual(1922235.5293810747 * scale)
+      })
+    })
   })
 
   describe('> cvtStateToHashedRoutes', () => {
diff --git a/src/routerModule/util.ts b/src/routerModule/util.ts
index 21ccdc6e7..e0115ee62 100644
--- a/src/routerModule/util.ts
+++ b/src/routerModule/util.ts
@@ -18,6 +18,7 @@ import {
   parseSearchParamForTemplateParcellationRegion,
   encodeId,
 } from './parseRouteToTmplParcReg'
+import { spaceMiscInfoMap } from "src/util/pureConstant.service"
 
 const endcodePath = (key: string, val: string|string[]) =>
   key[0] === '?'
@@ -34,6 +35,17 @@ const decodePath = (path: string) => {
   }
 }
 
+export const DEFAULT_NAV = {
+  orientation: [0, 0, 0, 1],
+  perspectiveOrientation: [
+    0.3140767216682434,
+    -0.7418519854545593,
+    0.4988985061645508,
+    -0.3195493221282959
+  ],
+  position: [0, 0, 0],
+}
+
 export const cvtFullRouteToState = (fullPath: UrlTree, state: any, _warnCb?: (arg: string) => void) => {
 
   const warnCb = _warnCb || ((...e: any[]) => console.warn(...e))
@@ -78,7 +90,7 @@ export const cvtFullRouteToState = (fullPath: UrlTree, state: any, _warnCb?: (ar
 
   // nav obj is almost always defined, regardless if standaloneVolume or not
   const cViewerState = returnObj['@'] && returnObj['@'][0]
-  let parsedNavObj = {}
+  let parsedNavObj: any
   if (cViewerState) {
     try {
       const [ cO, cPO, cPZ, cP, cZ ] = cViewerState.split(`${separator}${separator}`)
@@ -171,7 +183,12 @@ export const cvtFullRouteToState = (fullPath: UrlTree, state: any, _warnCb?: (ar
     returnState['viewerState']['regionsSelected'] = regionsSelected
     returnState['viewerState']['templateSelected'] = templateSelected
 
-    returnState['viewerState']['navigation'] = parsedNavObj
+    const { scale } = spaceMiscInfoMap.get(templateSelected.id) || { scale: 1 }
+    returnState['viewerState']['navigation'] = parsedNavObj || ({
+      ...DEFAULT_NAV,
+      zoom: 350000 * scale,
+      perspectiveZoom: 1922235.5293810747 * scale
+    })
   } catch (e) {
     // if error, show error on UI?
     warnCb(`parse template, parc, region error`, e)
diff --git a/src/util/pureConstant.service.ts b/src/util/pureConstant.service.ts
index 74a1280e0..c6145b3d2 100644
--- a/src/util/pureConstant.service.ts
+++ b/src/util/pureConstant.service.ts
@@ -52,7 +52,7 @@ type TIAVAtlas = {
   } & THasId)[]
 } & THasId
 
-const spaceMiscInfoMap = new Map([
+export const spaceMiscInfoMap = new Map([
   ['minds/core/referencespace/v1.0.0/a1655b99-82f1-420f-a3c2-fe80fd4c8588', {
     name: 'bigbrain',
     scale: 1,
-- 
GitLab