AAE-4879 Update tooltip card to receive custom HTML (#6930)

This commit is contained in:
Pablo Martinez Garcia
2021-04-16 10:39:19 +02:00
committed by GitHub
parent e9bf84e13c
commit 684cdb88ea
5 changed files with 34 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
<div @tooltip class="adf-tooltip-card" [style.width.px]="width"> <div @tooltip class="adf-tooltip-card" [style.width.px]="width">
<img [src]="image" [width]="width" alt="{{text}}"> <img *ngIf="image " [src]="image" [width]="width" alt="{{text}}">
<hr *ngIf="image"/> <hr *ngIf="image" />
<p>{{text}}</p> <p *ngIf="text">{{text}}</p>
<div *ngIf="htmlContent" [innerHTML]="sanitizedHtmlContent()" class="adf-tooltip-card-content"></div>
</div> </div>

View File

@@ -17,7 +17,7 @@
padding: 12px; padding: 12px;
border-radius: 6px; border-radius: 6px;
p { p, div {
font-size: mat-font-size($alfresco-typography, caption); font-size: mat-font-size($alfresco-typography, caption);
color: mat-color($foreground, text, 0.75); color: mat-color($foreground, text, 0.75);
margin: 0; margin: 0;

View File

@@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { Component, Input } from '@angular/core'; import { Component, Input, SecurityContext } from '@angular/core';
import { animate, style, transition, trigger } from '@angular/animations'; import { animate, style, transition, trigger } from '@angular/animations';
import { DomSanitizer } from '@angular/platform-browser';
@Component({ @Component({
selector: 'adf-tooltip-card-component', selector: 'adf-tooltip-card-component',
@@ -37,5 +38,12 @@ import { animate, style, transition, trigger } from '@angular/animations';
export class TooltipCardComponent { export class TooltipCardComponent {
@Input() image = ''; @Input() image = '';
@Input() text = ''; @Input() text = '';
@Input() htmlContent = '';
@Input() width = '300'; @Input() width = '300';
constructor(private sanitizer: DomSanitizer) { }
sanitizedHtmlContent(): string {
return this.sanitizer.sanitize(SecurityContext.HTML, this.htmlContent);
}
} }

View File

@@ -25,7 +25,7 @@ import { By } from '@angular/platform-browser';
import { TooltipCardComponent } from './tooltip-card.component'; import { TooltipCardComponent } from './tooltip-card.component';
@Component({ @Component({
template: `<span #span [adf-tooltip-card]="'Sample text'" [image]="'/assets/testImg.png'" [width]="'400'" class="test-component"></span>` template: `<span #span [adf-tooltip-card]="'Sample text'" [image]="'/assets/testImg.png'" [width]="'400'" [htmlContent]="'this is the <b>html</b> raw code'" class="test-component"></span>`
}) })
class TestComponent { class TestComponent {
@ViewChild(TooltipCardDirective, { static: true }) @ViewChild(TooltipCardDirective, { static: true })
@@ -74,12 +74,14 @@ describe('TooltipCardDirective', () => {
fixture.detectChanges(); fixture.detectChanges();
tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card'); tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card');
expect(tooltipCard).not.toBeNull(); expect(tooltipCard).not.toBeNull();
const text = overlay.querySelector<HTMLElement>('div.adf-tooltip-card p'); const text = tooltipCard.querySelector<HTMLElement>('p');
const img = overlay.querySelector<HTMLElement>('div.adf-tooltip-card img'); const img = tooltipCard.querySelector<HTMLElement>('img');
const div = tooltipCard.querySelector<HTMLElement>('div');
expect(tooltipCard.getAttribute('style')).toBe('width: 400px;'); expect(tooltipCard.getAttribute('style')).toBe('width: 400px;');
expect(text.textContent.trim()).toEqual('Sample text'); expect(text.textContent.trim()).toEqual('Sample text');
expect(img.getAttribute('src')).toEqual('/assets/testImg.png'); expect(img.getAttribute('src')).toEqual('/assets/testImg.png');
expect(img.getAttribute('width')).toEqual('400'); expect(img.getAttribute('width')).toEqual('400');
expect(div.innerHTML).toEqual('this is the <b>html</b> raw code');
}); });
it('should hide tooltip-card on mouse leave', () => { it('should hide tooltip-card on mouse leave', () => {

View File

@@ -25,6 +25,14 @@ export class TooltipCardDirective implements OnInit {
@Input('adf-tooltip-card') text = ''; @Input('adf-tooltip-card') text = '';
@Input() image = ''; @Input() image = '';
@Input() width = '300'; @Input() width = '300';
@Input() htmlContent = '';
@Input() originX: 'start' | 'center' | 'end' = 'start';
@Input() originY: 'top' | 'center' | 'bottom' = 'top';
@Input() overlayX: 'start' | 'center' | 'end' = 'start';
@Input() overlayY: 'top' | 'center' | 'bottom' = 'bottom';
@Input() offsetX = 0;
@Input() offsetY = -8;
private overlayRef: OverlayRef; private overlayRef: OverlayRef;
constructor( constructor(
@@ -37,11 +45,12 @@ export class TooltipCardDirective implements OnInit {
const positionStrategy = this.overlayPositionBuilder const positionStrategy = this.overlayPositionBuilder
.flexibleConnectedTo(this.elementRef) .flexibleConnectedTo(this.elementRef)
.withPositions([{ .withPositions([{
originX: 'start', originX: this.originX,
originY: 'top', originY: this.originY,
overlayX: 'start', overlayX: this.overlayX,
overlayY: 'bottom', overlayY: this.overlayY,
offsetY: -8 offsetY: this.offsetY,
offsetX: this.offsetX
}]); }]);
this.overlayRef = this.overlay.create({ positionStrategy }); this.overlayRef = this.overlay.create({ positionStrategy });
@@ -54,6 +63,7 @@ export class TooltipCardDirective implements OnInit {
tooltipRef.instance.text = this.text; tooltipRef.instance.text = this.text;
tooltipRef.instance.image = this.image; tooltipRef.instance.image = this.image;
tooltipRef.instance.width = this.width; tooltipRef.instance.width = this.width;
tooltipRef.instance.htmlContent = this.htmlContent;
} }
@HostListener('mouseout') @HostListener('mouseout')