Skip to content

Commit c39b7ff

Browse files
committed
Add statistics pages - add support for parameters in menu sections and add id params
1 parent e7e408d commit c39b7ff

8 files changed

Lines changed: 64 additions & 21 deletions

File tree

src/app/+collection-page/collection-page-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import { LinkMenuItemModel } from '../shared/menu/menu-item/models/link.model';
7575
data: {
7676
menu: {
7777
public: [{
78-
id: 'statistics_collection',
78+
id: 'statistics_collection_:id',
7979
active: true,
8080
visible: true,
8181
model: {

src/app/+community-page/community-page-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { LinkMenuItemModel } from '../shared/menu/menu-item/models/link.model';
5151
data: {
5252
menu: {
5353
public: [{
54-
id: 'statistics_community',
54+
id: 'statistics_community_:id',
5555
active: true,
5656
visible: true,
5757
model: {

src/app/+item-page/item-page-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { LinkMenuItemModel } from '../shared/menu/menu-item/models/link.model';
4848
data: {
4949
menu: {
5050
public: [{
51-
id: 'statistics_item',
51+
id: 'statistics_item_:id',
5252
active: true,
5353
visible: true,
5454
model: {

src/app/shared/menu/menu.effects.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MenuEffects } from './menu.effects';
1414
describe('MenuEffects', () => {
1515
let menuEffects: MenuEffects;
1616
let routeDataMenuSection: MenuSection;
17+
let routeDataMenuSectionResolved: MenuSection;
1718
let routeDataMenuChildSection: MenuSection;
1819
let toBeRemovedMenuSection: MenuSection;
1920
let alreadyPresentMenuSection: MenuSection;
@@ -23,13 +24,23 @@ describe('MenuEffects', () => {
2324

2425
function init() {
2526
routeDataMenuSection = {
26-
id: 'mockSection',
27+
id: 'mockSection_:idparam',
2728
active: false,
2829
visible: true,
2930
model: {
3031
type: MenuItemType.LINK,
3132
text: 'menu.section.mockSection',
32-
link: ''
33+
link: 'path/:linkparam'
34+
} as LinkMenuItemModel
35+
};
36+
routeDataMenuSectionResolved = {
37+
id: 'mockSection_id_param_resolved',
38+
active: false,
39+
visible: true,
40+
model: {
41+
type: MenuItemType.LINK,
42+
text: 'menu.section.mockSection',
43+
link: 'path/link_param_resolved'
3344
} as LinkMenuItemModel
3445
};
3546
routeDataMenuChildSection = {
@@ -70,6 +81,10 @@ describe('MenuEffects', () => {
7081
menu: {
7182
[MenuID.PUBLIC]: [routeDataMenuSection, alreadyPresentMenuSection]
7283
}
84+
},
85+
params: {
86+
idparam: 'id_param_resolved',
87+
linkparam: 'link_param_resolved',
7388
}
7489
},
7590
firstChild: {
@@ -120,7 +135,7 @@ describe('MenuEffects', () => {
120135
});
121136

122137
expect(menuEffects.buildRouteMenuSections$).toBeObservable(expected);
123-
expect(menuService.addSection).toHaveBeenCalledWith(MenuID.PUBLIC, routeDataMenuSection);
138+
expect(menuService.addSection).toHaveBeenCalledWith(MenuID.PUBLIC, routeDataMenuSectionResolved);
124139
expect(menuService.addSection).toHaveBeenCalledWith(MenuID.PUBLIC, routeDataMenuChildSection);
125140
expect(menuService.addSection).not.toHaveBeenCalledWith(MenuID.PUBLIC, alreadyPresentMenuSection);
126141
expect(menuService.removeSection).toHaveBeenCalledWith(MenuID.PUBLIC, toBeRemovedMenuSection.id);

src/app/shared/menu/menu.effects.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class MenuEffects {
1919
/**
2020
* On route change, build menu sections for every menu type depending on the current route data
2121
*/
22-
@Effect({ dispatch: false })
22+
@Effect({dispatch: false})
2323
public buildRouteMenuSections$: Observable<Action> = this.actions$
2424
.pipe(
2525
ofType(ROUTER_NAVIGATED),
@@ -73,20 +73,8 @@ export class MenuEffects {
7373

7474
if (hasValue(data) && hasValue(data.menu) && hasValue(data.menu[menuID])) {
7575

76-
const menuSections = data.menu[menuID];
77-
[...menuSections]
78-
.forEach((menuSection) => {
79-
80-
if (hasValue(menuSection.model) && hasValue(menuSection.model.link)) {
81-
let substitute: RegExpMatchArray;
82-
do {
83-
substitute = menuSection.model.link.match(/\/:(.*?)\//);
84-
if (substitute) {
85-
menuSection.model.link = menuSection.model.link.replace(substitute[0], `/${params[substitute[1]]}/`);
86-
}
87-
} while (substitute);
88-
}
89-
});
76+
let menuSections: MenuSection[] | MenuSection = data.menu[menuID];
77+
menuSections = this.resolveSubstitutions(menuSections, params);
9078

9179
if (!last) {
9280
return [...menuSections, ...this.resolveRouteMenuSections(route.firstChild, menuID)]
@@ -98,4 +86,30 @@ export class MenuEffects {
9886
return !last ? this.resolveRouteMenuSections(route.firstChild, menuID) : [];
9987
}
10088

89+
private resolveSubstitutions(object, params) {
90+
91+
if (typeof object === 'string') {
92+
let match: RegExpMatchArray;
93+
do {
94+
match = object.match(/:(\w+)/);
95+
if (match) {
96+
const substitute = params[match[1]];
97+
if (hasValue(substitute)) {
98+
object = object.replace(match[0], `${substitute}`);
99+
}
100+
}
101+
} while (match);
102+
} else if (Array.isArray(object)) {
103+
object.forEach((entry, index) => {
104+
object[index] = this.resolveSubstitutions(object[index], params);
105+
});
106+
} else {
107+
Object.keys(object).forEach((key) => {
108+
object = Object.assign({}, object, {
109+
[key]: this.resolveSubstitutions(object[key], params)
110+
});
111+
});
112+
}
113+
return object;
114+
}
101115
}

src/app/statistics-page/statistics-page/statistics-page.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
[report]="report"
1313
class="m-2 {{ report.id }}">
1414
</ds-statistics-table>
15+
<ng-container *ngIf="!!hasData$">
16+
<div *ngIf="!(hasData$ | async)">
17+
{{ 'statistics.page.no-data' | translate }}
18+
</div>
19+
</ng-container>
1520

1621
</div>

src/app/statistics-page/statistics-page/statistics-page.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export abstract class StatisticsPageComponent<T extends DSpaceObject> implements
2929
*/
3030
reports$: Observable<UsageReport[]>;
3131

32+
hasData$: Observable<boolean>;
33+
3234
constructor(
3335
protected route: ActivatedRoute,
3436
protected router: Router,
@@ -40,6 +42,11 @@ export abstract class StatisticsPageComponent<T extends DSpaceObject> implements
4042
ngOnInit(): void {
4143
this.scope$ = this.getScope$();
4244
this.reports$ = this.getReports$();
45+
this.hasData$ = this.reports$.pipe(
46+
map((reports) => reports.some(
47+
(report) => report.points.length > 0
48+
)),
49+
);
4350
}
4451

4552
/**

src/assets/i18n/en.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,8 @@
27832783

27842784
"statistics.breadcrumbs": "Statistics",
27852785

2786+
"statistics.page.no-data": "No data available",
2787+
27862788
"statistics.table.no-data": "No data available",
27872789

27882790
"statistics.table.title.TotalVisits": "Total visits",

0 commit comments

Comments
 (0)