]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/theme/theme.service.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / core / theme / theme.service.ts
CommitLineData
1a00c561 1import { Injectable } from '@angular/core'
ffb321be
C
2import { AuthService } from '@app/core/auth'
3import { ServerService } from '@app/core/server'
4import { environment } from '../../../environments/environment'
5import { PluginService } from '@app/core/plugins/plugin.service'
ba430d75 6import { ServerConfig, ServerConfigTheme } from '@shared/models'
88a7f93f 7import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
7c93905d 8import { first } from 'rxjs/operators'
1a00c561
RK
9
10@Injectable()
11export 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
ba430d75
C
23 private serverConfig: ServerConfig
24
ffb321be
C
25 constructor (
26 private auth: AuthService,
27 private pluginService: PluginService,
28 private server: ServerService
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
ba430d75
C
35 this.serverConfig = this.server.getTmpConfig()
36 this.server.getConfig()
37 .subscribe(config => {
38 this.serverConfig = config
39
40 const themes = this.serverConfig.theme.registered
a2ffd046
C
41
42 this.removeThemeFromLocalStorageIfNeeded(themes)
43 this.injectThemes(themes)
ffb321be
C
44
45 this.listenUserTheme()
46 })
47 }
48
a2ffd046
C
49 private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
50 this.themes = themes
ffb321be
C
51
52 console.log('Injecting %d themes.', this.themes.length)
53
a2ffd046 54 const head = this.getHeadElement()
ffb321be
C
55
56 for (const theme of this.themes) {
a2ffd046
C
57 // Already added this theme?
58 if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
ffb321be
C
59
60 for (const css of theme.css) {
61 const link = document.createElement('link')
62
63 const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}`
64 link.setAttribute('href', href)
65 link.setAttribute('rel', 'alternate stylesheet')
66 link.setAttribute('type', 'text/css')
67 link.setAttribute('title', theme.name)
68 link.setAttribute('disabled', '')
69
a2ffd046
C
70 if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
71
ffb321be
C
72 head.appendChild(link)
73 }
74 }
75 }
76
77 private getCurrentTheme () {
a2ffd046
C
78 if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
79
ffb321be
C
80 if (this.auth.isLoggedIn()) {
81 const theme = this.auth.getUser().theme
82 if (theme !== 'instance-default') return theme
83 }
84
ba430d75 85 return this.serverConfig.theme.default
1a00c561
RK
86 }
87
ffb321be
C
88 private loadTheme (name: string) {
89 const links = document.getElementsByTagName('link')
90 for (let i = 0; i < links.length; i++) {
91 const link = links[ i ]
92 if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
93 link.disabled = link.getAttribute('title') !== name
94 }
95 }
96 }
97
98 private updateCurrentTheme () {
a2ffd046 99 if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
ffb321be
C
100
101 const currentTheme = this.getCurrentTheme()
102
103 console.log('Enabling %s theme.', currentTheme)
104
105 this.loadTheme(currentTheme)
d00dc28d 106
ffb321be
C
107 const theme = this.getTheme(currentTheme)
108 if (theme) {
109 console.log('Adding scripts of theme %s.', currentTheme)
f0c5e8b6 110 this.pluginService.addPlugin(theme, true)
ffb321be
C
111
112 this.pluginService.reloadLoadedScopes()
a2ffd046
C
113
114 peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme))
115 } else {
116 peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
e3f7f600 117 }
ffb321be
C
118
119 this.oldThemeName = currentTheme
120 }
121
122 private listenUserTheme () {
a2ffd046
C
123 // We don't need them anymore
124 this.themeFromLocalStorage = undefined
125 this.themeDOMLinksFromLocalStorage = []
126
d00dc28d
C
127 if (!this.auth.isLoggedIn()) {
128 this.updateCurrentTheme()
129 }
130
ffb321be 131 this.auth.userInformationLoaded
7c93905d 132 .pipe(first())
ffb321be 133 .subscribe(() => this.updateCurrentTheme())
1a00c561
RK
134 }
135
a2ffd046
C
136 private loadAndSetFromLocalStorage () {
137 const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
138 if (!lastActiveThemeString) return
139
140 try {
141 const lastActiveTheme = JSON.parse(lastActiveThemeString)
142 this.themeFromLocalStorage = lastActiveTheme
143
144 this.injectThemes([ lastActiveTheme ], true)
145 this.updateCurrentTheme()
146 } catch (err) {
147 console.error('Cannot parse last active theme.', err)
148 return
149 }
150 }
151
152 private removeThemePlugins (themeName: string) {
153 const oldTheme = this.getTheme(themeName)
154 if (oldTheme) {
155 console.log('Removing scripts of old theme %s.', themeName)
156 this.pluginService.removePlugin(oldTheme)
157 }
158 }
159
160 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
161 if (!this.themeFromLocalStorage) return
162
163 const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
164 if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
165 // Need to remove this theme: we loaded an old version or a theme that does not exist anymore
166 this.removeThemePlugins(this.themeFromLocalStorage.name)
167 this.oldThemeName = undefined
168
169 const head = this.getHeadElement()
170 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
171 head.removeChild(htmlLinkElement)
172 }
173
174 this.themeFromLocalStorage = undefined
175 this.themeDOMLinksFromLocalStorage = []
176 }
177 }
178
179 private getHeadElement () {
180 return document.getElementsByTagName('head')[0]
181 }
182
ffb321be
C
183 private getTheme (name: string) {
184 return this.themes.find(t => t.name === name)
1a00c561
RK
185 }
186}