Newer
Older
import { Injectable } from "@angular/core";
import { select, Store } from "@ngrx/store";
import { from, Observable } from "rxjs";
import { map, switchMap } from "rxjs/operators";
import { NgSegLayerSpec, SxplrRegion } from "src/atlasComponents/sapi/sxplrTypes";
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { atlasSelection } from "src/state";
import { getParcNgId } from "../config.service";
type NgMapReturnType = {
region: SxplrRegion
layer: NgSegLayerSpec
}
@Injectable({
providedIn: 'root'
})
export class BaseService {
constructor(private sapi: SAPI, private store$: Store){}
public selectedATP$ = this.store$.pipe(
atlasSelection.fromRootStore.distinctATP(),
)
public selectedATPR$ = this.selectedATP$.pipe(
switchMap(({ atlas, template, parcellation }) =>
this.store$.pipe(
select(atlasSelection.selectors.selectedParcAllRegions),
map(regions => ({
atlas, template, parcellation, regions
})),
)
)
)
#translatedNgMap = this.selectedATPR$.pipe(
switchMap(({ atlas, parcellation, regions, template }) =>
from(this.sapi.getTranslatedLabelledNgMap(parcellation, template)).pipe(
map(record => {
const regionmap = new Map<string, SxplrRegion>()
for (const r of regions) {
regionmap.set(r.name, r)
}
const returnVal: Record<string, Record<number, NgMapReturnType>> = {}
for (const [ /* url */ , { layer, region }] of Object.entries(record)) {
const actualRegion = regionmap.get(name) || (() => {
console.log(`region with name ${name} cannot be found. Viewer may not behave properly`)
return { name, id: '', parentIds: [], type: 'SxplrRegion' }
})()
const ngId = getParcNgId(atlas, template, parcellation, actualRegion)
if (!returnVal[ngId]) {
returnVal[ngId] = {}
}
returnVal[ngId][label] = {
region: actualRegion,
layer
}
}
}
return returnVal
})
)
)
)
public completeNgIdLabelRegionMap$: Observable<Record<string, Record<number, SxplrRegion>>> = this.#translatedNgMap.pipe(
map(val => {
const returnObj: Record<string, Record<number, SxplrRegion>> = {}
for (const [ ngId, obj ] of Object.entries(val)) {
for (const [label, layer] of Object.entries(obj)) {
if (!returnObj[ngId]) {
returnObj[ngId] = {}
}
returnObj[ngId][label] = layer.region
}
}
return returnObj
})
)
}