fuse-angular/src/app/app.component.ts

165 lines
5.8 KiB
TypeScript
Raw Normal View History

import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Platform } from '@angular/cdk/platform';
import { TranslateService } from '@ngx-translate/core';
2018-05-29 11:58:48 +00:00
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
2018-05-29 11:58:48 +00:00
import { FuseConfigService } from '@fuse/services/config.service';
import { FuseNavigationService } from '@fuse/components/navigation/navigation.service';
import { FuseSidebarService } from '@fuse/components/sidebar/sidebar.service';
import { FuseSplashScreenService } from '@fuse/services/splash-screen.service';
import { FuseTranslationLoaderService } from '@fuse/services/translation-loader.service';
2018-05-20 07:12:31 +00:00
import { navigation } from 'app/navigation/navigation';
import { locale as navigationEnglish } from 'app/navigation/i18n/en';
import { locale as navigationTurkish } from 'app/navigation/i18n/tr';
@Component({
selector : 'app',
templateUrl: './app.component.html',
2017-07-12 12:35:07 +00:00
styleUrls : ['./app.component.scss']
})
2018-05-29 11:58:48 +00:00
export class AppComponent implements OnInit, OnDestroy
{
2018-05-29 11:58:48 +00:00
fuseConfig: any;
2018-07-13 09:54:02 +00:00
navigation: any;
2018-05-29 11:58:48 +00:00
// Private
private _unsubscribeAll: Subject<any>;
2018-05-20 07:12:31 +00:00
/**
* Constructor
*
* @param {DOCUMENT} document
2018-05-29 11:58:48 +00:00
* @param {FuseConfigService} _fuseConfigService
2018-05-20 07:12:31 +00:00
* @param {FuseNavigationService} _fuseNavigationService
* @param {FuseSidebarService} _fuseSidebarService
2018-05-20 07:12:31 +00:00
* @param {FuseSplashScreenService} _fuseSplashScreenService
* @param {FuseTranslationLoaderService} _fuseTranslationLoaderService
* @param {Platform} _platform
2018-05-20 07:12:31 +00:00
* @param {TranslateService} _translateService
*/
constructor(
@Inject(DOCUMENT) private document: any,
2018-05-29 11:58:48 +00:00
private _fuseConfigService: FuseConfigService,
2018-05-20 07:12:31 +00:00
private _fuseNavigationService: FuseNavigationService,
private _fuseSidebarService: FuseSidebarService,
2018-05-20 07:12:31 +00:00
private _fuseSplashScreenService: FuseSplashScreenService,
private _fuseTranslationLoaderService: FuseTranslationLoaderService,
private _translateService: TranslateService,
private _platform: Platform
)
2017-07-12 12:35:07 +00:00
{
2018-05-29 11:58:48 +00:00
// Get default navigation
2018-05-20 07:12:31 +00:00
this.navigation = navigation;
2018-05-29 11:58:48 +00:00
// Register the navigation to the service
this._fuseNavigationService.register('main', this.navigation);
// Set the main navigation as our current navigation
this._fuseNavigationService.setCurrentNavigation('main');
// Add languages
2018-05-20 07:12:31 +00:00
this._translateService.addLangs(['en', 'tr']);
// Set the default language
2018-05-20 07:12:31 +00:00
this._translateService.setDefaultLang('en');
// Set the navigation translations
2018-05-20 07:12:31 +00:00
this._fuseTranslationLoaderService.loadTranslations(navigationEnglish, navigationTurkish);
// Use a language
2018-05-20 07:12:31 +00:00
this._translateService.use('en');
2018-05-29 11:58:48 +00:00
/**
* ------------------------------------------------------------------
* ngxTranslate Fix Start
* ------------------------------------------------------------------
* If you are using a language other than the default one, i.e. Turkish in this case,
* you may encounter an issue where some of the components are not actually being
* translated when your app first initialized.
*
* This is related to ngxTranslate module and below there is a temporary fix while we
* are moving the multi language implementation over to the Angular's core language
* service.
**/
// Set the default language to 'en' and then back to 'tr'.
// '.use' cannot be used here as ngxTranslate won't switch to a language that's already
// been selected and there is no way to force it, so we overcome the issue by switching
// the default language back and forth.
/**
setTimeout(() => {
this._translateService.setDefaultLang('en');
this._translateService.setDefaultLang('tr');
});
*/
/**
* ------------------------------------------------------------------
* ngxTranslate Fix End
* ------------------------------------------------------------------
*/
// Add is-mobile class to the body if the platform is mobile
if ( this._platform.ANDROID || this._platform.IOS )
{
this.document.body.classList.add('is-mobile');
}
2018-05-29 11:58:48 +00:00
// Set the private defaults
this._unsubscribeAll = new Subject();
}
// -----------------------------------------------------------------------------------------------------
// @ Lifecycle hooks
// -----------------------------------------------------------------------------------------------------
/**
* On init
*/
ngOnInit(): void
{
// Subscribe to config changes
this._fuseConfigService.config
.pipe(takeUntil(this._unsubscribeAll))
.subscribe((config) => {
this.fuseConfig = config;
if ( this.fuseConfig.layout.width === 'boxed' )
{
this.document.body.classList.add('boxed');
}
else
{
this.document.body.classList.remove('boxed');
}
2018-05-29 11:58:48 +00:00
});
}
/**
* On destroy
*/
ngOnDestroy(): void
{
// Unsubscribe from all subscriptions
this._unsubscribeAll.next();
this._unsubscribeAll.complete();
2017-07-12 12:35:07 +00:00
}
// -----------------------------------------------------------------------------------------------------
2018-06-15 14:45:27 +00:00
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Toggle sidebar open
*
* @param key
*/
2018-06-15 14:46:41 +00:00
toggleSidebarOpen(key): void
{
this._fuseSidebarService.getSidebar(key).toggleOpen();
}
}