Project

General

Profile

1
import {Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Location} from '@angular/common';
4
import {User, Session} from './utils/helper.class';
5
import {RouterHelper} from '../utils/routerHelper.class';
6
import {StringUtils} from '../utils/string-utils.class';
7
import {EnvProperties} from '../utils/properties/env-properties';
8

    
9
// declare var logoutClicked;
10
@Component({
11
    selector: 'user-mini',
12
    template: `
13
      <ng-container *ngIf="!dashboard">
14

    
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"
20
                                                                                             viewBox="0 0 20 20"
21
                                                                                             xmlns="http://www.w3.org/2000/svg"
22
                                                                                             ratio="1"><circle
23
                  fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none"
24
                                                                                                        stroke="#000"
25
                                                                                                        stroke-width="1.1"
26
                                                                                                        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>
27
            </a>
28
            <a *ngIf="!loggedIn" class="loginLink" (click)="logIn()">Sign in <span
29
                class=" uk-margin-small-left uk-margin-small-right uk-icon"><svg width="20" height="20"
30
                                                                                 viewBox="0 0 20 20"
31
                                                                                 xmlns="http://www.w3.org/2000/svg"
32
                                                                                 ratio="1"><circle fill="none"
33
                                                                                                   stroke="#000"
34
                                                                                                   stroke-width="1.1"
35
                                                                                                   cx="9.9" cy="6.4"
36
                                                                                                   r="4.4"></circle><path
37
                fill="none" stroke="#000" stroke-width="1.1"
38
                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>
39
            </a>
40

    
41

    
42
            <div *ngIf="loggedIn" class="uk-navbar-dropdown uk-navbar-dropdown-bottom-left" id="userMenu"
43
                 (click)="onClick('userMenu')">
44
              <div class="uk-navbar-dropdown-grid uk-child-width-1-1 uk-grid uk-grid-stack" uk-grid="">
45
                <div class="uk-first-column uk-height-max-medium uk-overflow-auto">
46
                  <ul class="uk-nav uk-navbar-dropdown-nav">
47
                    <!--li><a href="" >My profile</a></li>
48
                    <li><a   routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
49
                    <li *ngIf="isAuthorized"><a   routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li-->
50
                    <ng-container *ngFor="let item of userMenuItems ">
51
                      <li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
52
                        <a *ngIf="item.route.length > 0" routerLink="{{item.route}}"
53
                           [queryParams]=item.params>{{item.title}}</a>
54
                        <a *ngIf="item.route.length == 0 && item.url.length > 0" href="{{item.url}}"
55
                           target="_blank">{{item.title}}</a>
56
                      </li>
57
                    </ng-container>
58
                    <li *ngIf="userMenuItems.length > 0" class="uk-nav-divider"></li>
59
                    <li><a (click)="logOut()" id="logout">Log out</a></li>
60
                  </ul>
61
                </div>
62
                <div id="logout1"></div>
63
              </div>
64
            </div>
65
          </li>
66
        </ul>
67
      </ng-container>
68

    
69
      <ng-container *ngIf="!dashboard">
70

    
71
        <li *ngIf="mobileView" class="uk-nav-header uk-parent ">
72

    
73
          <span *ngIf="loggedIn">
74
            <span>{{user.fullname + " "}}<span class="uk-margin-small-right uk-icon"><svg width="20" height="20"
75
                                                                                          viewBox="0 0 20 20"
76
                                                                                          xmlns="http://www.w3.org/2000/svg"
77
                                                                                          ratio="1"><circle fill="none"
78
                                                                                                            stroke="#000"
79
                                                                                                            stroke-width="1.1"
80
                                                                                                            cx="9.9"
81
                                                                                                            cy="6.4"
82
                                                                                                            r="4.4"></circle><path
83
                fill="none" stroke="#000" stroke-width="1.1"
84
                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>
85
          </span>
86
          <span *ngIf="!loggedIn">
87
              <a class="loginLink" (click)="logIn()">Sign in <span class="uk-margin-small-right uk-icon"><svg width="20"
88
                                                                                                              height="20"
89
                                                                                                              viewBox="0 0 20 20"
90
                                                                                                              xmlns="http://www.w3.org/2000/svg"
91
                                                                                                              ratio="1"><circle
92
                  fill="none" stroke="#000" stroke-width="1.1" cx="9.9" cy="6.4" r="4.4"></circle><path fill="none"
93
                                                                                                        stroke="#000"
94
                                                                                                        stroke-width="1.1"
95
                                                                                                        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>
96
              </a>
97
         </span>
98

    
99
          <ul *ngIf="loggedIn" class="uk-nav-sub">
100
            <!--li><a href="" >My profile</a></li>
101
            <li><a   routerLinkActive="uk-link" routerLink="/myclaims">My Claims</a></li>
102
            <li *ngIf="isAuthorized"><a   routerLinkActive="uk-link" routerLink="/claims">Manage all Claims</a></li-->
103
            <ng-container *ngFor="let item of userMenuItems ">
104
              <li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
105
                <a *ngIf="item.route.length > 0" routerLink="{{item.route}}">{{item.title}}</a>
106
                <a *ngIf="item.route.length == 0 && item.url.length > 0" href="{{item.url}}">{{item.title}}</a>
107
              </li>
108
            </ng-container>
109
            <li *ngIf="userMenuItems.length > 0" class="uk-nav-divider"></li>
110
            <li><a (click)="logOut()" id="logout">Log out</a></li>
111
          </ul>
112
        </li>
113
      </ng-container>
114

    
115
      <ng-container *ngIf="dashboard">
116
        <a *ngIf="!loggedIn" [class]="'loginLink '+colors" (click)="logIn()">
117
          Sign in <span class=" uk-margin-small-left uk-margin-small-right uk-icon">
118
            <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1">
119
              <circle [attr.class]="'loginLink userBox '+colors" cx="9.9" cy="6.4" r="4.4" stroke-width="1.1"></circle>
120
              <path [attr.class]="'loginLink userBox '+colors"
121
                    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" stroke-width="1.1"></path>
122
            </svg> </span>
123
        </a>
124
        <a *ngIf="loggedIn" class="user_action_image uk-icon">
125
          <svg height="60" width="60">
126
            <circle [attr.class]="'userBox '+colors" cx="30" cy="30" r="20" stroke-width="2"></circle>
127
            <text *ngIf="firstLetters" [attr.class]="'userBox '+colors" x="50%" y="50%" text-anchor="middle" dy=".4em"
128
                  font-size="1.5em">
129
              {{firstLetters}}
130
            </text>
131
            <svg *ngIf="!firstLetters" ratio="1" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" width="20"
132
                 x="20">
133
              <circle [attr.class]="'icon userBox '+colors" cx="9.9" cy="6.4" r="4.4" stroke-width="1.1"></circle>
134
              <path [attr.class]="'icon userBox '+colors"
135
                    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" stroke-width="1.1"></path>
136
            </svg>
137
          </svg>
138
          <i [class]="'material-icons md-'+colors">keyboard_arrow_down</i>
139
        </a>
140
        <div *ngIf="loggedIn" uk-dropdown="mode: click; offset: -2; delay-hide: 0; flip: false; pos: bottom-right"
141
             #userMenu
142
             class="uk-padding-remove-horizontal">
143
          <ul class="uk-nav uk-dropdown-nav">
144
            <ng-container *ngFor="let item of userMenuItems ">
145
              <li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
146
                <a *ngIf="item.route.length > 0" routerLink="{{item.route}}"
147
                   [queryParams]=item.params>{{item.title}}</a>
148
                <a *ngIf="item.route.length == 0 && item.url.length > 0" href="{{item.url}}"
149
                   target="_blank">{{item.title}}</a>
150
              </li>
151
            </ng-container>
152
            <li *ngIf="userMenuItems.length > 0" class="uk-nav-divider"></li>
153
            <li><a (click)="logOut()" id="logout">Log out</a></li>
154
          </ul>
155
        </div>
156
      </ng-container>
157

    
158
      <div id="logout2"></div>
159
    `
160
})
161

    
162
export class UserMiniComponent implements OnInit, OnChanges {
163
    @Input() user: User;
164
    public loggedIn: boolean = false;
165
    public isAuthorized: boolean = false;
166
    @Input() public mobileView: boolean = false;
167
    public firstLetters: string = "";
168
    @Input() public dashboard: boolean = false;
169
    @Input() public colors: string = "light";  // 'dark' on light background - 'light' on dark background
170
    public server: boolean = true;
171
    public routerHelper: RouterHelper = new RouterHelper();
172
    @Input() userMenuItems;
173
    @Input() logInUrl;
174
    @Input() logOutUrl;
175
    @Input() cookieDomain;
176
    @Input() redirectUrl: string;
177
    sub: any;
178

    
179
    constructor(private router: Router, private  route: ActivatedRoute, private location: Location) {
180
    }
181

    
182
    ngOnInit() {
183

    
184
        if (typeof document !== 'undefined') {
185
            this.server = false;
186
        }
187
        this.initialize();
188
        this.sub = this.route.queryParams.subscribe(params => {
189
            this.initialize();
190
        });
191

    
192

    
193
    }
194

    
195
    ngOnChanges(changes: SimpleChanges): void {
196
        if (changes.user) {
197
            this.initialize();
198
        }
199
    }
200

    
201
    ngOnDestroy() {
202
        if (this.sub) {
203
            this.sub.unsubscribe();
204
        }
205
    }
206

    
207
    initialize() {
208
        if (!this.redirectUrl && typeof location !== 'undefined') {
209
            this.redirectUrl = location.pathname;
210
        }
211
        if (this.user) {
212
            this.loggedIn = true;
213
            this.parseName();
214
            this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
215

    
216
        } else {
217
            this.loggedIn = false;
218
            this.isAuthorized = false;
219
            this.user = null;
220
        }
221

    
222
    }
223

    
224
    logOut() {
225
        if (this.user) {
226
            Session.removeUser();
227
            window.location.href = this.logOutUrl + StringUtils.URIEncode(location.href);
228

    
229
        }
230
        this.loggedIn = false;
231
        this.isAuthorized = false;
232
        this.user = new User();
233
    }
234

    
235
    logIn() {
236
        Session.setReloadUrl(location.protocol + "//" + location.host, this.redirectUrl, location.search);
237
        window.location.href = this.logInUrl;
238
    }
239

    
240
    onClick(id: string) {
241
        let el: HTMLElement = document.getElementById(id);
242
        el.classList.remove('uk-open');
243
    }
244

    
245
    parseName() {
246
        this.firstLetters = "";
247
        if (this.user && this.user.firstname) {
248
            this.firstLetters += this.user.firstname.substr(0, 1);
249
        }
250
        if (this.user && this.user.lastname) {
251
            this.firstLetters += this.user.lastname.substr(0, 1);
252
        }
253
        if (!this.firstLetters && this.user && this.user.fullname) {
254
            let matches = this.user.fullname.match(/\b(\w)/g);
255
            this.firstLetters += matches.join('');
256
        }
257
    }
258
}
(9-9/10)