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

Commit

Permalink
feat(tooltip): expose isOpen property
Browse files Browse the repository at this point in the history
Add support for toggling the isOpen
property of the tooltip.

Closes #4179
Closes #2148
Fixes #590
  • Loading branch information
RobJacobs authored and wesleycho committed Aug 10, 2015
1 parent dbd6f73 commit 99b87cc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/popover/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ will display:
`tooltip` directive for supported values.
- `popover-append-to-body`: Should the tooltip be appended to `$body` instead of
the parent element?
- `popover-is-open` <i class="glyphicon glyphicon-eye-open"></i>
_(Default: false)_:
Whether to show the popover.

The popover directives require the `$position` service.

Expand Down
31 changes: 31 additions & 0 deletions src/popover/test/popover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ describe('popover', function() {
}));

});

describe( 'is-open', function() {
beforeEach(inject(function ($compile) {
scope.isOpen = false;
elmBody = angular.element(
'<div><span popover="popover text" popover-placement="left" popover-is-open="isOpen">Trigger here</span></div>'
);
$compile(elmBody)(scope);
scope.$digest();
elm = elmBody.find('span');
elmScope = elm.scope();
tooltipScope = elmScope.$$childTail;
}));

it( 'should show and hide with the controller value', function() {
expect(tooltipScope.isOpen).toBe(false);
elmScope.isOpen = true;
elmScope.$digest();
expect(tooltipScope.isOpen).toBe(true);
elmScope.isOpen = false;
elmScope.$digest();
expect(tooltipScope.isOpen).toBe(false);
});

it( 'should update the controller value', function() {
elm.trigger('click');
expect(elmScope.isOpen).toBe(true);
elm.trigger('click');
expect(elmScope.isOpen).toBe(false);
});
});

});

Expand Down
7 changes: 6 additions & 1 deletion src/tooltip/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ will display:
- `tooltip-append-to-body`: Should the tooltip be appended to `$body` instead of
the parent element?
- `tooltip-class`: Custom class to be applied to the tooltip.
- `tooltip-is-open` <i class="glyphicon glyphicon-eye-open"></i>
_(Default: false)_:
Whether to show the tooltip.

The tooltip directives require the `$position` service.

Expand All @@ -38,9 +41,11 @@ provided hide triggers:
- `mouseenter`: `mouseleave`
- `click`: `click`
- `focus`: `blur`
- `none`: ``

For any non-supported value, the trigger will be used to both show and hide the
tooltip.
tooltip. Using the 'none' trigger will disable the internal trigger(s), one can
then use the `tooltip-is-open` attribute exclusively to show and hide the tooltip.

**$tooltipProvider**

Expand Down
43 changes: 43 additions & 0 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,35 @@ describe('tooltip', function() {
}));

});

describe( 'with an is-open attribute', function() {
beforeEach(inject(function ($compile) {
scope.isOpen = false;
elm = $compile(angular.element(
'<span tooltip="tooltip text" tooltip-is-open="isOpen" >Selector Text</span>'
))(scope);
elmScope = elm.scope();
tooltipScope = elmScope.$$childTail;
scope.$digest();
}));

it( 'should show and hide with the controller value', function() {
expect(tooltipScope.isOpen).toBe(false);
elmScope.isOpen = true;
elmScope.$digest();
expect(tooltipScope.isOpen).toBe(true);
elmScope.isOpen = false;
elmScope.$digest();
expect(tooltipScope.isOpen).toBe(false);
});

it( 'should update the controller value', function() {
elm.trigger('mouseenter');
expect(elmScope.isOpen).toBe(true);
elm.trigger('mouseleave');
expect(elmScope.isOpen).toBe(false);
});
});

describe( 'with a trigger attribute', function() {
var scope, elmBody, elm, elmScope;
Expand Down Expand Up @@ -398,6 +427,20 @@ describe('tooltip', function() {
elm.trigger('fakeTriggerAttr');
expect( tooltipScope.isOpen ).toBeFalsy();
}));

it( 'should not show when trigger is set to "none"', inject( function( $compile ) {
elmBody = angular.element(
'<div><input tooltip="Hello!" tooltip-trigger="none" /></div>'
);
$compile(elmBody)(scope);
scope.$apply();
elm = elmBody.find('input');
elmScope = elm.scope();
tooltipScope = elmScope.$$childTail;
expect( tooltipScope.isOpen ).toBeFalsy();
elm.trigger('mouseenter');
expect( tooltipScope.isOpen ).toBeFalsy();
}));
});

