navigator.component.ts 5.1 KB
Newer Older
kunw's avatar
kunw committed
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
import { supportedLangs, enLang, languageNames, CommonRoutes } from '../../shared/shared.const';
kunw's avatar
kunw committed
13
import { AppConfigService } from '../../app-config.service';
kunw's avatar
kunw committed
14
import { SearchTriggerService } from '../global-search/search-trigger.service';
15
import { MessageHandlerService } from '../../shared/message-handler/message-handler.service';
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
    @Output() showAccountSettingsModal = new EventEmitter<ModalEvent>();
    @Output() showPwdChangeModal = new EventEmitter<ModalEvent>();

Steven Zou's avatar
Steven Zou committed
28
    private selectedLang: string = enLang;
Steven Zou's avatar
Steven Zou committed
29
    private appTitle: string = 'APP_TITLE.HARBOR';
30

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

    ngOnInit(): void {
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);
        });
Steven Zou's avatar
Steven Zou committed
47 48 49
        if (this.appConfigService.isIntegrationMode()) {
            this.appTitle = 'APP_TITLE.VIC';
        }
50 51 52
    }

    public get isSessionValid(): boolean {
kunw's avatar
kunw committed
53
        return this.session.getCurrentUser() != null;
54 55
    }

kunw's avatar
kunw committed
56
    public get accountName(): string {
kunw's avatar
kunw committed
57
        return this.session.getCurrentUser() ? this.session.getCurrentUser().username : "N/A";
Steven Zou's avatar
Steven Zou committed
58 59 60 61 62 63
    }

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

kunw's avatar
kunw committed
64 65 66 67 68 69 70 71
    public get admiralLink(): string {
        return this.appConfigService.getAdmiralEndpoint(window.location.href);
    }

    public get isIntegrationMode(): boolean {
        return this.appConfigService.isIntegrationMode();
    }

72
    public get canDownloadCert(): boolean {
73 74 75 76
        return this.session.getCurrentUser() &&
            this.session.getCurrentUser().has_admin_role > 0 &&
            this.appConfigService.getConfig() &&
            this.appConfigService.getConfig().has_ca_root;
77 78
    }

Steven Zou's avatar
Steven Zou committed
79 80 81 82 83 84
    public get canChangePassword(): boolean {
        return this.session.getCurrentUser() &&
        this.appConfigService.getConfig() &&
        this.appConfigService.getConfig().auth_mode != 'ldap_auth';
    }

Steven Zou's avatar
Steven Zou committed
85 86
    matchLang(lang: string): boolean {
        return lang.trim() === this.selectedLang;
kunw's avatar
kunw committed
87 88
    }

89 90 91
    //Open the account setting dialog
    openAccountSettingsModal(): void {
        this.showAccountSettingsModal.emit({
Steven Zou's avatar
Steven Zou committed
92
            modalName: modalEvents.USER_PROFILE,
93 94 95 96 97 98 99
            modalFlag: true
        });
    }

    //Open change password dialog
    openChangePwdModal(): void {
        this.showPwdChangeModal.emit({
Steven Zou's avatar
Steven Zou committed
100 101 102 103 104 105 106 107 108
            modalName: modalEvents.CHANGE_PWD,
            modalFlag: true
        });
    }

    //Open about dialog
    openAboutDialog(): void {
        this.showPwdChangeModal.emit({
            modalName: modalEvents.ABOUT,
109 110 111 112 113
            modalFlag: true
        });
    }

    //Log out system
kunw's avatar
kunw committed
114
    logOut(): void {
115 116
        this.session.signOff()
            .then(() => {
kunw's avatar
kunw committed
117
                //Naviagte to the sign in route
kunw's avatar
kunw committed
118
                this.router.navigate([CommonRoutes.EMBEDDED_SIGN_IN]);
119
            })
kunw's avatar
kunw committed
120
            .catch(error => {
121
                this.msgHandler.handleError(error);
kunw's avatar
kunw committed
122
            });
kunw's avatar
kunw committed
123
        //Confirm search result panel is close
kunw's avatar
kunw committed
124
        this.searchTrigger.closeSearch(true);
125
    }
Steven Zou's avatar
Steven Zou committed
126 127 128

    //Switch languages
    switchLanguage(lang: string): void {
kunw's avatar
kunw committed
129
        if (supportedLangs.find(supportedLang => supportedLang === lang.trim())) {
Steven Zou's avatar
Steven Zou committed
130
            this.translate.use(lang);
kunw's avatar
kunw committed
131
        } else {
Steven Zou's avatar
Steven Zou committed
132 133
            this.translate.use(enLang);//Use default
            //TODO:
kunw's avatar
kunw committed
134
            console.error('Language ' + lang.trim() + ' is not suppoted');
Steven Zou's avatar
Steven Zou committed
135
        }
Steven Zou's avatar
Steven Zou committed
136 137
        //Try to switch backend lang
        //this.session.switchLanguage(lang).catch(error => console.error(error));
Steven Zou's avatar
Steven Zou committed
138 139 140 141
    }

    //Handle the home action
    homeAction(): void {
kunw's avatar
kunw committed
142
        if (this.session.getCurrentUser() != null) {
Steven Zou's avatar
Steven Zou committed
143
            //Navigate to default page
kunw's avatar
kunw committed
144 145
            this.router.navigate([CommonRoutes.HARBOR_DEFAULT]);
        } else {
Steven Zou's avatar
Steven Zou committed
146
            //Naviagte to signin page
kunw's avatar
kunw committed
147
            this.router.navigate([CommonRoutes.HARBOR_ROOT]);
Steven Zou's avatar
Steven Zou committed
148
        }
kunw's avatar
kunw committed
149 150

        //Confirm search result panel is close
kunw's avatar
kunw committed
151
        this.searchTrigger.closeSearch(true);
kunw's avatar
kunw committed
152 153 154
    }

    registryAction(): void {
kunw's avatar
kunw committed
155
        this.searchTrigger.closeSearch(true);
Steven Zou's avatar
Steven Zou committed
156
    }
kunw's avatar
kunw committed
157
}