Skip to content

Commit

Permalink
108588: Passed the scope to the browse sections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrevryghem committed Dec 13, 2023
1 parent 243179e commit f544bea
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 51 deletions.
17 changes: 14 additions & 3 deletions src/app/browse-by/abstract-browse-by-type.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Component, Input, OnDestroy } from '@angular/core';
import { Component, Input, OnDestroy, OnChanges, SimpleChanges } from '@angular/core';
import { BrowseByDataType } from './browse-by-switcher/browse-by-data-type';
import { Context } from '../core/shared/context.model';
import { Subscription } from 'rxjs';
import { BehaviorSubject, Subscription } from 'rxjs';
import { hasValue } from '../shared/empty.util';

@Component({
selector: 'ds-abstract-browse-by-type',
template: '',
})
export abstract class AbstractBrowseByTypeComponent implements OnDestroy {
export abstract class AbstractBrowseByTypeComponent implements OnChanges, OnDestroy {

/**
* The optional context
Expand All @@ -20,11 +20,22 @@ export abstract class AbstractBrowseByTypeComponent implements OnDestroy {
*/
@Input() browseByType: BrowseByDataType;

/**
* The ID of the {@link Community} or {@link Collection} of the scope to display
*/
@Input() scope: string;

scope$: BehaviorSubject<string> = new BehaviorSubject(undefined);

/**
* List of subscriptions
*/
subs: Subscription[] = [];

ngOnChanges(changes: SimpleChanges): void {
this.scope$.next(this.scope);
}

ngOnDestroy(): void {
this.subs.filter((sub: Subscription) => hasValue(sub)).forEach((sub: Subscription) => sub.unsubscribe());
}
Expand Down
10 changes: 5 additions & 5 deletions src/app/browse-by/browse-by-date/browse-by-date.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ export class BrowseByDateComponent extends BrowseByMetadataComponent implements
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
this.subs.push(
observableCombineLatest([this.route.params, this.route.queryParams, this.route.data,
observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.route.data,
this.currentPagination$, this.currentSort$]).pipe(
map(([routeParams, queryParams, data, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams, data), currentPage, currentSort];
map(([routeParams, queryParams, scope, data, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams, data), scope, currentPage, currentSort];
})
).subscribe(([params, currentPage, currentSort]: [Params, PaginationComponentOptions, SortOptions]) => {
).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
const metadataKeys = params.browseDefinition ? params.browseDefinition.metadataKeys : this.defaultMetadataKeys;
this.browseId = params.id || this.defaultBrowseId;
this.startsWith = +params.startsWith || params.startsWith;
const searchOptions = browseParamsToOptions(params, currentPage, currentSort, this.browseId, this.fetchThumbnails);
const searchOptions = browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, this.fetchThumbnails);
this.updatePageWithItems(searchOptions, this.value, undefined);
this.updateStartsWithOptions(this.browseId, metadataKeys, params.scope);
}));
Expand Down
39 changes: 12 additions & 27 deletions src/app/browse-by/browse-by-guard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, Data } from '@angular/router';
import { Injectable } from '@angular/core';
import { DSpaceObjectDataService } from '../core/data/dspace-object-data.service';
import { hasNoValue, hasValue } from '../shared/empty.util';
import { map, switchMap } from 'rxjs/operators';
import { getFirstCompletedRemoteData, getFirstSucceededRemoteDataPayload } from '../core/shared/operators';
import { getFirstCompletedRemoteData } from '../core/shared/operators';
import { TranslateService } from '@ngx-translate/core';
import { Observable, of as observableOf } from 'rxjs';
import { BrowseDefinitionDataService } from '../core/browse/browse-definition-data.service';
import { BrowseDefinition } from '../core/shared/browse-definition.model';
import { DSONameService } from '../core/breadcrumbs/dso-name.service';
import { DSpaceObject } from '../core/shared/dspace-object.model';
import { RemoteData } from '../core/data/remote-data';
import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths';

