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

Commit

Permalink
fix(tooltip): close tooltip on esc
Browse files Browse the repository at this point in the history
- On keypress of escape, close tooltip/popover

Closes #6177
Fixes #6108
  • Loading branch information
iamfozzy authored and wesleycho committed Aug 18, 2016
1 parent f147d22 commit f5ff12c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('tooltip', function() {
}));

afterEach(function() {
$document.off('keypress');
$document.off('keyup');
});

function trigger(element, evt) {
Expand Down Expand Up @@ -295,10 +295,9 @@ describe('tooltip', function() {
trigger(elm, 'mouseenter');
expect(tooltipScope.isOpen).toBe(false);

$timeout.flush(500);
expect(tooltipScope.isOpen).toBe(false);
elmScope.disabled = true;
elmScope.$digest();
$timeout.flush(500);

expect(tooltipScope.isOpen).toBe(false);
});
Expand Down Expand Up @@ -343,22 +342,24 @@ describe('tooltip', function() {
expect(tooltipScope.isOpen).toBe(true);
expect(tooltipScope2.isOpen).toBe(true);

var evt = $.Event('keypress');
var evt = $.Event('keyup');
evt.which = 27;

$document.trigger(evt);
tooltipScope.$digest();
tooltipScope2.$digest();
$timeout.flush();

expect(tooltipScope.isOpen).toBe(true);
expect(tooltipScope2.isOpen).toBe(false);

var evt2 = $.Event('keypress');
var evt2 = $.Event('keyup');
evt2.which = 27;

$document.trigger(evt2);
tooltipScope.$digest();
tooltipScope2.$digest();
$timeout.flush(500);

expect(tooltipScope.isOpen).toBe(false);
expect(tooltipScope2.isOpen).toBe(false);
Expand Down
16 changes: 9 additions & 7 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
*/
this.$get = ['$window', '$compile', '$timeout', '$document', '$uibPosition', '$interpolate', '$rootScope', '$parse', '$$stackedMap', function($window, $compile, $timeout, $document, $position, $interpolate, $rootScope, $parse, $$stackedMap) {
var openedTooltips = $$stackedMap.createNew();
$document.on('keypress', keypressListener);
$document.on('keyup', keypressListener);

$rootScope.$on('$destroy', function() {
$document.off('keypress', keypressListener);
$document.off('keyup', keypressListener);
});

function keypressListener(e) {
if (e.which === 27) {
var last = openedTooltips.top();
if (last) {
last.value.close();
openedTooltips.removeTop();
last = null;
}
}
Expand Down Expand Up @@ -207,9 +206,6 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
// By default, the tooltip is not open.
// TODO add ability to start tooltip opened
ttScope.isOpen = false;
openedTooltips.add(ttScope, {
close: hide
});

function toggleTooltipBind() {
if (!ttScope.isOpen) {
Expand Down Expand Up @@ -336,6 +332,10 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
}
});

openedTooltips.add(ttScope, {
close: hide
});

prepObservers();
}

Expand All @@ -348,6 +348,9 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
tooltip.remove();
tooltip = null;
}

openedTooltips.remove(ttScope);

if (tooltipLinkedScope) {
tooltipLinkedScope.$destroy();
tooltipLinkedScope = null;
Expand Down Expand Up @@ -563,7 +566,6 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
scope.$on('$destroy', function onDestroyTooltip() {
unregisterTriggers();
removeTooltip();
openedTooltips.remove(ttScope);
ttScope = null;
});
};
Expand Down

0 comments on commit f5ff12c

Please sign in to comment.