AAE-3936 Tooltip card directive (#6330)

* AAE-3936 Tooltip card directive

* AAE-3936 Fix styles
This commit is contained in:
Pablo Martinez Garcia
2020-11-10 11:06:26 +01:00
committed by GitHub
parent 7eda1bcad5
commit a7fb195874
8 changed files with 256 additions and 3 deletions

View File

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

View File

@@ -0,0 +1,39 @@
@import '~@angular/material/theming';
@mixin adf-tooltip-card-directive($theme) {
$primary: map-get($theme, primary);
$accent: map-get($theme, accent);
$foreground: map-get($theme, foreground);
$background: map-get($theme, background);
:host {
display: block;
}
div.adf-tooltip-card {
@include mat-elevation(8);
background-color: mat-color($background, card);
border: 1px solid mat-color($primary);
padding: 12px;
border-radius: 6px;
p {
font-size: mat-font-size($alfresco-typography, caption);
color: mat-color($foreground, text, 0.75);
margin: 0;
}
hr {
border: 1px solid mat-color($primary);
margin: 6px 0;
}
img {
border-radius: 6px;
}
}
::ng-deep .cdk-overlay-connected-position-bounding-box {
margin-top: 10px !important;
}
}

View File

@@ -0,0 +1,41 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Input } from '@angular/core';
import { animate, style, transition, trigger } from '@angular/animations';
@Component({
selector: 'adf-tooltip-card-component',
styleUrls: ['./tooltip-card.component.scss'],
templateUrl: './tooltip-card.component.html',
animations: [
trigger('tooltip', [
transition(':enter', [
style({ opacity: 0 }),
animate(500, style({ opacity: 1 }))
]),
transition(':leave', [
animate(500, style({ opacity: 0 }))
])
])
]
})
export class TooltipCardComponent {
@Input() image = '';
@Input() text = '';
@Input() width = '300';
}

View File

@@ -0,0 +1,97 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, ElementRef, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TooltipCardDirective } from './tooltip-card.directive';
import { CommonModule } from '@angular/common';
import { OverlayContainer, OverlayModule } from '@angular/cdk/overlay';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { By } from '@angular/platform-browser';
import { TooltipCardComponent } from './tooltip-card.component';
@Component({
template: `<span #span [adf-tooltip-card]="'Sample text'" [image]="'/assets/testImg.png'" [width]="'400'" class="test-component"></span>`
})
class TestComponent {
@ViewChild(TooltipCardDirective, { static: true })
public directive: TooltipCardDirective;
@ViewChild('span', { static: true })
public span: ElementRef;
}
describe('TooltipCardDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let overlay: HTMLElement;
let overlayContainer: OverlayContainer;
beforeEach((() => {
TestBed.configureTestingModule({
imports: [
CommonModule,
OverlayModule,
NoopAnimationsModule
],
declarations: [
TooltipCardDirective,
TooltipCardComponent,
TestComponent
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
overlayContainer = TestBed.inject(OverlayContainer);
overlay = overlayContainer.getContainerElement();
});
afterEach(() => {
TestBed.resetTestingModule();
});
it('should display tooltip-card on mouse enter', () => {
let tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card');
expect(tooltipCard).toBeNull();
const span = fixture.debugElement.query(By.css('span.test-component'));
span.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card');
expect(tooltipCard).not.toBeNull();
const text = overlay.querySelector<HTMLElement>('div.adf-tooltip-card p');
const img = overlay.querySelector<HTMLElement>('div.adf-tooltip-card img');
expect(tooltipCard.getAttribute('style')).toBe('width: 400px;');
expect(text.textContent.trim()).toEqual('Sample text');
expect(img.getAttribute('src')).toEqual('/assets/testImg.png');
expect(img.getAttribute('width')).toEqual('400');
});
it('should hide tooltip-card on mouse leave', () => {
const span = fixture.debugElement.query(By.css('span.test-component'));
span.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
let tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card');
expect(tooltipCard).not.toBeNull();
span.triggerEventHandler('mouseout', {});
fixture.detectChanges();
tooltipCard = overlay.querySelector<HTMLElement>('div.adf-tooltip-card');
expect(tooltipCard).toBeNull();
});
});

View File

@@ -0,0 +1,63 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ComponentRef, Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
import { Overlay, OverlayPositionBuilder, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { TooltipCardComponent } from './tooltip-card.component';
@Directive({ selector: '[adf-tooltip-card]' })
export class TooltipCardDirective implements OnInit {
@Input('adf-tooltip-card') text = '';
@Input() image = '';
@Input() width = '300';
private overlayRef: OverlayRef;
constructor(
private overlay: Overlay,
private overlayPositionBuilder: OverlayPositionBuilder,
private elementRef: ElementRef) {
}
ngOnInit(): void {
const positionStrategy = this.overlayPositionBuilder
.flexibleConnectedTo(this.elementRef)
.withPositions([{
originX: 'start',
originY: 'top',
overlayX: 'start',
overlayY: 'bottom',
offsetY: -8
}]);
this.overlayRef = this.overlay.create({ positionStrategy });
}
@HostListener('mouseenter')
show() {
const tooltipRef: ComponentRef<TooltipCardComponent>
= this.overlayRef.attach(new ComponentPortal(TooltipCardComponent));
tooltipRef.instance.text = this.text;
tooltipRef.instance.image = this.image;
tooltipRef.instance.width = this.width;
}
@HostListener('mouseout')
hide() {
this.overlayRef.detach();
}
}