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

Commit a79a2ba

Browse files
anton-dev-uaJosh David Miller
authored and
Josh David Miller
committed
feat(tooltip): added popup-delay option
1 parent beb257f commit a79a2ba

File tree

4 files changed

+135
-21
lines changed

4 files changed

+135
-21
lines changed

src/popover/test/popoverSpec.js

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('popover', function() {
2-
var elm,
2+
var elm,
33
elmBody,
4-
scope,
4+
scope,
55
elmScope;
66

77
// load the popover code
@@ -11,8 +11,8 @@ describe('popover', function() {
1111
beforeEach(module('template/popover/popover.html'));
1212

1313
beforeEach(inject(function($rootScope, $compile) {
14-
elmBody = angular.element(
15-
'<div><span popover="popover text">Selector Text</span></div>'
14+
elmBody = angular.element(
15+
'<div><span popover="popover text">Selector Text</span></div>'
1616
);
1717

1818
scope = $rootScope;
@@ -24,7 +24,7 @@ describe('popover', function() {
2424

2525
it('should not be open initially', inject(function() {
2626
expect( elmScope.tt_isOpen ).toBe( false );
27-
27+
2828
// We can only test *that* the popover-popup element wasn't created as the
2929
// implementation is templated and replaced.
3030
expect( elmBody.children().length ).toBe( 1 );
@@ -51,8 +51,8 @@ describe('popover', function() {
5151
}));
5252

5353
it('should allow specification of placement', inject( function( $compile ) {
54-
elm = $compile( angular.element(
55-
'<span popover="popover text" popover-placement="bottom">Selector Text</span>'
54+
elm = $compile( angular.element(
55+
'<span popover="popover text" popover-placement="bottom">Selector Text</span>'
5656
) )( scope );
5757
elmScope = elm.scope();
5858

@@ -62,7 +62,7 @@ describe('popover', function() {
6262

6363
it('should work inside an ngRepeat', inject( function( $compile ) {
6464

65-
elm = $compile( angular.element(
65+
elm = $compile( angular.element(
6666
'<ul>'+
6767
'<li ng-repeat="item in items">'+
6868
'<span popover="{{item.popover}}">{{item.name}}</span>'+
@@ -73,11 +73,11 @@ describe('popover', function() {
7373
scope.items = [
7474
{ name: "One", popover: "First popover" }
7575
];
76-
76+
7777
scope.$digest();
78-
78+
7979
var tt = angular.element( elm.find("li > span")[0] );
80-
80+
8181
tt.trigger( 'click' );
8282

8383
expect( tt.text() ).toBe( scope.items[0].name );
@@ -93,15 +93,15 @@ describe('popover', function() {
9393
scope.popoverTitle = "Popover Title";
9494
scope.alt = "Alt Message";
9595

96-
elmBody = $compile( angular.element(
96+
elmBody = $compile( angular.element(
9797
'<div><span alt={{alt}} popover="{{popoverContent}}" popover-title="{{popoverTitle}}">Selector Text</span></div>'
9898
) )( scope );
9999

100100
$compile( elmBody )( scope );
101101
scope.$digest();
102102
elm = elmBody.find( 'span' );
103103
elmScope = elm.scope();
104-
104+
105105
elm.trigger( 'click' );
106106
expect( elm.attr( 'alt' ) ).toBe( scope.alt );
107107

@@ -113,6 +113,23 @@ describe('popover', function() {
113113
elm.trigger( 'click' );
114114
}));
115115

116+
117+
it( 'should allow specification of delay', inject( function ($timeout, $compile) {
118+
119+
elm = $compile( angular.element(
120+
'<span popover="popover text" popover-popup-delay="1000">Selector Text</span>'
121+
) )( scope );
122+
elmScope = elm.scope();
123+
scope.$digest();
124+
125+
elm.trigger( 'click' );
126+
expect( elmScope.tt_isOpen ).toBe( false );
127+
128+
$timeout.flush();
129+
expect( elmScope.tt_isOpen ).toBe( true );
130+
131+
} ) );
132+
116133
});
117134

118135

src/tooltip/docs/demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<a><span tooltip-placement="bottom" tooltip="On the Bottom!">bottom</span></a>
1414
pharetra convallis posuere morbi leo urna,
1515
<a><span tooltip-animation="false" tooltip="I don't fade. :-(">fading</span></a>
16-
at elementum eu, facilisis sed odio morbi quis commodo odio. In cursus
17-
turpis massa tincidunt dui ut.
16+
at elementum eu, facilisis sed odio morbi quis commodo odio. In cursus
17+
<a><span tooltip-popup-delay='1000' tooltip='appears with delay'>delayed</span></a> turpis massa tincidunt dui ut.
1818
</p>
1919
</div>
2020
</div>

src/tooltip/test/tooltip.spec.js

+78
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,84 @@ describe('tooltip', function() {
121121

122122
expect(elmBody.children().length).toBe(1);
123123
}));
124+
125+
describe('with specified popup delay', function () {
126+
127+
beforeEach(inject(function ($compile) {
128+
scope.delay='1000';
129+
elm = $compile(angular.element(
130+
'<span tooltip="tooltip text" tooltip-popup-delay="{{delay}}">Selector Text</span>'
131+
))(scope);
132+
elmScope = elm.scope();
133+
scope.$digest();
134+
}));
135+
136+
it('should open after timeout', inject(function ($timeout) {
137+
138+
elm.trigger('mouseenter');
139+
expect(elmScope.tt_isOpen).toBe(false);
140+
141+
$timeout.flush();
142+
expect(elmScope.tt_isOpen).toBe(true);
143+
144+
}));
145+
146+
it('should not open if mouseleave before timeout', inject(function ($timeout) {
147+
elm.trigger('mouseenter');
148+
expect(elmScope.tt_isOpen).toBe(false);
149+
150+
elm.trigger('mouseleave');
151+
$timeout.flush();
152+
expect(elmScope.tt_isOpen).toBe(false);
153+
}));
154+
155+
it('should use default popup delay if specified delay is not a number', function(){
156+
scope.delay='text1000';
157+
scope.$digest();
158+
elm.trigger('mouseenter');
159+
expect(elmScope.tt_isOpen).toBe(true);
160+
});
161+
162+
});
163+
164+
});
165+
166+
describe('tooltip with popup delay configured through provider', function(){
167+
168+
var elm,
169+
elmBody,
170+
scope,
171+
elmScope;
172+
173+
beforeEach(module('ui.bootstrap.tooltip', function($tooltipProvider){
174+
$tooltipProvider.options({popupDelay: 1000});
175+
}));
176+
177+
// load the template
178+
beforeEach(module('template/tooltip/tooltip-popup.html'));
179+
180+
beforeEach(inject(function($rootScope, $compile) {
181+
elmBody = angular.element(
182+
'<div><span tooltip="tooltip text">Selector Text</span></div>'
183+
);
184+
185+
scope = $rootScope;
186+
$compile(elmBody)(scope);
187+
scope.$digest();
188+
elm = elmBody.find('span');
189+
elmScope = elm.scope();
190+
}));
191+
192+
it('should open after timeout', inject(function($timeout) {
193+
194+
elm.trigger( 'mouseenter' );
195+
expect( elmScope.tt_isOpen ).toBe( false );
196+
197+
$timeout.flush();
198+
expect( elmScope.tt_isOpen ).toBe( true );
199+
200+
}));
201+
124202
});
125203

126204

src/tooltip/tooltip.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* The following features are still outstanding: popup delay, animation as a
2+
* The following features are still outstanding: animation as a
33
* function, placement as a function, inside, support for more triggers than
44
* just mouse enter/leave, html tooltips, and selector delegation.
55
*/
@@ -13,7 +13,8 @@ angular.module( 'ui.bootstrap.tooltip', [] )
1313
// The default options tooltip and popover.
1414
var defaultOptions = {
1515
placement: 'top',
16-
animation: true
16+
animation: true,
17+
popupDelay: 0
1718
};
1819

1920
// The options specified to the provider globally.
@@ -66,7 +67,8 @@ angular.module( 'ui.bootstrap.tooltip', [] )
6667
scope: true,
6768
link: function link ( scope, element, attrs ) {
6869
var tooltip = $compile( template )( scope ),
69-
transitionTimeout;
70+
transitionTimeout,
71+
popupTimeout;
7072

7173
attrs.$observe( type, function ( val ) {
7274
scope.tt_content = val;
@@ -84,9 +86,23 @@ angular.module( 'ui.bootstrap.tooltip', [] )
8486
scope.tt_animation = angular.isDefined( val ) ? $parse( val ) : function(){ return options.animation; };
8587
});
8688

89+
attrs.$observe( type+'PopupDelay', function ( val ) {
90+
var delay = parseInt( val, 10 );
91+
scope.tt_popupDelay = ! isNaN(delay) ? delay : options.popupDelay;
92+
});
93+
8794
// By default, the tooltip is not open.
8895
// TODO add ability to start tooltip opened
8996
scope.tt_isOpen = false;
97+
98+
//show the tooltip with delay if specified, otherwise show it immediately
99+
function showWithDelay() {
100+
if( scope.tt_popupDelay ){
101+
popupTimeout = $timeout( show, scope.tt_popupDelay );
102+
}else {
103+
scope.$apply( show );
104+
}
105+
}
90106

91107
// Show the tooltip popup element.
92108
function show() {
@@ -101,7 +117,7 @@ angular.module( 'ui.bootstrap.tooltip', [] )
101117
}
102118

103119
// If there is a pending remove transition, we must cancel it, lest the
104-
// toolip be mysteriously removed.
120+
// tooltip be mysteriously removed.
105121
if ( transitionTimeout ) {
106122
$timeout.cancel( transitionTimeout );
107123
}
@@ -161,6 +177,9 @@ angular.module( 'ui.bootstrap.tooltip', [] )
161177
// First things first: we don't show it anymore.
162178
//tooltip.removeClass( 'in' );
163179
scope.tt_isOpen = false;
180+
181+
//if tooltip is going to be shown after delay, we must cancel this
182+
$timeout.cancel( popupTimeout );
164183

165184
// And now we remove it from the DOM. However, if we have animation, we
166185
// need to wait for it to expire beforehand.
@@ -178,14 +197,14 @@ angular.module( 'ui.bootstrap.tooltip', [] )
178197
if ( ! angular.isDefined( defaultTriggerHide ) ) {
179198
element.bind( defaultTriggerShow, function toggleTooltipBind () {
180199
if ( ! scope.tt_isOpen ) {
181-
scope.$apply( show );
200+
showWithDelay();
182201
} else {
183202
scope.$apply( hide );
184203
}
185204
});
186205
} else {
187206
element.bind( defaultTriggerShow, function showTooltipBind() {
188-
scope.$apply( show );
207+
showWithDelay();
189208
});
190209
element.bind( defaultTriggerHide, function hideTooltipBind() {
191210
scope.$apply( hide );

0 commit comments

Comments
 (0)