Project

General

Profile

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

    
10
@Component({
11
    selector: 'user-mini',
12
    template: `
13
<ul *ngIf="!mobileView"  class="uk-navbar-nav">
14
   <li class="uk-parent">
15

    
16
  <a   *ngIf="loggedIn" >
17
       <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>
18
     </a>
19
         <a  *ngIf="!loggedIn" class="loginLink" [href]="loginUrl"  >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>
20
       </a>
21

    
22

    
23
  <div *ngIf="loggedIn" class="uk-navbar-dropdown uk-navbar-dropdown-bottom-left"   id="userMenu" (click)="onClick('userMenu')"  >
24
     <div class="uk-navbar-dropdown-grid uk-child-width-1-1 uk-grid uk-grid-stack" uk-grid="">
25
        <div class="uk-first-column">
26
           <ul class="uk-nav uk-navbar-dropdown-nav">
27
             <li><a href="" >My profile</a></li>
28
             <li><a   routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
29
             <li *ngIf="isAuthorized"><a   routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li>
30
              <li><a (click)="logOut()">Log out</a></li>
31
           </ul>
32
        </div>
33
     </div>
34
  </div>   </li>
35
  </ul>
36

    
37
<li *ngIf="mobileView" class="uk-nav-header uk-parent " >
38

    
39
    <span  *ngIf="loggedIn" >
40
      <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>
41
    </span>
42
    <span  *ngIf="!loggedIn">
43
        <a  class="loginLink" [href]="loginUrl"  >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>
44
        </a>
45
   </span>
46

    
47
  <ul *ngIf="loggedIn" class="uk-nav-sub">
48
  <li><a href="" >My profile</a></li>
49
  <li><a   routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
50
  <li *ngIf="isAuthorized"><a   routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li>
51
  <li><a (click)="logOut()" >Log out</a></li>
52
 </ul>
53
</li>
54

    
55
    `
56
})
57

    
58
export class UserMiniComponent {
59
    public user: User;
60
    public loggedIn: boolean = false;
61
    public isAuthorized: boolean = false;
62
    @Input() public mobileView:boolean = false ;
63
    public server: boolean = true;
64
    public routerHelper:RouterHelper = new RouterHelper();
65
    public loginUrl= OpenaireProperties.getLoginURL();
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
        }else{
96
          Session.removeUser();
97
            this.loggedIn = false;
98
             this.isAuthorized = false;
99
            this.user = null;
100
        }
101
      }else {
102
            this.loggedIn = false;
103
             this.isAuthorized = false;
104
            this.user = null;
105
      }
106

    
107
    }
108
    gotoUserPage(){
109
      this.redirectUrl = this.location.path();
110
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != "" && this.redirectUrl !="user-info"){
111
          this.router.navigate([this.baseUrl], { queryParams: this.routerHelper.createQueryParam("redirectUrl",this.redirectUrl )});
112
      }else{
113
        this.router.navigate([this.baseUrl]);
114
      }
115
    }
116
    logOut(){
117
      if(Session.isLoggedIn()){
118
        Session.removeUser();
119
        window.location.href = OpenaireProperties.getLogoutURL()+ location.href;
120

    
121
      }
122
      this.loggedIn = false;
123
       this.isAuthorized = false;
124
      this.user = new User();
125
    }
126

    
127
    onClick(id: string) {
128
          var el: HTMLElement = document.getElementById(id);
129
          el.classList.remove('uk-open');
130
      }
131

    
132
}
(7-7/7)