From ffb321bedca46d6987c7b31dd58e5dea96ea2ea2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Jul 2019 14:06:19 +0200 Subject: WIP plugins: load theme on client side --- client/src/app/core/theme/theme.service.ts | 124 ++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 30 deletions(-) (limited to 'client/src/app/core/theme/theme.service.ts') diff --git a/client/src/app/core/theme/theme.service.ts b/client/src/app/core/theme/theme.service.ts index 50c19ecac..ad59c203b 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts @@ -1,41 +1,105 @@ 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) + } + } + } + + private getCurrentTheme () { + if (this.auth.isLoggedIn()) { + const theme = this.auth.getUser().theme + if (theme !== 'instance-default') return theme + } + + return this.server.getConfig().theme.default } - 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 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) + + this.pluginService.reloadLoadedScopes() } + + this.oldThemeName = currentTheme + } + + private listenUserTheme () { + 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) } } -- cgit v1.2.3