ngx-admin/src/app/app.component.ts

71 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-04-11 19:09:02 +03:00
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
2017-09-20 13:55:34 +03:00
import { Component, OnInit } from '@angular/core';
2019-07-15 18:52:01 +03:00
import { ActivatedRoute } from '@angular/router';
import { withLatestFrom, filter } from 'rxjs/operators';
import { NbThemeService } from '@nebular/theme';
2017-09-20 13:55:34 +03:00
import { AnalyticsService } from './@core/utils/analytics.service';
2019-07-15 18:52:01 +03:00
import { AbService } from './@core/utils/ab.service';
2019-12-20 11:09:37 +03:00
import { SeoService } from './@core/utils/seo.service';
2020-04-01 16:23:58 +03:00
import { CurrentThemeService } from './@core/utils/theme.service';
2017-04-11 19:09:02 +03:00
@Component({
2017-05-06 15:35:15 +03:00
selector: 'ngx-app',
2017-04-13 14:24:23 +03:00
template: '<router-outlet></router-outlet>',
2017-04-11 19:09:02 +03:00
})
2017-09-20 13:55:34 +03:00
export class AppComponent implements OnInit {
2020-04-01 14:46:32 +03:00
themes = ['default', 'cosmic', 'corporate', 'dark', 'material-dark', 'material-light'];
2019-07-15 18:52:01 +03:00
constructor(private analytics: AnalyticsService,
private seoService: SeoService,
private activatedRoute: ActivatedRoute,
private abService: AbService,
2020-04-01 16:23:58 +03:00
private themeService: NbThemeService,
private currentThemeService: CurrentThemeService) {
2019-07-15 18:52:01 +03:00
this.themeService.onThemeChange()
.subscribe((theme: any) => {
this.analytics.trackEvent('changeTheme', theme.name);
});
this.activatedRoute.queryParams
.subscribe((params: any) => {
if (params.theme && this.themes.includes(params.theme)) {
this.themeService.changeTheme(params.theme);
2020-04-01 17:25:59 +03:00
this.currentThemeService.setCurrentTheme(params.theme);
2020-04-01 16:23:58 +03:00
} else {
this.themeService.changeTheme(this.currentThemeService.getCurrentTheme());
2019-07-15 18:52:01 +03:00
}
});
2017-09-20 13:55:34 +03:00
}
ngOnInit(): void {
2019-07-15 18:52:01 +03:00
const variants = [
AbService.VARIANT_THEME_CORPORATE,
AbService.VARIANT_THEME_DEFAULT,
AbService.VARIANT_THEME_COSMIC,
AbService.VARIANT_THEME_DARK,
];
2017-09-20 13:55:34 +03:00
this.analytics.trackPageViews();
2019-12-20 11:09:37 +03:00
this.seoService.trackCanonicalChanges();
2019-07-15 18:52:01 +03:00
this.abService.onAbEvent()
.pipe(
withLatestFrom(this.activatedRoute.queryParams),
filter(([e, params]: [{ name: string }, any]) => !params.theme),
)
.subscribe(([e, params]: [{ name: string }, any]) => {
const themeName = e.name.replace('theme-change-', '');
if (variants.includes(e.name) && this.themes.includes(themeName)) {
this.themeService.changeTheme(themeName);
}
});
2017-09-20 13:55:34 +03:00
}
2017-04-11 19:09:02 +03:00
}