2019-01-29 15:20:00 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-03-08 09:19:05 +01:00
|
|
|
|
|
|
|
import { LoginDataService } from './login-data.service';
|
2019-01-29 15:20:00 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-08 09:19:05 +01:00
|
|
|
* Service to set the theme for OpenSlides.
|
2019-01-29 15:20:00 +01:00
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ThemeService {
|
2019-04-08 10:57:43 +02:00
|
|
|
/**
|
|
|
|
* Constant, that describes the default theme class.
|
|
|
|
*/
|
|
|
|
public static DEFAULT_THEME = 'openslides-theme';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant path of the logo with dark colors for bright themes.
|
|
|
|
*/
|
2019-05-07 21:51:38 +02:00
|
|
|
public static STANDARD_LOGO = '/assets/img/openslides-logo.svg';
|
2019-04-08 10:57:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant path of the logo with white colors for dark themes.
|
|
|
|
*/
|
2019-05-07 21:51:38 +02:00
|
|
|
public static STANDARD_LOGO_DARK_THEME = '/assets/img/openslides-logo-dark.svg';
|
2019-04-08 10:57:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the current theme as member.
|
|
|
|
*/
|
|
|
|
private currentTheme: string;
|
|
|
|
|
2019-01-29 15:20:00 +01:00
|
|
|
/**
|
2019-03-08 09:19:05 +01:00
|
|
|
* Here it will subscribe to the observer from login data service. The stheme is part of
|
|
|
|
* the login data, so get it from there and not from the config. This service will
|
|
|
|
* also cache the theme and provide the right theme on login.
|
2019-01-29 15:20:00 +01:00
|
|
|
*
|
2019-03-08 09:19:05 +01:00
|
|
|
* @param loginDataService must be injected to get the theme.
|
2019-01-29 15:20:00 +01:00
|
|
|
*/
|
2019-03-08 09:19:05 +01:00
|
|
|
public constructor(loginDataService: LoginDataService) {
|
|
|
|
loginDataService.theme.subscribe(newTheme => {
|
|
|
|
if (!newTheme) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 14:43:38 +02:00
|
|
|
this.changeTheme(newTheme);
|
2019-01-29 15:20:00 +01:00
|
|
|
});
|
|
|
|
}
|
2019-04-08 10:57:43 +02:00
|
|
|
|
2019-06-17 14:43:38 +02:00
|
|
|
/**
|
|
|
|
* Function to change the theme and ensures, that old themes are removed.
|
|
|
|
*
|
|
|
|
* @param theme The theme which is applied.
|
|
|
|
*/
|
|
|
|
private changeTheme(theme: string): void {
|
|
|
|
this.currentTheme = theme;
|
|
|
|
|
|
|
|
const classList = document.getElementsByTagName('body')[0].classList; // Get the classlist of the body.
|
|
|
|
const toRemove = Array.from(classList).filter((item: string) => item.includes('theme'));
|
|
|
|
if (toRemove.length) {
|
|
|
|
classList.remove(...toRemove); // Remove all old themes.
|
|
|
|
}
|
|
|
|
classList.add(theme, ThemeService.DEFAULT_THEME); // Add the new theme.
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:57:43 +02:00
|
|
|
/**
|
|
|
|
* Returns the logo relative to the used theme.
|
|
|
|
*
|
|
|
|
* @param shouldDefault If this method should return the default logo.
|
|
|
|
*
|
|
|
|
* @returns the path to the logo.
|
|
|
|
*/
|
|
|
|
public getLogoRelativeToTheme(shouldDefault?: boolean): string {
|
|
|
|
if (this.currentTheme) {
|
|
|
|
return this.currentTheme.includes('dark') && !shouldDefault
|
|
|
|
? ThemeService.STANDARD_LOGO_DARK_THEME
|
|
|
|
: ThemeService.STANDARD_LOGO;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 15:20:00 +01:00
|
|
|
}
|