1 import { DOCUMENT } from '@angular/common';
2 import { Inject, Injectable, OnDestroy } from '@angular/core';
4 import { Subscription } from 'rxjs';
6 import { Color } from '../enum/color.enum';
7 import { SummaryService } from './summary.service';
10 export class FaviconService implements OnDestroy {
16 @Inject(DOCUMENT) private document: HTMLDocument,
17 private summaryService: SummaryService
21 this.url = this.document.getElementById('cdFavicon')?.getAttribute('href');
23 this.sub = this.summaryService.subscribe((summary) => {
24 this.changeIcon(summary.health_status);
28 changeIcon(status?: string) {
29 if (status === this.oldStatus) {
33 this.oldStatus = status;
35 const favicon = this.document.getElementById('cdFavicon');
36 const faviconSize = 16;
37 const radius = faviconSize / 4;
39 const canvas = this.document.createElement('canvas');
40 canvas.width = faviconSize;
41 canvas.height = faviconSize;
43 const context = canvas.getContext('2d');
44 const img = this.document.createElement('img');
48 // Draw Original Favicon as Background
49 context.drawImage(img, 0, 0, faviconSize, faviconSize);
52 // Cut notification circle area
54 context.globalCompositeOperation = 'destination-out';
56 context.arc(canvas.width - radius, radius, radius + 2, 0, 2 * Math.PI);
60 // Draw Notification Circle
62 context.arc(canvas.width - radius, radius, radius, 0, 2 * Math.PI);
64 context.fillStyle = Color[status];
69 favicon.setAttribute('href', canvas.toDataURL('image/png'));
75 this.sub?.unsubscribe();