-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored
core/browser
services (#213)
* added favicon ref enum * moved services into `core/browser` * missed a service rename * converted `FaviconService` to use `Renderer2` * converted `ColorsService` to use `Renderer2` * adds sizes any to favicon.svg
- Loading branch information
1 parent
1d4e748
commit f97406d
Showing
14 changed files
with
97 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,59 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { environment } from 'src/environments/environment'; | ||
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core'; | ||
import { FaviconRef } from 'src/app/core/svg/favicon-ref.enum'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class FaviconService { | ||
private readonly head: HTMLHeadElement; | ||
private readonly renderer: Renderer2; | ||
|
||
constructor( | ||
@Inject(DOCUMENT) private readonly document: HTMLDocument, | ||
private readonly rendererFactory: RendererFactory2, | ||
) { | ||
this.initializeFavicon(); | ||
this.head = this.document.head; | ||
this.renderer = this.rendererFactory.createRenderer(this.head, null); | ||
|
||
this.replaceFavicon(FaviconRef.getDefault()); | ||
} | ||
|
||
private initializeFavicon(): void { | ||
const favicon = this.document.getElementById('favicon'); | ||
private replaceFavicon(faviconRef: FaviconRef): void { | ||
const favicon = this.svgFavicon; | ||
|
||
if (favicon?.getAttribute('href') !== faviconRef) { | ||
this.removeFavicon(favicon); | ||
this.addFavicon(faviconRef); | ||
} | ||
} | ||
|
||
if (favicon.getAttribute('href') !== environment.iconRef) { | ||
this.removeFavicon(); | ||
this.addFavicon(); | ||
private removeFavicon(favicon: HTMLLinkElement): void { | ||
if (typeof favicon !== 'undefined') { | ||
this.renderer.removeChild(this.head, favicon); | ||
} | ||
} | ||
|
||
private removeFavicon(): void { | ||
const favicon = this.document.getElementById('favicon'); | ||
private addFavicon(faviconRef: FaviconRef): void { | ||
const favicon = this.renderer.createElement('link'); | ||
|
||
favicon.parentNode.removeChild(favicon); | ||
this.renderer.setAttribute(favicon, 'rel', 'icon'); | ||
this.renderer.setAttribute(favicon, 'type', 'image/svg+xml'); | ||
this.renderer.setAttribute(favicon, 'sizes', 'any'); | ||
this.renderer.setAttribute(favicon, 'href', faviconRef); | ||
|
||
this.renderer.insertBefore(this.head, favicon, this.appleTouchIcon); | ||
} | ||
|
||
private addFavicon(): void { | ||
const favicon = this.document.createElement('link'); | ||
private get svgFavicon(): HTMLLinkElement { | ||
return Array.from(this.favicons) | ||
.find(favicon => favicon.getAttribute('type') === 'image/svg+xml'); | ||
} | ||
|
||
favicon.setAttribute('id', 'favicon'); | ||
favicon.setAttribute('rel', 'icon'); | ||
favicon.setAttribute('type', 'image/svg+xml'); | ||
favicon.setAttribute('href', environment.iconRef); | ||
private get appleTouchIcon(): HTMLLinkElement { | ||
return Array.from(this.favicons) | ||
.find(favicon => favicon.getAttribute('type') === 'apple-touch-icon'); | ||
} | ||
|
||
this.document.head.appendChild(favicon); | ||
private get favicons(): NodeListOf<HTMLLinkElement> { | ||
return this.head.querySelectorAll('link[rel="icon"]'); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { environment } from 'src/environments/environment'; | ||
|
||
export enum FaviconRef { | ||
DEV_DEFAULT = 'assets/icon/inanity.dev.svg', | ||
PROD_DEFAULT = 'assets/icon/inanity.svg', | ||
} | ||
|
||
export namespace FaviconRef { | ||
export function getDefault(): FaviconRef { | ||
return environment.production | ||
? FaviconRef.PROD_DEFAULT | ||
: FaviconRef.DEV_DEFAULT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters