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

Skip to content
Snippets Groups Projects
navbar.component.ts 4.57 KiB
Newer Older
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';
import { VERSION } from 'app/app.constants';
import { AccountService } from 'app/core/auth/account.service';
import { OAuth2Config } from 'app/core/auth/oauth2-config.model';
import { OAuth2ConfigService } from 'app/core/auth/oauth2-config.service';
import { LANGUAGES } from 'app/core/language/language.constants';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
import { LoginService } from 'app/core/login/login.service';
import { ProfileService } from 'app/layouts/profiles/profile.service';
import { PagesService } from 'app/shared/service/pages-service';
import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager';
import { JhiLanguageService } from 'ng-jhipster';
import { SessionStorageService } from 'ngx-webstorage';

Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed

@Component({
  selector: 'jhi-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['navbar.scss'],
})
export class NavbarComponent implements OnInit {
  inProduction?: boolean;
  isNavbarCollapsed = true;
  languages = LANGUAGES;
  swaggerEnabled?: boolean;
  version: string;
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 languageService: JhiLanguageService,
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    private sessionStorage: SessionStorageService,
    private accountService: AccountService,
    private fb: FormBuilder,
    //    private loginModalService: LoginModalService,
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
    private profileService: ProfileService,
    private router: Router,
    private location: Location, // for redirect in OAuth2
    public oAuth2ConfigService: OAuth2ConfigService,
Michael Breu's avatar
Michael Breu committed
    private watchlistManager: WatchlistManager,
    private pagesService: PagesService,
  ) {
    this.configs = [];
    this.oAuth2ConfigService.getAllConfigs().subscribe((loadedConfigs: OAuth2Config[]) => {
      this.configs = loadedConfigs;
    });
    this.version = VERSION ? (VERSION.toLowerCase().startsWith('v') ? VERSION : 'v' + VERSION) : '';
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed
  }

  ngOnInit(): void {
    this.profileService.getProfileInfo().subscribe(profileInfo => {
      this.inProduction = profileInfo.inProduction;
      this.swaggerEnabled = profileInfo.swaggerEnabled;
    });
  }

  changeLanguage(languageKey: string): void {
    this.sessionStorage.store('locale', languageKey);
    this.languageService.changeLanguage(languageKey);
  }

  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")})
    ;
}
Lukas Kaltenbrunner's avatar
Lukas Kaltenbrunner committed

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

  toggleEditable(event: { target: { checked: any; }; }) {
    if ( event.target.checked ) {
        this.contentEditable = true;
   }
}

  getCurrentBookmarkListName(): string {
    if (this.watchlistManager.currentWatchlist) {
      const name = this.watchlistManager.currentWatchlist.userWatchList.name;
      if (name) {
        return name;
      }
  //  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(['']);
          }
        },
        () => (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();
    this.router.navigate(['']);
  }

  toggleNavbar(): void {
    this.isNavbarCollapsed = !this.isNavbarCollapsed;
  }

  getImageUrl(): string {
    return this.isAuthenticated() ? this.accountService.getImageUrl() : '';
  }
}