describe( 'with an append-to-body attribute', function() {
Expand Down
49 changes: 36 additions & 13 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
var triggerMap = {
'mouseenter': 'mouseleave',
'click': 'click',
'focus': 'blur'
'focus': 'blur',
'none': ''
};

// The options specified to the provider globally.
Expand Down Expand Up @@ -65,7 +66,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
* Returns the actual instance of the $tooltip service.
* TODO support multiple triggers
*/
this.$get = [ '$window', '$compile', '$timeout', '$document', '$position', '$interpolate', '$rootScope', function ( $window, $compile, $timeout, $document, $position, $interpolate, $rootScope ) {
this.$get = [ '$window', '$compile', '$timeout', '$document', '$position', '$interpolate', '$rootScope', '$parse', function ( $window, $compile, $timeout, $document, $position, $interpolate, $rootScope, $parse ) {
return function $tooltip ( type, prefix, defaultTriggerShow, options ) {
options = angular.extend( {}, defaultOptions, globalOptions, options );

Expand Down Expand Up @@ -127,6 +128,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
var hasEnableExp = angular.isDefined(attrs[prefix+'Enable']);
var ttScope = scope.$new(true);
var repositionScheduled = false;
var isOpenExp = angular.isDefined(attrs[prefix + 'IsOpen']) ? $parse(attrs[prefix + 'IsOpen']) : false;

var positionTooltip = function () {
if (!tooltip) { return; }
Expand Down Expand Up @@ -211,7 +213,13 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap

// And show the tooltip.
ttScope.isOpen = true;
ttScope.$apply(); // digest required as $apply is not called
if (isOpenExp) {
isOpenExp.assign(ttScope.origScope, ttScope.isOpen);
}

if (!$rootScope.$$phase) {
ttScope.$apply(); // digest required as $apply is not called
}

// Return positioning function as promise callback for correct
// positioning after draw.
Expand All @@ -222,7 +230,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
function hide() {
// First things first: we don't show it anymore.
ttScope.isOpen = false;

if (isOpenExp) {
isOpenExp.assign(ttScope.origScope, ttScope.isOpen);
}

//if tooltip is going to be shown after delay, we must cancel this
$timeout.cancel( popupTimeout );
popupTimeout = null;
Expand Down Expand Up @@ -265,7 +276,9 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
repositionScheduled = true;
tooltipLinkedScope.$$postDigest(function() {
repositionScheduled = false;
positionTooltipAsync();
if (ttScope.isOpen) {
positionTooltipAsync();
}
});
}
});
Expand Down Expand Up @@ -333,6 +346,14 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
}, 0, false);
}
});

if (isOpenExp) {
scope.$watch(isOpenExp, function(val) {
if (val !== ttScope.isOpen) {
toggleTooltipBind();
}
});
}

function prepPopupClass() {
ttScope.popupClass = attrs[prefix + 'Class'];
Expand Down Expand Up @@ -364,14 +385,16 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap

triggers = getTriggers( val );

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);
}
});
if (triggers.show !== 'none') {
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

5 comments on commit 99b87cc

@zauker
Copy link

@zauker zauker commented on 99b87cc Sep 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which version of Angular UI Bootstrap will be available the option popover-is-open ?

@icfantv
Copy link
Contributor

@icfantv icfantv commented on 99b87cc Sep 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.13.4. - it's in master right now if you want to build and use the SNAPSHOT version.

@zauker
Copy link

@zauker zauker commented on 99b87cc Sep 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@icfantv yes i saw. I'll download it to try this feature, but in my project i need to use a Stable version, there is a planned date or a road map for the release of 0.13.4 as stable and not as snapshot?

@wesleycho
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zauker: Looking like we'll likely do a release this Thursday evening.

@zauker
Copy link

@zauker zauker commented on 99b87cc Sep 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wesleycho Great, thank you for your fast reply.

Please sign in to comment.