forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fd36e4
commit caa57cc
Showing
8 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...nts/specific-field/metrics/item-page-metrics-field/item-page-metrics-field.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<ng-container *ngIf="!metrics.disabled"> | ||
<div data-badge-type='medium-donut' class='altmetric-embed' data-badge-details='right' [attr.data-doi]='metrics.url'> | ||
</div> | ||
<a id="plumx" class="plumx-plum-print-popup" href="https://plu.mx/plum/a/?doi={{metrics.url}}" data-popup="right" | ||
data-hide-when-empty="true"></a> | ||
</ng-container> |
57 changes: 57 additions & 0 deletions
57
.../specific-field/metrics/item-page-metrics-field/item-page-metrics-field.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { Item } from 'src/app/core/shared/item.model'; | ||
import { of as observableOf } from 'rxjs'; | ||
import { ItemPageMetricsFieldComponent } from './item-page-metrics-field.component'; | ||
import { APP_CONFIG } from 'src/config/app-config.interface'; | ||
|
||
describe('ItemPageMetricsFieldComponent', () => { | ||
let component: ItemPageMetricsFieldComponent; | ||
let fixture: ComponentFixture<ItemPageMetricsFieldComponent>; | ||
const URI = '/10.13039/501100000289'; | ||
|
||
const appConfigProvider = { | ||
provide: APP_CONFIG, | ||
useValue: { ui: { altmetric: 'altmetric-url', plumx: 'plumx-url' } } | ||
}; | ||
|
||
beforeEach(async () => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ItemPageMetricsFieldComponent], | ||
providers: [appConfigProvider], | ||
}); | ||
|
||
fixture = TestBed.createComponent(ItemPageMetricsFieldComponent); | ||
component = fixture.componentInstance; | ||
|
||
const mockItem: Item = Object.assign(new Item(), { | ||
bundles: observableOf({}), | ||
metadata: { | ||
'dc.title': [ | ||
{ | ||
language: 'en_US', | ||
value: 'This is just another title', | ||
}, | ||
], | ||
'dc.identifier.uri': [{ value: 'http://dx.doi.org' + URI }], | ||
}, | ||
}); | ||
|
||
component.item = mockItem; | ||
|
||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should set data-doi attribute correctly', () => { | ||
const doiAttribute = fixture.nativeElement.querySelector('.altmetric-embed').getAttribute('data-doi'); | ||
expect(doiAttribute).toBe(URI); | ||
}); | ||
|
||
it('should generate a valid anchor element', () => { | ||
const metricsFieldElement = fixture.nativeElement; | ||
const anchorElement = metricsFieldElement.querySelector('a'); | ||
expect(anchorElement).not.toBeNull(); | ||
|
||
const expectedHref = 'https://plu.mx/plum/a/?doi=' + URI; | ||
expect(anchorElement.getAttribute('href')).toBe(expectedHref); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
...nents/specific-field/metrics/item-page-metrics-field/item-page-metrics-field.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Component, Input, Inject, OnInit } from '@angular/core'; | ||
import { AltmetricMenuItemModel } from 'src/app/shared/menu/menu-item/models/altmetric.model'; | ||
import { Item } from 'src/app/core/shared/item.model'; | ||
import { AppConfig, APP_CONFIG } from 'src/config/app-config.interface'; | ||
|
||
@Component({ | ||
selector: 'ds-item-page-metrics-field', | ||
templateUrl: './item-page-metrics-field.component.html', | ||
}) | ||
export class ItemPageMetricsFieldComponent implements OnInit { | ||
|
||
@Input() metrics = { url: '', disabled: true } as AltmetricMenuItemModel; | ||
@Input() item: Item; | ||
|
||
constructor(@Inject(APP_CONFIG) private appConfig: AppConfig) {} | ||
|
||
ngOnInit() { | ||
this.metrics.url = this.item.firstMetadataValue('dc.identifier.uri') ? (new URL(this.item.firstMetadataValue('dc.identifier.uri')).pathname) : ''; | ||
if (this.metrics.url !== '') { | ||
this.metrics.disabled = false; | ||
} | ||
|
||
this.loadExternalScript(this.appConfig.ui.altmetric) | ||
.catch(error => console.error('Script loading error:', error)); | ||
|
||
this.loadExternalScript(this.appConfig.ui.plumx) | ||
.catch(error => console.error('Script loading error:', error)); | ||
} | ||
|
||
private loadExternalScript(scriptUrl: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
if (!this.metrics.disabled) { | ||
const script = document.createElement('script'); | ||
script.src = scriptUrl; | ||
script.onload = () => { | ||
console.log('External script has been loaded: ' + scriptUrl); | ||
resolve(); | ||
}; | ||
script.onerror = (error) => { | ||
this.metrics.disabled = true; | ||
reject(error); | ||
}; | ||
document.body.appendChild(script); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters