This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
navbar.component.ts 5.53 KiB
Newer Older
Michael Breu's avatar
Michael Breu committed
import { Location } from '@angular/common';
Michael Breu's avatar
Michael Breu committed
import { Component, OnInit } from '@angular/core';
Michael Breu's avatar
Michael Breu committed
import { FormBuilder } from '@angular/forms';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
import { Router } from '@angular/router';
Michael Breu's avatar
Michael Breu committed
import { TranslateService } from '@ngx-translate/core';
import { SessionStorageService } from 'ngx-webstorage';

Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
import { VERSION } from 'app/app.constants';
Michael Breu's avatar
Michael Breu committed
import { LANGUAGES } from 'app/config/language.constants';
import { Account } from 'app/core/auth/account.model';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
import { AccountService } from 'app/core/auth/account.service';
Michael Breu's avatar
Michael Breu committed
import { OAuth2Config } from 'app/core/auth/oauth2-config.model';
import { OAuth2ConfigService } from 'app/core/auth/oauth2-config.service';
Michael Breu's avatar
Michael Breu committed
import { LoginService } from 'app/login/login.service';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
import { ProfileService } from 'app/layouts/profiles/profile.service';
Michael Breu's avatar
Michael Breu committed
import { EntityNavbarItems } from 'app/entities/entity-navbar-items';
Michael Breu's avatar
Michael Breu committed
import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager';
import { PagesService } from 'app/shared/service/pages-service';
import { ApplicationInfoService } from 'app/core/application/applicationInfo.service';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
@Component({
  selector: 'jhi-navbar',
  templateUrl: './navbar.component.html',
Michael Breu's avatar
Michael Breu committed
  styleUrls: ['./navbar.component.scss'],
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
})
export class NavbarComponent implements OnInit {
  inProduction?: boolean;
  isNavbarCollapsed = true;
  languages = LANGUAGES;
Michael Breu's avatar
Michael Breu committed
  openAPIEnabled?: boolean;
  version = '';
Michael Breu's avatar
Michael Breu committed
  oerLink?: String;

Michael Breu's avatar
Michael Breu committed
  account: Account | null = null;
  entitiesNavbarItems: any[] = [];
Michael Breu's avatar
Michael Breu committed
  authenticationError = false;

  loginForm = this.fb.group({
    username: [''],
    password: [''],
    rememberMe: [false],
  });

  public configs: OAuth2Config[] = [];

Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  constructor(
    private loginService: LoginService,
Michael Breu's avatar
Michael Breu committed
    public translateService: TranslateService,
Michael Breu's avatar
Michael Breu committed
    private sessionStorageService: SessionStorageService,
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    private accountService: AccountService,
Michael Breu's avatar
Michael Breu committed
    private fb: FormBuilder,
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    private profileService: ProfileService,
Michael Breu's avatar
Michael Breu committed
    private router: Router,
    private location: Location, // for redirect in OAuth2
    public oAuth2ConfigService: OAuth2ConfigService,
    private watchlistManager: WatchlistManager,
    private pagesService: PagesService,
    private applicationInfoService: ApplicationInfoService
Michael Breu's avatar
Michael Breu committed
    if (VERSION) {
      this.version = VERSION.toLowerCase().startsWith('v') ? VERSION : `v${VERSION}`;
    }
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  }

  ngOnInit(): void {
Michael Breu's avatar
Michael Breu committed
    this.entitiesNavbarItems = EntityNavbarItems;
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    this.profileService.getProfileInfo().subscribe(profileInfo => {
      this.inProduction = profileInfo.inProduction;
Michael Breu's avatar
Michael Breu committed
      this.openAPIEnabled = profileInfo.openAPIEnabled;
    });

Michael Breu's avatar
Michael Breu committed
    this.oAuth2ConfigService.getAllConfigs().subscribe((loadedConfigs: OAuth2Config[]) => {
      this.configs = loadedConfigs;
    });
Michael Breu's avatar
Michael Breu committed
    this.accountService.getAuthenticationState().subscribe(account => {
      this.account = account;
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    });
Michael Breu's avatar
Michael Breu committed
    this.applicationInfoService.loadOerLink().subscribe(
      (res: string) => {
        this.oerLink = res;
      },
      error =>
        // eslint-disable-next-line no-console
        console.error(error)
    );
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  }

  changeLanguage(languageKey: string): void {
Michael Breu's avatar
Michael Breu committed
    this.sessionStorageService.store('locale', languageKey);
    this.translateService.use(languageKey);
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  }

  collapseNavbar(): void {
    this.isNavbarCollapsed = true;
  }
Michael Breu's avatar
Michael Breu committed
  resetPagesCache(): void {
    this.pagesService.resetCache().subscribe(() => {
      // eslint-disable-next-line no-console
      console.log('Successfully emptied page cache');
    });
  }

  isAuthenticated(): boolean {
    return this.accountService.isAuthenticated();
  }

Michael Breu's avatar
Michael Breu committed
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  toggleEditable(event: Event): void {
    const element = <HTMLInputElement>document.getElementById('datapolicy');
    const isChecked = element.checked;
    if (isChecked) {
Michael Breu's avatar
Michael Breu committed
      this.contentEditable = true;
    } else {
      this.contentEditable = false;
    }
  }

  getCurrentBookmarkListName(): string {
    if (this.watchlistManager.currentWatchlist) {
      const name = this.watchlistManager.currentWatchlist.userWatchList.name;
      if (name) {
        return name;
      }
    }
    return '';
  }

  //  login(): void {
  //    this.loginModalService.open();
  //  }

Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  login(): void {
Michael Breu's avatar
Michael Breu committed
    this.loginService
      .login({
        username: this.loginForm.get('username')!.value,
        password: this.loginForm.get('password')!.value,
        rememberMe: this.loginForm.get('rememberMe')!.value,
      })
      .subscribe(
        () => {
          this.authenticationError = false;
          if (
            this.router.url === '/account/register' ||
            this.router.url.startsWith('/account/activate') ||
            this.router.url.startsWith('/account/reset/')
          ) {
            this.router.navigate(['']);
Michael Breu's avatar
Michael Breu committed
        },
        () => (this.authenticationError = true)
      );
  }

  loginWithGitLab(registrationId: string): void {
    location.href = `${location.origin}${this.location.prepareExternalUrl('oauth2/authorization/' + registrationId)}`;
    // If you have configured multiple OIDC providers, then, you can update this URL to /login.
    // It will show a Spring Security generated login page with links to configured OIDC providers.
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  logout(): void {
    this.collapseNavbar();
    this.loginService.logout();
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    this.router.navigate(['']);
  }

  toggleNavbar(): void {
    this.isNavbarCollapsed = !this.isNavbarCollapsed;
  }
Michael Breu's avatar
Michael Breu committed

  getImageUrl(): string {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return this.isAuthenticated() ? this.accountService.getImageUrl() : '';
  }
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
}