Skip to content
Snippets Groups Projects
app.component.ts 3.97 KiB
import {Component, HostListener, Inject} from '@angular/core';
import {AuthService} from "./services/auth.service";
import {StorageService} from "./services/storage.service";
import {ActivatedRoute, Router} from "@angular/router";
import {AlertService} from "./services/alert.service";
import {
  faCheckSquare,
  faClone,
  faCube,
  faHome,
  faList,
  faMoon,
  faSignInAlt,
  faSignOutAlt,
  faSlidersH,
  faStream,
  faSun,
  faToggleOn
} from "@fortawesome/free-solid-svg-icons";
import {DOCUMENT} from "@angular/common";
import {WINDOW} from "./env/window.provider";
import {Title} from "@angular/platform-browser";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  public faHome = faHome;
  public faClone = faClone;
  public faStream = faStream;
  public faSlidersH = faSlidersH;
  public faSignInAlt = faSignInAlt;
  public faSignOutAlt = faSignOutAlt;
  public faCube = faCube;
  public faList = faList;
  public faCheckSquare = faCheckSquare;
  public faSun = faSun;
  public faMoon = faMoon;
  public faToggleOn = faToggleOn;

  public darkMode = false;

  constructor(
    private titleService: Title,
    private authService: AuthService,
    private storageService: StorageService,
    private router: Router,
    private route: ActivatedRoute,
    private alertService: AlertService,
    @Inject(DOCUMENT) document,
    @Inject(WINDOW) private window: Window
  ) {
    // set the title based on the host
    let title = 'VLASS Manager'
    switch (this.window.location.hostname) {
      case 'archive-test.nrao.edu':
      case 'data-test.nrao.edu':
      case 'vlass-test.nrao.edu':
        title = 'TEST | ' + title;
        break;
      case 'archive-new.nrao.edu':
      case 'data.nrao.edu':
      case 'vlass.nrao.edu':
        // leave the title alone
        break;
      case 'webtest.aoc.nrao.edu':
        title = 'DEV | ' + title;
        break;
      case 'localhost':
        title = 'LOCAL | ' + title;
        break;
      default:
        title = '~?~ | ' + title
        break;
    }
    this.titleService.setTitle(title);
    /*
    this.route.queryParams.subscribe(params => {
      if (params.hasOwnProperty('ticket')) {
        console.log(params.ticket);
        if (this.authService.logIn(decodeURIComponent(params.ticket))) {
          this.alertService.success('You are logged in.');
        } else {
          this.alertService.error('Login failed');
        }
      }
    });
     */

    const dm = this.storageService.getLocal('darkmode');
    this.darkMode = (dm === 'true');
  }

  @HostListener('window:scroll', ['$event'])
  onWindowScroll(e) {
    let stickyNav = document.getElementById('sticky_nav');
    if (window.pageYOffset > 100) {
      stickyNav.classList.add('sticky');
    } else {
      stickyNav.classList.remove('sticky');
    }
  }

  isLoggedIn(): boolean {
    return this.authService.isAuthenticated();
  }

  signIn(): void {
    this.authService.signIn();
  }

  signOut(): void {
    // delete all the cookies
      const cookies = document.cookie.split("; ");
      for (let c = 0; c < cookies.length; c++) {
        const d = window.location.hostname.split(".");
        while (d.length > 0) {
          const cookieBase = encodeURIComponent(cookies[c].split(";")[0].split("=")[0]) + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=' + d.join('.') + ' ;path=';
          const p = location.pathname.split('/');
          document.cookie = cookieBase + '/';
          while (p.length > 0) {
            document.cookie = cookieBase + p.join('/');
            p.pop();
          }
          d.shift();
        }
      }
      window.location.replace('https://my.nrao.edu/cas/logout?url=' + window.location.href);
    /*
    this.alertService.success('You are logged out.');
    this.storageService.deleteLocal('jwt');
    this.router.navigate(['/']);
     */
  }

  setDarkMode(dm: boolean): void {
    this.darkMode = dm;
    this.storageService.saveLocal('darkmode', dm.toString());
  }

}