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
  visibleAdminRepo: boolean = false;
33
  adminRepository: Repository;
34

    
35
  constructor(public authService: AuthenticationService,
36
              private repositoryService: RepositoryService,
37
              private route: ActivatedRoute,
38
              private router: Router,
39
              private sharedService: SharedService) { }
40

    
41
  ngOnInit() {
42

    
43
    // this.router.events.subscribe((val) => {
44
    //
45
    //   console.log("*******************  Router changed to: " + this.router.url);
46
    //
47
    //   let route = this.router.url;
48
    //   let repositoryID = '';
49
    //   let index: number = 0;
50
    //
51
    //   if(route.includes('repository') || route.includes('repositoryAdmin')) {
52
    //     let repositoryIndex = route.indexOf('repository');
53
    //     repositoryID = route.substr(repositoryIndex).split('/')[1];
54
    //
55
    //     if(!route.includes('repositoryAdmin')) {
56
    //       this.visibleAdminRepo = false;
57
    //       index = this.reposOfUser.findIndex(x => x.id === repositoryID);
58
    //       if(index>5)
59
    //         this.showMoreRepos();
60
    //     }
61
    //
62
    //     if(route.includes('repositoryAdmin')) {
63
    //       //fixme make it work with the subject below
64
    //       this.repositoryService.getRepositoryById(repositoryID).subscribe(
65
    //         r => {
66
    //           this.adminRepository = r;
67
    //           this.visibleAdminRepo = true;
68
    //         },error => { console.log(error); }
69
    //       );
70
    //     }
71
    //   } else {
72
    //     this.visibleAdminRepo = false;
73
    //   }
74
    //
75
    //   console.log("******************  this.visibleAdminRepo: " + this.visibleAdminRepo);
76
    // });
77

    
78
    const pathName = window.location.pathname;
79
    if (pathName.includes('sources')) {
80
      this.toggle[1] = 1;
81
    } else if (pathName.includes('compatibility')) {
82
      this.toggle[2] = 2;
83
    } else if (pathName.includes('content')) {
84
      this.toggle[3] = 3;
85
    } else if (pathName.includes('admin')) {
86
      this.toggle[4] = 4;
87
    }
88

    
89
    this.getIsUserLoggedIn();
90
    this.getUserName();
91
    this.getIsUserAdmin();
92

    
93
    const baseUrl = window.location.origin;
94
    this.inBeta = ( baseUrl.includes('beta') || baseUrl.includes('athenarc') );
95

    
96
    if(this.sharedService.getRepositoriesOfUser() && this.sharedService.getRepositoriesOfUser().length>0) {
97
      this.reposOfUser = this.sharedService.getRepositoriesOfUser();
98
      this.initSideMenuRepos();
99
    } else {
100
      this.getReposOfUser();
101
    }
102

    
103
    this.sharedService.repositoriesOfUser$.subscribe(
104
      r => {
105
        this.reposOfUser = r;
106
        this.initSideMenuRepos();
107
      }
108
    );
109

    
110
    // this.getReposOfUser();
111
  }
112

    
113
  onClick(id: string) {
114
    const el: HTMLElement = document.getElementById(id);
115
    el.classList.remove('uk-open');
116
  }
117

    
118

    
119
  login() {
120
    this.authService.loginWithState();
121
  }
122

    
123
  logout() {
124
    this.authService.logout();
125
  }
126

    
127

    
128
  getUserName() {
129
    this.userName = this.authService.getUserName();
130
    return this.userName;
131
  }
132

    
133
  getIsUserLoggedIn() {
134
    this.userLoggedIn = this.authService.getIsUserLoggedIn();
135
    return this.userLoggedIn;
136
  }
137

    
138
  getIsUserAdmin() {
139
    this.isUserAdmin = (this.authService.getUserRole().includes('ROLE_ADMIN') ||
140
      this.authService.getUserRole().includes('ROLE_PROVIDE_ADMIN'));
141
    return this.isUserAdmin;
142
  }
143

    
144
  setToggle(position: number) {
145
    if (this.toggle[position] === position) {
146
      this.toggle[position] = 0;
147
    } else {
148
      this.toggle[position] = position;
149
    }
150
  }
151

    
152
  checkIfCollapsed(position: number) {
153
    return this.toggle[position] === position;
154
  }
155

    
156
  getReposOfUser(): void {
157
    this.repositoryService.getRepositoriesSnippetsOfUser()
158
      .subscribe(
159
        repos => {
160
            this.reposOfUser = repos;
161
            // this.sharedService.setRepositoriesOfUser(repos);
162
            this.initSideMenuRepos();
163
          },
164
        error => { console.log(error); }
165
      );
166
  }
167

    
168
  initSideMenuRepos() {
169

    
170
    if(this.reposOfUser.length>5) {
171
      for(let _i = 0; _i < 5; _i++) {
172
        this.visibleReposOfUser.push(this.reposOfUser[_i]);
173
      }
174
    } else {
175
      this.visibleReposOfUser = Object.assign([], this.reposOfUser);
176
    }
177

    
178
    let route = this.router.url;
179
    let repositoryID = '';
180
    let index: number = 0;
181
    if(route.includes('repository')) {
182

    
183
      console.log('************* route: '+ route);
184
      let repositoryIndex = route.indexOf('repository');
185
      repositoryID = route.substr(repositoryIndex).split('/')[1];
186

    
187
      console.log('************* repositoryID: '+ repositoryID);
188

    
189
      if(!route.includes('repositoryAdmin')) {
190
        this.visibleAdminRepo = false;
191
        index = this.reposOfUser.findIndex(x => x.id === repositoryID);
192
        console.log('************* index: '+ index);
193
        if (index > 5)
194
          this.showMoreRepos();
195
      }
196
    }
197

    
198
    // let repository: Repository;
199
    // this.sharedService.repository$.subscribe(
200
    //   r => {
201
    //     repository = r;
202
    //     console.log("************ initSideMenu got repo from subject *****************")
203
    //     // console.log("RepositoryID: ", this.repository.id);
204
    //   }
205
    // );
206
    //
207
    // if(this.sharedService.getRepository()) {
208
    //   repository = this.sharedService.getRepository();
209
    //   console.log("************ initSideMenu got repo from copy *****************")
210
    // }
211

    
212

    
213

    
214
  }
215

    
216
  showMoreRepos() {
217
    console.log('************* show more repos');
218
    this.visibleReposOfUser = Object.assign([], this.reposOfUser);
219
    this.allReposVisible = true;
220
  }
221

    
222
  showLessRepos() {
223
    this.visibleReposOfUser = [];
224
    for(let _i = 0; _i < 5; _i++) {
225
      this.visibleReposOfUser.push(this.reposOfUser[_i]);
226
    }
227
    this.allReposVisible = false;
228
  }
229
}
(2-2/2)