PeerTube/client/src/app/core/theme/theme.service.ts

191 lines
5.8 KiB
TypeScript
Raw Normal View History

2018-09-06 12:00:53 +02:00
import { Injectable } from '@angular/core'
2019-07-10 14:06:19 +02:00
import { AuthService } from '@app/core/auth'
import { ServerService } from '@app/core/server'
import { environment } from '../../../environments/environment'
import { PluginService } from '@app/core/plugins/plugin.service'
2019-12-18 15:31:54 +01:00
import { ServerConfig, ServerConfigTheme } from '@shared/models'
2019-12-13 09:32:36 +01:00
import { first } from 'rxjs/operators'
import { User } from '@app/shared/users/user.model'
import { UserService } from '@app/shared/users/user.service'
import { LocalStorageService } from '@app/shared/misc/storage.service'
2018-09-06 12:00:53 +02:00
@Injectable()
export class ThemeService {
2019-07-10 14:06:19 +02:00
private oldThemeName: string
private themes: ServerConfigTheme[] = []
2019-10-25 10:57:23 +02:00
private themeFromLocalStorage: ServerConfigTheme
private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
2019-12-18 15:31:54 +01:00
private serverConfig: ServerConfig
2019-07-10 14:06:19 +02:00
constructor (
private auth: AuthService,
private userService: UserService,
2019-07-10 14:06:19 +02:00
private pluginService: PluginService,
private server: ServerService,
private localStorageService: LocalStorageService
2019-07-10 14:06:19 +02:00
) {}
initialize () {
2019-10-25 10:57:23 +02:00
// Try to load from local storage first, so we don't have to wait network requests
this.loadAndSetFromLocalStorage()
2019-12-18 15:31:54 +01:00
this.serverConfig = this.server.getTmpConfig()
this.server.getConfig()
.subscribe(config => {
this.serverConfig = config
const themes = this.serverConfig.theme.registered
2019-10-25 10:57:23 +02:00
this.removeThemeFromLocalStorageIfNeeded(themes)
this.injectThemes(themes)
2019-07-10 14:06:19 +02:00
this.listenUserTheme()
})
}
2019-10-25 10:57:23 +02:00
private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
this.themes = themes
2019-07-10 14:06:19 +02:00
console.log('Injecting %d themes.', this.themes.length)
2019-10-25 10:57:23 +02:00
const head = this.getHeadElement()
2019-07-10 14:06:19 +02:00
for (const theme of this.themes) {
2019-10-25 10:57:23 +02:00
// Already added this theme?
if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
2019-07-10 14:06:19 +02:00
for (const css of theme.css) {
const link = document.createElement('link')
const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}`
link.setAttribute('href', href)
link.setAttribute('rel', 'alternate stylesheet')
link.setAttribute('type', 'text/css')
link.setAttribute('title', theme.name)
link.setAttribute('disabled', '')
2019-10-25 10:57:23 +02:00
if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
2019-07-10 14:06:19 +02:00
head.appendChild(link)
}
}
}
private getCurrentTheme () {
2019-10-25 10:57:23 +02:00
if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
const theme = this.auth.isLoggedIn()
? this.auth.getUser().theme
: this.userService.getAnonymousUser().theme
2019-07-10 14:06:19 +02:00
if (theme !== 'instance-default') return theme
2019-12-18 15:31:54 +01:00
return this.serverConfig.theme.default
2018-09-06 12:00:53 +02:00
}
2019-07-10 14:06:19 +02:00
private loadTheme (name: string) {
const links = document.getElementsByTagName('link')
for (let i = 0; i < links.length; i++) {
const link = links[ i ]
if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
link.disabled = link.getAttribute('title') !== name
}
}
}
private updateCurrentTheme () {
2019-10-25 10:57:23 +02:00
if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
2019-07-10 14:06:19 +02:00
const currentTheme = this.getCurrentTheme()
console.log('Enabling %s theme.', currentTheme)
this.loadTheme(currentTheme)
2019-07-10 14:06:19 +02:00
const theme = this.getTheme(currentTheme)
if (theme) {
console.log('Adding scripts of theme %s.', currentTheme)
2019-07-16 16:09:58 +02:00
this.pluginService.addPlugin(theme, true)
2019-07-10 14:06:19 +02:00
this.pluginService.reloadLoadedScopes()
2019-10-25 10:57:23 +02:00
this.localStorageService.setItem(User.KEYS.THEME, JSON.stringify(theme), false)
2019-10-25 10:57:23 +02:00
} else {
this.localStorageService.removeItem(User.KEYS.THEME, false)
2018-09-11 11:41:10 +02:00
}
2019-07-10 14:06:19 +02:00
this.oldThemeName = currentTheme
}
private listenUserTheme () {
2019-10-25 10:57:23 +02:00
// We don't need them anymore
this.themeFromLocalStorage = undefined
this.themeDOMLinksFromLocalStorage = []
if (!this.auth.isLoggedIn()) {
this.updateCurrentTheme()
this.localStorageService.watch([User.KEYS.THEME]).subscribe(
() => this.updateCurrentTheme()
)
}
2019-07-10 14:06:19 +02:00
this.auth.userInformationLoaded
2019-12-13 09:32:36 +01:00
.pipe(first())
2019-07-10 14:06:19 +02:00
.subscribe(() => this.updateCurrentTheme())
2018-09-06 12:00:53 +02:00
}
2019-10-25 10:57:23 +02:00
private loadAndSetFromLocalStorage () {
const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.THEME)
2019-10-25 10:57:23 +02:00
if (!lastActiveThemeString) return
try {
const lastActiveTheme = JSON.parse(lastActiveThemeString)
this.themeFromLocalStorage = lastActiveTheme
this.injectThemes([ lastActiveTheme ], true)
this.updateCurrentTheme()
} catch (err) {
console.error('Cannot parse last active theme.', err)
return
}
}
private removeThemePlugins (themeName: string) {
const oldTheme = this.getTheme(themeName)
if (oldTheme) {
console.log('Removing scripts of old theme %s.', themeName)
this.pluginService.removePlugin(oldTheme)
}
}
private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
if (!this.themeFromLocalStorage) return
const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
// Need to remove this theme: we loaded an old version or a theme that does not exist anymore
this.removeThemePlugins(this.themeFromLocalStorage.name)
this.oldThemeName = undefined
const head = this.getHeadElement()
for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
head.removeChild(htmlLinkElement)
}
this.themeFromLocalStorage = undefined
this.themeDOMLinksFromLocalStorage = []
}
}
private getHeadElement () {
return document.getElementsByTagName('head')[0]
}
2019-07-10 14:06:19 +02:00
private getTheme (name: string) {
return this.themes.find(t => t.name === name)
2018-09-06 12:00:53 +02:00
}
}