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',
18
  templateUrl: './app.component.html',
19
})
20

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