import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { AvatarConfigService } from './avatar-config.service' ; import { AvatarSource } from './sources/avatar-source.enum' ; import { Source } from './sources/source' ; /**
 * list of Supported avatar sources
 */ export const defaultSources = [ AvatarSource . FACEBOOK , AvatarSource . GOOGLE , AvatarSource . TWITTER , AvatarSource . INSTAGRAM , AvatarSource . VKONTAKTE , AvatarSource . SKYPE , AvatarSource . GRAVATAR , AvatarSource . GITHUB , AvatarSource . CUSTOM , AvatarSource . INITIALS , AvatarSource . VALUE ] ; /**
 * list of default colors
 */ export const defaultColors = [ '#1abc9c' , '#3498db' , '#f1c40f' , '#8e44ad' , '#e74c3c' , '#d35400' , '#2c3e50' , '#7f8c8d' ] ; /**
 * Provides utilities methods related to Avatar component
 */ @ Injectable ( ) export class AvatarService { public avatarSources : AvatarSource [ ] = defaultSources ; public avatarColors : string [ ] = defaultColors ; private readonly failedSources = new Map < string , Source > ( ) ; constructor ( private http , private avatarConfigService ) { this . overrideAvatarSources ( ) ; this . overrideAvatarColors ( ) ; } public fetchAvatar ( avatarUrl ) { return this . http . get ( avatarUrl ) ; } public getRandomColor ( avatarText ) { if ( ! avatarText ) { return 'transparent' ; } const asciiCodeSum = this . calculateAsciiCode ( avatarText ) ; return this . avatarColors [ asciiCodeSum % this . avatarColors . length ] ; } public compareSources ( sourceType1 , sourceType2 ) { return ( this . getSourcePriority ( sourceType1 ) - this . getSourcePriority ( sourceType2 ) ) ; } public isSource ( source ) { return this . avatarSources . includes ( source as AvatarSource ) ; } public isTextAvatar ( sourceType ) { return [ AvatarSource . INITIALS , AvatarSource . VALUE ] . includes ( sourceType ) ; } private buildSourceKey ( source ) { return source . sourceType + '-' + source . sourceId ; } public sourceHasFailedBefore ( source ) { return this . failedSources . has ( this . buildSourceKey ( source ) ) ; } public markSourceAsFailed ( source ) { this . failedSources . set ( this . buildSourceKey ( source ) , source ) ; } private overrideAvatarSources ( ) { this . avatarSources = this . avatarConfigService . getAvatarSources ( defaultSources ) ; } private overrideAvatarColors ( ) { this . avatarColors = this . avatarConfigService . getAvatarColors ( defaultColors ) ; } private calculateAsciiCode ( value ) { return value . split ( '' ) . map ( letter => letter . charCodeAt ( 0 ) ) . reduce ( ( previous , current ) => previous + current ) ; } private getSourcePriority ( sourceType ) { return this . avatarSources . indexOf ( sourceType ) ; } }