Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {AfterContentChecked, Directive, ElementRef, Input} from '@angular/core'; |
| 2 | import {FeatureFlagService} from "../../service/featureFlag.service"; |
| 3 | import * as _ from 'lodash'; |
| 4 | |
| 5 | /************************************************************************ |
| 6 | Feature Flag Directive |
| 7 | Example: |
| 8 | <div featureFlag [flagName]='"<flag name>"'></div> |
| 9 | ************************************************************************/ |
| 10 | @Directive({ |
| 11 | selector: '[featureFlag]' |
| 12 | }) |
| 13 | export class BasicFeatureFlagDirective implements AfterContentChecked { |
| 14 | @Input() flagName: string; |
| 15 | element: ElementRef; |
| 16 | |
| 17 | constructor(el: ElementRef, private _featureToggleService: FeatureFlagService) { |
| 18 | this.element = el; |
| 19 | } |
| 20 | |
| 21 | ngAfterContentChecked(): void { |
| 22 | if (!_.isNil(this.element)) { |
| 23 | const isFeatureOn: boolean = this._featureToggleService.isFeatureOn(this.flagName); |
| 24 | if(!isFeatureOn){ |
| 25 | this._featureToggleService.hideElement(this.element) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |