Project

General

Profile

1
/**
2
 * Created by stefania on 7/5/16.
3
 */
4
import { Component, OnInit } from '@angular/core';
5
import { AuthenticationService } from '../../services/authentication.service';
6
import { environment } from '../../../environments/environment';
7
import {Repository, RepositorySnippet} from '../../domain/typeScriptClasses';
8
import { RepositoryService } from '../../services/repository.service';
9
import { ActivatedRoute, Router } from "@angular/router";
10
import { SharedService } from "../../services/shared.service";
11

    
12
@Component({
13
  selector: 'side-menu',
14
  templateUrl: './sidemenu.component.html',
15
})
16

    
17
export class SideMenuComponent implements OnInit {
18
  userLoggedIn = false;
19
  userName = '';
20
  isUserAdmin = false;
21
  adminHomePage = environment.FAQ_HOMEPAGE;
22

    
23
  inBeta: boolean;
24

    
25
  toggle: number[] = [];
26

    
27
  userEmail: string;
28
  reposOfUser: RepositorySnippet[] = [];
29
  visibleReposOfUser: RepositorySnippet[] = [];
30
  allReposVisible: boolean = false;
31

    
32
  constructor(public authService: AuthenticationService,
33
              private repositoryService: RepositoryService,
34
              private route: ActivatedRoute,
35
              private router: Router,
36
              private sharedService: SharedService) { }
37

    
38
  ngOnInit() {
39

    
40
    const pathName = window.location.pathname;
41
    if (pathName.includes('sources')) {
42
      this.toggle[1] = 1;
43
    } else if (pathName.includes('compatibility')) {
44
      this.toggle[2] = 2;
45
    } else if (pathName.includes('content')) {
46
      this.toggle[3] = 3;
47
    } else if (pathName.includes('admin')) {
48
      this.toggle[4] = 4;
49
    }
50

    
51
    this.getIsUserLoggedIn();
52
    this.getUserName();
53
    this.getIsUserAdmin();
54

    
55
    const baseUrl = window.location.origin;
56
    this.inBeta = ( baseUrl.includes('beta') || baseUrl.includes('athenarc') );
57

    
58
    if(this.sharedService.getRepositoriesOfUser() && this.sharedService.getRepositoriesOfUser().length>0) {
59
      this.reposOfUser = this.sharedService.getRepositoriesOfUser();
60
      this.initSideMenuRepos();
61
    } else {
62
      this.getReposOfUser();
63
    }
64

    
65
    this.sharedService.repositoriesOfUser$.subscribe(
66
      r => {
67
        this.reposOfUser = r;
68
        this.initSideMenuRepos();
69
      }
70
    );
71

    
72
    // this.getReposOfUser();
73
  }
74

    
75
  onClick(id: string) {
76
    const el: HTMLElement = document.getElementById(id);
77
    el.classList.remove('uk-open');
78
  }
79

    
80

    
81
  login() {
82
    this.authService.loginWithState();
83
  }
84

    
85
  logout() {
86
    this.authService.logout();
87
  }
88

    
89

    
90
  getUserName() {
91
    this.userName = this.authService.getUserName();
92
    return this.userName;
93
  }
94

    
95
  getIsUserLoggedIn() {
96
    this.userLoggedIn = this.authService.getIsUserLoggedIn();
97
    return this.userLoggedIn;
98
  }
99

    
100
  getIsUserAdmin() {
101
    this.isUserAdmin = (this.authService.getUserRole().includes('ROLE_ADMIN') ||
102
      this.authService.getUserRole().includes('ROLE_PROVIDE_ADMIN'));
103
    return this.isUserAdmin;
104
  }
105

    
106
  setToggle(position: number) {
107
    if (this.toggle[position] === position) {
108
      this.toggle[position] = 0;
109
    } else {
110
      this.toggle[position] = position;
111
    }
112
  }
113

    
114
  checkIfCollapsed(position: number) {
115
    return this.toggle[position] === position;
116
  }
117

    
118
  getReposOfUser(): void {
119
    this.repositoryService.getRepositoriesOfUser()
120
      .subscribe(
121
        repos => {
122
            this.reposOfUser = repos;
123
            // this.sharedService.setRepositoriesOfUser(repos);
124
            this.initSideMenuRepos();
125
          },
126
        error => { console.log(error); }
127
      );
128
  }
129

    
130
  initSideMenuRepos() {
131

    
132
    let index: number = 0;
133

    
134
    if(this.reposOfUser.length>5) {
135
      for(let _i = 0; _i < 5; _i++) {
136
        this.visibleReposOfUser.push(this.reposOfUser[_i]);
137
      }
138
    } else {
139
      this.visibleReposOfUser = Object.assign([], this.reposOfUser);
140
    }
141

    
142
    let route = this.router.url;
143
    let repositoryID = '';
144
    if(route.includes('repository') || route.includes('repositoryAdmin')) {
145
      let repositoryIndex = route.indexOf('repository');
146
      repositoryID = route.substr(repositoryIndex).split('/')[1];
147

    
148
      if(!route.includes('repositoryAdmin')) {
149
        index = this.reposOfUser.findIndex(x => x.id === repositoryID);
150
        if(index>5)
151
          this.showMoreRepos();
152
      }
153

    
154
      if(route.includes('repositoryAdmin')) {
155

    
156
      }
157
    }
158
  }
159

    
160
  showMoreRepos() {
161
    this.visibleReposOfUser = Object.assign([], this.reposOfUser);
162
    this.allReposVisible = true;
163
  }
164

    
165
  showLessRepos() {
166
    this.visibleReposOfUser = [];
167
    for(let _i = 0; _i < 5; _i++) {
168
      this.visibleReposOfUser.push(this.reposOfUser[_i]);
169
    }
170
    this.allReposVisible = false;
171
  }
172
}
(2-2/2)