mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
w2p-85140 ds-rss component now adds a button to all search pages and community and collection, link-head service adds the rss to the head element
This commit is contained in:
@@ -164,8 +164,12 @@ import { SequenceService } from './shared/sequence.service';
|
||||
import { CoreState } from './core-state.model';
|
||||
import { GroupDataService } from './eperson/group-data.service';
|
||||
import { SubmissionAccessesModel } from './config/models/config-submission-accesses.model';
|
||||
<<<<<<< HEAD
|
||||
import { AccessStatusObject } from '../shared/object-list/access-status-badge/access-status.model';
|
||||
import { AccessStatusDataService } from './data/access-status-data.service';
|
||||
=======
|
||||
import { LinkHeadService } from './services/link-head.service';
|
||||
>>>>>>> 354768d98 (w2p-85140 ds-rss component now adds a button to all search pages and community and collection, link-head service adds the rss to the head element)
|
||||
|
||||
/**
|
||||
* When not in production, endpoint responses can be mocked for testing purposes
|
||||
@@ -205,6 +209,7 @@ const PROVIDERS = [
|
||||
SectionFormOperationsService,
|
||||
FormService,
|
||||
EPersonDataService,
|
||||
LinkHeadService,
|
||||
HALEndpointService,
|
||||
HostWindowService,
|
||||
ItemDataService,
|
||||
|
84
src/app/core/services/link-head.service.ts
Normal file
84
src/app/core/services/link-head.service.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Injectable, Optional, RendererFactory2, ViewEncapsulation, Inject } from '@angular/core';
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
|
||||
@Injectable()
|
||||
export class LinkHeadService {
|
||||
constructor(
|
||||
private rendererFactory: RendererFactory2,
|
||||
@Inject(DOCUMENT) private document
|
||||
) {
|
||||
|
||||
}
|
||||
addTag(tag: LinkDefinition, forceCreation?: boolean) {
|
||||
|
||||
try {
|
||||
const renderer = this.rendererFactory.createRenderer(this.document, {
|
||||
id: '-1',
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
styles: [],
|
||||
data: {}
|
||||
});
|
||||
|
||||
const link = renderer.createElement('link');
|
||||
|
||||
const head = this.document.head;
|
||||
const selector = this._parseSelector(tag);
|
||||
|
||||
if (head === null) {
|
||||
throw new Error('<head> not found within DOCUMENT.');
|
||||
}
|
||||
|
||||
Object.keys(tag).forEach((prop: string) => {
|
||||
return renderer.setAttribute(link, prop, tag[prop]);
|
||||
});
|
||||
|
||||
renderer.appendChild(head, link);
|
||||
|
||||
} catch (e) {
|
||||
console.error('Error within linkService : ', e);
|
||||
}
|
||||
}
|
||||
|
||||
removeTag(attrSelector: string) {
|
||||
if (attrSelector) {
|
||||
try {
|
||||
const renderer = this.rendererFactory.createRenderer(this.document, {
|
||||
id: '-1',
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
styles: [],
|
||||
data: {}
|
||||
});
|
||||
const head = this.document.head;
|
||||
if (head === null) {
|
||||
throw new Error('<head> not found within DOCUMENT.');
|
||||
}
|
||||
const linkTags = this.document.querySelectorAll('link[' + attrSelector + ']');
|
||||
for (const link of linkTags) {
|
||||
renderer.removeChild(head, link);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error while removing tag ' + e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _parseSelector(tag: LinkDefinition): string {
|
||||
const attr: string = tag.rel ? 'rel' : 'hreflang';
|
||||
return `${attr}="${tag[attr]}"`;
|
||||
}
|
||||
}
|
||||
|
||||
export declare type LinkDefinition = {
|
||||
charset?: string;
|
||||
crossorigin?: string;
|
||||
href?: string;
|
||||
hreflang?: string;
|
||||
media?: string;
|
||||
rel?: string;
|
||||
rev?: string;
|
||||
sizes?: string;
|
||||
target?: string;
|
||||
type?: string;
|
||||
} & {
|
||||
[prop: string]: string;
|
||||
};
|
@@ -15,6 +15,7 @@
|
||||
<button class="dropdown-item" *ngFor="let direction of (sortDirections | dsKeys)" (click)="doSortDirectionChange(direction.value)"><i [ngClass]="{'invisible': direction.value !== (sortDirection$ |async)}" class="fas fa-check" aria-hidden="true"></i> {{'sorting.' + direction.key | translate}} </button>
|
||||
</div>
|
||||
</div>
|
||||
<ds-rss></ds-rss>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
5
src/app/shared/rss-feed/rss.component.html
Normal file
5
src/app/shared/rss-feed/rss.component.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<ng-container *ngIf="isEnabled$ | async">
|
||||
<div *ngIf="route$ | async as route" class="d-inline-block float-right margin-right">
|
||||
<a [href]="route" class="btn btn-secondary"><i class="fas fa-rss-square"></i></a>
|
||||
</div>
|
||||
</ng-container>
|
12
src/app/shared/rss-feed/rss.component.scss
Normal file
12
src/app/shared/rss-feed/rss.component.scss
Normal file
@@ -0,0 +1,12 @@
|
||||
:host {
|
||||
.dropdown-toggle::after {
|
||||
display: none;
|
||||
}
|
||||
.dropdown-item {
|
||||
padding-left: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
.margin-right {
|
||||
margin-right: .5em;
|
||||
}
|
109
src/app/shared/rss-feed/rss.component.spec.ts
Normal file
109
src/app/shared/rss-feed/rss.component.spec.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
|
||||
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
|
||||
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
||||
import { RemoteData } from '../../core/data/remote-data';
|
||||
import { GroupDataService } from '../../core/eperson/group-data.service';
|
||||
import { PaginationService } from '../../core/pagination/pagination.service';
|
||||
import { LinkHeadService } from '../../core/services/link-head.service';
|
||||
import { Collection } from '../../core/shared/collection.model';
|
||||
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
|
||||
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
|
||||
import { PaginationComponentOptions } from '../pagination/pagination-component-options.model';
|
||||
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
|
||||
import { PaginatedSearchOptions } from '../search/paginated-search-options.model';
|
||||
import { PaginationServiceStub } from '../testing/pagination-service.stub';
|
||||
import { createPaginatedList } from '../testing/utils.test';
|
||||
import { RSSComponent } from './rss.component';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
|
||||
|
||||
|
||||
describe('RssComponent', () => {
|
||||
let comp: RSSComponent;
|
||||
let options: SortOptions;
|
||||
let fixture: ComponentFixture<RSSComponent>;
|
||||
let uuid: string;
|
||||
let query: string;
|
||||
let groupDataService: GroupDataService;
|
||||
let linkHeadService: LinkHeadService;
|
||||
let configurationDataService: ConfigurationDataService;
|
||||
let paginationService;
|
||||
|
||||
beforeEach(waitForAsync(() => {
|
||||
const mockCollection: Collection = Object.assign(new Collection(), {
|
||||
id: 'ce41d451-97ed-4a9c-94a1-7de34f16a9f4',
|
||||
name: 'test-collection',
|
||||
_links: {
|
||||
mappedItems: {
|
||||
href: 'https://rest.api/collections/ce41d451-97ed-4a9c-94a1-7de34f16a9f4/mappedItems'
|
||||
},
|
||||
self: {
|
||||
href: 'https://rest.api/collections/ce41d451-97ed-4a9c-94a1-7de34f16a9f4'
|
||||
}
|
||||
}
|
||||
});
|
||||
configurationDataService = jasmine.createSpyObj('configurationDataService', {
|
||||
findByPropertyName: createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), {
|
||||
name: 'test',
|
||||
values: [
|
||||
'org.dspace.ctask.general.ProfileFormats = test'
|
||||
]
|
||||
}))
|
||||
});
|
||||
linkHeadService = jasmine.createSpyObj('linkHeadService', {
|
||||
addTag: ''
|
||||
});
|
||||
const mockCollectionRD: RemoteData<Collection> = createSuccessfulRemoteDataObject(mockCollection);
|
||||
const mockSearchOptions = observableOf(new PaginatedSearchOptions({
|
||||
pagination: Object.assign(new PaginationComponentOptions(), {
|
||||
id: 'search-page-configuration',
|
||||
pageSize: 10,
|
||||
currentPage: 1
|
||||
}),
|
||||
sort: new SortOptions('dc.title', SortDirection.ASC),
|
||||
scope: mockCollection.id
|
||||
}));
|
||||
groupDataService = jasmine.createSpyObj('groupsDataService', {
|
||||
findAllByHref: createSuccessfulRemoteDataObject$(createPaginatedList([])),
|
||||
getGroupRegistryRouterLink: ''
|
||||
});
|
||||
paginationService = new PaginationServiceStub();
|
||||
const searchConfigService = {
|
||||
paginatedSearchOptions: mockSearchOptions
|
||||
};
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{ provide: GroupDataService, useValue: groupDataService },
|
||||
{ provide: LinkHeadService, useValue: linkHeadService },
|
||||
{ provide: ConfigurationDataService, useValue: configurationDataService },
|
||||
{ provide: SearchConfigurationService, useValue: searchConfigService},
|
||||
{ provide: PaginationService, useValue: paginationService }
|
||||
],
|
||||
declarations: [RSSComponent]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
options = new SortOptions('dc.title', SortDirection.DESC);
|
||||
uuid = '2cfcf65e-0a51-4bcb-8592-b8db7b064790';
|
||||
query = 'test';
|
||||
fixture = TestBed.createComponent(RSSComponent);
|
||||
comp = fixture.componentInstance;
|
||||
});
|
||||
|
||||
it('should formulate the correct url given params in url', () => {
|
||||
const route = comp.formulateRoute(uuid, options, query);
|
||||
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test');
|
||||
});
|
||||
|
||||
it('should skip uuid if its null', () => {
|
||||
const route = comp.formulateRoute(null, options, query);
|
||||
expect(route).toBe('/opensearch/search?format=atom&sort=dc.title&sort_direction=DESC&query=test');
|
||||
});
|
||||
|
||||
it('should default to query * if none provided', () => {
|
||||
const route = comp.formulateRoute(null, options, null);
|
||||
expect(route).toBe('/opensearch/search?format=atom&sort=dc.title&sort_direction=DESC&query=*');
|
||||
});
|
||||
});
|
||||
|
94
src/app/shared/rss-feed/rss.component.ts
Normal file
94
src/app/shared/rss-feed/rss.component.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
OnDestroy,
|
||||
OnInit,
|
||||
ViewEncapsulation
|
||||
} from '@angular/core';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { GroupDataService } from '../../core/eperson/group-data.service';
|
||||
import { LinkHeadService } from '../../core/services/link-head.service';
|
||||
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
|
||||
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
|
||||
import { environment } from '../../../../src/environments/environment';
|
||||
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
|
||||
import { SortOptions } from '../../core/cache/models/sort-options.model';
|
||||
import { PaginationService } from '../../core/pagination/pagination.service';
|
||||
|
||||
|
||||
/**
|
||||
* The default pagination controls component.
|
||||
*/
|
||||
@Component({
|
||||
exportAs: 'rssComponent',
|
||||
selector: 'ds-rss',
|
||||
styleUrls: ['rss.component.scss'],
|
||||
templateUrl: 'rss.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.Default,
|
||||
encapsulation: ViewEncapsulation.Emulated
|
||||
})
|
||||
export class RSSComponent implements OnInit, OnDestroy {
|
||||
|
||||
route$: BehaviorSubject<string>;
|
||||
|
||||
isEnabled$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
|
||||
uuid: string;
|
||||
configuration$: Observable<string>;
|
||||
sortOption$: Observable<SortOptions>;
|
||||
|
||||
constructor(private groupDataService: GroupDataService,
|
||||
private linkHeadService: LinkHeadService,
|
||||
private configurationService: ConfigurationDataService,
|
||||
private searchConfigurationService: SearchConfigurationService,
|
||||
protected paginationService: PaginationService) {
|
||||
}
|
||||
ngOnDestroy(): void {
|
||||
this.linkHeadService.removeTag("rel='alternate'");
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.configuration$ = this.searchConfigurationService.getCurrentConfiguration('default');
|
||||
|
||||
this.configurationService.findByPropertyName('websvc.opensearch.enable').pipe(
|
||||
getFirstCompletedRemoteData(),
|
||||
).subscribe((result) => {
|
||||
const enabled = Boolean(result.payload.values[0]);
|
||||
this.isEnabled$.next(enabled);
|
||||
});
|
||||
|
||||
this.searchConfigurationService.getCurrentQuery('').subscribe((query) => {
|
||||
this.sortOption$ = this.paginationService.getCurrentSort(this.searchConfigurationService.paginationID, null, true);
|
||||
this.sortOption$.subscribe((sort) => {
|
||||
this.uuid = this.groupDataService.getUUIDFromString(window.location.href);
|
||||
|
||||
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, sort, query);
|
||||
|
||||
this.linkHeadService.addTag({
|
||||
href: route,
|
||||
type: 'application/atom+xml',
|
||||
rel: 'alternate',
|
||||
title: 'Sitewide Atom feed'
|
||||
});
|
||||
this.route$ = new BehaviorSubject<string>(route);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
formulateRoute(uuid: string, sort: SortOptions, query: string): string {
|
||||
let route = 'search?format=atom';
|
||||
if (uuid) {
|
||||
route += `&scope=${uuid}`;
|
||||
}
|
||||
if (sort.direction && sort.field) {
|
||||
route += `&sort=${sort.field}&sort_direction=${sort.direction}`;
|
||||
}
|
||||
if (query) {
|
||||
route += `&query=${query}`;
|
||||
} else {
|
||||
route += `&query=*`;
|
||||
}
|
||||
route = '/opensearch/' + route;
|
||||
return route;
|
||||
}
|
||||
}
|
@@ -173,10 +173,9 @@ import { BitstreamRequestACopyPageComponent } from './bitstream-request-a-copy-p
|
||||
import { DsSelectComponent } from './ds-select/ds-select.component';
|
||||
import { LogInOidcComponent } from './log-in/methods/oidc/log-in-oidc.component';
|
||||
import { ThemedItemListPreviewComponent } from './object-list/my-dspace-result-list-element/item-list-preview/themed-item-list-preview.component';
|
||||
import { ExternalLinkMenuItemComponent } from './menu/menu-item/external-link-menu-item.component';
|
||||
import { RSSComponent } from './rss-feed/rss.component';
|
||||
|
||||
const MODULES = [
|
||||
// Do NOT include UniversalModule, HttpModule, or JsonpModule here
|
||||
CommonModule,
|
||||
SortablejsModule,
|
||||
FileUploadModule,
|
||||
@@ -239,6 +238,7 @@ const COMPONENTS = [
|
||||
AbstractListableElementComponent,
|
||||
ObjectCollectionComponent,
|
||||
PaginationComponent,
|
||||
RSSComponent,
|
||||
SearchFormComponent,
|
||||
PageWithSidebarComponent,
|
||||
SidebarDropdownComponent,
|
||||
|
17
src/environments/environment.dev.ts
Normal file
17
src/environments/environment.dev.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export const environment = {
|
||||
ui: {
|
||||
ssl: false,
|
||||
host: 'localhost',
|
||||
port: 18080,
|
||||
nameSpace: '/'
|
||||
},
|
||||
rest: {
|
||||
ssl: false,
|
||||
host: 'localhost',
|
||||
port: 8080,
|
||||
nameSpace: '/server'
|
||||
},
|
||||
universal: {
|
||||
preboot: false
|
||||
}
|
||||
};
|
17
src/environments/environment.prod.ts
Normal file
17
src/environments/environment.prod.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export const environment = {
|
||||
ui: {
|
||||
ssl: false,
|
||||
host: 'localhost',
|
||||
port: 18080,
|
||||
nameSpace: '/'
|
||||
},
|
||||
rest: {
|
||||
ssl: false,
|
||||
host: 'localhost',
|
||||
port: 8080,
|
||||
nameSpace: '/server'
|
||||
},
|
||||
universal: {
|
||||
preboot: true
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user