-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
tooltip.ts
753 lines (642 loc) · 25.5 KB
/
tooltip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AnimationEvent} from '@angular/animations';
import {AriaDescriber, FocusMonitor} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {BooleanInput, coerceBooleanProperty, NumberInput} from '@angular/cdk/coercion';
import {ESCAPE, hasModifierKey} from '@angular/cdk/keycodes';
import {BreakpointObserver, Breakpoints, BreakpointState} from '@angular/cdk/layout';
import {
FlexibleConnectedPositionStrategy,
HorizontalConnectionPos,
OriginConnectionPosition,
Overlay,
OverlayConnectionPosition,
OverlayRef,
ScrollStrategy,
VerticalConnectionPos,
} from '@angular/cdk/overlay';
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {ComponentPortal} from '@angular/cdk/portal';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Directive,
ElementRef,
Inject,
InjectionToken,
Input,
NgZone,
OnDestroy,
OnInit,
Optional,
ViewContainerRef,
ViewEncapsulation,
} from '@angular/core';
import {Observable, Subject} from 'rxjs';
import {take, takeUntil} from 'rxjs/operators';
import {matTooltipAnimations} from './tooltip-animations';
/** Possible positions for a tooltip. */
export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | 'after';
/**
* Options for how the tooltip trigger should handle touch gestures.
* See `MatTooltip.touchGestures` for more information.
*/
export type TooltipTouchGestures = 'auto' | 'on' | 'off';
/** Possible visibility states of a tooltip. */
export type TooltipVisibility = 'initial' | 'visible' | 'hidden';
/** Time in ms to throttle repositioning after scroll events. */
export const SCROLL_THROTTLE_MS = 20;
/** CSS class that will be attached to the overlay panel. */
export const TOOLTIP_PANEL_CLASS = 'mat-tooltip-panel';
/** Options used to bind passive event listeners. */
const passiveListenerOptions = normalizePassiveListenerOptions({passive: true});
/**
* Time between the user putting the pointer on a tooltip
* trigger and the long press event being fired.
*/
const LONGPRESS_DELAY = 500;
/**
* Creates an error to be thrown if the user supplied an invalid tooltip position.
* @docs-private
*/
export function getMatTooltipInvalidPositionError(position: string) {
return Error(`Tooltip position "${position}" is invalid.`);
}
/** Injection token that determines the scroll handling while a tooltip is visible. */
export const MAT_TOOLTIP_SCROLL_STRATEGY =
new InjectionToken<() => ScrollStrategy>('mat-tooltip-scroll-strategy');
/** @docs-private */
export function MAT_TOOLTIP_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy {
return () => overlay.scrollStrategies.reposition({scrollThrottle: SCROLL_THROTTLE_MS});
}
/** @docs-private */
export const MAT_TOOLTIP_SCROLL_STRATEGY_FACTORY_PROVIDER = {
provide: MAT_TOOLTIP_SCROLL_STRATEGY,
deps: [Overlay],
useFactory: MAT_TOOLTIP_SCROLL_STRATEGY_FACTORY,
};
/** Default `matTooltip` options that can be overridden. */
export interface MatTooltipDefaultOptions {
showDelay: number;
hideDelay: number;
touchendHideDelay: number;
touchGestures?: TooltipTouchGestures;
position?: TooltipPosition;
}
/** Injection token to be used to override the default options for `matTooltip`. */
export const MAT_TOOLTIP_DEFAULT_OPTIONS =
new InjectionToken<MatTooltipDefaultOptions>('mat-tooltip-default-options', {
providedIn: 'root',
factory: MAT_TOOLTIP_DEFAULT_OPTIONS_FACTORY
});
/** @docs-private */
export function MAT_TOOLTIP_DEFAULT_OPTIONS_FACTORY(): MatTooltipDefaultOptions {
return {
showDelay: 0,
hideDelay: 0,
touchendHideDelay: 1500,
};
}
/**
* Directive that attaches a material design tooltip to the host element. Animates the showing and
* hiding of a tooltip provided position (defaults to below the element).
*
* https://material.io/design/components/tooltips.html
*/
@Directive({
selector: '[matTooltip]',
exportAs: 'matTooltip',
})
export class MatTooltip implements OnDestroy, OnInit {
_overlayRef: OverlayRef | null;
_tooltipInstance: TooltipComponent | null;
private _portal: ComponentPortal<TooltipComponent>;
private _position: TooltipPosition = 'below';
private _disabled: boolean = false;
private _tooltipClass: string|string[]|Set<string>|{[key: string]: any};
private _scrollStrategy: () => ScrollStrategy;
/** Allows the user to define the position of the tooltip relative to the parent element */
@Input('matTooltipPosition')
get position(): TooltipPosition { return this._position; }
set position(value: TooltipPosition) {
if (value !== this._position) {
this._position = value;
if (this._overlayRef) {
this._updatePosition();
if (this._tooltipInstance) {
this._tooltipInstance!.show(0);
}
this._overlayRef.updatePosition();
}
}
}
/** Disables the display of the tooltip. */
@Input('matTooltipDisabled')
get disabled(): boolean { return this._disabled; }
set disabled(value) {
this._disabled = coerceBooleanProperty(value);
// If tooltip is disabled, hide immediately.
if (this._disabled) {
this.hide(0);
}
}
/** The default delay in ms before showing the tooltip after show is called */
@Input('matTooltipShowDelay') showDelay: number = this._defaultOptions.showDelay;
/** The default delay in ms before hiding the tooltip after hide is called */
@Input('matTooltipHideDelay') hideDelay: number = this._defaultOptions.hideDelay;
/**
* How touch gestures should be handled by the tooltip. On touch devices the tooltip directive
* uses a long press gesture to show and hide, however it can conflict with the native browser
* gestures. To work around the conflict, Angular Material disables native gestures on the
* trigger, but that might not be desirable on particular elements (e.g. inputs and draggable
* elements). The different values for this option configure the touch event handling as follows:
* - `auto` - Enables touch gestures for all elements, but tries to avoid conflicts with native
* browser gestures on particular elements. In particular, it allows text selection on inputs
* and textareas, and preserves the native browser dragging on elements marked as `draggable`.
* - `on` - Enables touch gestures for all elements and disables native
* browser gestures with no exceptions.
* - `off` - Disables touch gestures. Note that this will prevent the tooltip from
* showing on touch devices.
*/
@Input('matTooltipTouchGestures') touchGestures: TooltipTouchGestures = 'auto';
/** The message to be displayed in the tooltip */
@Input('matTooltip')
get message() { return this._message; }
set message(value: string) {
this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this._message);
// If the message is not a string (e.g. number), convert it to a string and trim it.
this._message = value != null ? `${value}`.trim() : '';
if (!this._message && this._isTooltipVisible()) {
this.hide(0);
} else {
this._updateTooltipMessage();
this._ngZone.runOutsideAngular(() => {
// The `AriaDescriber` has some functionality that avoids adding a description if it's the
// same as the `aria-label` of an element, however we can't know whether the tooltip trigger
// has a data-bound `aria-label` or when it'll be set for the first time. We can avoid the
// issue by deferring the description by a tick so Angular has time to set the `aria-label`.
Promise.resolve().then(() => {
this._ariaDescriber.describe(this._elementRef.nativeElement, this.message);
});
});
}
}
private _message = '';
/** Classes to be passed to the tooltip. Supports the same syntax as `ngClass`. */
@Input('matTooltipClass')
get tooltipClass() { return this._tooltipClass; }
set tooltipClass(value: string|string[]|Set<string>|{[key: string]: any}) {
this._tooltipClass = value;
if (this._tooltipInstance) {
this._setTooltipClass(this._tooltipClass);
}
}
/** Manually-bound passive event listeners. */
private _passiveListeners = new Map<string, EventListenerOrEventListenerObject>();
/** Timer started at the last `touchstart` event. */
private _touchstartTimeout: number;
/** Emits when the component is destroyed. */
private readonly _destroyed = new Subject<void>();
constructor(
private _overlay: Overlay,
private _elementRef: ElementRef<HTMLElement>,
private _scrollDispatcher: ScrollDispatcher,
private _viewContainerRef: ViewContainerRef,
private _ngZone: NgZone,
private _platform: Platform,
private _ariaDescriber: AriaDescriber,
private _focusMonitor: FocusMonitor,
@Inject(MAT_TOOLTIP_SCROLL_STRATEGY) scrollStrategy: any,
@Optional() private _dir: Directionality,
@Optional() @Inject(MAT_TOOLTIP_DEFAULT_OPTIONS)
private _defaultOptions: MatTooltipDefaultOptions,
/**
* @deprecated _hammerLoader parameter to be removed.
* @breaking-change 9.0.0
*/
// Note that we need to give Angular something to inject here so it doesn't throw.
@Inject(ElementRef) _hammerLoader?: any) {
this._scrollStrategy = scrollStrategy;
if (_defaultOptions) {
if (_defaultOptions.position) {
this.position = _defaultOptions.position;
}
if (_defaultOptions.touchGestures) {
this.touchGestures = _defaultOptions.touchGestures;
}
}
_focusMonitor.monitor(_elementRef)
.pipe(takeUntil(this._destroyed))
.subscribe(origin => {
// Note that the focus monitor runs outside the Angular zone.
if (!origin) {
_ngZone.run(() => this.hide(0));
} else if (origin === 'keyboard') {
_ngZone.run(() => this.show());
}
});
_ngZone.runOutsideAngular(() => {
_elementRef.nativeElement.addEventListener('keydown', this._handleKeydown);
});
}
/**
* Setup styling-specific things
*/
ngOnInit() {
// This needs to happen in `ngOnInit` so the initial values for all inputs have been set.
this._setupPointerEvents();
}
/**
* Dispose the tooltip when destroyed.
*/
ngOnDestroy() {
const nativeElement = this._elementRef.nativeElement;
clearTimeout(this._touchstartTimeout);
if (this._overlayRef) {
this._overlayRef.dispose();
this._tooltipInstance = null;
}
// Clean up the event listeners set in the constructor
nativeElement.removeEventListener('keydown', this._handleKeydown);
this._passiveListeners.forEach((listener, event) => {
nativeElement.removeEventListener(event, listener, passiveListenerOptions);
});
this._passiveListeners.clear();
this._destroyed.next();
this._destroyed.complete();
this._ariaDescriber.removeDescription(nativeElement, this.message);
this._focusMonitor.stopMonitoring(nativeElement);
}
/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
show(delay: number = this.showDelay): void {
if (this.disabled || !this.message || (this._isTooltipVisible() &&
!this._tooltipInstance!._showTimeoutId && !this._tooltipInstance!._hideTimeoutId)) {
return;
}
const overlayRef = this._createOverlay();
this._detach();
this._portal = this._portal || new ComponentPortal(TooltipComponent, this._viewContainerRef);
this._tooltipInstance = overlayRef.attach(this._portal).instance;
this._tooltipInstance.afterHidden()
.pipe(takeUntil(this._destroyed))
.subscribe(() => this._detach());
this._setTooltipClass(this._tooltipClass);
this._updateTooltipMessage();
this._tooltipInstance!.show(delay);
}
/** Hides the tooltip after the delay in ms, defaults to tooltip-delay-hide or 0ms if no input */
hide(delay: number = this.hideDelay): void {
if (this._tooltipInstance) {
this._tooltipInstance.hide(delay);
}
}
/** Shows/hides the tooltip */
toggle(): void {
this._isTooltipVisible() ? this.hide() : this.show();
}
/** Returns true if the tooltip is currently visible to the user */
_isTooltipVisible(): boolean {
return !!this._tooltipInstance && this._tooltipInstance.isVisible();
}
/**
* Handles the keydown events on the host element.
* Needs to be an arrow function so that we can use it in addEventListener.
*/
private _handleKeydown = (event: KeyboardEvent) => {
if (this._isTooltipVisible() && event.keyCode === ESCAPE && !hasModifierKey(event)) {
event.preventDefault();
event.stopPropagation();
this._ngZone.run(() => this.hide(0));
}
}
/** Create the overlay config and position strategy */
private _createOverlay(): OverlayRef {
if (this._overlayRef) {
return this._overlayRef;
}
const scrollableAncestors =
this._scrollDispatcher.getAncestorScrollContainers(this._elementRef);
// Create connected position strategy that listens for scroll events to reposition.
const strategy = this._overlay.position()
.flexibleConnectedTo(this._elementRef)
.withTransformOriginOn('.mat-tooltip')
.withFlexibleDimensions(false)
.withViewportMargin(8)
.withScrollableContainers(scrollableAncestors);
strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => {
if (this._tooltipInstance) {
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
// After position changes occur and the overlay is clipped by
// a parent scrollable then close the tooltip.
this._ngZone.run(() => this.hide(0));
}
}
});
this._overlayRef = this._overlay.create({
direction: this._dir,
positionStrategy: strategy,
panelClass: TOOLTIP_PANEL_CLASS,
scrollStrategy: this._scrollStrategy()
});
this._updatePosition();
this._overlayRef.detachments()
.pipe(takeUntil(this._destroyed))
.subscribe(() => this._detach());
return this._overlayRef;
}
/** Detaches the currently-attached tooltip. */
private _detach() {
if (this._overlayRef && this._overlayRef.hasAttached()) {
this._overlayRef.detach();
}
this._tooltipInstance = null;
}
/** Updates the position of the current tooltip. */
private _updatePosition() {
const position =
this._overlayRef!.getConfig().positionStrategy as FlexibleConnectedPositionStrategy;
const origin = this._getOrigin();
const overlay = this._getOverlayPosition();
position.withPositions([
{...origin.main, ...overlay.main},
{...origin.fallback, ...overlay.fallback}
]);
}
/**
* Returns the origin position and a fallback position based on the user's position preference.
* The fallback position is the inverse of the origin (e.g. `'below' -> 'above'`).
*/
_getOrigin(): {main: OriginConnectionPosition, fallback: OriginConnectionPosition} {
const isLtr = !this._dir || this._dir.value == 'ltr';
const position = this.position;
let originPosition: OriginConnectionPosition;
if (position == 'above' || position == 'below') {
originPosition = {originX: 'center', originY: position == 'above' ? 'top' : 'bottom'};
} else if (
position == 'before' ||
(position == 'left' && isLtr) ||
(position == 'right' && !isLtr)) {
originPosition = {originX: 'start', originY: 'center'};
} else if (
position == 'after' ||
(position == 'right' && isLtr) ||
(position == 'left' && !isLtr)) {
originPosition = {originX: 'end', originY: 'center'};
} else {
throw getMatTooltipInvalidPositionError(position);
}
const {x, y} = this._invertPosition(originPosition.originX, originPosition.originY);
return {
main: originPosition,
fallback: {originX: x, originY: y}
};
}
/** Returns the overlay position and a fallback position based on the user's preference */
_getOverlayPosition(): {main: OverlayConnectionPosition, fallback: OverlayConnectionPosition} {
const isLtr = !this._dir || this._dir.value == 'ltr';
const position = this.position;
let overlayPosition: OverlayConnectionPosition;
if (position == 'above') {
overlayPosition = {overlayX: 'center', overlayY: 'bottom'};
} else if (position == 'below') {
overlayPosition = {overlayX: 'center', overlayY: 'top'};
} else if (
position == 'before' ||
(position == 'left' && isLtr) ||
(position == 'right' && !isLtr)) {
overlayPosition = {overlayX: 'end', overlayY: 'center'};
} else if (
position == 'after' ||
(position == 'right' && isLtr) ||
(position == 'left' && !isLtr)) {
overlayPosition = {overlayX: 'start', overlayY: 'center'};
} else {
throw getMatTooltipInvalidPositionError(position);
}
const {x, y} = this._invertPosition(overlayPosition.overlayX, overlayPosition.overlayY);
return {
main: overlayPosition,
fallback: {overlayX: x, overlayY: y}
};
}
/** Updates the tooltip message and repositions the overlay according to the new message length */
private _updateTooltipMessage() {
// Must wait for the message to be painted to the tooltip so that the overlay can properly
// calculate the correct positioning based on the size of the text.
if (this._tooltipInstance) {
this._tooltipInstance.message = this.message;
this._tooltipInstance._markForCheck();
this._ngZone.onMicrotaskEmpty.asObservable().pipe(
take(1),
takeUntil(this._destroyed)
).subscribe(() => {
if (this._tooltipInstance) {
this._overlayRef!.updatePosition();
}
});
}
}
/** Updates the tooltip class */
private _setTooltipClass(tooltipClass: string|string[]|Set<string>|{[key: string]: any}) {
if (this._tooltipInstance) {
this._tooltipInstance.tooltipClass = tooltipClass;
this._tooltipInstance._markForCheck();
}
}
/** Inverts an overlay position. */
private _invertPosition(x: HorizontalConnectionPos, y: VerticalConnectionPos) {
if (this.position === 'above' || this.position === 'below') {
if (y === 'top') {
y = 'bottom';
} else if (y === 'bottom') {
y = 'top';
}
} else {
if (x === 'end') {
x = 'start';
} else if (x === 'start') {
x = 'end';
}
}
return {x, y};
}
/** Binds the pointer events to the tooltip trigger. */
private _setupPointerEvents() {
// The mouse events shouldn't be bound on mobile devices, because they can prevent the
// first tap from firing its click event or can cause the tooltip to open for clicks.
if (!this._platform.IOS && !this._platform.ANDROID) {
this._passiveListeners
.set('mouseenter', () => this.show())
.set('mouseleave', () => this.hide());
} else if (this.touchGestures !== 'off') {
this._disableNativeGesturesIfNecessary();
const touchendListener = () => {
clearTimeout(this._touchstartTimeout);
this.hide(this._defaultOptions.touchendHideDelay);
};
this._passiveListeners
.set('touchend', touchendListener)
.set('touchcancel', touchendListener)
.set('touchstart', () => {
// Note that it's important that we don't `preventDefault` here,
// because it can prevent click events from firing on the element.
clearTimeout(this._touchstartTimeout);
this._touchstartTimeout = setTimeout(() => this.show(), LONGPRESS_DELAY);
});
}
this._passiveListeners.forEach((listener, event) => {
this._elementRef.nativeElement.addEventListener(event, listener, passiveListenerOptions);
});
}
/** Disables the native browser gestures, based on how the tooltip has been configured. */
private _disableNativeGesturesIfNecessary() {
const element = this._elementRef.nativeElement;
const style = element.style;
const gestures = this.touchGestures;
if (gestures !== 'off') {
// If gestures are set to `auto`, we don't disable text selection on inputs and
// textareas, because it prevents the user from typing into them on iOS Safari.
if (gestures === 'on' || (element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA')) {
style.userSelect = style.msUserSelect = style.webkitUserSelect =
(style as any).MozUserSelect = 'none';
}
// If we have `auto` gestures and the element uses native HTML dragging,
// we don't set `-webkit-user-drag` because it prevents the native behavior.
if (gestures === 'on' || !element.draggable) {
(style as any).webkitUserDrag = 'none';
}
style.touchAction = 'none';
style.webkitTapHighlightColor = 'transparent';
}
}
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_hideDelay: NumberInput;
static ngAcceptInputType_showDelay: NumberInput;
}
/**
* Internal component that wraps the tooltip's content.
* @docs-private
*/
@Component({
selector: 'mat-tooltip-component',
templateUrl: 'tooltip.html',
styleUrls: ['tooltip.css'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [matTooltipAnimations.tooltipState],
host: {
// Forces the element to have a layout in IE and Edge. This fixes issues where the element
// won't be rendered if the animations are disabled or there is no web animations polyfill.
'[style.zoom]': '_visibility === "visible" ? 1 : null',
'(body:click)': 'this._handleBodyInteraction()',
'aria-hidden': 'true',
}
})
export class TooltipComponent implements OnDestroy {
/** Message to display in the tooltip */
message: string;
/** Classes to be added to the tooltip. Supports the same syntax as `ngClass`. */
tooltipClass: string|string[]|Set<string>|{[key: string]: any};
/** The timeout ID of any current timer set to show the tooltip */
_showTimeoutId: number | null;
/** The timeout ID of any current timer set to hide the tooltip */
_hideTimeoutId: number | null;
/** Property watched by the animation framework to show or hide the tooltip */
_visibility: TooltipVisibility = 'initial';
/** Whether interactions on the page should close the tooltip */
private _closeOnInteraction: boolean = false;
/** Subject for notifying that the tooltip has been hidden from the view */
private readonly _onHide: Subject<any> = new Subject();
/** Stream that emits whether the user has a handset-sized display. */
_isHandset: Observable<BreakpointState> = this._breakpointObserver.observe(Breakpoints.Handset);
constructor(
private _changeDetectorRef: ChangeDetectorRef,
private _breakpointObserver: BreakpointObserver) {}
/**
* Shows the tooltip with an animation originating from the provided origin
* @param delay Amount of milliseconds to the delay showing the tooltip.
*/
show(delay: number): void {
// Cancel the delayed hide if it is scheduled
if (this._hideTimeoutId) {
clearTimeout(this._hideTimeoutId);
this._hideTimeoutId = null;
}
// Body interactions should cancel the tooltip if there is a delay in showing.
this._closeOnInteraction = true;
this._showTimeoutId = setTimeout(() => {
this._visibility = 'visible';
this._showTimeoutId = null;
// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
this._markForCheck();
}, delay);
}
/**
* Begins the animation to hide the tooltip after the provided delay in ms.
* @param delay Amount of milliseconds to delay showing the tooltip.
*/
hide(delay: number): void {
// Cancel the delayed show if it is scheduled
if (this._showTimeoutId) {
clearTimeout(this._showTimeoutId);
this._showTimeoutId = null;
}
this._hideTimeoutId = setTimeout(() => {
this._visibility = 'hidden';
this._hideTimeoutId = null;
// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
this._markForCheck();
}, delay);
}
/** Returns an observable that notifies when the tooltip has been hidden from view. */
afterHidden(): Observable<void> {
return this._onHide.asObservable();
}
/** Whether the tooltip is being displayed. */
isVisible(): boolean {
return this._visibility === 'visible';
}
ngOnDestroy() {
this._onHide.complete();
}
_animationStart() {
this._closeOnInteraction = false;
}
_animationDone(event: AnimationEvent): void {
const toState = event.toState as TooltipVisibility;
if (toState === 'hidden' && !this.isVisible()) {
this._onHide.next();
}
if (toState === 'visible' || toState === 'hidden') {
this._closeOnInteraction = true;
}
}
/**
* Interactions on the HTML body should close the tooltip immediately as defined in the
* material design spec.
* https://material.io/design/components/tooltips.html#behavior
*/
_handleBodyInteraction(): void {
if (this._closeOnInteraction) {
this.hide(0);
}
}
/**
* Marks that the tooltip needs to be checked in the next change detection run.
* Mainly used for rendering the initial text before positioning a tooltip, which
* can be problematic in components with OnPush change detection.
*/
_markForCheck(): void {
this._changeDetectorRef.markForCheck();
}
}