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

refactor(tooltip): remove observers #1817

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe('tooltip', function() {
expect( elmScope.tt_isOpen ).toBeFalsy();
}));

it('should not share triggers among different element instances - issue 692', inject( function ($compile) {
it('should only set up triggers once', inject( function ($compile) {

scope.test = true;
elmBody = angular.element(
Expand All @@ -290,10 +290,12 @@ describe('tooltip', function() {

scope.$apply('test = false');

elm2.trigger('mouseenter');
// click trigger isn't set
elm2.click();
expect( elmScope2.tt_isOpen ).toBeFalsy();

elm2.click();
// mouseenter trigger is still set
elm2.trigger('mouseenter');
expect( elmScope2.tt_isOpen ).toBeTruthy();
}));
});
Expand Down
24 changes: 18 additions & 6 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
if(hasEnableExp && !scope.$eval(attrs[prefix+'Enable'])) {
return;
}

prepareTooltip();

if ( scope.tt_popupDelay ) {
// Do nothing if the tooltip was already scheduled to pop-up.
// This happens if show is triggered multiple times before any hide is triggered.
Expand Down Expand Up @@ -242,6 +245,11 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
}
}

function prepareTooltip() {
prepPlacement();
prepPopupDelay();
}

/**
* Observe the relevant attributes.
*/
Expand All @@ -257,21 +265,24 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
scope.tt_title = val;
});

attrs.$observe( prefix+'Placement', function ( val ) {
function prepPlacement() {
var val = attrs[ prefix + 'Placement' ];
scope.tt_placement = angular.isDefined( val ) ? val : options.placement;
});
}

attrs.$observe( prefix+'PopupDelay', function ( val ) {
function prepPopupDelay() {
var val = attrs[ prefix + 'PopupDelay' ];
var delay = parseInt( val, 10 );
scope.tt_popupDelay = ! isNaN(delay) ? delay : options.popupDelay;
});
}

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

attrs.$observe( prefix+'Trigger', function ( val ) {
function prepTriggers() {
var val = attrs[ prefix + 'Trigger' ];
unregisterTriggers();

triggers = getTriggers( val );
Expand All @@ -282,7 +293,8 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
element.bind( triggers.show, showTooltipBind );
element.bind( triggers.hide, hideTooltipBind );
}
});
}
prepTriggers();

var animation = scope.$eval(attrs[prefix + 'Animation']);
scope.tt_animation = angular.isDefined(animation) ? !!animation : options.animation;
Expand Down