ngx-admin/src/app/@theme/components/theme-switcher/theme-switcher.component.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-05-11 17:25:02 +03:00
import { Component, OnInit, Input } from '@angular/core';
import { NbThemeService } from '@nebular/theme';
import { NbJSThemeOptions } from '@nebular/theme/services/js-themes/theme.options';
import { AnalyticsService } from '../../../@core/utils/analytics.service';
@Component({
selector: 'ngx-theme-switcher',
template: `
2018-05-11 17:25:02 +03:00
<ngx-switcher
[firstValue]="false"
[secondValue]="true"
[firstValueLabel]="'Light'"
[secondValueLabel]="'Cosmic'"
[value]="currentBoolTheme()"
(valueChange)="toggleTheme($event)"
[vertical]="vertical"
>
</ngx-switcher>
`,
})
export class ThemeSwitcherComponent implements OnInit {
theme: NbJSThemeOptions;
2018-05-11 17:25:02 +03:00
firstTheme = 'default';
secondTheme = 'cosmic';
@Input() vertical: boolean = false;
constructor(
private themeService: NbThemeService,
private analyticsService: AnalyticsService,
) {}
ngOnInit() {
this.themeService.getJsTheme()
.subscribe((theme: NbJSThemeOptions) => this.theme = theme);
}
toggleTheme(theme: boolean) {
2018-05-11 17:25:02 +03:00
const themeName = this.boolToTheme(theme);
this.themeService.changeTheme(themeName);
this.analyticsService.trackEvent('switchTheme');
}
currentBoolTheme() {
return this.themeToBool(this.theme);
}
private themeToBool(theme: NbJSThemeOptions) {
2018-05-11 17:25:02 +03:00
return theme.name === this.secondTheme;
}
private boolToTheme(theme: boolean) {
2018-05-11 17:25:02 +03:00
return theme ? this.secondTheme : this.firstTheme;
}
}