Skip to content

Commit a1a54df

Browse files
IlyaSurmayvalorkin
authored andcommitted
fix(tooltip): fix delay (#3463)
fixes #3038
1 parent bf6361e commit a1a54df

File tree

6 files changed

+200
-11
lines changed

6 files changed

+200
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<button type="button" class="btn btn-primary"
2+
tooltip="Vivamus sagittis lacus vel augue laoreet rutrum faucibus." [delay]="500">
3+
Tooltip with 0.5sec delay
4+
</button>
5+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'demo-tooltip-delay',
5+
templateUrl: './delay.html'
6+
})
7+
export class DemoTooltipDelayComponent {}

demo/src/app/components/+tooltip/demos/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DemoTooltipTriggersCustomComponent } from './triggers-custom/triggers-c
1010
import { DemoTooltipTriggersManualComponent } from './triggers-manual/triggers-manual';
1111
import { DemoTooltipDynamicHtmlComponent } from './dynamic-html/dynamic-html';
1212
import { DemoTooltipClassComponent } from './class/class';
13+
import { DemoTooltipDelayComponent } from './delay/delay';
1314

1415
export const DEMO_COMPONENTS = [
1516
DemoTooltipBasicComponent,
@@ -23,5 +24,6 @@ export const DEMO_COMPONENTS = [
2324
DemoTooltipStylingLocalComponent,
2425
DemoTooltipTriggersCustomComponent,
2526
DemoTooltipTriggersManualComponent,
26-
DemoTooltipClassComponent
27+
DemoTooltipClassComponent,
28+
DemoTooltipDelayComponent
2729
];

demo/src/app/components/+tooltip/tooltip-section.list.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
NgApiDocComponent,
2121
NgApiDocConfigComponent
2222
} from '../../docs/api-docs';
23+
import { DemoTooltipDelayComponent } from './demos/delay/delay';
2324

2425
export const demoComponentContent: ContentSection[] = [
2526
{
@@ -131,6 +132,13 @@ export const demoComponentContent: ContentSection[] = [
131132
component: require('!!raw-loader?lang=typescript!./demos/class/class.ts'),
132133
html: require('!!raw-loader?lang=markup!./demos/class/class.html'),
133134
outlet: DemoTooltipClassComponent
135+
},
136+
{
137+
title: 'Tooltip with delay',
138+
anchor: 'tooltip-delay',
139+
component: require('!!raw-loader?lang=typescript!./demos/delay/delay.ts'),
140+
html: require('!!raw-loader?lang=markup!./demos/delay/delay.html'),
141+
outlet: DemoTooltipDelayComponent
134142
}
135143
]
136144
},

