]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { first } from 'rxjs/operators' |
1a00c561 | 2 | import { Injectable } from '@angular/core' |
a4ff3100 | 3 | import { UserLocalStorageKeys } from '@root-helpers/users' |
2989628b | 4 | import { HTMLServerConfig, ServerConfigTheme } from '@shared/models' |
a4ff3100 | 5 | import { environment } from '../../../environments/environment' |
67ed6552 C |
6 | import { AuthService } from '../auth' |
7 | import { PluginService } from '../plugins/plugin.service' | |
8 | import { ServerService } from '../server' | |
67ed6552 | 9 | import { UserService } from '../users/user.service' |
a4ff3100 | 10 | import { LocalStorageService } from '../wrappers/storage.service' |
1a00c561 RK |
11 | |
12 | @Injectable() | |
13 | export class ThemeService { | |
ffb321be C |
14 | |
15 | private oldThemeName: string | |
16 | private themes: ServerConfigTheme[] = [] | |
17 | ||
a2ffd046 C |
18 | private themeFromLocalStorage: ServerConfigTheme |
19 | private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] | |
20 | ||
2989628b | 21 | private serverConfig: HTMLServerConfig |
ba430d75 | 22 | |
ffb321be C |
23 | constructor ( |
24 | private auth: AuthService, | |
d3217560 | 25 | private userService: UserService, |
ffb321be | 26 | private pluginService: PluginService, |
d3217560 RK |
27 | private server: ServerService, |
28 | private localStorageService: LocalStorageService | |
ffb321be C |
29 | ) {} |
30 | ||
31 | initialize () { | |
a2ffd046 C |
32 | // Try to load from local storage first, so we don't have to wait network requests |
33 | this.loadAndSetFromLocalStorage() | |
34 | ||
2989628b C |
35 | this.serverConfig = this.server.getHTMLConfig() |
36 | const themes = this.serverConfig.theme.registered | |
ba430d75 | 37 | |
2989628b C |
38 | this.removeThemeFromLocalStorageIfNeeded(themes) |
39 | this.injectThemes(themes) | |
a2ffd046 | 40 | |
2989628b | 41 | this.listenUserTheme() |
ffb321be C |
42 | } |
43 | ||
a2ffd046 C |
44 | private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) { |
45 | this.themes = themes | |
ffb321be C |
46 | |
47 | console.log('Injecting %d themes.', this.themes.length) | |
48 | ||
a2ffd046 | 49 | const head = this.getHeadElement() |
ffb321be C |
50 | |
51 | for (const theme of this.themes) { | |
a2ffd046 C |
52 | // Already added this theme? |
53 | if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue | |
ffb321be C |
54 | |
55 | for (const css of theme.css) { | |
56 | const link = document.createElement('link') | |
57 | ||
58 | const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}` | |
59 | link.setAttribute('href', href) | |
60 | link.setAttribute('rel', 'alternate stylesheet') | |
61 | link.setAttribute('type', 'text/css') | |
62 | link.setAttribute('title', theme.name) | |
63 | link.setAttribute('disabled', '') | |
64 | ||
a2ffd046 C |
65 | if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link) |
66 | ||
ffb321be C |
67 | head.appendChild(link) |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | private getCurrentTheme () { | |
a2ffd046 C |
73 | if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name |
74 | ||
d3217560 RK |
75 | const theme = this.auth.isLoggedIn() |
76 | ? this.auth.getUser().theme | |
77 | : this.userService.getAnonymousUser().theme | |
ffb321be | 78 | |
d3217560 | 79 | if (theme !== 'instance-default') return theme |
5c48aa8c C |
80 | |
81 | const instanceTheme = this.serverConfig.theme.default | |
82 | if (instanceTheme !== 'default') return instanceTheme | |
83 | ||
84 | // Default to dark theme if available and wanted by the user | |
85 | if ( | |
86 | this.themes.find(t => t.name === 'dark') && | |
87 | window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches | |
88 | ) { | |
89 | return 'dark' | |
90 | } | |
91 | ||
92 | return instanceTheme | |
1a00c561 RK |
93 | } |
94 | ||
ffb321be C |
95 | private loadTheme (name: string) { |
96 | const links = document.getElementsByTagName('link') | |
97 | for (let i = 0; i < links.length; i++) { | |
98 | const link = links[ i ] | |
99 | if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) { | |
100 | link.disabled = link.getAttribute('title') !== name | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | private updateCurrentTheme () { | |
a2ffd046 | 106 | if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName) |
ffb321be C |
107 | |
108 | const currentTheme = this.getCurrentTheme() | |
109 | ||
110 | console.log('Enabling %s theme.', currentTheme) | |
111 | ||
112 | this.loadTheme(currentTheme) | |
d00dc28d | 113 | |
ffb321be C |
114 | const theme = this.getTheme(currentTheme) |
115 | if (theme) { | |
116 | console.log('Adding scripts of theme %s.', currentTheme) | |
72f611ca | 117 | |
f0c5e8b6 | 118 | this.pluginService.addPlugin(theme, true) |
ffb321be C |
119 | |
120 | this.pluginService.reloadLoadedScopes() | |
a2ffd046 | 121 | |
a4ff3100 | 122 | this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) |
a2ffd046 | 123 | } else { |
a4ff3100 | 124 | this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) |
e3f7f600 | 125 | } |
ffb321be C |
126 | |
127 | this.oldThemeName = currentTheme | |
128 | } | |
129 | ||
130 | private listenUserTheme () { | |
a2ffd046 C |
131 | // We don't need them anymore |
132 | this.themeFromLocalStorage = undefined | |
133 | this.themeDOMLinksFromLocalStorage = [] | |
134 | ||
d00dc28d C |
135 | if (!this.auth.isLoggedIn()) { |
136 | this.updateCurrentTheme() | |
d3217560 | 137 | |
a4ff3100 | 138 | this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( |
d3217560 RK |
139 | () => this.updateCurrentTheme() |
140 | ) | |
d00dc28d C |
141 | } |
142 | ||
ffb321be | 143 | this.auth.userInformationLoaded |
7c93905d | 144 | .pipe(first()) |
ffb321be | 145 | .subscribe(() => this.updateCurrentTheme()) |
1a00c561 RK |
146 | } |
147 | ||
a2ffd046 | 148 | private loadAndSetFromLocalStorage () { |
a4ff3100 | 149 | const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) |
a2ffd046 C |
150 | if (!lastActiveThemeString) return |
151 | ||
152 | try { | |
153 | const lastActiveTheme = JSON.parse(lastActiveThemeString) | |
154 | this.themeFromLocalStorage = lastActiveTheme | |
155 | ||
156 | this.injectThemes([ lastActiveTheme ], true) | |
157 | this.updateCurrentTheme() | |
158 | } catch (err) { | |
159 | console.error('Cannot parse last active theme.', err) | |
160 | return | |
161 | } | |
162 | } | |
163 | ||
164 | private removeThemePlugins (themeName: string) { | |
165 | const oldTheme = this.getTheme(themeName) | |
166 | if (oldTheme) { | |
167 | console.log('Removing scripts of old theme %s.', themeName) | |
168 | this.pluginService.removePlugin(oldTheme) | |
169 | } | |
170 | } | |
171 | ||
172 | private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) { | |
173 | if (!this.themeFromLocalStorage) return | |
174 | ||
175 | const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name) | |
176 | if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) { | |
177 | // Need to remove this theme: we loaded an old version or a theme that does not exist anymore | |
178 | this.removeThemePlugins(this.themeFromLocalStorage.name) | |
179 | this.oldThemeName = undefined | |
180 | ||
181 | const head = this.getHeadElement() | |
182 | for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) { | |
183 | head.removeChild(htmlLinkElement) | |
184 | } | |
185 | ||
186 | this.themeFromLocalStorage = undefined | |
187 | this.themeDOMLinksFromLocalStorage = [] | |
188 | } | |
189 | } | |
190 | ||
191 | private getHeadElement () { | |
192 | return document.getElementsByTagName('head')[0] | |
193 | } | |
194 | ||
ffb321be C |
195 | private getTheme (name: string) { |
196 | return this.themes.find(t => t.name === name) | |
1a00c561 RK |
197 | } |
198 | } |