Project

General

Profile

1
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
2
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
3
import {User, Session} from './utils/helper.class';
4
import {RouterHelper} from '../utils/routerHelper.class';
5
import {StringUtils} from '../utils/string-utils.class';
6
import {properties} from "../../../environments/environment";
7
import {Subscriber} from "rxjs";
8
import {MenuItem} from "../sharedComponents/menu";
9

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

    
16
      <ul *ngIf="!mobileView" class="uk-navbar-nav" [class.uk-dark]="dark">
17
        <li class="uk-parent">
18
          <a *ngIf="loggedIn" class="login">
19
            <svg height="60" width="60">
20
              <circle cx="30" cy="30" r="20" stroke-width="2"></circle>
21
              <text *ngIf="firstLetters" x="50%" y="50%" text-anchor="middle" dy=".4em"
22
                    font-size="16">
23
                {{firstLetters}}
24
              </text>
25
              <svg *ngIf="!firstLetters" ratio="1" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" width="20"
26
                   x="20">
27
                <circle cx="9.9" cy="6.4" r="4.4" stroke-width="1.1"></circle>
28
                <path 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>
29
              </svg>
30
            </svg>
31
          </a>
32
          <a *ngIf="!loggedIn" class="loginLink" id="userMenu-parent" (click)="logIn()">Sign in <span
33
              class=" uk-margin-small-left uk-margin-small-right uk-icon"><svg width="20" height="20"
34
                                                                               viewBox="0 0 20 20"
35
                                                                               xmlns="http://www.w3.org/2000/svg"
36
                                                                               ratio="1"><circle fill="none"
37
                                                                                                 stroke="#000"
38
                                                                                                 stroke-width="1.1"
39
                                                                                                 cx="9.9" cy="6.4"
40
                                                                                                 r="4.4"></circle><path
41
              fill="none" stroke="#000" stroke-width="1.1"
42
              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>
43
          </a>
44

    
45

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

    
72
    <ng-container *ngIf="!dashboard">
73

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

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

    
161
    <div id="logout2"></div>
162
  `
163
})
164

    
165
export class UserMiniComponent implements OnInit, OnChanges {
166
  @Input() user: User;
167
  public loggedIn: boolean = false;
168
  public isAuthorized: boolean = false;
169
  @Input() public mobileView: boolean = false;
170
  public firstLetters: string = "";
171
  @Input() public dashboard: boolean = false;
172
  public server: boolean = true;
173
  public routerHelper: RouterHelper = new RouterHelper();
174
  @Input() userMenuItems;
175
  @Input() logInUrl;
176
  @Input() logOutUrl;
177
  @Input() cookieDomain;
178
  @Input() fixRedirectUrl: string;
179
  @Input() redirectUrl: string;
180
  @Input() dark: boolean = false;
181
  search: string = '';
182
  subscriptions = [];
183
  
184
  constructor(private router: Router, private  route: ActivatedRoute) {
185
    this.subscriptions.push(this.router.events.forEach(event => {
186
      if (event instanceof NavigationStart) {
187
        this.initialize(event.url);
188
      }
189
    }));
190
  }
191
  
192
  ngOnInit() {
193
    if (typeof document !== 'undefined') {
194
      this.server = false;
195
    }
196
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
197
      this.initialize();
198
    }));
199
  }
200
  
201
  ngOnChanges(changes: SimpleChanges): void {
202
    if (changes.user) {
203
      this.initialize();
204
    }
205
  }
206

    
207
  ngOnDestroy() {
208
    this.subscriptions.forEach(subscription => {
209
      if (subscription instanceof Subscriber) {
210
        subscription.unsubscribe();
211
      }
212
    });
213
  }
214
  
215
  initialize(url: string = null) {
216
    if (url) {
217
      let parts = StringUtils.split(url, ['?']);
218
      this.redirectUrl = properties.baseLink + parts[0];
219
      if (parts.length == 2) {
220
        this.search = parts[1];
221
      } else {
222
        this.search = null;
223
      }
224
    } else if (typeof location !== 'undefined') {
225
      this.redirectUrl = location.pathname;
226
      this.search = location.search;
227
    }
228
    if (this.user) {
229
      this.loggedIn = true;
230
      this.parseName();
231
      this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
232
      
233
    } else {
234
      this.loggedIn = false;
235
      this.isAuthorized = false;
236
      this.user = null;
237
    }
238
    
239
  }
240
  
241
  logOut() {
242
    if (this.user) {
243
      Session.removeUser();
244
      // New method
245
      if (properties.logoutUrl.includes('openid_logout')) {
246
        Session.setReloadUrl(location.protocol + "//" + location.host, this.redirectUrl, this.search);
247
        window.location.href = this.logOutUrl;
248
      } else {
249
        window.location.href = this.logOutUrl + StringUtils.URIEncode(location.href);
250
      }
251
    }
252
    this.loggedIn = false;
253
    this.isAuthorized = false;
254
    this.user = new User();
255
  }
256
  
257
  logIn() {
258
    Session.setReloadUrl(location.protocol + "//" + location.host,
259
      this.fixRedirectUrl ? (properties.baseLink + this.fixRedirectUrl) : this.redirectUrl, this.search);
260
    window.location.href = this.logInUrl;
261
  }
262

    
263
  isTheActiveSubMenu(menuItem: MenuItem): boolean {
264
    let currentRoute = this.router.url.split('?')[0];
265
    if (menuItem.route == currentRoute || menuItem.route == (currentRoute + "/") ) {
266
      return true;
267
    }
268
    return false;
269
  }
270
  parseName() {
271
    this.firstLetters = "";
272
    if (this.user && this.user.firstname) {
273
      this.firstLetters += this.user.firstname.substr(0, 1);
274
    }
275
    if (this.user && this.user.lastname) {
276
      this.firstLetters += this.user.lastname.substr(0, 1);
277
    }
278
    if (!this.firstLetters && this.user && this.user.fullname) {
279
      let matches = this.user.fullname.match(/\b(\w)/g);
280
      this.firstLetters += matches.join('');
281
    }
282
  }
283
}
(9-9/11)