From 7a1d31952f733644c82570e20c8aef11f3aeed8d Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Wed, 23 Aug 2023 10:15:00 +0200 Subject: [PATCH] (feat): make abstract links as hyperlinks --- .../lib/metadata-info/linkify.directive.ts | 11 ++- .../metadata-info.component.html | 44 +++--------- .../metadata-info.component.spec.ts | 71 ++++++++++++++++++- .../metadata-info/metadata-info.component.ts | 7 ++ 4 files changed, 90 insertions(+), 43 deletions(-) diff --git a/libs/ui/elements/src/lib/metadata-info/linkify.directive.ts b/libs/ui/elements/src/lib/metadata-info/linkify.directive.ts index 3ef699bad1..e213400499 100644 --- a/libs/ui/elements/src/lib/metadata-info/linkify.directive.ts +++ b/libs/ui/elements/src/lib/metadata-info/linkify.directive.ts @@ -1,16 +1,13 @@ /* eslint-disable @angular-eslint/directive-selector */ -import { Directive, ElementRef, Renderer2, OnInit } from '@angular/core' +import { Directive, ElementRef, Renderer2, AfterViewInit } from '@angular/core' @Directive({ selector: '[gnUiLinkify]', }) -export class GnUiLinkifyDirective implements OnInit { +export class GnUiLinkifyDirective implements AfterViewInit { constructor(private el: ElementRef, private renderer: Renderer2) {} - - ngOnInit() { - setTimeout(() => { - this.processLinks() - }, 0) + ngAfterViewInit() { + this.processLinks() } private processLinks() { diff --git a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.html b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.html index fe1fb2cf6a..49ee82ace6 100644 --- a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.html +++ b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.html @@ -1,15 +1,9 @@ -

+

record.metadata.about

-

+

@@ -18,26 +12,16 @@ record.metadata.keywords

- {{ keyword }} + {{ keyword }}
- +

{{ metadata.lineage }}

-
+

record.metadata.updatedOn

@@ -58,10 +42,7 @@

- +
record.metadata.isOpenData @@ -74,17 +55,12 @@
- + - + \ No newline at end of file diff --git a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.spec.ts b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.spec.ts index fbb3957396..143d87f182 100644 --- a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.spec.ts +++ b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.spec.ts @@ -66,7 +66,6 @@ describe('MetadataInfoComponent', () => { fixture.detectChanges() }) it('should not display a message for no usage or constraints', () => { - // Use waitForAsync to handle asynchronous changes in the DOM. fixture.whenStable().then(() => { const displayedElement = fixture.nativeElement.getElementsByClassName('noUsage') @@ -75,7 +74,6 @@ describe('MetadataInfoComponent', () => { }) it('should display the keywords section', () => { - // Use waitForAsync to handle asynchronous changes in the DOM. fixture.whenStable().then(() => { const displayedElement = fixture.nativeElement.querySelector('ng-container') @@ -83,4 +81,73 @@ describe('MetadataInfoComponent', () => { }) }) }) + describe('When a section is not empty', () => { + beforeEach(() => { + component.metadata = { + id: '', + uuid: '', + title: '', + metadataUrl: '', + keywords: ['banana', 'pear'], + constraints: ['no usage'], + } + fixture.detectChanges() + }) + it('should not display a message for no usage or constraints', () => { + fixture.whenStable().then(() => { + const displayedElement = + fixture.nativeElement.getElementsByClassName('noUsage') + expect(displayedElement).toBeFalsy() + }) + }) + + it('should display the keywords section', () => { + fixture.whenStable().then(() => { + const displayedElement = + fixture.nativeElement.querySelector('ng-container') + expect(displayedElement).toBeTruthy() + }) + }) + }) + describe('#abstract', () => { + describe('When abstract contains a link', () => { + beforeEach(() => { + component.metadata = { + id: '', + uuid: '', + title: '', + metadataUrl: '', + keywords: ['banana', 'pear'], + constraints: ['no usage'], + abstract: 'This is a text with a link http://www.google.com', + } + fixture.detectChanges() + }) + it('should create a link in the abstract', () => { + const paragraph = component.getAbstract() + expect(paragraph).toContain('open_in_new' + ) + }) + }) + describe('When abstract does not contain a link', () => { + beforeEach(() => { + component.metadata = { + id: '', + uuid: '', + title: '', + metadataUrl: '', + keywords: ['banana', 'pear'], + constraints: ['no usage'], + abstract: 'This is a text without a link', + } + fixture.detectChanges() + }) + it('should not create a hyperlink or icon', () => { + const paragraph = component.getAbstract() + expect(paragraph).toEqual('This is a text without a link') + }) + }) + }) }) diff --git a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.ts b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.ts index 3583dd03be..86cd1a790a 100644 --- a/libs/ui/elements/src/lib/metadata-info/metadata-info.component.ts +++ b/libs/ui/elements/src/lib/metadata-info/metadata-info.component.ts @@ -30,4 +30,11 @@ export class MetadataInfoComponent { onKeywordClick(keyword: string) { this.keyword.emit(keyword) } + + getAbstract() { + return this.metadata.abstract.replace(/(\bhttps?:\/\/\S+\b)/g, (match) => { + return `${match} open_in_new` + }) + } }