11import { Injectable } from '@angular/core' ;
2- import { ActivatedRouteSnapshot , Resolve , RouterStateSnapshot } from '@angular/router' ;
3- import { combineLatest as observableCombineLatest , combineLatest , Observable } from 'rxjs' ;
2+ import { ActivatedRoute , ActivatedRouteSnapshot , Resolve , RouterStateSnapshot } from '@angular/router' ;
3+ import { combineLatest as observableCombineLatest , Observable } from 'rxjs' ;
44import { MenuID } from './shared/menu/menu-id.model' ;
55import { MenuState } from './shared/menu/menu-state.model' ;
66import { MenuItemType } from './shared/menu/menu-item-type.model' ;
@@ -12,7 +12,7 @@ import { RemoteData } from './core/data/remote-data';
1212import { TextMenuItemModel } from './shared/menu/menu-item/models/text.model' ;
1313import { BrowseService } from './core/browse/browse.service' ;
1414import { MenuService } from './shared/menu/menu.service' ;
15- import { filter , find , map , take } from 'rxjs/operators' ;
15+ import { filter , find , map , switchMap , take } from 'rxjs/operators' ;
1616import { hasValue } from './shared/empty.util' ;
1717import { FeatureID } from './core/data/feature-authorization/feature-id' ;
1818import {
@@ -44,6 +44,7 @@ import {
4444 METADATA_IMPORT_SCRIPT_NAME ,
4545 ScriptDataService
4646} from './core/data/processes/script-data.service' ;
47+ import { environment } from '../environments/environment' ;
4748
4849/**
4950 * Creates all of the app's menus
@@ -52,7 +53,11 @@ import {
5253 providedIn : 'root'
5354} )
5455export class MenuResolver implements Resolve < boolean > {
56+
57+ private activatedRouteLastChild : ActivatedRoute ;
58+
5559 constructor (
60+ protected route : ActivatedRoute ,
5661 protected menuService : MenuService ,
5762 protected browseService : BrowseService ,
5863 protected authorizationService : AuthorizationDataService ,
@@ -65,7 +70,7 @@ export class MenuResolver implements Resolve<boolean> {
6570 * Initialize all menus
6671 */
6772 resolve ( route : ActivatedRouteSnapshot , state : RouterStateSnapshot ) : Observable < boolean > {
68- return combineLatest ( [
73+ return observableCombineLatest ( [
6974 this . createPublicMenu$ ( ) ,
7075 this . createAdminMenu$ ( ) ,
7176 ] ) . pipe (
@@ -89,20 +94,25 @@ export class MenuResolver implements Resolve<boolean> {
8994 * Initialize all menu sections and items for {@link MenuID.PUBLIC}
9095 */
9196 createPublicMenu$ ( ) : Observable < boolean > {
92- const menuList : any [ ] = [
93- /* Communities & Collections tree */
94- {
95- id : `browse_global_communities_and_collections` ,
96- active : false ,
97- visible : true ,
98- index : 0 ,
99- model : {
100- type : MenuItemType . LINK ,
101- text : `menu.section.browse_global_communities_and_collections` ,
102- link : `/community-list`
103- } as LinkMenuItemModel
104- }
105- ] ;
97+ const menuList : any [ ] = [ ] ;
98+
99+ /* Communities & Collections tree */
100+ const CommunityCollectionMenuItem = {
101+ id : `browse_global_communities_and_collections` ,
102+ active : false ,
103+ visible : environment . layout . navbar . showCommunityCollection ,
104+ index : 0 ,
105+ model : {
106+ type : MenuItemType . LINK ,
107+ text : `menu.section.communities_and_collections` ,
108+ link : `/community-list`
109+ } as LinkMenuItemModel
110+ } ;
111+
112+ if ( environment . layout . navbar . showCommunityCollection ) {
113+ menuList . push ( CommunityCollectionMenuItem ) ;
114+ }
115+
106116 // Read the different Browse-By types from config and add them to the browse menu
107117 this . browseService . getBrowseDefinitions ( )
108118 . pipe ( getFirstCompletedRemoteData < PaginatedList < BrowseDefinition > > ( ) )
@@ -140,9 +150,79 @@ export class MenuResolver implements Resolve<boolean> {
140150 } ) ) ) ;
141151 } ) ;
142152
153+ this . createStatisticsMenu ( ) ;
143154 return this . waitForMenu$ ( MenuID . PUBLIC ) ;
144155 }
145156
157+ createStatisticsMenu ( ) {
158+ this . activatedRouteLastChild = this . getActivatedRoute ( this . route ) ;
159+ observableCombineLatest ( [
160+ this . getAuthorizedUsageStatistics ( ) ,
161+ this . getAuthorizedLoginStatistics ( ) ,
162+ this . getAuthorizedWorkflowStatistics ( )
163+ ] ) . pipe ( take ( 1 ) ) . subscribe ( ( [ canViewUsage , canViewLogin , canViewWorkflow ] ) => {
164+ const menuList = [ ] ;
165+ if ( canViewUsage || canViewLogin || canViewWorkflow ) {
166+ menuList . push (
167+ {
168+ id : 'statistics' ,
169+ active : false ,
170+ visible : true ,
171+ index : 1 ,
172+ model : {
173+ type : MenuItemType . TEXT ,
174+ text : 'menu.section.statistics'
175+ } as TextMenuItemModel ,
176+ }
177+ ) ;
178+
179+ if ( canViewUsage ) {
180+ menuList . push ( {
181+ id : 'statistics_site' ,
182+ parentID : 'statistics' ,
183+ active : false ,
184+ visible : true ,
185+ model : {
186+ type : MenuItemType . LINK ,
187+ text : 'menu.section.statistics.site' ,
188+ link : '/statistics'
189+ } as LinkMenuItemModel
190+ } ) ;
191+ }
192+
193+ if ( canViewLogin ) {
194+ menuList . push ( {
195+ id : 'statistics_login' ,
196+ parentID : 'statistics' ,
197+ active : false ,
198+ visible : true ,
199+ model : {
200+ type : MenuItemType . LINK ,
201+ text : 'menu.section.statistics.login' ,
202+ link : '/statistics/login'
203+ } as LinkMenuItemModel
204+ } ) ;
205+ }
206+
207+ if ( canViewWorkflow ) {
208+ menuList . push ( {
209+ id : 'statistics_workflow' ,
210+ parentID : 'statistics' ,
211+ active : false ,
212+ visible : true ,
213+ model : {
214+ type : MenuItemType . LINK ,
215+ text : 'menu.section.statistics.workflow' ,
216+ link : '/statistics/workflow'
217+ } as LinkMenuItemModel
218+ } ) ;
219+ }
220+ }
221+ menuList . forEach ( ( menuSection ) => this . menuService . addSection ( MenuID . ADMIN , Object . assign ( menuSection , {
222+ shouldPersistOnRouteChange : true
223+ } ) ) ) ;
224+ } ) ;
225+ }
146226 /**
147227 * Initialize all menu sections and items for {@link MenuID.ADMIN}
148228 */
@@ -161,7 +241,7 @@ export class MenuResolver implements Resolve<boolean> {
161241 * edit_community / edit_collection is only included if the current user is a Community or Collection admin
162242 */
163243 createMainMenuSections ( ) {
164- combineLatest ( [
244+ observableCombineLatest ( [
165245 this . authorizationService . isAuthorized ( FeatureID . IsCollectionAdmin ) ,
166246 this . authorizationService . isAuthorized ( FeatureID . IsCommunityAdmin ) ,
167247 this . authorizationService . isAuthorized ( FeatureID . AdministratorOf )
@@ -652,4 +732,65 @@ export class MenuResolver implements Resolve<boolean> {
652732 } ) ) ) ;
653733 } ) ;
654734 }
735+
736+ /**
737+ * Get activated route of the deepest activated route
738+ */
739+ getActivatedRoute ( route ) {
740+ if ( route . children . length > 0 ) {
741+ return this . getActivatedRoute ( route . firstChild ) ;
742+ } else {
743+ return route ;
744+ }
745+ }
746+
747+ /**
748+ * Checking authorization for Usage
749+ */
750+ getAuthorizedUsageStatistics ( ) {
751+ return this . activatedRouteLastChild . data . pipe (
752+ switchMap ( ( data ) => {
753+ return this . authorizationService . isAuthorized ( FeatureID . CanViewUsageStatistics , this . getObjectUrl ( data ) ) . pipe (
754+ map ( ( canViewUsageStatistics : boolean ) => {
755+ return canViewUsageStatistics ;
756+ } ) ) ;
757+ } )
758+ ) ;
759+ }
760+
761+ /**
762+ * Checking authorization for Login
763+ */
764+ getAuthorizedLoginStatistics ( ) {
765+ return this . activatedRouteLastChild . data . pipe (
766+ switchMap ( ( data ) => {
767+ return this . authorizationService . isAuthorized ( FeatureID . CanViewLoginStatistics , this . getObjectUrl ( data ) ) . pipe (
768+ map ( ( canViewLoginStatistics : boolean ) => {
769+ return canViewLoginStatistics ;
770+ } ) ) ;
771+ } )
772+ ) ;
773+ }
774+
775+ /**
776+ * Checking authorization for Workflow
777+ */
778+ getAuthorizedWorkflowStatistics ( ) {
779+ return this . activatedRouteLastChild . data . pipe (
780+ switchMap ( ( data ) => {
781+ return this . authorizationService . isAuthorized ( FeatureID . CanViewWorkflowStatistics , this . getObjectUrl ( data ) ) . pipe (
782+ map ( ( canViewWorkflowStatistics : boolean ) => {
783+ return canViewWorkflowStatistics ;
784+ } ) ) ;
785+ } )
786+ ) ;
787+ }
788+
789+ /**
790+ * Get statistics route dso data
791+ */
792+ getObjectUrl ( data ) {
793+ const object = data . site ? data . site : data . dso ?. payload ;
794+ return object ?. _links ?. self ?. href ;
795+ }
655796}
0 commit comments