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

Commit ca9196f

Browse files
committed
feat(tooltip): add multiple trigger support
- Adds support for multiple triggers Closes #3987 Closes #4077
1 parent 8dc13be commit ca9196f

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/tooltip/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ will display:
2020
- `tooltip-animation`: Should it fade in and out? Defaults to "true".
2121
- `tooltip-popup-delay`: For how long should the user have to have the mouse
2222
over the element before the tooltip shows (in milliseconds)? Defaults to 0.
23-
- `tooltip-trigger`: What should trigger a show of the tooltip?
23+
- `tooltip-trigger`: What should trigger a show of the tooltip? Supports a space separated list of event names.
2424
Note: this attribute is no longer observable. See `tooltip-enable`.
2525
- `tooltip-enable`: Is it enabled? It will enable or disable the configured
2626
`tooltip-trigger`.

src/tooltip/test/tooltip.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,27 @@ describe('tooltip', function() {
364364
elm2.trigger('mouseenter');
365365
expect( tooltipScope2.isOpen ).toBeTruthy();
366366
}));
367+
368+
it( 'should accept multiple triggers based on the map for mapped triggers', inject( function( $compile ) {
369+
elmBody = angular.element(
370+
'<div><input tooltip="Hello!" tooltip-trigger="focus fakeTriggerAttr" /></div>'
371+
);
372+
$compile(elmBody)(scope);
373+
scope.$apply();
374+
elm = elmBody.find('input');
375+
elmScope = elm.scope();
376+
tooltipScope = elmScope.$$childTail;
377+
378+
expect( tooltipScope.isOpen ).toBeFalsy();
379+
elm.trigger('focus');
380+
expect( tooltipScope.isOpen ).toBeTruthy();
381+
elm.trigger('blur');
382+
expect( tooltipScope.isOpen ).toBeFalsy();
383+
elm.trigger('fakeTriggerAttr');
384+
expect( tooltipScope.isOpen ).toBeTruthy();
385+
elm.trigger('fakeTriggerAttr');
386+
expect( tooltipScope.isOpen ).toBeFalsy();
387+
}));
367388
});
368389

369390
describe( 'with an append-to-body attribute', function() {

src/tooltip/tooltip.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
8484
* trigger; else it will just use the show trigger.
8585
*/
8686
function getTriggers ( trigger ) {
87-
var show = trigger || options.trigger || defaultTriggerShow;
88-
var hide = triggerMap[show] || show;
87+
var show = (trigger || options.trigger || defaultTriggerShow).split(' ');
88+
var hide = show.map(function(trigger) {
89+
return triggerMap[trigger] || trigger;
90+
});
8991
return {
9092
show: show,
9193
hide: hide
@@ -332,8 +334,12 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
332334
}
333335

334336
var unregisterTriggers = function () {
335-
element.unbind(triggers.show, showTooltipBind);
336-
element.unbind(triggers.hide, hideTooltipBind);
337+
triggers.show.forEach(function(trigger) {
338+
element.unbind(trigger, showTooltipBind);
339+
});
340+
triggers.hide.forEach(function(trigger) {
341+
element.unbind(trigger, hideTooltipBind);
342+
});
337343
};
338344

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

343349
triggers = getTriggers( val );
344350

345-
if ( triggers.show === triggers.hide ) {
346-
element.bind( triggers.show, toggleTooltipBind );
347-
} else {
348-
element.bind( triggers.show, showTooltipBind );
349-
element.bind( triggers.hide, hideTooltipBind );
350-
}
351+
triggers.show.forEach(function(trigger, idx) {
352+
if (trigger === triggers.hide[idx]) {
353+
element.bind(trigger, toggleTooltipBind);
354+
} else if (trigger) {
355+
element.bind(trigger, showTooltipBind);
356+
element.bind(triggers.hide[idx], hideTooltipBind);
357+
}
358+
});
351359
}
352360
prepTriggers();
353361

0 commit comments

Comments
 (0)