homepage/src/app/components/languagetoggle/languagetoggle.component.ts
2018-03-25 14:35:44 +02:00

52 lines
1.5 KiB
TypeScript

import {Component, Input, OnInit} from '@angular/core';
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
import {StateService} from '../../services/state.service';
@Component({
selector: 'app-languagetoggle',
templateUrl: './languagetoggle.component.html',
styleUrls: ['./languagetoggle.component.scss']
})
export class LanguagetoggleComponent implements OnInit {
/**
* This is the language for which the component is defined. Typically, when the currently set language is equal to this variable,
* the component will be visible.
*/
@Input()
private language: string;
/**
* if this is set to true, the component will be visible
*/
private show = false;
constructor(private router: Router, private route: ActivatedRoute, private stateService: StateService) {
}
ngOnInit() {
this.route.pathFromRoot.forEach(p => {
this.router.events.filter(e => e instanceof NavigationEnd).subscribe(e => {
console.log('language toggle is detecting a navigation end: have to update values, things might have changed!');
this.updateValues();
});
});
}
private updateValues() {
const lang = this.stateService.getLanguage();
if (lang == null) {
console.log('languagetoggle: language is null from stateservice, ignoring');
return;
}
console.log('languagetoggle gets lang: ' + lang);
if (lang === this.language) {
console.log('languagetoggle: showing the component ' + lang);
this.show = true;
} else {
this.show = false;
}
}
}