import { Inject , Injectable , Optional , SkipSelf } from '@angular/core' ; import { Subject } from 'rxjs' ; import { IconModuleOptions } from './icon-options.interface' ; import { ICON_OPTIONS_TOKEN } from './icon-options.token' ; import { IconDefinition , SingleIconDefinition } from './iconsets/iconset.interface' ; import { uxIconset } from './iconsets/ux-iconset' ; @ Injectable ( ) export class IconService { /** Emit whenever the iconset changes */ iconsChanged$ = new Subject < IconChangeEvent > ( ) ; /** Store a list of all icon */ private _icons : ReadonlyArray < SingleIconDefinition > = [ ... uxIconset ] ; /** Inject a parent service if one exists */ constructor ( @ Optional ( ) @ SkipSelf ( ) private _iconService , @ Optional ( ) @ Inject ( ICON_OPTIONS_TOKEN ) options ? ) { if ( options && options . icons ) { this . setIcons ( options . icons ) ; } } /** Define multiple icon definitions. This will override icon definitions if a name and size collision occurs */ setIcons ( icons ) { icons . forEach ( icon => this . setIcon ( icon ) ) ; } /** Provide an icon definition which will override if necessary */ setIcon ( { name , icon , iconset , size } ) { if ( Array . isArray ( size ) ) { return size . forEach ( variant => this . setIcon ( { name , icon , iconset , size : variant } ) ) ; } this . _icons = this . _icons . filter ( definition => ! ( definition . name === name && definition . size === size ) ) ; this . _icons = [ ... this . _icons , { name , icon , iconset , size } ] ; this . iconsChanged$ . next ( { name , size } ) ; } /** Find an icon based on the given name and size if provided */ getIcon ( name , size ? ) { if ( ! name ) { return ; } if ( size ) { const sizedIcon = this . _icons . find ( definition => definition . name === name && definition . size === size ) ; if ( sizedIcon ) { return sizedIcon ; } } const icon = this . _icons . find ( definition => definition . name === name && definition . size === undefined ) ; if ( ! icon && this . _iconService ) { return this . _iconService . getIcon ( name , size ) ; } else if ( ! icon ) { console . warn ( `Icon '${ name }' was not found.` ) ; } return icon ; } } export interface IconChangeEvent { name : string ; size ? : string ; }