Skip to content

Commit

Permalink
Merged dspace-cris-2023_02_x into DSC-1590
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Barbasso committed Apr 24, 2024
2 parents 761c84a + f835e19 commit 97ce799
Show file tree
Hide file tree
Showing 100 changed files with 953 additions and 393 deletions.
4 changes: 2 additions & 2 deletions src/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ import { EditItemMode } from './submission/models/edititem-mode.model';
import { AuditDataService } from './audit/audit-data.service';
import { Audit } from './audit/model/audit.model';
import { ItemExportFormat } from './itemexportformat/model/item-export-format.model';
import { MetricsComponentsDataService } from './layout/metrics-components-data.service';
import { MetricsComponentsService } from './layout/metrics-components.service';
import { MetricsComponent } from './layout/models/metrics-component.model';
import { Metric } from './shared/metric.model';
import { MetricsDataService } from './data/metrics-data.service';
Expand Down Expand Up @@ -354,7 +354,7 @@ const PROVIDERS = [
FilteredDiscoveryPageResponseParsingService,
{ provide: NativeWindowService, useFactory: NativeWindowFactory },
TabDataService,
MetricsComponentsDataService,
MetricsComponentsService,
MetricsDataService,
VocabularyService,
VocabularyDataService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { VocabularyEntry } from '../../submission/vocabularies/models/vocabulary
import { FormFieldMetadataValueObject } from '../../../shared/form/builder/models/form-field-metadata-value.model';
import { FormFieldLanguageValueObject } from '../../../shared/form/builder/models/form-field-language-value.model';
import { CoreState } from '../../core-state.model';
import { Metadata } from '../../shared/metadata.utils';
import { ConfidenceType } from '../../shared/confidence-type';

/**
* Provides methods to dispatch JsonPatch Operations Actions
Expand Down Expand Up @@ -148,6 +150,11 @@ export class JsonPatchOperationsBuilder {
} else {
operationValue = value;
}
//Update confidence if was added once the field was already created, value is set only in constructor of FormFieldMetadataValueObject
if (Metadata.hasValidAuthority(operationValue.authority) && (isEmpty(operationValue.confidence) || operationValue.confidence === -1)) {
operationValue.confidence = ConfidenceType.CF_ACCEPTED;
}

} else if (value instanceof Date) {
if (securityLevel != null) {
operationValue = new FormFieldMetadataValueObject(dateToISOFormat(value), null, securityLevel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Metric } from '../shared/metric.model';
import { MetricsComponentsDataService } from './metrics-components-data.service';
import { MetricsComponentsService } from './metrics-components.service';

describe('MetricsComponentsDataService', () => {
describe('MetricsComponentsService', () => {

let service: MetricsComponentsDataService;
let service: MetricsComponentsService;

beforeEach(() => {
service = new MetricsComponentsDataService();
service = new MetricsComponentsService();

});

Expand Down Expand Up @@ -44,8 +44,8 @@ describe('MetricsComponentsDataService', () => {

expect(result.length).toBe(1);
expect(result[0].metrics.length).toBe(2);
expect(result[0].metrics[0].metricType).toBe('views');
expect(result[0].metrics[1].metricType).toBe('downloads');
expect(result[0].metrics[0].metricType).toBe('downloads');
expect(result[0].metrics[1].metricType).toBe('views');

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CrisLayoutMetricRow } from './models/tab.model';
/**
* A service responsible for managing metrics objects
*/
export class MetricsComponentsDataService {
export class MetricsComponentsService {

/**
* Get matching metrics for item.
Expand All @@ -20,17 +20,13 @@ export class MetricsComponentsDataService {

computeMetricsRows(itemMetrics: Metric[], maxColumn, metricTypes: string[]): CrisLayoutMetricRow[] {

// support
const typeMap = {};
metricTypes.forEach((type) => typeMap[type] = type);

// filter, enrich, order
const metrics = itemMetrics
.filter((metric) => typeMap[metric.metricType])
.map((metric) => {
return { ...metric, position: typeMap[metric.metricType].position };
const metrics: Metric[] = itemMetrics
.filter((metric: Metric) => metricTypes.includes(metric.metricType))
.map((metric: Metric) => {
return { ...metric, position: metricTypes.indexOf(metric.metricType) };
})
.sort((metric) => metric.position);
.sort((metricA, metricB ) => metricA.position - metricB.position);

// chunker
const totalRow = Math.ceil(metrics.length / maxColumn);
Expand Down
105 changes: 105 additions & 0 deletions src/app/core/shared/client-math.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { DOCUMENT } from '@angular/common';
import {
Inject,
Injectable,
} from '@angular/core';
import { Observable, ReplaySubject, Subject } from 'rxjs';
import { environment } from 'src/environments/environment';
import {
NativeWindowRef,
NativeWindowService,
} from '../services/window.service';import { MathJaxConfig, MathService } from './math.service';

@Injectable({
providedIn: 'root'
})
/**
* Provide the MathService for CSR
*/
export class ClientMathService extends MathService {

protected isReady$: Subject<boolean>;

protected mathJaxOptions = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
svg: {
fontCache: 'global'
},
startup: {
typeset: false
}
};

protected mathJax: MathJaxConfig = {
source: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js',
id: 'MathJaxScript'
};
protected mathJaxFallback: MathJaxConfig = {
source: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-chtml.min.js',
id: 'MathJaxBackupScript'
};

constructor(
@Inject(DOCUMENT) private _document: Document,
@Inject(NativeWindowService) protected _window: NativeWindowRef,
) {
super();

this.isReady$ = new ReplaySubject<boolean>();

void this.registerMathJaxAsync(this.mathJax)
.then(() => this.isReady$.next(true))
.catch(_ => {
void this.registerMathJaxAsync(this.mathJaxFallback)
.then(() => this.isReady$.next(true));
});
}

/**
* Register the specified MathJax script in the document
*
* @param config The configuration object for the script
*/
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
if (environment.markdown.mathjax) {
return new Promise<void>((resolve, reject) => {

const optionsScript: HTMLScriptElement = this._document.createElement('script');
optionsScript.type = 'text/javascript';
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`;
this._document.head.appendChild(optionsScript);

const script: HTMLScriptElement = this._document.createElement('script');
script.id = config.id;
script.type = 'text/javascript';
script.src = config.source;
script.crossOrigin = 'anonymous';
script.async = true;
script.onload = () => resolve();
script.onerror = error => reject(error);
this._document.head.appendChild(script);
});
}
return Promise.resolve();
}

/**
* Return the status of the script registration
*/
ready(): Observable<boolean> {
return this.isReady$;
}

/**
* Render the specified element using the MathJax JavaScript
*
* @param element The element to render with MathJax
*/
render(element: HTMLElement) {
if (environment.markdown.mathjax) {
this._window.nativeWindow.MathJax.typesetPromise([element]);
}
}
}
47 changes: 47 additions & 0 deletions src/app/core/shared/math.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TestBed } from '@angular/core/testing';
import { Observable, of } from 'rxjs';
import { MathService, MathJaxConfig } from './math.service';

export class MockMathService extends MathService {
protected mathJaxOptions: any = {};
protected mathJax: MathJaxConfig = { source: '', id: '' };
protected mathJaxFallback: MathJaxConfig = { source: '', id: '' };

protected registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
return Promise.resolve();
}

ready(): Observable<boolean> {
return of(true);
}

render(element: HTMLElement): void {
return;
}
}

describe('MathService', () => {
let service: MockMathService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = new MockMathService();
spyOn(service, 'render');
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should be ready', (done) => {
service.ready().subscribe(isReady => {
expect(isReady).toBe(true);
done();
});
});

it('should render', () => {
service.render(document.createElement('div'));
expect(service.render).toHaveBeenCalled();
});
});
19 changes: 19 additions & 0 deletions src/app/core/shared/math.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Observable } from 'rxjs';

export interface MathJaxConfig {
source: string;
id: string;
}

/**
* This service is used to provide the MathJax library with the ability to render markdown code
*/
export abstract class MathService {
protected abstract mathJaxOptions: any;
protected abstract mathJax: MathJaxConfig;
protected abstract mathJaxFallback: MathJaxConfig;

protected abstract registerMathJaxAsync(config: MathJaxConfig): Promise<any>;
abstract ready(): Observable<boolean>;
abstract render(element: HTMLElement): void;
}
44 changes: 44 additions & 0 deletions src/app/core/shared/server-math.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Injectable } from '@angular/core';
import { Observable, ReplaySubject, Subject } from 'rxjs';
import { MathJaxConfig, MathService } from './math.service';

@Injectable({
providedIn: 'root'
})
/**
* Provide the MathService for SSR
*/
export class ServerMathService extends MathService {

protected signal: Subject<boolean>;

protected mathJaxOptions = {};

protected mathJax: MathJaxConfig = {
source: '',
id: ''
};
protected mathJaxFallback: MathJaxConfig = {
source: '',
id: ''
};

constructor() {
super();

this.signal = new ReplaySubject<boolean>();
this.signal.next(true);
}

protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
return Promise.resolve();
}

ready(): Observable<boolean> {
return this.signal;
}

render(element: HTMLElement) {
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('AdvancedAttachmentComponent', () => {
{ provide: 'fieldProvider', useValue: mockField },
{ provide: 'itemProvider', useValue: testItem },
{ provide: 'renderingSubTypeProvider', useValue: '' },
{ provide: 'tabNameProvider', useValue: '' },
{ provide: BitstreamDataService, useValue: mockBitstreamDataService },
{ provide: AuthorizationDataService, useValue: mockAuthorizedService },
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ export class AdvancedAttachmentComponent extends AttachmentComponent implements
@Inject('fieldProvider') public fieldProvider: LayoutField,
@Inject('itemProvider') public itemProvider: Item,
@Inject('renderingSubTypeProvider') public renderingSubTypeProvider: string,
@Inject('tabNameProvider') public tabNameProvider: string,
protected bitstreamDataService: BitstreamDataService,
protected translateService: TranslateService
) {
super(fieldProvider, itemProvider, renderingSubTypeProvider, bitstreamDataService, translateService);
super(fieldProvider, itemProvider, renderingSubTypeProvider, tabNameProvider, bitstreamDataService, translateService);
}

getBitstreamsByItem(options?: FindListOptions): Observable<PaginatedList<Bitstream>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class AttachmentRenderComponent implements OnInit {
* The bitstream
*/
@Input() renderingType: AttachmentRenderingType | string;
/**
* The tab name
*/
@Input() tabName: string;

/**
* Directive hook used to place the dynamic render component
Expand Down Expand Up @@ -77,6 +81,7 @@ export class AttachmentRenderComponent implements OnInit {
attachentComponentRef = this.attachmentValueViewRef.createComponent(factory, 0, this.getComponentInjector());
(attachentComponentRef.instance as any).item = this.item;
(attachentComponentRef.instance as any).bitstream = this.bitstream;
(attachentComponentRef.instance as any).tabName = this.tabName;
}
return attachentComponentRef;
}
Expand All @@ -87,7 +92,8 @@ export class AttachmentRenderComponent implements OnInit {
getComponentInjector() {
const providers = [
{provide: 'itemProvider', useValue: this.item, deps: []},
{provide: 'bitstreamProvider', useValue: this.bitstream, deps: []}
{provide: 'bitstreamProvider', useValue: this.bitstream, deps: []},
{provide: 'tabNameProvider', useValue: this.tabName, deps: []}
];

return Injector.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<div class="d-flex flex-column align-items-end gap-3">
<ng-container *ngFor="let renderingType of allAttachmentProviders">
<div class="text-nowrap">
<ds-attachment-render [renderingType]="renderingType" [bitstream]="attachment" [item]="item"></ds-attachment-render>
<ds-attachment-render [renderingType]="renderingType" [bitstream]="attachment" [item]="item" [tabName]="tabName"></ds-attachment-render>
</div>
</ng-container>
<!-- Default download button -->
<div class="text-nowrap">
<ds-attachment-render [renderingType]="AttachmentRenderingType.DOWNLOAD" [bitstream]="attachment" [item]="item"></ds-attachment-render>
<ds-attachment-render [renderingType]="AttachmentRenderingType.DOWNLOAD" [bitstream]="attachment" [item]="item" [tabName]="tabName"></ds-attachment-render>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('BitstreamAttachmentComponent', () => {
{provide: 'fieldProvider', useValue: {}},
{provide: 'itemProvider', useValue: {}},
{provide: 'renderingSubTypeProvider', useValue: ''},
{provide: 'tabNameProvider', useValue: '' },
{provide: BitstreamDataService, useValue: {}},
{provide: TranslateService, useValue: {}},
],
Expand Down
Loading

0 comments on commit 97ce799

Please sign in to comment.