active-menu.directive.ts 940B

12345678910111213141516171819202122232425262728
  1. import { Directive, OnInit, ElementRef, Renderer, Input } from '@angular/core';
  2. import { TranslateService, LangChangeEvent } from '@ngx-translate/core';
  3. @Directive({
  4. selector: '[jhiActiveMenu]'
  5. })
  6. export class ActiveMenuDirective implements OnInit {
  7. @Input()
  8. jhiActiveMenu: string;
  9. constructor(private el: ElementRef, private renderer: Renderer, private translateService: TranslateService) {}
  10. ngOnInit() {
  11. this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
  12. this.updateActiveFlag(event.lang);
  13. });
  14. this.updateActiveFlag(this.translateService.currentLang);
  15. }
  16. updateActiveFlag(selectedLanguage) {
  17. if (this.jhiActiveMenu === selectedLanguage) {
  18. this.renderer.setElementClass(this.el.nativeElement, 'active', true);
  19. } else {
  20. this.renderer.setElementClass(this.el.nativeElement, 'active', false);
  21. }
  22. }
  23. }