import { Injectable } from '@angular/core'
-import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
+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'
+import { ServerConfigTheme } from '@shared/models'
@Injectable()
export class ThemeService {
- private theme = document.querySelector('body')
- private darkTheme = false
- private previousTheme: { [ id: string ]: string } = {}
-
- constructor () {
- // initialise the alternative theme with dark theme colors
- this.previousTheme['mainBackgroundColor'] = '#111111'
- this.previousTheme['mainForegroundColor'] = '#fff'
- this.previousTheme['submenuColor'] = 'rgb(32,32,32)'
- this.previousTheme['inputColor'] = 'gray'
- this.previousTheme['inputPlaceholderColor'] = '#fff'
-
- this.darkTheme = (peertubeLocalStorage.getItem('theme') === 'dark')
- if (this.darkTheme) this.toggleDarkTheme(false)
+
+ private oldThemeName: string
+ private themes: ServerConfigTheme[] = []
+
+ constructor (
+ private auth: AuthService,
+ private pluginService: PluginService,
+ private server: ServerService
+ ) {}
+
+ initialize () {
+ this.server.configLoaded
+ .subscribe(() => {
+ this.injectThemes()
+
+ this.listenUserTheme()
+ })
+ }
+
+ private injectThemes () {
+ this.themes = this.server.getConfig().theme.registered
+
+ console.log('Injecting %d themes.', this.themes.length)
+
+ const head = document.getElementsByTagName('head')[0]
+
+ for (const theme of this.themes) {
+
+ 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', '')
+
+ head.appendChild(link)
+ }
+ }
}
- toggleDarkTheme (setLocalStorage = true) {
- // switch properties
- this.switchProperty('mainBackgroundColor')
- this.switchProperty('mainForegroundColor')
- this.switchProperty('submenuColor')
- this.switchProperty('inputColor')
- this.switchProperty('inputPlaceholderColor')
-
- if (setLocalStorage) {
- this.darkTheme = !this.darkTheme
- peertubeLocalStorage.setItem('theme', (this.darkTheme) ? 'dark' : 'default')
+ private getCurrentTheme () {
+ if (this.auth.isLoggedIn()) {
+ const theme = this.auth.getUser().theme
+ if (theme !== 'instance-default') return theme
}
+
+ return this.server.getConfig().theme.default
+ }
+
+ 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 () {
+ if (this.oldThemeName) {
+ const oldTheme = this.getTheme(this.oldThemeName)
+ if (oldTheme) {
+ console.log('Removing scripts of old theme %s.', this.oldThemeName)
+ this.pluginService.removePlugin(oldTheme)
+ }
+ }
+
+ const currentTheme = this.getCurrentTheme()
+
+ console.log('Enabling %s theme.', currentTheme)
+
+ this.loadTheme(currentTheme)
+
+ const theme = this.getTheme(currentTheme)
+ if (theme) {
+ console.log('Adding scripts of theme %s.', currentTheme)
+ this.pluginService.addPlugin(theme, true)
+
+ this.pluginService.reloadLoadedScopes()
+ }
+
+ this.oldThemeName = currentTheme
+ }
+
+ private listenUserTheme () {
+ if (!this.auth.isLoggedIn()) {
+ this.updateCurrentTheme()
+ }
+
+ this.auth.userInformationLoaded
+ .subscribe(() => this.updateCurrentTheme())
}
- private switchProperty (property: string, newValue?: string) {
- const propertyOldvalue = window.getComputedStyle(this.theme).getPropertyValue('--' + property)
- this.theme.style.setProperty('--' + property, (newValue) ? newValue : this.previousTheme[property])
- this.previousTheme[property] = propertyOldvalue
+ private getTheme (name: string) {
+ return this.themes.find(t => t.name === name)
}
}