Project

General

Profile

1
/**
2
 * Created by stefania on 3/21/16.
3
 */
4

    
5
import {ChangeDetectorRef, Component, HostListener, OnInit} from '@angular/core';
6
import {MenuItem, RootMenuItem, SideMenuItem} from './openaireLibrary/sharedComponents/menu';
7
import {ActivatedRoute, NavigationEnd, NavigationStart, Params, Router} from '@angular/router';
8
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
9
import {CommunitiesService} from './openaireLibrary/connect/communities/communities.service';
10
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
11
import {Session, User} from './openaireLibrary/login/utils/helper.class';
12
import {HelperFunctions} from './openaireLibrary/utils/HelperFunctions.class';
13
import {UserManagementService} from './openaireLibrary/services/user-management.service';
14
import {ConnectHelper} from "./openaireLibrary/connect/connectHelper";
15
import {Header} from './openaireLibrary/sharedComponents/navigationBar.component';
16
import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
17
import {properties} from "../environments/environment";
18
import {BehaviorSubject, Subscriber} from "rxjs";
19
import {CommunityInfo} from "./openaireLibrary/connect/community/communityInfo";
20
import {CommunityService} from "./openaireLibrary/connect/community/community.service";
21
import {arrow_left} from "./openaireLibrary/utils/icons/icons";
22

    
23
declare var UIkit: any;
24

    
25
@Component({
26
  selector: 'app',
27
  templateUrl: './app.component.html',
28
})
29

    
30
export class AppComponent implements OnInit {
31
  title = 'Research Community Dashboard | Administrator';
32
  properties: EnvProperties = properties;
33
  user: User;
34
  params: BehaviorSubject<Params> = new BehaviorSubject<Params>(null);
35
  hasSidebar: boolean = false;
36
  hasHeader: boolean = false;
37
  hasAdminMenu: boolean = false;
38
  isFrontPage: boolean = false;
39
  isDashboard: boolean = false;
40
  sideBarItems: MenuItem[] = [];
41
  specialSideBarMenuItem: MenuItem = null;
42
  menuItems: RootMenuItem[] = [];
43
  menuHeader: Header = {
44
    route: "/",
45
    url: null,
46
    title: "Default menu header",
47
    logoUrl: null,
48
    logoSmallUrl: null,
49
    position: 'center',
50
    badge: false,
51
    stickyAnimation: false
52
  };
53
  userMenuItems: MenuItem[] = [];
54
  loading: boolean = true;
55
  paramsResolved: boolean = false;
56
  innerWidth;
57
  public community: CommunityInfo = null;
58
  private subscriptions: any[] = [];
59
  headerLogoUrl: string;
60
  
61
  constructor(private route: ActivatedRoute,
62
              private propertiesService: EnvironmentSpecificService,
63
              private communitiesService: CommunitiesService,
64
              private communityService: CommunityService,
65
              private router: Router,
66
              private cdr: ChangeDetectorRef,
67
              private layoutService: LayoutService,
68
              private userManagementService: UserManagementService) {
69
    this.subscriptions.push(this.router.events.subscribe(event => {
70
      if (event instanceof NavigationEnd) {
71
        let r = this.route;
72
        while (r.firstChild) {
73
          r = r.firstChild;
74
        }
75
        let params = r.snapshot.params;
76
        this.paramsResolved = true;
77
        this.params.next(params);
78
      }
79
    }));
80
  }
81
  
82
  ngOnInit() {
83
    if (typeof document !== 'undefined' && window) {
84
      this.innerWidth = window.innerWidth;
85
    }
86
    this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
87
      this.hasSidebar = hasSidebar;
88
      this.cdr.detectChanges();
89
    }));
90
    this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
91
      this.hasHeader = hasHeader;
92
      this.cdr.detectChanges();
93
    }));
94
    this.subscriptions.push(this.layoutService.hasAdminMenu.subscribe(hasAdminMenu => {
95
      this.hasAdminMenu = hasAdminMenu;
96
      this.cdr.detectChanges();
97
    }));
98
    this.subscriptions.push(this.layoutService.isFrontPage.subscribe(isFrontPage => {
99
      this.isFrontPage = isFrontPage;
100
      this.cdr.detectChanges();
101
    }));
102
    this.subscriptions.push(this.layoutService.isDashboard.subscribe(isDashboard => {
103
      this.isDashboard = isDashboard;
104
      this.cdr.detectChanges();
105
    }));
106
    this.layoutService.setSmallScreen((this.innerWidth && this.innerWidth < 1219));
107
    this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
108
    this.subscriptions.push(this.params.subscribe(params => {
109
      if (this.paramsResolved) {
110
        this.loading = true;
111
        if (params && params['community']) {
112
          if (!this.community || this.community.communityId !== params['community']) {
113
            this.subscriptions.push(this.communityService.getCommunityNew(params['community']).subscribe(community => {
114
              if (community) {
115
                this.community = community;
116
                this.buildMenu();
117
                this.loading = false;
118
              } else {
119
                this.community = null;
120
                this.navigateToError();
121
                this.buildMenu();
122
                this.loading = false;
123
              }
124
            }));
125
          } else {
126
            this.buildMenu();
127
            this.loading = false;
128
          }
129
        } else {
130
          this.communityService.setCommunity(null);
131
          this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
132
          this.community = null;
133
          this.buildMenu();
134
          this.loading = false;
135
        }
136
      }
137
    }));
