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
import {StringUtils} from '../utils/string-utils.class';
10
import {LogoutOpenaireService} from './utils/logoutOpenaire.service';
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
              <li><a (click)="logOut()" id="logout" >Log out</a></li>
33
           </ul>
34
        </div>
35
        <div id="logout1"></div>
36
     </div>
37
  </div>   </li>
38
  </ul>
39

    
40
<li *ngIf="mobileView" class="uk-nav-header uk-parent " >
41

    
42
    <span  *ngIf="loggedIn" >
43
      <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>
44
    </span>
45
    <span  *ngIf="!loggedIn">
46
        <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>
47
        </a>
48
   </span>
49

    
50
  <ul *ngIf="loggedIn" class="uk-nav-sub">
51
  <li><a href="" >My profile</a></li>
52
  <li><a   routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
53
  <li *ngIf="isAuthorized"><a   routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li>
54
  <li><a (click)="logOut()" id="logout" >Log out</a></li>
55
 </ul>
56
</li>
57
<div id="logout2"></div>
58
    `
59
})
60

    
61
export class UserMiniComponent {
62
    public user: User;
63
    public loggedIn: boolean = false;
64
    public isAuthorized: boolean = false;
65
    @Input() public mobileView:boolean = false ;
66
    public server: boolean = true;
67
    public routerHelper:RouterHelper = new RouterHelper();
68

    
69
    public redirectUrl: string = "";
70
    private baseUrl = "user-info";
71
    sub:any;
72
    constructor( private router: Router, private  route: ActivatedRoute, private location: Location, private logoutOpenaire: LogoutOpenaireService) {}
73

    
74
    ngOnInit() {
75
      if( typeof document !== 'undefined') {
76
        this.server = false;
77
      }
78
      this.initialize();
79
      this.sub =  this.route.queryParams.subscribe(params => {
80
        this.initialize();
81
      });
82
    }
83
    ngOnDestroy(){
84
      this.sub.unsubscribe();
85
    }
86
    initialize(){
87
      this.redirectUrl = this.location.path();
88
      if(Session.isLoggedIn()){
89
        if(Session.isUserValid()){
90
          this.loggedIn = Session.isLoggedIn();
91
          this.user = Session.getUser();
92
          if(Session.isAdminUser()){
93
           this.isAuthorized = true;
94
           }else {
95
                 this.isAuthorized = false;
96
           }
97

    
98
        }else{
99
          Session.removeUser();
100
            this.loggedIn = false;
101
             this.isAuthorized = false;
102
            this.user = null;
103
        }
104
      }else {
105
            this.loggedIn = false;
106
             this.isAuthorized = false;
107
            this.user = null;
108
      }
109

    
110
    }
111
    gotoUserPage(){
112
      this.redirectUrl = this.location.path();
113
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != "" && this.redirectUrl !="user-info"){
114
          this.router.navigate([this.baseUrl], { queryParams: this.routerHelper.createQueryParam("redirectUrl",this.redirectUrl )});
115
      }else{
116
        this.router.navigate([this.baseUrl]);
117
      }
118
    }
119
    logOut(){
120
      if(Session.isLoggedIn()){
121
        Session.removeUser();
122
        // console.log("Try to Logout!");
123
        // this.logoutOpenaire.logout().subscribe(params => {
124
        //   console.log("Logout!");
125
        // });
126
        logoutClicked();
127
        console.log("Redirect to "+location.href);
128
        window.location.href = OpenaireProperties.getLogoutURL()+ StringUtils.URIEncode(location.href);
129

    
130
      }
131
      this.loggedIn = false;
132
       this.isAuthorized = false;
133
      this.user = new User();
134
    }
135

    
136
    logIn(){
137
      Session.setCurrentUrl(location.pathname);
138
      Session.setCurrentParameters(location.search);
139
      window.location.href = OpenaireProperties.getLoginURL();
140
    }
141

    
142
    onClick(id: string) {
143
          var el: HTMLElement = document.getElementById(id);
144
          el.classList.remove('uk-open');
145
      }
146

    
147
}
(8-8/8)