OpenSlides/client/src/app/core/ui-services/theme.service.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2019-03-08 09:19:05 +01:00
import { LoginDataService } from './login-data.service';
/**
2019-03-08 09:19:05 +01:00
* Service to set the theme for OpenSlides.
*/
@Injectable({
providedIn: 'root'
})
export class ThemeService {
/**
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-03-08 09:19:05 +01:00
* @param loginDataService must be injected to get the theme.
*/
2019-03-08 09:19:05 +01:00
public constructor(loginDataService: LoginDataService) {
loginDataService.theme.subscribe(newTheme => {
if (!newTheme) {
return;
}
const classList = document.getElementsByTagName('body')[0].classList; // Get the classlist of the body.
2019-03-08 09:19:05 +01:00
const toRemove = Array.from(classList).filter((item: string) => item.includes('theme'));
if (toRemove.length) {
classList.remove(...toRemove); // Remove all old themes.
}
2019-03-08 09:19:05 +01:00
classList.add(newTheme); // Add the new theme.
});
}
}