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

Commit

Permalink
feat(tooltip): add multiple trigger support
Browse files Browse the repository at this point in the history
- Adds support for multiple triggers

Closes #3987
Closes #4077
  • Loading branch information
wesleycho committed Aug 2, 2015
1 parent 8dc13be commit ca9196f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/tooltip/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ will display:
- `tooltip-animation`: Should it fade in and out? Defaults to "true".
- `tooltip-popup-delay`: For how long should the user have to have the mouse
over the element before the tooltip shows (in milliseconds)? Defaults to 0.
- `tooltip-trigger`: What should trigger a show of the tooltip?
- `tooltip-trigger`: What should trigger a show of the tooltip? Supports a space separated list of event names.
Note: this attribute is no longer observable. See `tooltip-enable`.
- `tooltip-enable`: Is it enabled? It will enable or disable the configured
`tooltip-trigger`.
Expand Down
21 changes: 21 additions & 0 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ describe('tooltip', function() {
elm2.trigger('mouseenter');
expect( tooltipScope2.isOpen ).toBeTruthy();
}));

it( 'should accept multiple triggers based on the map for mapped triggers', inject( function( $compile ) {
elmBody = angular.element(
'<div><input tooltip="Hello!" tooltip-trigger="focus fakeTriggerAttr" /></div>'
);
$compile(elmBody)(scope);
scope.$apply();
elm = elmBody.find('input');
elmScope = elm.scope();
tooltipScope = elmScope.$$childTail;

expect( tooltipScope.isOpen ).toBeFalsy();
elm.trigger('focus');
expect( tooltipScope.isOpen ).toBeTruthy();
elm.trigger('blur');
expect( tooltipScope.isOpen ).toBeFalsy();
elm.trigger('fakeTriggerAttr');
expect( tooltipScope.isOpen ).toBeTruthy();
elm.trigger('fakeTriggerAttr');
expect( tooltipScope.isOpen ).toBeFalsy();
}));
});

describe( 'with an append-to-body attribute', function() {
Expand Down
28 changes: 18 additions & 10 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
* trigger; else it will just use the show trigger.
*/
function getTriggers ( trigger ) {
var show = trigger || options.trigger || defaultTriggerShow;
var hide = triggerMap[show] || show;
var show = (trigger || options.trigger || defaultTriggerShow).split(' ');
var hide = show.map(function(trigger) {
return triggerMap[trigger] || trigger;
});
return {
show: show,
hide: hide
Expand Down Expand Up @@ -332,8 +334,12 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
}

var unregisterTriggers = function () {
element.unbind(triggers.show, showTooltipBind);
element.unbind(triggers.hide, hideTooltipBind);
triggers.show.forEach(function(trigger) {
element.unbind(trigger, showTooltipBind);
});
triggers.hide.forEach(function(trigger) {
element.unbind(trigger, hideTooltipBind);
});
};

function prepTriggers() {
Expand All @@ -342,12 +348,14 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap

triggers = getTriggers( val );

if ( triggers.show === triggers.hide ) {
element.bind( triggers.show, toggleTooltipBind );
} else {
element.bind( triggers.show, showTooltipBind );
element.bind( triggers.hide, hideTooltipBind );
}
triggers.show.forEach(function(trigger, idx) {
if (trigger === triggers.hide[idx]) {
element.bind(trigger, toggleTooltipBind);
} else if (trigger) {
element.bind(trigger, showTooltipBind);
element.bind(triggers.hide[idx], hideTooltipBind);
}
});
}
prepTriggers();

Expand Down

0 comments on commit ca9196f

Please sign in to comment.