Project

General

Profile

1
import {ChangeDetectorRef, Component, HostListener, OnInit} from '@angular/core';
2
import {MenuItem, RootMenuItem} from './openaireLibrary/sharedComponents/menu';
3
import {ActivatedRoute, NavigationEnd, Params, Router} from '@angular/router';
4
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
5
import {Session, User} from './openaireLibrary/login/utils/helper.class';
6
import {UserManagementService} from './openaireLibrary/services/user-management.service';
7
import {Header} from './openaireLibrary/sharedComponents/navigationBar.component';
8
import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
9
import {properties} from "../environments/environment";
10
import {BehaviorSubject, Subscriber} from "rxjs";
11
import {CommunityInfo} from "./openaireLibrary/connect/community/communityInfo";
12
import {CommunityService} from "./openaireLibrary/connect/community/community.service";
13
import {arrow_left} from "./openaireLibrary/utils/icons/icons";
14
import {SmoothScroll} from "./openaireLibrary/utils/smooth-scroll";
15

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