Skip to content
Snippets Groups Projects
Commit 2c21c25a authored by Xiao Gui's avatar Xiao Gui
Browse files

chore: only show regional features if available

parent 512109e1
No related branches found
No related tags found
No related merge requests found
import { Directive, EventEmitter, OnDestroy, Output } from "@angular/core"; import { Directive, EventEmitter, Inject, OnDestroy, Optional, Output } from "@angular/core";
import { KG_REGIONAL_FEATURE_KEY, TBSSummary } from "../type"; import { KG_REGIONAL_FEATURE_KEY, TBSSummary } from "../type";
import { BsFeatureService } from "../../service"; import { BsFeatureService, TFeatureCmpInput } from "../../service";
import { BsRegionInputBase } from "../../bsRegionInputBase"; import { BsRegionInputBase } from "../../bsRegionInputBase";
import { merge, of, Subscription } from "rxjs"; import { merge, of, Subscription } from "rxjs";
import { filter, mapTo, startWith, switchMap, tap } from "rxjs/operators"; import { catchError, mapTo, startWith, switchMap, tap } from "rxjs/operators";
import { IRegionalFeatureReadyDirective } from "../../type"; import { IRegionalFeatureReadyDirective } from "../../type";
import { REGISTERED_FEATURE_INJECT_DATA } from "../../constants";
@Directive({ @Directive({
selector: '[kg-regional-features-list-directive]', selector: '[kg-regional-features-list-directive]',
...@@ -14,21 +15,27 @@ import { IRegionalFeatureReadyDirective } from "../../type"; ...@@ -14,21 +15,27 @@ import { IRegionalFeatureReadyDirective } from "../../type";
export class KgRegionalFeaturesListDirective extends BsRegionInputBase implements IRegionalFeatureReadyDirective, OnDestroy { export class KgRegionalFeaturesListDirective extends BsRegionInputBase implements IRegionalFeatureReadyDirective, OnDestroy {
public kgRegionalFeatures: TBSSummary[] = [] public kgRegionalFeatures: TBSSummary[] = []
public kgRegionalFeatures$ = this.region$.pipe( public kgRegionalFeatures$ = this.region$.pipe(
filter(v => !!v),
// must not use switchmapto here // must not use switchmapto here
switchMap(() => { switchMap(() => {
this.busyEmitter.emit(true) this.busyEmitter.emit(true)
return this.getFeatureInstancesList(KG_REGIONAL_FEATURE_KEY).pipe( return merge(
tap(() => { of([]),
this.busyEmitter.emit(false) this.getFeatureInstancesList(KG_REGIONAL_FEATURE_KEY).pipe(
}) catchError(() => of([])),
tap(() => {
this.busyEmitter.emit(false)
}),
)
) )
}), }),
startWith([]) startWith([])
) )
constructor(svc: BsFeatureService){ constructor(
super(svc) svc: BsFeatureService,
@Optional() @Inject(REGISTERED_FEATURE_INJECT_DATA) data: TFeatureCmpInput,
){
super(svc, data)
this.sub.push( this.sub.push(
this.kgRegionalFeatures$.subscribe(val => { this.kgRegionalFeatures$.subscribe(val => {
this.kgRegionalFeatures = val this.kgRegionalFeatures = val
......
import { Component, ComponentFactory, ComponentFactoryResolver, Injector, Input, OnChanges, ViewChild, ViewContainerRef } from "@angular/core"; import { Component, ComponentFactory, ComponentFactoryResolver, Injector, Input, OnChanges, ViewChild, ViewContainerRef } from "@angular/core";
import { TRegion } from "../type"; import { TRegion } from "../type";
import { BsFeatureService, TFeatureCmpInput, TRegisteredFeature } from "../service"; import { BsFeatureService, TFeatureCmpInput } from "../service";
import { BehaviorSubject, Observable } from "rxjs"; import { BehaviorSubject, combineLatest, Observable } from "rxjs";
import { map } from "rxjs/operators"; import { map, scan } from "rxjs/operators";
import { REGISTERED_FEATURE_INJECT_DATA } from "../constants"; import { REGISTERED_FEATURE_INJECT_DATA } from "../constants";
@Component({ @Component({
...@@ -23,13 +23,37 @@ export class RegionalFeatureWrapperCmp implements OnChanges{ ...@@ -23,13 +23,37 @@ export class RegionalFeatureWrapperCmp implements OnChanges{
private weakmap = new WeakMap<(new () => any), ComponentFactory<any>>() private weakmap = new WeakMap<(new () => any), ComponentFactory<any>>()
public registeredFeatures$ = this.svc.registeredFeatures$ public registeredFeatures$: Observable<{
name: string
icon: string
}[]>
constructor( constructor(
private svc: BsFeatureService, private svc: BsFeatureService,
private cfr: ComponentFactoryResolver, private cfr: ComponentFactoryResolver,
private injector: Injector, private injector: Injector,
){ ){
this.registeredFeatures$ = this.registeredFeatureMasterStream$.pipe(
scan((acc, curr) => {
return {
...acc,
...curr
}
}),
map(obj => {
const returnArr = []
for (const name in obj) {
if (obj[name].busy || obj[name].results.length === 0) {
continue
}
returnArr.push({
name,
icon: obj[name].icon
})
}
return returnArr
})
)
} }
private regionOnDestroyCb: (() => void)[] = [] private regionOnDestroyCb: (() => void)[] = []
...@@ -37,11 +61,15 @@ export class RegionalFeatureWrapperCmp implements OnChanges{ ...@@ -37,11 +61,15 @@ export class RegionalFeatureWrapperCmp implements OnChanges{
if (!this.region) return if (!this.region) return
const { region } = this const { region } = this
for (const feat of this.svc.registeredFeatures){ for (const feat of this.svc.registeredFeatures){
const { name, icon } = feat
const ctrl = new feat.Ctrl(this.svc, { region }) const ctrl = new feat.Ctrl(this.svc, { region })
const sub = ctrl.busy$.subscribe( const sub = combineLatest([
flag => { ctrl.busy$,
this.busyMasterStream$.next({ ctrl.results$
[feat.name]: flag ]).subscribe(
([busy, results]) => {
this.registeredFeatureMasterStream$.next({
[name]: { busy, results, icon }
}) })
} }
) )
...@@ -50,16 +78,20 @@ export class RegionalFeatureWrapperCmp implements OnChanges{ ...@@ -50,16 +78,20 @@ export class RegionalFeatureWrapperCmp implements OnChanges{
} }
private cleanUpRegionalFeature(){ private cleanUpRegionalFeature(){
while (this.regionOnDestroyCb.length) this.regionOnDestroyCb.pop()() while (this.regionOnDestroyCb.length) this.regionOnDestroyCb.pop()()
this.busyMasterStream$.next({}) this.registeredFeatureMasterStream$.next({})
} }
private busyMasterStream$ = new BehaviorSubject<{ private registeredFeatureMasterStream$ = new BehaviorSubject<{
[key: string]: boolean [key: string]: {
icon: string
busy: boolean
results: any[]
}
}>({}) }>({})
public busy$: Observable<boolean> = this.busyMasterStream$.pipe( public busy$: Observable<boolean> = this.registeredFeatureMasterStream$.pipe(
map(obj => { map(obj => {
for (const key in obj) { for (const key in obj) {
if(obj[key]) return true if(obj[key].busy) return true
} }
return false return false
}), }),
...@@ -71,8 +103,15 @@ export class RegionalFeatureWrapperCmp implements OnChanges{ ...@@ -71,8 +103,15 @@ export class RegionalFeatureWrapperCmp implements OnChanges{
} }
public activatedFeatureName: string = null public activatedFeatureName: string = null
activateFeature(feat: TRegisteredFeature){ activateFeature(featNameObj: {
this.activatedFeatureName = feat.name name: string
}){
const feat = this.svc.registeredFeatures.find(f => f.name === featNameObj.name)
if (!feat) {
console.log(`cannot find feature with name ${featNameObj.name}`)
return
}
this.activatedFeatureName = featNameObj.name
if (!this.regionalFeatureContainerRef) { if (!this.regionalFeatureContainerRef) {
console.warn(`regionalFeatureContainerRef not defined.`) console.warn(`regionalFeatureContainerRef not defined.`)
return return
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment