-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…64)
- Loading branch information
1 parent
fdb836c
commit 9f53006
Showing
11 changed files
with
88 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core'; | ||
import { Store, select } from '@ngrx/store'; | ||
import { distinctUntilChanged, filter } from 'rxjs/operators'; | ||
|
||
import { getTheme } from 'ish-core/store/configuration'; | ||
|
||
/** | ||
* Service to add the configured/selected theme’s CSS file in the HTML’s head. | ||
* | ||
* See: "Angular: Multiple Themes Without Killing Bundle Size (With Material or Not)" by @Kmathy15 | ||
* https://medium.com/better-programming/angular-multiple-themes-without-killing-bundle-size-with-material-or-not-5a80849b6b34 | ||
*/ | ||
@Injectable({ providedIn: 'root' }) | ||
export class ThemeService { | ||
private renderer: Renderer2; | ||
private head: HTMLElement; | ||
private themeLinks: HTMLElement[] = []; | ||
|
||
constructor( | ||
private rendererFactory: RendererFactory2, | ||
@Inject(DOCUMENT) private document: Document, | ||
private store: Store<{}> | ||
) {} | ||
|
||
init() { | ||
this.head = this.document.head; | ||
this.renderer = this.rendererFactory.createRenderer(undefined, undefined); | ||
this.store | ||
.pipe(select(getTheme)) | ||
.pipe( | ||
distinctUntilChanged(), | ||
filter(x => !!x) | ||
) | ||
.subscribe(async theme => { | ||
await this.loadCss(`${theme}.css`); | ||
|
||
// remove style of previous theme | ||
if (this.themeLinks.length === 2) { | ||
this.renderer.removeChild(this.head, this.themeLinks.shift()); | ||
} | ||
}); | ||
} | ||
|
||
private async loadCss(filename: string) { | ||
return new Promise(resolve => { | ||
const linkEl: HTMLElement = this.renderer.createElement('link'); | ||
this.renderer.setAttribute(linkEl, 'rel', 'stylesheet'); | ||
this.renderer.setAttribute(linkEl, 'type', 'text/css'); | ||
this.renderer.setAttribute(linkEl, 'href', filename); | ||
this.renderer.setProperty(linkEl, 'onload', resolve); | ||
this.renderer.appendChild(this.head, linkEl); | ||
this.themeLinks = [...this.themeLinks, linkEl]; | ||
}); | ||
} | ||
} |
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