Expand All @@ -19,11 +16,10 @@ import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths';
*/
export class BrowseByGuard implements CanActivate {

constructor(protected dsoService: DSpaceObjectDataService,
protected translate: TranslateService,
protected browseDefinitionService: BrowseDefinitionDataService,
protected dsoNameService: DSONameService,
protected router: Router,
constructor(
protected translate: TranslateService,
protected browseDefinitionService: BrowseDefinitionDataService,
protected router: Router,
) {
}

Expand All @@ -39,25 +35,14 @@ export class BrowseByGuard implements CanActivate {
} else {
browseDefinition$ = observableOf(route.data.browseDefinition);
}
const scope = route.queryParams.scope;
const scope = route.queryParams.scope ?? route.parent.params.id;
const value = route.queryParams.value;
const metadataTranslated = this.translate.instant(`browse.metadata.${id}`);
return browseDefinition$.pipe(
switchMap((browseDefinition: BrowseDefinition | undefined) => {
if (hasValue(browseDefinition)) {
if (hasValue(scope)) {
const dso$: Observable<DSpaceObject> = this.dsoService.findById(scope).pipe(getFirstSucceededRemoteDataPayload());
return dso$.pipe(
map((dso: DSpaceObject) => {
const name = this.dsoNameService.getName(dso);
route.data = this.createData(title, id, browseDefinition, name, metadataTranslated, value, route);
return true;
})
);
} else {
route.data = this.createData(title, id, browseDefinition, '', metadataTranslated, value, route);
return observableOf(true);
}
route.data = this.createData(title, id, browseDefinition, metadataTranslated, value, route, scope);
return observableOf(true);
} else {
void this.router.navigate([PAGE_NOT_FOUND_PATH]);
return observableOf(false);
Expand All @@ -66,14 +51,14 @@ export class BrowseByGuard implements CanActivate {
);
}

private createData(title, id, browseDefinition, collection, field, value, route) {
private createData(title: string, id: string, browseDefinition: BrowseDefinition, field: string, value: string, route: ActivatedRouteSnapshot, scope: string): Data {
return Object.assign({}, route.data, {
title: title,
id: id,
browseDefinition: browseDefinition,
collection: collection,
field: field,
value: hasValue(value) ? `"${value}"` : ''
value: hasValue(value) ? `"${value}"` : '',
scope: scope,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ export class BrowseByMetadataComponent extends AbstractBrowseByTypeComponent imp
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
this.subs.push(
observableCombineLatest([this.route.params, this.route.queryParams, this.currentPagination$, this.currentSort$]).pipe(
map(([routeParams, queryParams, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams),currentPage,currentSort];
observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.currentPagination$, this.currentSort$]).pipe(
map(([routeParams, queryParams, scope, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams), scope, currentPage, currentSort];
})
).subscribe(([params, currentPage, currentSort]: [Params, PaginationComponentOptions, SortOptions]) => {
this.browseId = params.id || this.defaultBrowseId;
).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
this.browseId = params.id || this.defaultBrowseId;
this.authority = params.authority;

if (typeof params.value === 'string'){
Expand All @@ -150,10 +150,9 @@ export class BrowseByMetadataComponent extends AbstractBrowseByTypeComponent imp
}

if (isNotEmpty(this.value)) {
this.updatePageWithItems(
browseParamsToOptions(params, currentPage, currentSort, this.browseId, this.fetchThumbnails), this.value, this.authority);
this.updatePageWithItems(browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, this.fetchThumbnails), this.value, this.authority);
} else {
this.updatePage(browseParamsToOptions(params, currentPage, currentSort, this.browseId, false));
this.updatePage(browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, false));
}
}));
this.updateStartsWithTextOptions();
Expand Down Expand Up @@ -254,12 +253,14 @@ export function getBrowseSearchOptions(defaultBrowseId: string,
/**
* Function to transform query and url parameters into searchOptions used to fetch browse entries or items
* @param params URL and query parameters
* @param scope The scope to show the results
* @param paginationConfig Pagination configuration
* @param sortConfig Sorting configuration
* @param metadata Optional metadata definition to fetch browse entries/items for
* @param fetchThumbnail Optional parameter for requesting thumbnail images
*/
export function browseParamsToOptions(params: any,
scope: string,
paginationConfig: PaginationComponentOptions,
sortConfig: SortOptions,
metadata?: string,
Expand All @@ -269,7 +270,7 @@ export function browseParamsToOptions(params: any,
paginationConfig,
sortConfig,
params.startsWith,
params.scope,
scope,
fetchThumbnail
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class BrowseBySwitcherComponent extends AbstractComponentLoaderComponent<

@Input() browseByType: BrowseByDataType;

@Input() scope: string;

protected inputNamesDependentForComponent: (keyof this & string)[] = [
...this.inputNamesDependentForComponent,
'browseByType',
Expand All @@ -21,6 +23,7 @@ export class BrowseBySwitcherComponent extends AbstractComponentLoaderComponent<
protected inputNames: (keyof this & string)[] = [
...this.inputNames,
'browseByType',
'scope',
];

public getComponent(): GenericConstructor<AbstractBrowseByTypeComponent> {
Expand Down
10 changes: 5 additions & 5 deletions src/app/browse-by/browse-by-title/browse-by-title.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export class BrowseByTitleComponent extends BrowseByMetadataComponent implements
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
this.subs.push(
observableCombineLatest([this.route.params, this.route.queryParams, this.currentPagination$, this.currentSort$]).pipe(
map(([routeParams, queryParams, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams), currentPage, currentSort];
observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.currentPagination$, this.currentSort$]).pipe(
map(([routeParams, queryParams, scope, currentPage, currentSort]) => {
return [Object.assign({}, routeParams, queryParams), scope, currentPage, currentSort];
})
).subscribe(([params, currentPage, currentSort]: [Params, PaginationComponentOptions, SortOptions]) => {
).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
this.startsWith = +params.startsWith || params.startsWith;
this.browseId = params.id || this.defaultBrowseId;
this.updatePageWithItems(browseParamsToOptions(params, currentPage, currentSort, this.browseId, this.fetchThumbnails), undefined, undefined);
this.updatePageWithItems(browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, this.fetchThumbnails), undefined, undefined);
}));
this.updateStartsWithTextOptions();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<ds-browse-by-switcher [browseByType]="browseByType$ | async">
<ds-browse-by-switcher [browseByType]="browseByType$ | async"
[scope]="scope$ | async">
</ds-browse-by-switcher>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { BrowseByDataType } from '../../../../browse-by/browse-by-switcher/browse-by-data-type';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Data } from '@angular/router';
import { map } from 'rxjs/operators';
import { BrowseDefinition } from '../../../../core/shared/browse-definition.model';

Expand All @@ -14,6 +14,8 @@ export class ComcolBrowseByComponent implements OnInit {

browseByType$: Observable<BrowseByDataType>;

scope$: Observable<string>;

constructor(
protected route: ActivatedRoute,
) {
Expand All @@ -26,6 +28,9 @@ export class ComcolBrowseByComponent implements OnInit {
this.browseByType$ = this.route.data.pipe(
map((data: { browseDefinition: BrowseDefinition }) => data.browseDefinition.getRenderType()),
);
this.scope$ = this.route.data.pipe(
map((data: Data) => data.scope),
);
}

}

0 comments on commit f544bea

Please sign in to comment.