Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit f5ff12c

Browse files
iamfozzywesleycho
authored andcommitted
fix(tooltip): close tooltip on esc
- On keypress of escape, close tooltip/popover Closes #6177 Fixes #6108
1 parent f147d22 commit f5ff12c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/tooltip/test/tooltip.spec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('tooltip', function() {
2727
}));
2828

2929
afterEach(function() {
30-
$document.off('keypress');
30+
$document.off('keyup');
3131
});
3232

3333
function trigger(element, evt) {
@@ -295,10 +295,9 @@ describe('tooltip', function() {
295295
trigger(elm, 'mouseenter');
296296
expect(tooltipScope.isOpen).toBe(false);
297297

298-
$timeout.flush(500);
299-
expect(tooltipScope.isOpen).toBe(false);
300298
elmScope.disabled = true;
301299
elmScope.$digest();
300+
$timeout.flush(500);
302301

303302
expect(tooltipScope.isOpen).toBe(false);
304303
});
@@ -343,22 +342,24 @@ describe('tooltip', function() {
343342
expect(tooltipScope.isOpen).toBe(true);
344343
expect(tooltipScope2.isOpen).toBe(true);
345344

346-
var evt = $.Event('keypress');
345+
var evt = $.Event('keyup');
347346
evt.which = 27;
348347

349348
$document.trigger(evt);
350349
tooltipScope.$digest();
351350
tooltipScope2.$digest();
351+
$timeout.flush();
352352

353353
expect(tooltipScope.isOpen).toBe(true);
354354
expect(tooltipScope2.isOpen).toBe(false);
355355

356-
var evt2 = $.Event('keypress');
356+
var evt2 = $.Event('keyup');
357357
evt2.which = 27;
358358

359359
$document.trigger(evt2);
360360
tooltipScope.$digest();
361361
tooltipScope2.$digest();
362+
$timeout.flush(500);
362363

363364
expect(tooltipScope.isOpen).toBe(false);
364365
expect(tooltipScope2.isOpen).toBe(false);

src/tooltip/tooltip.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,17 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
7171
*/
7272
this.$get = ['$window', '$compile', '$timeout', '$document', '$uibPosition', '$interpolate', '$rootScope', '$parse', '$$stackedMap', function($window, $compile, $timeout, $document, $position, $interpolate, $rootScope, $parse, $$stackedMap) {
7373
var openedTooltips = $$stackedMap.createNew();
74-
$document.on('keypress', keypressListener);
74+
$document.on('keyup', keypressListener);
7575

7676
$rootScope.$on('$destroy', function() {
77-
$document.off('keypress', keypressListener);
77+
$document.off('keyup', keypressListener);
7878
});
7979

8080
function keypressListener(e) {
8181
if (e.which === 27) {
8282
var last = openedTooltips.top();
8383
if (last) {
8484
last.value.close();
85-
openedTooltips.removeTop();
8685
last = null;
8786
}
8887
}
@@ -207,9 +206,6 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
207206
// By default, the tooltip is not open.
208207
// TODO add ability to start tooltip opened
209208
ttScope.isOpen = false;
210-
openedTooltips.add(ttScope, {
211-
close: hide
212-
});
213209

214210
function toggleTooltipBind() {
215211
if (!ttScope.isOpen) {
@@ -336,6 +332,10 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
336332
}
337333
});
338334

335+
openedTooltips.add(ttScope, {
336+
close: hide
337+
});
338+
339339
prepObservers();
340340
}
341341

@@ -348,6 +348,9 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
348348
tooltip.remove();
349349
tooltip = null;
350350
}
351+
352+
openedTooltips.remove(ttScope);
353+
351354
if (tooltipLinkedScope) {
352355
tooltipLinkedScope.$destroy();
353356
tooltipLinkedScope = null;
@@ -563,7 +566,6 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
563566
scope.$on('$destroy', function onDestroyTooltip() {
564567
unregisterTriggers();
565568
removeTooltip();
566-
openedTooltips.remove(ttScope);
567569
ttScope = null;
568570
});
569571
};

0 commit comments

Comments
 (0)