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 {OpenaireProperties} from '../utils/properties/openaireProperties';
|
9
|
import {StringUtils} from '../utils/string-utils.class';
|
10
|
|
11
|
@Component({
|
12
|
selector: 'user-mini',
|
13
|
template: `
|
14
|
<ul *ngIf="!mobileView" class="uk-navbar-nav">
|
15
|
<li class="uk-parent">
|
16
|
|
17
|
<a *ngIf="loggedIn" >
|
18
|
<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>
|
19
|
</a>
|
20
|
<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>
|
21
|
</a>
|
22
|
|
23
|
|
24
|
<div *ngIf="loggedIn" class="uk-navbar-dropdown uk-navbar-dropdown-bottom-left" id="userMenu" (click)="onClick('userMenu')" >
|
25
|
<div class="uk-navbar-dropdown-grid uk-child-width-1-1 uk-grid uk-grid-stack" uk-grid="">
|
26
|
<div class="uk-first-column">
|
27
|
<ul class="uk-nav uk-navbar-dropdown-nav">
|
28
|
<li><a href="" >My profile</a></li>
|
29
|
<li><a routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
|
30
|
<li *ngIf="isAuthorized"><a routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li>
|
31
|
<li><a (click)="logOut()">Log out</a></li>
|
32
|
</ul>
|
33
|
</div>
|
34
|
</div>
|
35
|
</div> </li>
|
36
|
</ul>
|
37
|
|
38
|
<li *ngIf="mobileView" class="uk-nav-header uk-parent " >
|
39
|
|
40
|
<span *ngIf="loggedIn" >
|
41
|
<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>
|
42
|
</span>
|
43
|
<span *ngIf="!loggedIn">
|
44
|
<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>
|
45
|
</a>
|
46
|
</span>
|
47
|
|
48
|
<ul *ngIf="loggedIn" class="uk-nav-sub">
|
49
|
<li><a href="" >My profile</a></li>
|
50
|
<li><a routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
|
51
|
<li *ngIf="isAuthorized"><a routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li>
|
52
|
<li><a (click)="logOut()" >Log out</a></li>
|
53
|
</ul>
|
54
|
</li>
|
55
|
|
56
|
`
|
57
|
})
|
58
|
|
59
|
export class UserMiniComponent {
|
60
|
public user: User;
|
61
|
public loggedIn: boolean = false;
|
62
|
public isAuthorized: boolean = false;
|
63
|
@Input() public mobileView:boolean = false ;
|
64
|
public server: boolean = true;
|
65
|
public routerHelper:RouterHelper = new RouterHelper();
|
66
|
|
67
|
public redirectUrl: string = "";
|
68
|
private baseUrl = "user-info";
|
69
|
sub:any;
|
70
|
constructor( private router: Router, private route: ActivatedRoute, private location: Location) {}
|
71
|
|
72
|
ngOnInit() {
|
73
|
if( typeof document !== 'undefined') {
|
74
|
this.server = false;
|
75
|
}
|
76
|
this.initialize();
|
77
|
this.sub = this.route.queryParams.subscribe(params => {
|
78
|
this.initialize();
|
79
|
});
|
80
|
}
|
81
|
ngOnDestroy(){
|
82
|
this.sub.unsubscribe();
|
83
|
}
|
84
|
initialize(){
|
85
|
this.redirectUrl = this.location.path();
|
86
|
if(Session.isLoggedIn()){
|
87
|
if(Session.isUserValid()){
|
88
|
this.loggedIn = Session.isLoggedIn();
|
89
|
this.user = Session.getUser();
|
90
|
if(Session.isAdminUser()){
|
91
|
this.isAuthorized = true;
|
92
|
}else {
|
93
|
this.isAuthorized = false;
|
94
|
}
|
95
|
|
96
|
}else{
|
97
|
Session.removeUser();
|
98
|
this.loggedIn = false;
|
99
|
this.isAuthorized = false;
|
100
|
this.user = null;
|
101
|
}
|
102
|
}else {
|
103
|
this.loggedIn = false;
|
104
|
this.isAuthorized = false;
|
105
|
this.user = null;
|
106
|
}
|
107
|
|
108
|
}
|
109
|
gotoUserPage(){
|
110
|
this.redirectUrl = this.location.path();
|
111
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != "" && this.redirectUrl !="user-info"){
|
112
|
this.router.navigate([this.baseUrl], { queryParams: this.routerHelper.createQueryParam("redirectUrl",this.redirectUrl )});
|
113
|
}else{
|
114
|
this.router.navigate([this.baseUrl]);
|
115
|
}
|
116
|
}
|
117
|
logOut(){
|
118
|
if(Session.isLoggedIn()){
|
119
|
Session.removeUser();
|
120
|
console.log("Redirect to "+location.href);
|
121
|
window.location.href = OpenaireProperties.getLogoutURL()+ StringUtils.URIEncode(location.href);
|
122
|
|
123
|
}
|
124
|
this.loggedIn = false;
|
125
|
this.isAuthorized = false;
|
126
|
this.user = new User();
|
127
|
}
|
128
|
|
129
|
logIn(){
|
130
|
Session.setCurrentUrl(location.pathname);
|
131
|
Session.setCurrentParameters(location.search);
|
132
|
window.location.href = OpenaireProperties.getLoginURL();
|
133
|
}
|
134
|
|
135
|
onClick(id: string) {
|
136
|
var el: HTMLElement = document.getElementById(id);
|
137
|
el.classList.remove('uk-open');
|
138
|
}
|
139
|
|
140
|
}
|