138
    this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
139
      this.user = user;
140
      if (this.user) {
141
        localStorage.setItem('user_id', this.user.id);
142
        localStorage.setItem('mining_backend_address', this.properties.miningBackendURL);
143
        localStorage.setItem('isCommunityManager', Session.isCommunityCurator(this.user) + '');
144
        this.buildMenu();
145
      }
146
    }));
147
  }
148
  
149
  @HostListener('window:resize', ['$event'])
150
  onResize(event) {
151
    if (this.layoutService.isSmallScreen && event.target.innerWidth > 1219) {
152
      this.layoutService.setSmallScreen(false);
153
    } else if (!this.layoutService.isSmallScreen && event.target.innerWidth < 1219) {
154
      this.layoutService.setSmallScreen(true);
155
      this.layoutService.setOpen(false);
156
    }
157
  }
158
  
159
  public ngOnDestroy() {
160
    this.subscriptions.forEach(value => {
161
      if (value instanceof Subscriber) {
162
        value.unsubscribe();
163
      }
164
    });
165
    this.userManagementService.clearSubscriptions();
166
    this.layoutService.clearSubscriptions();
167
  }
168
  
169
  public get open() {
170
    return this.layoutService.open;
171
  }
172
  
173
  private navigateToError() {
174
    this.router.navigate(['/error'], {queryParams: {'page': this.properties.baseLink + this.router.url}});
175
  }
176
  
177
  private buildMenu() {
178
    this.menuItems = [];
179
    this.userMenuItems = [];
180
    this.sideBarItems = [];
181
    if (this.user) {
182
      if (this.isCurator()) {
183
        this.userMenuItems.push(new MenuItem("", "Manage communities",
184
          "", "/", false, [], [], {}));
185
      }
186
      if (Session.isPortalAdministrator(this.user)) {
187
        this.userMenuItems.push(new MenuItem("adminOptions", "Super Admin options", "", "/admin-tools/portals", false, [], [], {}));
188
        this.userMenuItems.push(new MenuItem("connectOptions", "Connect portal options", "", "/connect/admin-tools/pages", false, [], [], {}));
189
        this.userMenuItems.push(new MenuItem("exploreOptions", "Explore portal options", "", "/openaire/admin-tools/pages", false, [], [], {}));
190
  
191
      }
192
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
193
    }
194
    if (this.community) {
195
      this.headerLogoUrl = this.community.logoUrl;
196
      this.menuHeader = {
197
        route: "/admin/" + this.community.communityId,
198
        url: null,
199
        title: 'Admin - ' + this.community.shortTitle,
200
        logoUrl: null,
201
        logoSmallUrl: null,
202
        position: 'center',
203
        badge: false,
204
        stickyAnimation: false
205
      };
206
      this.sideBarItems.push(new MenuItem("community", "Community Info", "", "/" + this.community.communityId, false, [], [], {}, null, null, null, "/" + this.community.communityId + "/info"));
207
      this.sideBarItems.push(new MenuItem("users", "Users", "", "/" + this.community.communityId + "/users", false, [], [], {}));
208
      this.sideBarItems.push(new MenuItem("admin-tools", "Pages & Entities", "", "/" + this.community.communityId + "/admin-tools/pages", false, [], [], {}, null, null, null, "/" + this.community.communityId + "/admin-tools"));
209
      this.sideBarItems.push(new MenuItem("claims", "Claims", "", "/" + this.community.communityId + "/claims", false, [], [], {}));
210
      this.sideBarItems.push(new MenuItem("customize", "Customize", "", "/" + this.community.communityId + "/customize-layout", false, [], [], {}));
211

    
212
      if (this.community.type === 'ri') {
213
        this.sideBarItems.push(new MenuItem("mining", "Mining", "", "/" + this.community.communityId + "/mining/manage-profiles", false, [], [], {}, null, null, null, "/" + this.community.communityId + "/mining"));
214
      }
215
      this.specialSideBarMenuItem = new MenuItem("back", "Manage communities", "", "/", false, [], null, {});
216
      this.specialSideBarMenuItem.icon = '<span class="uk-icon-button small uk-icon uk-button-secondary">' + arrow_left.data + '</span>';
217
      this.specialSideBarMenuItem.customClass = 'uk-text-uppercase uk-text-bold uk-text-secondary';
218
    } else {
219
      this.headerLogoUrl = null;
220
      this.menuHeader = {
221
        route: null,
222
        url: null,
223
        title: 'Admin - Research Community Dashboard',
224
        logoUrl: null,
225
        logoSmallUrl: null,
226
        position: 'center',
227
        badge: false,
228
        stickyAnimation: false
229
      };
230
      this.sideBarItems.push(new MenuItem("communities", "Manage Communities", "", "/", false, [], [], {}));
231
      this.specialSideBarMenuItem = null;
232
    }
233
  }
234
  
235
  private isCurator() {
236
    return this.user && (Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user) || Session.isKindOfCommunityManager(this.user));
237
  }
238
}
(3-3/7)