From 815b75b133ea5d458575517ed6ad9bb039a72aa6 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 14 Dec 2015 15:00:08 -0500 Subject: [PATCH] feat(tooltip): add appendToBody only attribute support - Add support for using only `tooltip-append-to-body` without any attribute value to enable appending tooltip to body --- src/popover/docs/readme.md | 2 +- src/tooltip/docs/readme.md | 2 +- src/tooltip/test/tooltip.spec.js | 42 ++++++++++++++++++++++++++++++++ src/tooltip/tooltip.js | 9 ++++++- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/popover/docs/readme.md b/src/popover/docs/readme.md index 66ec05d6b1..3365a588fc 100644 --- a/src/popover/docs/readme.md +++ b/src/popover/docs/readme.md @@ -42,7 +42,7 @@ will display: - `popover-trigger`: What should trigger the show of the popover? See the `tooltip` directive for supported values. - `popover-append-to-body`_(Default: false)_: Should the popover be appended to `$body` instead of - the parent element? Note that the presence of this attribute without a value implies `true`. + the parent element? - `popover-is-open` _(Default: false)_: Whether to show the popover. diff --git a/src/tooltip/docs/readme.md b/src/tooltip/docs/readme.md index 5cebb9f86e..4414dfa029 100644 --- a/src/tooltip/docs/readme.md +++ b/src/tooltip/docs/readme.md @@ -40,7 +40,7 @@ will display: - `tooltip-enable`: Is it enabled? It will enable or disable the configured `tooltip-trigger`. - `tooltip-append-to-body`_(Default: false)_: Should the tooltip be appended to `$body` instead of - the parent element? Note that the presence of this attribute without a value implies `true`. + the parent element? - `tooltip-class`: Custom class to be applied to the tooltip. - `tooltip-is-open` _(Default: false)_: diff --git a/src/tooltip/test/tooltip.spec.js b/src/tooltip/test/tooltip.spec.js index 65842ed75a..3508347ce5 100644 --- a/src/tooltip/test/tooltip.spec.js +++ b/src/tooltip/test/tooltip.spec.js @@ -966,6 +966,48 @@ describe('$uibTooltipProvider', function() { expect($body.children().length).toEqual(bodyLength + 1); })); + it('should append to the body when only attribute present', inject(function($rootScope, $compile, $document) { + $body = $document.find('body'); + elmBody = angular.element( + '
Selector Text
' + ); + + scope = $rootScope; + $compile(elmBody)(scope); + scope.$digest(); + elm = elmBody.find('span'); + elmScope = elm.scope(); + tooltipScope = elmScope.$$childTail; + + var bodyLength = $body.children().length; + trigger(elm, 'mouseenter'); + + expect(tooltipScope.isOpen).toBe(true); + expect(elmBody.children().length).toBe(1); + expect($body.children().length).toEqual(bodyLength + 1); + })); + + it('should not append to the body when attribute value is false', inject(function($rootScope, $compile, $document) { + $body = $document.find('body'); + elmBody = angular.element( + '
Selector Text
' + ); + + scope = $rootScope; + $compile(elmBody)(scope); + scope.$digest(); + elm = elmBody.find('span'); + elmScope = elm.scope(); + tooltipScope = elmScope.$$childTail; + + var bodyLength = $body.children().length; + trigger(elm, 'mouseenter'); + + expect(tooltipScope.isOpen).toBe(true); + expect(elmBody.children().length).toBe(2); + expect($body.children().length).toEqual(bodyLength); + })); + it('should close on location change', inject(function($rootScope, $compile) { elmBody = angular.element( '
Selector Text
' diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js index 5bfe0f8857..f2c74f5828 100644 --- a/src/tooltip/tooltip.js +++ b/src/tooltip/tooltip.js @@ -533,7 +533,14 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s var animation = scope.$eval(attrs[prefix + 'Animation']); ttScope.animation = angular.isDefined(animation) ? !!animation : options.animation; - var appendToBodyVal = scope.$eval(attrs[prefix + 'AppendToBody']); + var appendToBodyVal; + var appendKey = prefix + 'AppendToBody'; + if (appendKey in attrs && attrs[appendKey] === undefined) { + appendToBodyVal = true; + } else { + appendToBodyVal = scope.$eval(attrs[appendKey]); + } + appendToBody = angular.isDefined(appendToBodyVal) ? appendToBodyVal : appendToBody; // if a tooltip is attached to we need to remove it on