From bde8fbac98405c0084ba7a6476bf7ca92cd4be48 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 15 Jun 2023 18:52:13 -0400 Subject: [PATCH] Implement only-vsize and only-weight directives --- .../weight-directives/weight-directives.ts | 45 +++++++++++++++++++ frontend/src/app/shared/shared.module.ts | 8 ++++ 2 files changed, 53 insertions(+) create mode 100644 frontend/src/app/shared/components/weight-directives/weight-directives.ts diff --git a/frontend/src/app/shared/components/weight-directives/weight-directives.ts b/frontend/src/app/shared/components/weight-directives/weight-directives.ts new file mode 100644 index 000000000..879b6bee6 --- /dev/null +++ b/frontend/src/app/shared/components/weight-directives/weight-directives.ts @@ -0,0 +1,45 @@ +import { Directive, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core'; +import { Subscription } from 'rxjs'; +import { StateService } from '../../../services/state.service'; + +function createRateUnitDirective(checkFn: (rateUnit: string) => boolean): any { + @Directive() + class RateUnitDirective implements OnDestroy { + private subscription: Subscription; + private enabled: boolean; + private hasView: boolean = false; + + constructor( + private templateRef: TemplateRef, + private viewContainer: ViewContainerRef, + private stateService: StateService + ) { + this.subscription = this.stateService.rateUnits$.subscribe(rateUnit => { + this.enabled = checkFn(rateUnit); + this.updateView(); + }); + } + + updateView(): void { + if (this.enabled && !this.hasView) { + this.viewContainer.createEmbeddedView(this.templateRef); + this.hasView = true; + } else if (!this.enabled && this.hasView) { + this.viewContainer.clear(); + this.hasView = false; + } + } + + ngOnDestroy(): void { + this.subscription.unsubscribe(); + } + } + + return RateUnitDirective; +} + +@Directive({ selector: '[only-vsize]' }) +export class OnlyVsizeDirective extends createRateUnitDirective(rateUnit => rateUnit !== 'wu') {} + +@Directive({ selector: '[only-weight]' }) +export class OnlyWeightDirective extends createRateUnitDirective(rateUnit => rateUnit === 'wu') {} \ No newline at end of file diff --git a/frontend/src/app/shared/shared.module.ts b/frontend/src/app/shared/shared.module.ts index d68f1e4e0..5d9b91e11 100644 --- a/frontend/src/app/shared/shared.module.ts +++ b/frontend/src/app/shared/shared.module.ts @@ -98,6 +98,8 @@ import { ClockchainComponent } from '../components/clockchain/clockchain.compone import { ClockFaceComponent } from '../components/clock-face/clock-face.component'; import { ClockComponent } from '../components/clock/clock.component'; +import { OnlyVsizeDirective, OnlyWeightDirective } from './components/weight-directives/weight-directives'; + @NgModule({ declarations: [ ClipboardComponent, @@ -188,6 +190,9 @@ import { ClockComponent } from '../components/clock/clock.component'; ClockchainComponent, ClockComponent, ClockFaceComponent, + + OnlyVsizeDirective, + OnlyWeightDirective ], imports: [ CommonModule, @@ -303,6 +308,9 @@ import { ClockComponent } from '../components/clock/clock.component'; ClockchainComponent, ClockComponent, ClockFaceComponent, + + OnlyVsizeDirective, + OnlyWeightDirective ] }) export class SharedModule {