navigator.component.ts 4.39 KB
Newer Older
1 2
import { Component, Output, EventEmitter, OnInit, Inject } from '@angular/core';
import { Router, NavigationExtras } from '@angular/router';
Steven Zou's avatar
Steven Zou committed
3
import { TranslateService } from '@ngx-translate/core';
kunw's avatar
kunw committed
4

5
import { ModalEvent } from '../modal-event';
Steven Zou's avatar
Steven Zou committed
6
import { modalEvents } from '../modal-events.const';
7 8 9

import { SessionUser } from '../../shared/session-user';
import { SessionService } from '../../shared/session.service';
Steven Zou's avatar
Steven Zou committed
10 11
import { CookieService } from 'angular2-cookie/core';

12 13 14 15
import { supportedLangs, enLang, languageNames, signInRoute } from '../../shared/shared.const';

import { AppConfigService } from '../../app-config.service';
import { AppConfig } from '../../app-config';
16

kunw's avatar
kunw committed
17 18
@Component({
    selector: 'navigator',
19 20
    templateUrl: "navigator.component.html",
    styleUrls: ["navigator.component.css"]
kunw's avatar
kunw committed
21
})
22 23

export class NavigatorComponent implements OnInit {
kunw's avatar
kunw committed
24
    // constructor(private router: Router){}
25 26 27 28
    @Output() showAccountSettingsModal = new EventEmitter<ModalEvent>();
    @Output() showPwdChangeModal = new EventEmitter<ModalEvent>();

    private sessionUser: SessionUser = null;
Steven Zou's avatar
Steven Zou committed
29
    private selectedLang: string = enLang;
30
    private appConfig: AppConfig = new AppConfig();
31

Steven Zou's avatar
Steven Zou committed
32 33 34 35
    constructor(
        private session: SessionService,
        private router: Router,
        private translate: TranslateService,
36 37
        private cookie: CookieService,
        private appConfigService: AppConfigService) { }
38 39 40

    ngOnInit(): void {
        this.sessionUser = this.session.getCurrentUser();
Steven Zou's avatar
Steven Zou committed
41 42 43 44 45 46
        this.selectedLang = this.translate.currentLang;
        this.translate.onLangChange.subscribe(langChange => {
            this.selectedLang = langChange.lang;
            //Keep in cookie for next use
            this.cookie.put("harbor-lang", langChange.lang);
        });
47 48

        this.appConfig = this.appConfigService.getConfig();
49 50 51 52 53 54
    }

    public get isSessionValid(): boolean {
        return this.sessionUser != null;
    }

kunw's avatar
kunw committed
55
    public get accountName(): string {
Steven Zou's avatar
Steven Zou committed
56 57 58 59 60 61 62
        return this.sessionUser ? this.sessionUser.username : "";
    }

    public get currentLang(): string {
        return languageNames[this.selectedLang];
    }

63 64 65 66 67 68 69 70 71 72 73 74 75
    public get isIntegrationMode(): boolean {
        return this.appConfig.with_admiral && this.appConfig.admiral_endpoint.trim() != "";
    }

    public get admiralLink(): string {
        let routeSegments = [this.appConfig.admiral_endpoint,
        "?registry_url=",
        encodeURIComponent(window.location.href)
        ];

        return routeSegments.join("");
    }

Steven Zou's avatar
Steven Zou committed
76 77
    matchLang(lang: string): boolean {
        return lang.trim() === this.selectedLang;
kunw's avatar
kunw committed
78 79
    }

80 81 82
    //Open the account setting dialog
    openAccountSettingsModal(): void {
        this.showAccountSettingsModal.emit({
Steven Zou's avatar
Steven Zou committed
83
            modalName: modalEvents.USER_PROFILE,
84 85 86 87 88 89 90
            modalFlag: true
        });
    }

    //Open change password dialog
    openChangePwdModal(): void {
        this.showPwdChangeModal.emit({
Steven Zou's avatar
Steven Zou committed
91 92 93 94 95 96 97 98 99
            modalName: modalEvents.CHANGE_PWD,
            modalFlag: true
        });
    }

    //Open about dialog
    openAboutDialog(): void {
        this.showPwdChangeModal.emit({
            modalName: modalEvents.ABOUT,
100 101 102 103 104
            modalFlag: true
        });
    }

    //Log out system
kunw's avatar
kunw committed
105
    logOut(): void {
106 107 108
        this.session.signOff()
            .then(() => {
                this.sessionUser = null;
kunw's avatar
kunw committed
109 110
                //Naviagte to the sign in route
                this.router.navigate(["/sign-in"]);
111 112 113
            })
            .catch()//TODO:
    }
Steven Zou's avatar
Steven Zou committed
114 115 116

    //Switch languages
    switchLanguage(lang: string): void {
117
        if (supportedLangs.find(supportedLang => supportedLang === lang.trim())) {
Steven Zou's avatar
Steven Zou committed
118
            this.translate.use(lang);
119
        } else {
Steven Zou's avatar
Steven Zou committed
120 121
            this.translate.use(enLang);//Use default
            //TODO:
122
            console.error('Language ' + lang.trim() + ' is not suppoted');
Steven Zou's avatar
Steven Zou committed
123
        }
Steven Zou's avatar
Steven Zou committed
124 125
        //Try to switch backend lang
        //this.session.switchLanguage(lang).catch(error => console.error(error));
Steven Zou's avatar
Steven Zou committed
126 127 128 129
    }

    //Handle the home action
    homeAction(): void {
130
        if (this.sessionUser != null) {
Steven Zou's avatar
Steven Zou committed
131
            //Navigate to default page
Steven Zou's avatar
Steven Zou committed
132
            this.router.navigate(['harbor']);
133
        } else {
Steven Zou's avatar
Steven Zou committed
134 135 136 137
            //Naviagte to signin page
            this.router.navigate(['sign-in']);
        }
    }
138 139 140 141 142 143 144 145

    openSignUp(): void {
        let navigatorExtra: NavigationExtras = {
            queryParams: { "sign_up": true }
        };

        this.router.navigate([signInRoute], navigatorExtra);
    }
kunw's avatar
kunw committed
146
}