OpenSlides/client/src/app/site/site.component.ts

116 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, HostBinding, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
2018-06-25 17:03:52 +02:00
import { BreakpointObserver, Breakpoints, BreakpointState } from '@angular/cdk/layout';
import { AuthService } from 'app/core/services/auth.service';
import { OperatorService } from 'app/core/services/operator.service';
import { WebsocketService } from 'app/core/services/websocket.service';
import { TranslateService } from '@ngx-translate/core'; //showcase
import { BaseComponent } from 'app/base.component';
import { pageTransition, navItemAnim } from 'app/shared/animations';
import { MatDialog, MatSidenav } from '@angular/material';
import { ViewportService } from '../core/services/viewport.service';
@Component({
selector: 'app-site',
animations: [pageTransition, navItemAnim],
templateUrl: './site.component.html',
styleUrls: ['./site.component.scss']
})
export class SiteComponent extends BaseComponent implements OnInit {
/**
* HTML element of the side panel
*/
@ViewChild('sideNav') sideNav: MatSidenav;
/**
* Get the username from the operator (should be known already)
*/
username = this.operator.username;
2018-08-07 12:30:21 +02:00
/**
* Constructor
*
* @param authService
* @param websocketService
* @param operator
* @param router
* @param breakpointObserver
* @param translate
* @param dialog
*/
2018-06-25 17:03:52 +02:00
constructor(
private authService: AuthService,
private websocketService: WebsocketService,
private operator: OperatorService,
2018-06-25 17:03:52 +02:00
private router: Router,
public vp: ViewportService,
2018-08-07 12:30:21 +02:00
public translate: TranslateService,
public dialog: MatDialog
) {
super();
}
2018-06-25 17:03:52 +02:00
/**
* Initialize the site component
*/
2018-06-25 17:03:52 +02:00
ngOnInit() {
this.vp.checkForChange();
2018-08-22 16:03:49 +02:00
// get a translation via code: use the translation service
2018-08-16 17:03:39 +02:00
// this.translate.get('Motions').subscribe((res: string) => {
2018-08-22 16:03:49 +02:00
// console.log('translation of motions in the target language: ' + res);
// });
2018-08-22 16:03:49 +02:00
// start autoupdate if the user is logged in:
this.operator.whoAmI().subscribe(resp => {
if (resp.user) {
this.websocketService.connect();
} else {
//if whoami is not sucsessfull, forward to login again
this.operator.clear();
this.router.navigate(['/login']);
}
});
}
/**
* Closes the sidenav in mobile view
*/
toggleSideNav() {
if (this.vp.isMobile) {
this.sideNav.toggle();
}
}
/**
* Let the user change the language
* @param lang the desired language (en, de, fr, ...)
*/
2018-08-07 12:30:21 +02:00
selectLang(selection: string): void {
this.translate.use(selection).subscribe();
}
2018-08-07 12:30:21 +02:00
/**
* Get the name of a Language by abbreviation.
*/
getLangName(abbreviation: string): string {
if (abbreviation === 'en') {
return this.translate.instant('English');
} else if (abbreviation === 'de') {
return this.translate.instant('German');
} else if (abbreviation === 'fr') {
return this.translate.instant('French');
}
2018-06-25 17:03:52 +02:00
}
/**
* Function to log out the current user
*/
logOutButton() {
this.authService.logout().subscribe();
this.router.navigate(['/login']);
}
}