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.
Merge pull request #8 from mlibrary/issue-working7
add altmetrics and more.
- Loading branch information
Showing
35 changed files
with
764 additions
and
111 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ rest: | |
host: api7.dspace.org | ||
port: 443 | ||
nameSpace: /server | ||
item: | ||
showAltmetricBadge: true |
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
9 changes: 9 additions & 0 deletions
9
...ts/specific-field/metrics-dimensions/dimensions/item-page-dimensions-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,9 @@ | ||
<span | ||
dsDimensionsData | ||
[item]="item" | ||
class="__dimensions_badge_embed__" | ||
data-hide-zero-citations="true" | ||
data-style="large_rectangle" | ||
></span> | ||
|
||
|
71 changes: 71 additions & 0 deletions
71
...ents/specific-field/metrics-dimensions/dimensions/item-page-dimensions-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,71 @@ | ||
import { AfterViewInit, Component, EventEmitter, HostListener, Inject, Input, Output } from '@angular/core'; | ||
import { ExternalScriptLoaderDimensionsService } from 'src/app/shared/utils/scripts-loader-dimensions/external-script-loader-dimensions.service'; | ||
import { | ||
ExternalScriptsNames, | ||
ExternalScriptsStatus, | ||
} from 'src/app/shared/utils/scripts-loader-dimensions/external-script.model'; | ||
import { Item } from '../../../../../../core/shared/item.model'; | ||
import { APP_CONFIG, AppConfig } from 'src/config/app-config.interface'; | ||
|
||
@Component({ | ||
selector: 'ds-item-page-dimensions-field', | ||
templateUrl: './item-page-dimensions-field.component.html', | ||
}) | ||
export class ItemPageDimensionsFieldComponent implements AfterViewInit { | ||
@Input() item: Item; | ||
|
||
@Output() widgetLoaded = new EventEmitter<boolean>(); | ||
|
||
constructor( | ||
@Inject(APP_CONFIG) protected appConfig: AppConfig, | ||
private scriptLoader: ExternalScriptLoaderDimensionsService | ||
) {} | ||
|
||
ngAfterViewInit() { | ||
if (!this.appConfig.item.showAltmetricBadge) { | ||
return; | ||
} | ||
this.scriptLoader | ||
.load(ExternalScriptsNames.DIMENSIONS) | ||
.then((data) => this.reloadBadge(data)) | ||
.catch((error) => console.error(error)); | ||
} | ||
|
||
/** | ||
* We ensure that the badge is visible after the script is loaded | ||
* @param data The data returned from the promise | ||
*/ | ||
// private reloadBadge(data: any[]) { | ||
// if (data.find((element) => this.isLoaded(element))) { | ||
// const initMethod = '__dimensions_embed_installed__'; | ||
// window[initMethod](); | ||
// } | ||
// } | ||
|
||
// I got this method from the community. The init method has to be different. | ||
private reloadBadge(data: any[]) { | ||
if (data.find((element) => this.isLoaded(element))) { | ||
const initClass = '__dimensions_embed'; | ||
const initMethod = 'addBadges'; | ||
window[initClass][initMethod](); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Check if the script has been previously loaded in the DOM | ||
* @param element The resolve element from the promise | ||
* @returns true if the script has been already loaded, false if not | ||
*/ | ||
private isLoaded(element: any): unknown { | ||
return ( | ||
element.script === ExternalScriptsNames.DIMENSIONS && | ||
element.status === ExternalScriptsStatus.ALREADY_LOADED | ||
); | ||
} | ||
|
||
@HostListener('window:altmetric:show', ['$event']) | ||
private onWidgetShow(event: Event) { | ||
this.widgetLoaded.emit(true); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ents/specific-field/metrics-dimensions/dimensions/item-page-dimensions-field.directive.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,83 @@ | ||
import { Directive, ElementRef, Input, OnInit, Renderer2 } from '@angular/core'; | ||
import { Item } from 'src/app/core/shared/item.model'; | ||
|
||
/** | ||
* This directive adds the data-* attribute for the Altmetric badge dependening on the first identifier found in the item | ||
*/ | ||
@Directive({ | ||
selector: '[dsDimensionsData]', | ||
}) | ||
export class DimensionsDirective implements OnInit { | ||
@Input() item: Item; | ||
|
||
constructor(private renderer: Renderer2, private elementRef: ElementRef) {} | ||
|
||
ngOnInit(): void { | ||
const identifier = this.obtainFirstValidID(this.initItemIdentifiers()); | ||
if (identifier !== undefined) { | ||
this.renderer.setAttribute( | ||
this.elementRef.nativeElement, | ||
identifier.name, | ||
this.applyRegex(identifier.value, identifier.regex) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* This initialize an array of identifiers founded in the item. | ||
* It search for DOI, Handle, PMID, ISBN, ARXIV and URI. | ||
* Some identifiers may be stored with more text than the ID so this objects has a regex property to clean it | ||
*/ | ||
private initItemIdentifiers(): any[] { | ||
return [ | ||
{ | ||
name: 'data-doi', | ||
value: this.item.firstMetadataValue('dc.identifier.doi'), | ||
regex: /https?:\/\/(dx\.)?doi\.org\//gi, | ||
}, | ||
{ | ||
name: 'data-handle', | ||
value: this.item.firstMetadataValue('dc.identifier.uri'), | ||
regex: /http?:\/\/hdl\.handle\.net\//gi, | ||
}, | ||
{ | ||
name: 'data-pmid', | ||
value: this.item.firstMetadataValue('dc.identifier.pmid'), | ||
regex: '', | ||
}, | ||
{ | ||
name: 'data-isbn', | ||
value: this.item.firstMetadataValue('dc.identifier.isbn'), | ||
regex: '', | ||
}, | ||
{ | ||
name: 'data-arxiv-id', | ||
value: this.item.firstMetadataValue('dc.identifier.arxiv'), | ||
regex: '', | ||
}, | ||
{ | ||
name: 'data-uri', | ||
value: this.item.firstMetadataValue('dc.identifier.uri'), | ||
regex: '', | ||
}, | ||
]; | ||
} | ||
|
||
/** | ||
* This function obtains the first valid ID from the item | ||
* @returns Returns first valid identifier (not undefined), undefined otherwise | ||
*/ | ||
private obtainFirstValidID(itemIdentifiers: any[]): any { | ||
return itemIdentifiers.find((element) => element.value !== undefined); | ||
} | ||
|
||
/** | ||
* Apply the specified regex to clean the metadata and obtain only the ID | ||
* @param value The metadata value | ||
* @param regex The regex to apply | ||
* @returns The result is the ID clean | ||
*/ | ||
private applyRegex(value: string, regex: string): string { | ||
return value.replace(regex, ''); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...nents/specific-field/metrics-dimensions/item-page-metrics-dimensions-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,12 @@ | ||
<div class="item-page-field"> | ||
<ds-metadata-field-wrapper [hideIfNoTextContent]="false"> | ||
<div class="simple-view-element"> | ||
<!-- <h2 *ngIf="showTitle" class="simple-view-element-header"> | ||
{{ "item.page.metrics" | translate }} | ||
</h2> --> | ||
<div class="simple-view-element-body"> | ||
<ds-item-page-dimensions-field (widgetLoaded)="someWidgetHasLoaded($event)" [item]="item"></ds-item-page-dimensions-field> | ||
</div> | ||
</div> | ||
</ds-metadata-field-wrapper> | ||
</div> |
24 changes: 24 additions & 0 deletions
24
...ponents/specific-field/metrics-dimensions/item-page-metrics-dimensions-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,24 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { ItemPageFieldComponent } from '../item-page-field.component'; | ||
import { Item } from 'src/app/core/shared/item.model'; | ||
|
||
@Component({ | ||
selector: 'ds-item-page-metrics-dimensions-field', | ||
templateUrl: './item-page-metrics-dimensions-field.component.html', | ||
styleUrls: [ | ||
'../../../../../shared/metadata-field-wrapper/metadata-field-wrapper.component.scss', | ||
], | ||
}) | ||
export class ItemPageMetricsDimensionsFieldComponent extends ItemPageFieldComponent { | ||
|
||
@Input() item: Item; | ||
|
||
public showTitle = false; | ||
|
||
public someWidgetHasLoaded(widgetLoaded: boolean) { | ||
if (widgetLoaded) { | ||
this.showTitle = true; | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...ield-components/specific-field/metrics/altmetric/item-page-altmetric-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,10 @@ | ||
<span | ||
dsAltmetricData | ||
[item]="item" | ||
class="altmetric-embed" | ||
data-style="large_rectangle" | ||
data-hide-no-mentions="true" | ||
data-badge-type="1" | ||
data-badge-popover="right" | ||
data-link-target="_blank" | ||
></span> |
62 changes: 62 additions & 0 deletions
62
.../field-components/specific-field/metrics/altmetric/item-page-altmetric-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,62 @@ | ||
import { AfterViewInit, Component, EventEmitter, HostListener, Inject, Input, Output } from '@angular/core'; | ||
import { ExternalScriptLoaderService } from 'src/app/shared/utils/scripts-loader/external-script-loader.service'; | ||
import { | ||
ExternalScriptsNames, | ||
ExternalScriptsStatus, | ||
} from 'src/app/shared/utils/scripts-loader/external-script.model'; | ||
import { Item } from '../../../../../../core/shared/item.model'; | ||
import { APP_CONFIG, AppConfig } from 'src/config/app-config.interface'; | ||
|
||
@Component({ | ||
selector: 'ds-item-page-altmetric-field', | ||
templateUrl: './item-page-altmetric-field.component.html', | ||
}) | ||
export class ItemPageAltmetricFieldComponent implements AfterViewInit { | ||
@Input() item: Item; | ||
|
||
@Output() widgetLoaded = new EventEmitter<boolean>(); | ||
|
||
constructor( | ||
@Inject(APP_CONFIG) protected appConfig: AppConfig, | ||
private scriptLoader: ExternalScriptLoaderService | ||
) {} | ||
|
||
ngAfterViewInit() { | ||
if (!this.appConfig.item.showAltmetricBadge) { | ||
return; | ||
} | ||
|
||
this.scriptLoader | ||
.load(ExternalScriptsNames.ALTMETRIC) | ||
.then((data) => this.reloadBadge(data)) | ||
.catch((error) => console.error(error)); | ||
} | ||
|
||
/** | ||
* We ensure that the badge is visible after the script is loaded | ||
* @param data The data returned from the promise | ||
*/ | ||
private reloadBadge(data: any[]) { | ||
if (data.find((element) => this.isLoaded(element))) { | ||
const initMethod = '_altmetric_embed_init'; | ||
window[initMethod](); | ||
} | ||
} | ||
|
||
/** | ||
* Check if the script has been previously loaded in the DOM | ||
* @param element The resolve element from the promise | ||
* @returns true if the script has been already loaded, false if not | ||
*/ | ||
private isLoaded(element: any): unknown { | ||
return ( | ||
element.script === ExternalScriptsNames.ALTMETRIC && | ||
element.status === ExternalScriptsStatus.ALREADY_LOADED | ||
); | ||
} | ||
|
||
@HostListener('window:altmetric:show', ['$event']) | ||
private onWidgetShow(event: Event) { | ||
this.widgetLoaded.emit(true); | ||
} | ||
} |
Oops, something went wrong.