1
|
import {Component, ElementRef, Input} from '@angular/core';
|
2
|
import {Observable} from 'rxjs/Observable';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {Location} from '@angular/common';
|
5
|
// import {LoginService} from './login.service';
|
6
|
import {User,Session} from './utils/helper.class';
|
7
|
import {RouterHelper} from '../utils/routerHelper.class';
|
8
|
import {StringUtils} from '../utils/string-utils.class';
|
9
|
import{EnvProperties} from '../utils/properties/env-properties';
|
10
|
|
11
|
// declare var logoutClicked;
|
12
|
@Component({
|
13
|
selector: 'user-mini',
|
14
|
template: `
|
15
|
<ul *ngIf="!mobileView" class="uk-navbar-nav">
|
16
|
<li class="uk-parent">
|
17
|
|
18
|
<a *ngIf="loggedIn" >
|
19
|
<span>{{user.fullname+" "}} <span class="uk-margin-small-right uk-icon" ><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"><circle fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none" stroke="#000" stroke-width="1.1" d="M1.5,19 C2.3,14.5 5.8,11.2 10,11.2 C14.2,11.2 17.7,14.6 18.5,19.2"></path></svg></span></span>
|
20
|
</a>
|
21
|
<a *ngIf="!loggedIn" class="loginLink" (click)="logIn()" >Sign in | Register <span class="uk-margin-small-right uk-icon" ><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"><circle fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none" stroke="#000" stroke-width="1.1" d="M1.5,19 C2.3,14.5 5.8,11.2 10,11.2 C14.2,11.2 17.7,14.6 18.5,19.2"></path></svg> </span>
|
22
|
</a>
|
23
|
|
24
|
|
25
|
<div *ngIf="loggedIn" class="uk-navbar-dropdown uk-navbar-dropdown-bottom-left" id="userMenu" (click)="onClick('userMenu')" >
|
26
|
<div class="uk-navbar-dropdown-grid uk-child-width-1-1 uk-grid uk-grid-stack" uk-grid="">
|
27
|
<div class="uk-first-column">
|
28
|
<ul class="uk-nav uk-navbar-dropdown-nav">
|
29
|
<!--li><a href="" >My profile</a></li>
|
30
|
<li><a routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
|
31
|
<li *ngIf="isAuthorized"><a routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li-->
|
32
|
<ng-container *ngFor="let item of userMenuItems ">
|
33
|
<li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
|
34
|
<a *ngIf="item.route.length > 0" routerLink="{{item.route}}" [queryParams]=item.params >{{item.title}}</a>
|
35
|
<a *ngIf="item.route.length == 0 && item.url.length > 0" href="{{item.url}}" >{{item.title}}</a>
|
36
|
</li>
|
37
|
</ng-container>
|
38
|
<li><a (click)="logOut()" id="logout" >Log out</a></li>
|
39
|
</ul>
|
40
|
</div>
|
41
|
<div id="logout1"></div>
|
42
|
</div>
|
43
|
</div> </li>
|
44
|
</ul>
|
45
|
|
46
|
<li *ngIf="mobileView" class="uk-nav-header uk-parent " >
|
47
|
|
48
|
<span *ngIf="loggedIn" >
|
49
|
<span>{{user.fullname+" "}}<span class="uk-margin-small-right uk-icon" ><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"><circle fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none" stroke="#000" stroke-width="1.1" d="M1.5,19 C2.3,14.5 5.8,11.2 10,11.2 C14.2,11.2 17.7,14.6 18.5,19.2"></path></svg></span></span>
|
50
|
</span>
|
51
|
<span *ngIf="!loggedIn">
|
52
|
<a class="loginLink" (click)="logIn()" >Sign in | Register <span class="uk-margin-small-right uk-icon" ><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"><circle fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none" stroke="#000" stroke-width="1.1" d="M1.5,19 C2.3,14.5 5.8,11.2 10,11.2 C14.2,11.2 17.7,14.6 18.5,19.2"></path></svg></span>
|
53
|
</a>
|
54
|
</span>
|
55
|
|
56
|
<ul *ngIf="loggedIn" class="uk-nav-sub">
|
57
|
<!--li><a href="" >My profile</a></li>
|
58
|
<li><a routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
|
59
|
<li *ngIf="isAuthorized"><a routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li-->
|
60
|
<ng-container *ngFor="let item of userMenuItems ">
|
61
|
<li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
|
62
|
<a *ngIf="item.route.length > 0" routerLink="{{item.route}}" >{{item.title}}</a>
|
63
|
<a *ngIf="item.route.length == 0 && item.url.length > 0" href="{{item.url}}" >{{item.title}}</a>
|
64
|
</li>
|
65
|
</ng-container>
|
66
|
<li><a (click)="logOut()" id="logout" >Log out</a></li>
|
67
|
</ul>
|
68
|
</li>
|
69
|
<div id="logout2"></div>
|
70
|
`
|
71
|
})
|
72
|
|
73
|
export class UserMiniComponent {
|
74
|
public user: User;
|
75
|
public loggedIn: boolean = false;
|
76
|
public isAuthorized: boolean = false;
|
77
|
@Input() public mobileView:boolean = false ;
|
78
|
public server: boolean = true;
|
79
|
public routerHelper:RouterHelper = new RouterHelper();
|
80
|
@Input() userMenuItems;
|
81
|
@Input() logInUrl;
|
82
|
@Input() logOutUrl;
|
83
|
@Input() cookieDomain;
|
84
|
public redirectUrl: string = "";
|
85
|
private baseUrl = "user-info";
|
86
|
sub:any;
|
87
|
// public properties:EnvProperties;
|
88
|
|
89
|
constructor( private router: Router, private route: ActivatedRoute, private location: Location) {}
|
90
|
|
91
|
ngOnInit() {
|
92
|
|
93
|
if( typeof document !== 'undefined') {
|
94
|
this.server = false;
|
95
|
}
|
96
|
this.initialize();
|
97
|
this.sub = this.route.queryParams.subscribe(params => {
|
98
|
this.initialize();
|
99
|
});
|
100
|
|
101
|
|
102
|
}
|
103
|
ngOnDestroy(){
|
104
|
this.sub.unsubscribe();
|
105
|
}
|
106
|
initialize(){
|
107
|
this.redirectUrl = this.location.path();
|
108
|
if(Session.isLoggedIn()){
|
109
|
if(Session.isUserValid()){
|
110
|
this.loggedIn = Session.isLoggedIn();
|
111
|
this.user = Session.getUser();
|
112
|
if(Session.isClaimsCurator() || Session.isPortalAdministrator()){
|
113
|
this.isAuthorized = true;
|
114
|
}else {
|
115
|
this.isAuthorized = false;
|
116
|
}
|
117
|
|
118
|
}else{
|
119
|
Session.removeUser();
|
120
|
this.loggedIn = false;
|
121
|
this.isAuthorized = false;
|
122
|
this.user = null;
|
123
|
}
|
124
|
}else {
|
125
|
this.loggedIn = false;
|
126
|
this.isAuthorized = false;
|
127
|
this.user = null;
|
128
|
}
|
129
|
|
130
|
}
|
131
|
gotoUserPage(){
|
132
|
this.redirectUrl = this.location.path();
|
133
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != "" && this.redirectUrl !="user-info"){
|
134
|
this.router.navigate([this.baseUrl], { queryParams: this.routerHelper.createQueryParam("redirectUrl",this.redirectUrl )});
|
135
|
}else{
|
136
|
this.router.navigate([this.baseUrl]);
|
137
|
}
|
138
|
}
|
139
|
logOut(){
|
140
|
if(Session.isLoggedIn()){
|
141
|
Session.removeUser();
|
142
|
console.log("Try to Logout!");
|
143
|
// this.logoutOpenaire.logout().subscribe(params => {
|
144
|
// console.log("Logout!");
|
145
|
// });
|
146
|
// logoutClicked();
|
147
|
console.log("Redirect to "+location.href);
|
148
|
window.location.href = this.logOutUrl + StringUtils.URIEncode(location.href);
|
149
|
|
150
|
}
|
151
|
this.loggedIn = false;
|
152
|
this.isAuthorized = false;
|
153
|
this.user = new User();
|
154
|
}
|
155
|
|
156
|
logIn(){
|
157
|
console.log("LOgin is clicked " + this.logInUrl);
|
158
|
Session.setCurrentUrl(location.pathname);
|
159
|
Session.setCurrentParameters(location.search);
|
160
|
window.location.href = this.logInUrl
|
161
|
}
|
162
|
|
163
|
onClick(id: string) {
|
164
|
var el: HTMLElement = document.getElementById(id);
|
165
|
el.classList.remove('uk-open');
|
166
|
}
|
167
|
|
168
|
}
|