import { Injectable } from '@angular/core' ; import { BehaviorSubject , Subject } from 'rxjs' ; import { FacetDeselect , FacetDeselectAll , FacetEvent , FacetSelect } from './facet-events' ; import { Facet } from './models/facet' ; @ Injectable ( ) export class FacetService { /** The list of active facets */ facets$ = new BehaviorSubject < Facet [ ] > ( [ ] ) ; /** Emit all the events when they occur */ events$ = new Subject < FacetEvent > ( ) ; select ( facet ) { if ( this . isSelected ( facet ) || facet . disabled ) { return ; } this . facets$ . next ( [ ... this . facets$ . value , facet ] ) ; this . events$ . next ( new FacetSelect ( facet ) ) ; } deselect ( facet ) { if ( ! this . isSelected ( facet ) ) { return ; } this . facets$ . next ( this . facets$ . value . filter ( _facet => _facet !== facet ) ) ; this . events$ . next ( new FacetDeselect ( facet ) ) ; } deselectAll ( ) { this . facets$ . next ( [ ] ) ; this . events$ . next ( new FacetDeselectAll ( ) ) ; } toggle ( facet ) { this . isSelected ( facet ) ? this . deselect ( facet ) : this . select ( facet ) ; } isSelected ( facet ) { return this . facets$ . value . indexOf ( facet ) > - 1 ; } }