mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-14 05:23:06 +00:00
#4172 Several fixes to RSS component and object lists
* RSS button displayed on search result pages * RSS button displayed on Recent Items component * RSS button NOT displayed in other components like top-level community, etc. * RSS component tracks sort option changes
This commit is contained in:
@@ -2,12 +2,16 @@ import { AsyncPipe } from '@angular/common';
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
Input,
|
||||
OnChanges,
|
||||
OnDestroy,
|
||||
OnInit,
|
||||
SimpleChanges,
|
||||
ViewEncapsulation,
|
||||
} from '@angular/core';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
NavigationEnd,
|
||||
Router,
|
||||
} from '@angular/router';
|
||||
import {
|
||||
@@ -16,12 +20,10 @@ import {
|
||||
} from '@ngx-translate/core';
|
||||
import {
|
||||
BehaviorSubject,
|
||||
filter,
|
||||
Subscription,
|
||||
} from 'rxjs';
|
||||
import {
|
||||
map,
|
||||
switchMap,
|
||||
} from 'rxjs/operators';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { SortOptions } from '../../core/cache/models/sort-options.model';
|
||||
@@ -31,13 +33,14 @@ import { GroupDataService } from '../../core/eperson/group-data.service';
|
||||
import { PaginationService } from '../../core/pagination/pagination.service';
|
||||
import { LinkHeadService } from '../../core/services/link-head.service';
|
||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||
import { SearchService } from '../../core/shared/search/search.service';
|
||||
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
|
||||
import {
|
||||
hasValue,
|
||||
isUndefined,
|
||||
} from '../empty.util';
|
||||
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';
|
||||
import { SearchFilter } from '../search/models/search-filter.model';
|
||||
|
||||
/**
|
||||
* The Rss feed button component.
|
||||
*/
|
||||
@@ -51,30 +54,28 @@ import { SearchFilter } from '../search/models/search-filter.model';
|
||||
standalone: true,
|
||||
imports: [AsyncPipe, TranslateModule],
|
||||
})
|
||||
export class RSSComponent implements OnInit, OnDestroy {
|
||||
export class RSSComponent implements OnInit, OnDestroy, OnChanges {
|
||||
|
||||
route$: BehaviorSubject<string> = new BehaviorSubject<string>('');
|
||||
|
||||
isEnabled$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
|
||||
|
||||
isActivated$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
@Input() sortConfig?: SortOptions;
|
||||
|
||||
uuid: string;
|
||||
|
||||
subs: Subscription[] = [];
|
||||
openSearchUri: string;
|
||||
|
||||
constructor(private groupDataService: GroupDataService,
|
||||
private linkHeadService: LinkHeadService,
|
||||
private configurationService: ConfigurationDataService,
|
||||
private searchConfigurationService: SearchConfigurationService,
|
||||
private searchService: SearchService,
|
||||
private router: Router,
|
||||
private route: ActivatedRoute,
|
||||
protected paginationService: PaginationService,
|
||||
protected translateService: TranslateService) {
|
||||
}
|
||||
/**
|
||||
* Removes the linktag created when the component gets removed from the page.
|
||||
*/
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.linkHeadService.removeTag("rel='alternate'");
|
||||
this.subs.forEach(sub => {
|
||||
@@ -82,24 +83,52 @@ export class RSSComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates the link tags and the url to opensearch when the component is loaded.
|
||||
*/
|
||||
ngOnInit(): void {
|
||||
// Set initial activation state
|
||||
if (hasValue(this.route.snapshot.data?.enableRSS)) {
|
||||
this.isActivated$.next(this.route.snapshot.data.enableRSS);
|
||||
} else if (isUndefined(this.route.snapshot.data?.enableRSS)) {
|
||||
this.isActivated$.next(false);
|
||||
}
|
||||
|
||||
// Get initial UUID from URL
|
||||
this.uuid = this.groupDataService.getUUIDFromString(this.router.url);
|
||||
|
||||
// Check if RSS is enabled
|
||||
this.subs.push(this.configurationService.findByPropertyName('websvc.opensearch.enable').pipe(
|
||||
getFirstCompletedRemoteData(),
|
||||
).subscribe((result) => {
|
||||
if (result.hasSucceeded) {
|
||||
const enabled = (result.payload.values[0] === 'true');
|
||||
this.isEnabled$.next(enabled);
|
||||
|
||||
// If enabled, get the OpenSearch URI
|
||||
if (enabled) {
|
||||
this.getOpenSearchUri();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Listen for navigation events to update the UUID
|
||||
this.subs.push(this.router.events.pipe(
|
||||
filter(event => event instanceof NavigationEnd),
|
||||
).subscribe(() => {
|
||||
this.uuid = this.groupDataService.getUUIDFromString(this.router.url);
|
||||
this.updateRssLinks();
|
||||
}));
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
// If sortConfig changes, update the RSS links
|
||||
if (changes.sortConfig && this.openSearchUri && this.isEnabled$.getValue()) {
|
||||
this.updateRssLinks();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the OpenSearch URI and update RSS links
|
||||
*/
|
||||
private getOpenSearchUri(): void {
|
||||
this.subs.push(this.configurationService.findByPropertyName('websvc.opensearch.svccontext').pipe(
|
||||
getFirstCompletedRemoteData(),
|
||||
map((result: RemoteData<any>) => {
|
||||
@@ -108,35 +137,60 @@ export class RSSComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
switchMap((openSearchUri: string) =>
|
||||
this.searchConfigurationService.paginatedSearchOptions.pipe(
|
||||
map((searchOptions: PaginatedSearchOptions) => ({ openSearchUri, searchOptions })),
|
||||
),
|
||||
),
|
||||
).subscribe(({ openSearchUri, searchOptions }) => {
|
||||
if (!openSearchUri) {
|
||||
return null;
|
||||
}
|
||||
this.uuid = this.groupDataService.getUUIDFromString(this.router.url);
|
||||
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, openSearchUri, searchOptions.sort, searchOptions.query, searchOptions.filters, searchOptions.configuration, searchOptions.pagination?.pageSize, searchOptions.fixedFilter);
|
||||
this.addLinks(route);
|
||||
this.linkHeadService.addTag({
|
||||
href: environment.rest.baseUrl + '/' + openSearchUri + '/service',
|
||||
type: 'application/atom+xml',
|
||||
rel: 'search',
|
||||
title: 'Dspace',
|
||||
});
|
||||
this.route$.next(route);
|
||||
filter(uri => !!uri),
|
||||
).subscribe(uri => {
|
||||
this.openSearchUri = uri;
|
||||
this.updateRssLinks();
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Function created a route given the different params available to opensearch
|
||||
* @param uuid The uuid if a scope is present
|
||||
* @param opensearch openSearch uri
|
||||
* @param sort The sort options for the opensearch request
|
||||
* @param query The query string that was provided in the search
|
||||
* @returns The combine URL to opensearch
|
||||
* Update RSS links based on current search configuration and sortConfig input
|
||||
*/
|
||||
private updateRssLinks(): void {
|
||||
if (!this.openSearchUri || !this.isEnabled$.getValue()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove existing link tags before adding new ones
|
||||
this.linkHeadService.removeTag("rel='alternate'");
|
||||
|
||||
// Get the current search options and apply our sortConfig if provided
|
||||
const searchOptions = this.searchConfigurationService.paginatedSearchOptions.value;
|
||||
const modifiedOptions = { ...searchOptions };
|
||||
if (hasValue(this.sortConfig)) {
|
||||
modifiedOptions.sort = this.sortConfig;
|
||||
}
|
||||
|
||||
// Create the RSS feed URL
|
||||
const route = environment.rest.baseUrl + this.formulateRoute(
|
||||
this.uuid,
|
||||
this.openSearchUri,
|
||||
modifiedOptions.sort,
|
||||
modifiedOptions.query,
|
||||
modifiedOptions.filters,
|
||||
modifiedOptions.configuration,
|
||||
modifiedOptions.pagination?.pageSize,
|
||||
modifiedOptions.fixedFilter,
|
||||
);
|
||||
|
||||
// Add the link tags
|
||||
this.addLinks(route);
|
||||
|
||||
// Add the OpenSearch service link
|
||||
this.linkHeadService.addTag({
|
||||
href: environment.rest.baseUrl + '/' + this.openSearchUri + '/service',
|
||||
type: 'application/atom+xml',
|
||||
rel: 'search',
|
||||
title: 'Dspace',
|
||||
});
|
||||
|
||||
// Update the route subject
|
||||
this.route$.next(route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a route given the different params available to opensearch
|
||||
*/
|
||||
formulateRoute(uuid: string, opensearch: string, sort?: SortOptions, query?: string, searchFilters?: SearchFilter[], configuration?: string, pageSize?: number, fixedFilter?: string): string {
|
||||
let route = 'format=atom';
|
||||
@@ -158,9 +212,9 @@ export class RSSComponent implements OnInit, OnDestroy {
|
||||
route += `&rpp=${pageSize}`;
|
||||
}
|
||||
if (searchFilters) {
|
||||
for (const filter of searchFilters) {
|
||||
for (const val of filter.values) {
|
||||
route += '&' + filter.key + '=' + encodeURIComponent(val) + (filter.operator ? ',' + filter.operator : '');
|
||||
for (const searchFilter of searchFilters) {
|
||||
for (const val of searchFilter.values) {
|
||||
route += '&' + searchFilter.key + '=' + encodeURIComponent(val) + (searchFilter.operator ? ',' + searchFilter.operator : '');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,20 +225,8 @@ export class RSSComponent implements OnInit, OnDestroy {
|
||||
return route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the router url contains the specified route
|
||||
*
|
||||
* @param {string} route
|
||||
* @returns
|
||||
* @memberof MyComponent
|
||||
*/
|
||||
hasRoute(route: string) {
|
||||
return this.router.url.includes(route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates <link> tags in the header of the page
|
||||
* @param route The composed url to opensearch
|
||||
*/
|
||||
addLinks(route: string): void {
|
||||
this.linkHeadService.addTag({
|
||||
@@ -201,5 +243,4 @@ export class RSSComponent implements OnInit, OnDestroy {
|
||||
title: 'Sitewide RSS feed',
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user