Project

General

Profile

1
import {Component, ElementRef} 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

    
9
@Component({
10
    selector: 'user-mini',
11
    template: `
12

    
13
        <div *ngIf="!server" class=" custom-user-mini-panel uk-margin-top uk-margin-right uk-float-right">
14
        <span  *ngIf="loggedIn" >Hello <a  (click)="gotoUserPage();"   >{{user.fullname}}</a>!</span>
15
        <span  *ngIf="!loggedIn" >Hello  Guest!</span>
16
        <a  *ngIf="!loggedIn"   (click)="gotoUserPage();"   >Log in</a>
17
        </div>
18

    
19
    `
20
})
21

    
22
export class UserMiniComponent {
23
    public user: User;
24
    public loggedIn: boolean = false;
25
    public server: boolean = true;
26
    public routerHelper:RouterHelper = new RouterHelper();
27

    
28
    public redirectUrl: string = "";
29
    private baseUrl = "user-info";
30
    sub:any;
31
    constructor( private router: Router, private  route: ActivatedRoute, private location: Location) {}
32

    
33
    ngOnInit() {
34
      if( typeof document !== 'undefined') {
35
        this.server = false;
36
      }
37
      this.initialize();
38
      this.sub =  this.route.queryParams.subscribe(params => {
39
        this.initialize();
40
      });
41
    }
42
    ngOnDestroy(){
43
      this.sub.unsubscribe();
44
    }
45
    initialize(){
46
      this.redirectUrl = this.location.path();
47
      if(Session.isLoggedIn()){
48
        if(Session.isUserValid()){
49
          this.loggedIn = Session.isLoggedIn();
50
          this.user = Session.getUser();
51
        }else{
52
          Session.removeUser();
53
            this.loggedIn = false;
54
            this.user = null;
55
        }
56
      }else {
57
            this.loggedIn = false;
58
            this.user = null;
59
      }
60

    
61
    }
62
    gotoUserPage(){
63
      this.redirectUrl = this.location.path();
64
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != "" && this.redirectUrl !="user-info"){
65
          this.router.navigate([this.baseUrl], { queryParams: this.routerHelper.createQueryParam("redirectUrl",this.redirectUrl )});
66
      }else{
67
        this.router.navigate([this.baseUrl]);
68
      }
69
    }
70

    
71
}
(8-8/8)