-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.component.ts
More file actions
56 lines (50 loc) · 2.21 KB
/
app.component.ts
File metadata and controls
56 lines (50 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Component, HostListener, Inject, OnInit, DOCUMENT } from "@angular/core";
@Component({
selector: "app-root",
styleUrls: ["./app.component.scss"],
templateUrl: "./app.component.html",
standalone: false
})
export class AppComponent implements OnInit {
public title = "Samples";
private theme = "default-theme";
private styleElem: HTMLStyleElement | null = null;
private typefacesLoaded = ["Titillium Web", "Roboto"];
private typefaceUrl = "https://fonts.googleapis.com/css?family=";
constructor(@Inject(DOCUMENT) private document: Document) {}
public ngOnInit() {
// console.log("SB app ngOnInit");
this.createThemeStyle();
}
@HostListener("window:message", ["$event"])
protected onMessage(e: MessageEvent) {
if (e.origin === e.data.origin && typeof e.data.themeStyle === "string") {
if (this.styleElem) {
this.styleElem.textContent = e.data.themeStyle;
}
const typeface = window.getComputedStyle(this.document.body).fontFamily.replace(/\"/g, "");
if (!(typeface.match(/,/g) || []).length &&
!this.typefacesLoaded.includes(typeface)) {
this.typefacesLoaded.push(typeface);
this.createTypefaceLink(typeface);
}
} else if (e.origin === e.data.origin && typeof e.data.theme === "string") {
this.document.body.classList.remove(this.theme);
this.document.body.classList.add(e.data.theme);
this.theme = e.data.theme;
}
}
private createTypefaceLink(typeface: string) {
const typefaceElem = this.document.createElement("link");
typefaceElem.rel = "stylesheet";
typefaceElem.id = "ignteui-theme-typeface";
typefaceElem.href = this.typefaceUrl + typeface.split(" ").join("+");
document.head.insertBefore(typefaceElem, this.document.head.lastElementChild);
}
private createThemeStyle() {
this.styleElem = document.createElement("style");
this.styleElem.id = "igniteui-theme";
document.head.insertBefore(this.styleElem, this.document.head.lastElementChild);
this.document.body.classList.add("custom-body");
}
}