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

Commit 1ee467f

Browse files
author
Josh David Miller
committed
feat(tooltip): added appendToBody to $tooltip
Tooltips and popovers can now be appended to the body of the HTML document instead of directly after the element on which the directive was invoked, by setting `appendToBody` to true through $tooltipProvider.options(). The tooltip specs were refactored slightly to bucket all provider-related specs under a single `describe` for cleanliness.
2 parents a79a2ba + e5d593b commit 1ee467f

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

src/tooltip/test/tooltip.spec.js

+64-27
Original file line numberDiff line numberDiff line change
@@ -163,42 +163,79 @@ describe('tooltip', function() {
163163

164164
});
165165

166-
describe('tooltip with popup delay configured through provider', function(){
166+
describe( '$tooltipProvider', function() {
167167

168-
var elm,
169-
elmBody,
170-
scope,
171-
elmScope;
168+
describe( 'popupDelay', function() {
169+
var elm,
170+
elmBody,
171+
scope,
172+
elmScope;
172173

173-
beforeEach(module('ui.bootstrap.tooltip', function($tooltipProvider){
174-
$tooltipProvider.options({popupDelay: 1000});
175-
}));
174+
beforeEach(module('ui.bootstrap.tooltip', function($tooltipProvider){
175+
$tooltipProvider.options({popupDelay: 1000});
176+
}));
176177

177-
// load the template
178-
beforeEach(module('template/tooltip/tooltip-popup.html'));
178+
// load the template
179+
beforeEach(module('template/tooltip/tooltip-popup.html'));
179180

180-
beforeEach(inject(function($rootScope, $compile) {
181-
elmBody = angular.element(
182-
'<div><span tooltip="tooltip text">Selector Text</span></div>'
183-
);
181+
beforeEach(inject(function($rootScope, $compile) {
182+
elmBody = angular.element(
183+
'<div><span tooltip="tooltip text">Selector Text</span></div>'
184+
);
184185

185-
scope = $rootScope;
186-
$compile(elmBody)(scope);
187-
scope.$digest();
188-
elm = elmBody.find('span');
189-
elmScope = elm.scope();
190-
}));
186+
scope = $rootScope;
187+
$compile(elmBody)(scope);
188+
scope.$digest();
189+
elm = elmBody.find('span');
190+
elmScope = elm.scope();
191+
}));
191192

192-
it('should open after timeout', inject(function($timeout) {
193+
it('should open after timeout', inject(function($timeout) {
193194

194-
elm.trigger( 'mouseenter' );
195-
expect( elmScope.tt_isOpen ).toBe( false );
195+
elm.trigger( 'mouseenter' );
196+
expect( elmScope.tt_isOpen ).toBe( false );
196197

197-
$timeout.flush();
198-
expect( elmScope.tt_isOpen ).toBe( true );
198+
$timeout.flush();
199+
expect( elmScope.tt_isOpen ).toBe( true );
199200

200-
}));
201+
}));
202+
203+
});
204+
205+
describe('appendToBody', function() {
206+
var elm,
207+
elmBody,
208+
scope,
209+
elmScope,
210+
body;
211+
212+
// load the tooltip code
213+
beforeEach(module('ui.bootstrap.tooltip', function ( $tooltipProvider ) {
214+
$tooltipProvider.options({ appendToBody: true });
215+
}));
216+
217+
// load the template
218+
beforeEach(module('template/tooltip/tooltip-popup.html'));
219+
220+
it( 'should append to the body', inject( function( $rootScope, $compile, $document ) {
221+
$body = $document.find( 'body' );
222+
elmBody = angular.element(
223+
'<div><span tooltip="tooltip text">Selector Text</span></div>'
224+
);
201225

226+
scope = $rootScope;
227+
$compile(elmBody)(scope);
228+
scope.$digest();
229+
elm = elmBody.find('span');
230+
elmScope = elm.scope();
231+
232+
var bodyLength = $body.children().length;
233+
elm.trigger( 'mouseenter' );
234+
235+
expect( elmScope.tt_isOpen ).toBe( true );
236+
expect( elmBody.children().length ).toBe( 1 );
237+
expect( $body.children().length ).toEqual( bodyLength + 1 );
238+
}));
239+
});
202240
});
203241

204-

src/tooltip/tooltip.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module( 'ui.bootstrap.tooltip', [] )
2121
var globalOptions = {};
2222

2323
/**
24-
* The `options({})` allows global configuration of all dialogs in the
24+
* `options({})` allows global configuration of all tooltips in the
2525
* application.
2626
*
2727
* var app = angular.module( 'App', ['ui.bootstrap.tooltip'], function( $tooltipProvider ) {
@@ -37,7 +37,7 @@ angular.module( 'ui.bootstrap.tooltip', [] )
3737
* Returns the actual instance of the $tooltip service.
3838
* TODO support multiple triggers
3939
*/
40-
this.$get = [ '$window', '$compile', '$timeout', '$parse', function ( $window, $compile, $timeout, $parse ) {
40+
this.$get = [ '$window', '$compile', '$timeout', '$parse', '$document', function ( $window, $compile, $timeout, $parse, $document ) {
4141
return function $tooltip ( type, defaultTriggerShow, defaultTriggerHide ) {
4242
var options = angular.extend( {}, defaultOptions, globalOptions );
4343

@@ -66,9 +66,10 @@ angular.module( 'ui.bootstrap.tooltip', [] )
6666
restrict: 'EA',
6767
scope: true,
6868
link: function link ( scope, element, attrs ) {
69-
var tooltip = $compile( template )( scope ),
70-
transitionTimeout,
71-
popupTimeout;
69+
var tooltip = $compile( template )( scope );
70+
var transitionTimeout;
71+
var popupTimeout;
72+
var $body;
7273

7374
attrs.$observe( type, function ( val ) {
7475
scope.tt_content = val;
@@ -127,7 +128,12 @@ angular.module( 'ui.bootstrap.tooltip', [] )
127128

128129
// Now we add it to the DOM because need some info about it. But it's not
129130
// visible yet anyway.
130-
element.after( tooltip );
131+
if ( options.appendToBody ) {
132+
$body = $body || $document.find( 'body' );
133+
$body.append( tooltip );
134+
} else {
135+
element.after( tooltip );
136+
}
131137

132138
// Get the position of the directive element.
133139
position = getPosition( element );

0 commit comments

Comments
 (0)