import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { HoverActionDirective } from './hover-action.directive' ; @ Injectable ( ) export class HoverActionService { active : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ; private _focused : boolean = false ; private _hovered : boolean = false ; private _actions : HoverActionDirective [ ] = [ ] ; register ( action ) { this . _actions . push ( action ) ; } unregister ( action ) { this . _actions = this . _actions . filter ( actn => actn !== action ) ; } setFocusState ( focus ) { this . _focused = focus ; this . updateVisibility ( ) ; } setHoverState ( hover ) { this . _hovered = hover ; this . updateVisibility ( ) ; } updateVisibility ( ) { this . active . next ( this . _focused || this . _hovered || this . actionHasFocus ( ) ) ; } private actionHasFocus ( ) { return ! ! this . getFocusedAction ( ) ; } private getFocusedAction ( ) { return this . _actions . find ( action => action . focused ) ; } }