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