demo/src/ng-api-doc.ts

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,101 @@ export const ngdoc: any = {
142142
}
143143
]
144144
},
145+
"DateParsingConfig": {
146+
"fileName": "src/bs-moment/create/parsing.types.ts",
147+
"className": "DateParsingConfig",
148+
"description": "",
149+
"methods": [],
150+
"properties": [
151+
{
152+
"name": "_a",
153+
"type": "number[]",
154+
"description": "<p>DateArray [year, month, date, .....] </p>\n"
155+
},
156+
{
157+
"name": "_changeInProgress",
158+
"type": "boolean",
159+
"description": "<p>used in set offset </p>\n"
160+
},
161+
{
162+
"name": "_d",
163+
"type": "Date",
164+
"description": "<p>date value </p>\n"
165+
},
166+
{
167+
"name": "_f",
168+
"type": "string | string[]",
169+
"description": "<p>date format </p>\n"
170+
},
171+
{
172+
"name": "_i",
173+
"type": "DateInput",
174+
"description": "<p>input to parse: could be string, number[], number, Date, object </p>\n"
175+
},
176+
{
177+
"name": "_isPm",
178+
"type": "boolean",
179+
"description": "<p>is PM </p>\n"
180+
},
181+
{
182+
"name": "_isValid",
183+
"type": "boolean",
184+
"description": "<p>is valid </p>\n"
185+
},
186+
{
187+
"name": "_l",
188+
"type": "string",
189+
"description": "<p>locale key, &#39;en&#39; by default </p>\n"
190+
},
191+
{
192+
"name": "_locale",
193+
"type": "Locale",
194+
"description": "<p>date locale obj </p>\n"
195+
},
196+
{
197+
"name": "_meridiem",
198+
"type": "string",
199+
"description": "<p>date meridiem </p>\n"
200+
},
201+
{
202+
"name": "_nextDay",
203+
"type": "boolean",
204+
"description": "<p>add one day to result at the end of parsing </p>\n"
205+
},
206+
{
207+
"name": "_offset",
208+
"type": "number",
209+
"description": "<p>utc time offset </p>\n"
210+
},
211+
{
212+
"name": "_pf",
213+
"type": "DateParsingFlags",
214+
"description": "<p>date parsing flags </p>\n"
215+
},
216+
{
217+
"name": "_strict",
218+
"type": "boolean",
219+
"description": "<p>use strict parse format </p>\n"
220+
},
221+
{
222+
"name": "_tzm",
223+
"type": "number",
224+
"description": "<p>time zone </p>\n"
225+
},
226+
{
227+
"name": "_w",
228+
"type": "WeekParsing",
229+
"description": "<p>date specific info \nweek </p>\n"
230+
}
231+
]
232+
},
233+
"DateParsingFlags": {
234+
"fileName": "src/bs-moment/create/parsing.types.ts",
235+
"className": "DateParsingFlags",
236+
"description": "",
237+
"methods": [],
238+
"properties": []
239+
},
145240
"LocaleOptionsFormat": {
146241
"fileName": "src/chronos/locale/locale.class.ts",
147242
"className": "LocaleOptionsFormat",
@@ -156,13 +251,55 @@ export const ngdoc: any = {
156251
"methods": [],
157252
"properties": []
158253
},
254+
"CalendarSpec": {
255+
"fileName": "src/bs-moment/moment/calendar.ts",
256+
"className": "CalendarSpec",
257+
"description": "",
258+
"methods": [],
259+
"properties": []
260+
},
261+
"MomentFn": {
262+
"fileName": "src/bs-moment/test/chain.ts",
263+
"className": "MomentFn",
264+
"description": "",
265+
"methods": [],
266+
"properties": []
267+
},
268+
"MomentInputObject": {
269+
"fileName": "src/bs-moment/test/chain.ts",
270+
"className": "MomentInputObject",
271+
"description": "",
272+
"methods": [],
273+
"properties": []
274+
},
159275
"TimeUnit": {
160276
"fileName": "src/chronos/types.ts",
161277
"className": "TimeUnit",
162278
"description": "",
163279
"methods": [],
164280
"properties": []
165281
},
282+
"DateFormatterOptions": {
283+
"fileName": "src/bs-moment/types.ts",
284+
"className": "DateFormatterOptions",
285+
"description": "",
286+
"methods": [],
287+
"properties": []
288+
},
289+
"DateObject": {
290+
"fileName": "src/bs-moment/types.ts",
291+
"className": "DateObject",
292+
"description": "",
293+
"methods": [],
294+
"properties": []
295+
},
296+
"WeekParsing": {
297+
"fileName": "src/bs-moment/types.ts",
298+
"className": "WeekParsing",
299+
"description": "",
300+
"methods": [],
301+
"properties": []
302+
},
166303
"ButtonCheckboxDirective": {
167304
"fileName": "src/buttons/button-checkbox.directive.ts",
168305
"className": "ButtonCheckboxDirective",
@@ -2805,6 +2942,11 @@ export const ngdoc: any = {
28052942
"type": "string",
28062943
"description": "<p>Css class for tooltip container</p>\n"
28072944
},
2945+
{
2946+
"name": "delay",
2947+
"type": "number",
2948+
"description": "<p>Delay before showing the tooltip</p>\n"
2949+
},
28082950
{
28092951
"name": "isDisabled",
28102952
"type": "boolean",
@@ -2874,7 +3016,6 @@ export const ngdoc: any = {
28743016
},
28753017
{
28763018
"name": "tooltipPopupDelay",
2877-
"defaultValue": "0",
28783019
"type": "number",
28793020
"description": ""
28803021
},

src/tooltip/tooltip.directive.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { TooltipConfig } from './tooltip.config';
88
import { ComponentLoader, ComponentLoaderFactory } from '../component-loader/index';
99
import { OnChange } from '../utils/decorators';
1010
import { warnOnce } from '../utils/warn-once';
11+
import { parseTriggers } from '../utils/triggers';
12+
import { Observable } from 'rxjs/Observable';
13+
import 'rxjs/add/observable/timer';
1114

1215
@Directive({
1316
selector: '[tooltip], [tooltipHtml]',
@@ -64,6 +67,10 @@ export class TooltipDirective implements OnInit, OnDestroy {
6467
* Css class for tooltip container
6568
*/
6669
@Input() containerClass = '';
70+
/**
71+
* Delay before showing the tooltip
72+
*/
73+
@Input() delay: number;
6774

6875
/**
6976
* Emits an event when the tooltip is shown
@@ -144,7 +151,11 @@ export class TooltipDirective implements OnInit, OnDestroy {
144151
}
145152

146153
/** @deprecated */
147-
@Input('tooltipPopupDelay') _delay = 0;
154+
@Input('tooltipPopupDelay')
155+
set _tooltipPopupDelay(value: number) {
156+
warnOnce('tooltipPopupDelay is deprecated, use `delay` instead');
157+
this.delay = value;
158+
}
148159

149160
/** @deprecated */
150161
@Input('tooltipFadeDuration') _fadeDuration = 150;
@@ -165,19 +176,20 @@ export class TooltipDirective implements OnInit, OnDestroy {
165176
@Output()
166177
tooltipStateChanged: EventEmitter<boolean> = new EventEmitter<boolean>();
167178
protected _delayTimeoutId: number | any;
179+
protected _tooltipCancelShowFn: Function;
168180

169181
private _tooltip: ComponentLoader<TooltipContainerComponent>;
170182

171183
constructor(_viewContainerRef: ViewContainerRef,
172-
_renderer: Renderer2,
173-
_elementRef: ElementRef,
184+
private _renderer: Renderer2,
185+
private _elementRef: ElementRef,
174186
cis: ComponentLoaderFactory,
175187
config: TooltipConfig) {
176188
this._tooltip = cis
177189
.createLoader<TooltipContainerComponent>(
178-
_elementRef,
190+
this._elementRef,
179191
_viewContainerRef,
180-
_renderer
192+
this._renderer
181193
)
182194
.provide({provide: TooltipConfig, useValue: config});
183195

@@ -238,12 +250,26 @@ export class TooltipDirective implements OnInit, OnDestroy {
238250
placement: this.placement,
239251
containerClass: this.containerClass
240252
});
241-
}
253+
};
254+
const cancelDelayedTooltipShowing = () => {
255+
if (this._tooltipCancelShowFn) {
256+
this._tooltipCancelShowFn();
257+
}
258+
};
242259

243-
if (this._delay) {
244-
this._delayTimeoutId = setTimeout(() => {
260+
if (this.delay) {
261+
const timer = Observable.timer(this.delay).subscribe(() => {
245262
showTooltip();
246-
}, this._delay);
263+
cancelDelayedTooltipShowing();
264+
});
265+
266+
if (this.triggers) {
267+
const triggers = parseTriggers(this.triggers);
268+
this._tooltipCancelShowFn = this._renderer.listen(this._elementRef.nativeElement, triggers[0].close, () => {
269+
timer.unsubscribe();
270+
cancelDelayedTooltipShowing();
271+
});
272+
}
247273
} else {
248274
showTooltip();
249275
}

0 commit comments

Comments
 (0)