]>
Commit | Line | Data |
---|---|---|
1a00c561 | 1 | import { Injectable } from '@angular/core' |
ffb321be C |
2 | import { AuthService } from '@app/core/auth' |
3 | import { ServerService } from '@app/core/server' | |
4 | import { environment } from '../../../environments/environment' | |
5 | import { PluginService } from '@app/core/plugins/plugin.service' | |
6 | import { ServerConfigTheme } from '@shared/models' | |
88a7f93f | 7 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
7c93905d | 8 | import { first } from 'rxjs/operators' |
1a00c561 RK |
9 | |
10 | @Injectable() | |
11 | export class ThemeService { | |
ffb321be | 12 | |
a2ffd046 C |
13 | private static KEYS = { |
14 | LAST_ACTIVE_THEME: 'last_active_theme' | |
15 | } | |
16 | ||
ffb321be C |
17 | private oldThemeName: string |
18 | private themes: ServerConfigTheme[] = [] | |
19 | ||
a2ffd046 C |
20 | private themeFromLocalStorage: ServerConfigTheme |
21 | private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] | |
22 | ||
ffb321be C |
23 | constructor ( |
24 | private auth: AuthService, | |
25 | private pluginService: PluginService, | |
26 | private server: ServerService | |
27 | ) {} | |
28 | ||
29 | initialize () { | |
a2ffd046 C |
30 | // Try to load from local storage first, so we don't have to wait network requests |
31 | this.loadAndSetFromLocalStorage() | |
32 | ||
ffb321be C |
33 | this.server.configLoaded |
34 | .subscribe(() => { | |
a2ffd046 C |
35 | const themes = this.server.getConfig().theme.registered |
36 | ||
37 | this.removeThemeFromLocalStorageIfNeeded(themes) | |
38 | this.injectThemes(themes) | |
ffb321be C |
39 | |
40 | this.listenUserTheme() | |
41 | }) | |
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 | ||
ffb321be C |
75 | if (this.auth.isLoggedIn()) { |
76 | const theme = this.auth.getUser().theme | |
77 | if (theme !== 'instance-default') return theme | |
78 | } | |
79 | ||
80 | return this.server.getConfig().theme.default | |
1a00c561 RK |
81 | } |
82 | ||
ffb321be C |
83 | private loadTheme (name: string) { |
84 | const links = document.getElementsByTagName('link') | |
85 | for (let i = 0; i < links.length; i++) { | |
86 | const link = links[ i ] | |
87 | if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) { | |
88 | link.disabled = link.getAttribute('title') !== name | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | private updateCurrentTheme () { | |
a2ffd046 | 94 | if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName) |
ffb321be C |
95 | |
96 | const currentTheme = this.getCurrentTheme() | |
97 | ||
98 | console.log('Enabling %s theme.', currentTheme) | |
99 | ||
100 | this.loadTheme(currentTheme) | |
d00dc28d | 101 | |
ffb321be C |
102 | const theme = this.getTheme(currentTheme) |
103 | if (theme) { | |
104 | console.log('Adding scripts of theme %s.', currentTheme) | |
f0c5e8b6 | 105 | this.pluginService.addPlugin(theme, true) |
ffb321be C |
106 | |
107 | this.pluginService.reloadLoadedScopes() | |
a2ffd046 C |
108 | |
109 | peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme)) | |
110 | } else { | |
111 | peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME) | |
e3f7f600 | 112 | } |
ffb321be C |
113 | |
114 | this.oldThemeName = currentTheme | |
115 | } | |
116 | ||
117 | private listenUserTheme () { | |
a2ffd046 C |
118 | // We don't need them anymore |
119 | this.themeFromLocalStorage = undefined | |
120 | this.themeDOMLinksFromLocalStorage = [] | |
121 | ||
d00dc28d C |
122 | if (!this.auth.isLoggedIn()) { |
123 | this.updateCurrentTheme() | |
124 | } | |
125 | ||
ffb321be | 126 | this.auth.userInformationLoaded |
7c93905d | 127 | .pipe(first()) |
ffb321be | 128 | .subscribe(() => this.updateCurrentTheme()) |
1a00c561 RK |
129 | } |
130 | ||
a2ffd046 C |
131 | private loadAndSetFromLocalStorage () { |
132 | const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME) | |
133 | if (!lastActiveThemeString) return | |
134 | ||
135 | try { | |
136 | const lastActiveTheme = JSON.parse(lastActiveThemeString) | |
137 | this.themeFromLocalStorage = lastActiveTheme | |
138 | ||
139 | this.injectThemes([ lastActiveTheme ], true) | |
140 | this.updateCurrentTheme() | |
141 | } catch (err) { | |
142 | console.error('Cannot parse last active theme.', err) | |
143 | return | |
144 | } | |
145 | } | |
146 | ||
147 | private removeThemePlugins (themeName: string) { | |
148 | const oldTheme = this.getTheme(themeName) | |
149 | if (oldTheme) { | |
150 | console.log('Removing scripts of old theme %s.', themeName) | |
151 | this.pluginService.removePlugin(oldTheme) | |
152 | } | |
153 | } | |
154 | ||
155 | private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) { | |
156 | if (!this.themeFromLocalStorage) return | |
157 | ||
158 | const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name) | |
159 | if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) { | |
160 | // Need to remove this theme: we loaded an old version or a theme that does not exist anymore | |
161 | this.removeThemePlugins(this.themeFromLocalStorage.name) | |
162 | this.oldThemeName = undefined | |
163 | ||
164 | const head = this.getHeadElement() | |
165 | for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) { | |
166 | head.removeChild(htmlLinkElement) | |
167 | } | |
168 | ||
169 | this.themeFromLocalStorage = undefined | |
170 | this.themeDOMLinksFromLocalStorage = [] | |
171 | } | |
172 | } | |
173 | ||
174 | private getHeadElement () { | |
175 | return document.getElementsByTagName('head')[0] | |
176 | } | |
177 | ||
ffb321be C |
178 | private getTheme (name: string) { |
179 | return this.themes.find(t => t.name === name) | |
1a00c561 RK |
180 | } |
181 | } |