Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 222x 222x 222x 222x 222x 222x 147x 147x 147x 147x 147x 147x 147x 147x 308x 192x 192x 81x 111x 24x 192x 147x 147x 175x 31x 31x | import { ChangeDetectorRef, DestroyRef, Directive, Input, TemplateRef, ViewContainerRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { BehaviorSubject, Subscription, combineLatest } from 'rxjs';
import { distinctUntilChanged, filter } from 'rxjs/operators';
import { FeatureToggleService, FeatureToggleType } from 'ish-core/feature-toggle.module';
/**
* Structural directive.
* Used on an element, this element will only be rendered if the specified feature *is enabled*.
*
* For the negated case see {@link NotFeatureToggleDirective}.
* For the corresponding pipe see {@link FeatureTogglePipe}.
*
* @example
* <div *ishFeature="'quoting'">
* Only visible when quoting is enabled by configuration.
* </div>
*/
@Directive({
selector: '[ishFeature]',
})
export class FeatureToggleDirective {
private otherTemplateRef: TemplateRef<unknown>;
private subscription: Subscription;
private enabled$ = new BehaviorSubject<boolean>(undefined);
private tick$ = new BehaviorSubject<void>(undefined);
private destroyRef = inject(DestroyRef);
constructor(
private templateRef: TemplateRef<unknown>,
private viewContainer: ViewContainerRef,
private featureToggle: FeatureToggleService,
private cdRef: ChangeDetectorRef
) {
combineLatest([
this.enabled$.pipe(
distinctUntilChanged(),
filter(val => typeof val === 'boolean')
),
this.tick$,
])
.pipe(takeUntilDestroyed())
.subscribe(([enabled]) => {
this.viewContainer.clear();
if (enabled) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else if (this.otherTemplateRef) {
this.viewContainer.createEmbeddedView(this.otherTemplateRef);
}
this.cdRef.markForCheck();
});
}
@Input() set ishFeature(feature: 'always' | 'never' | FeatureToggleType) {
// end previous subscription and newly subscribe
Iif (this.subscription) {
// eslint-disable-next-line ban/ban
this.subscription.unsubscribe();
}
this.subscription = this.featureToggle
.enabled$(feature)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({ next: val => this.enabled$.next(val) });
}
@Input() set ishFeatureElse(otherTemplateRef: TemplateRef<unknown>) {
this.otherTemplateRef = otherTemplateRef;
this.tick$.next();
}
}
|