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

Commit a169511

Browse files
committed
feat(tooltip): add tooltip-template directive
Fixes #220
1 parent 7e3179a commit a169511

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed

src/tooltip/docs/demo.html

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<a href="#" tooltip-animation="false" tooltip="I don't fade. :-(">fading</a>
2121
at elementum eu, facilisis sed odio morbi quis commodo odio. In cursus
2222
<a href="#" tooltip-popup-delay='1000' tooltip='appears with delay'>delayed</a> turpis massa tincidunt dui ut.
23+
<a href="#" tooltip-template="myTooltipTemplate.html">Custom template</a>
24+
nunc sed velit dignissim sodales ut eu sem integer vitae. Turpis egestas
2325
</p>
2426

2527
<p>
@@ -58,4 +60,8 @@
5860
tooltip-enable="!inputModel" />
5961
</div>
6062
</form>
63+
64+
<script type="text/ng-template" id="myTooltipTemplate.html">
65+
<span>Special Tooltip with <strong>markup</strong> and {{ dynamicTooltipText }}</span>
66+
</script>
6167
</div>

src/tooltip/docs/readme.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
A lightweight, extensible directive for fancy tooltip creation. The tooltip
22
directive supports multiple placements, optional transition animation, and more.
33

4-
There are two versions of the tooltip: `tooltip` and `tooltip-html-unsafe`. The
5-
former takes text only and will escape any HTML provided. The latter takes
6-
whatever HTML is provided and displays it in a tooltip; it's called "unsafe"
7-
because the HTML is not sanitized. *The user is responsible for ensuring the
8-
content is safe to put into the DOM!*
4+
There are three versions of the tooltip: `tooltip`, `tooltip-template`, and
5+
`tooltip-html-unsafe`:
6+
7+
- `tooltip` takes text only and will escape any HTML provided.
8+
- `tooltip-template` takes text that specifies the location of a template to
9+
use for the tooltip.
10+
- `tooltip-html-unsafe` takes
11+
whatever HTML is provided and displays it in a tooltip; it's called "unsafe"
12+
because the HTML is not sanitized. *The user is responsible for ensuring the
13+
content is safe to put into the DOM!*
914

1015
The tooltip directives provide several optional attributes to control how they
1116
will display:
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
describe('tooltip template', function() {
2+
var elm,
3+
elmBody,
4+
scope,
5+
elmScope,
6+
tooltipScope;
7+
8+
// load the popover code
9+
beforeEach(module('ui.bootstrap.tooltip'));
10+
11+
// load the template
12+
beforeEach(module('template/tooltip/tooltip-template-popup.html'));
13+
14+
beforeEach(inject(function ($templateCache) {
15+
$templateCache.put('myUrl', [200, '<span>{{ myTemplateText }}</span>', {}]);
16+
}));
17+
18+
beforeEach(inject(function($rootScope, $compile) {
19+
elmBody = angular.element(
20+
'<div><span tooltip-template="{{ templateUrl }}">Selector Text</span></div>'
21+
);
22+
23+
scope = $rootScope;
24+
$compile(elmBody)(scope);
25+
scope.templateUrl = 'myUrl';
26+
27+
scope.$digest();
28+
elm = elmBody.find('span');
29+
elmScope = elm.scope();
30+
tooltipScope = elmScope.$$childTail;
31+
}));
32+
33+
it('should open on mouseenter', inject(function() {
34+
elm.trigger( 'mouseenter' );
35+
expect( tooltipScope.isOpen ).toBe( true );
36+
37+
expect( elmBody.children().length ).toBe( 2 );
38+
}));
39+
40+
it('should not open on mouseenter if templateUrl is empty', inject(function() {
41+
scope.templateUrl = null;
42+
scope.$digest();
43+
44+
elm.trigger( 'mouseenter' );
45+
expect( tooltipScope.isOpen ).toBe( false );
46+
47+
expect( elmBody.children().length ).toBe( 1 );
48+
}));
49+
50+
it('should show updated text', inject(function() {
51+
scope.myTemplateText = 'some text';
52+
scope.$digest();
53+
54+
elm.trigger( 'mouseenter' );
55+
expect( tooltipScope.isOpen ).toBe( true );
56+
57+
expect( elmBody.children().eq(1).text().trim() ).toBe( 'some text' );
58+
59+
scope.myTemplateText = 'new text';
60+
scope.$digest();
61+
62+
expect( elmBody.children().eq(1).text().trim() ).toBe( 'new text' );
63+
}));
64+
});
65+

src/tooltip/tooltip.js

+17
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,23 @@ function ($animate , $sce , $compile , $templateRequest) {
434434
return $tooltip( 'tooltip', 'tooltip', 'mouseenter' );
435435
}])
436436

437+
.directive( 'tooltipTemplatePopup', function () {
438+
return {
439+
restrict: 'EA',
440+
replace: true,
441+
scope: { title: '@', content: '@', placement: '@', animation: '&', isOpen: '&',
442+
originScope: '&' },
443+
templateUrl: 'template/tooltip/tooltip-template-popup.html'
444+
};
445+
})
446+
447+
.directive( 'tooltipTemplate', [ '$tooltip', function ( $tooltip ) {
448+
return $tooltip( 'tooltipTemplate', 'tooltipTemplate', 'mouseenter' );
449+
}])
450+
451+
/*
452+
Deprecated
453+
*/
437454
.directive( 'tooltipHtmlUnsafePopup', function () {
438455
return {
439456
restrict: 'EA',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="tooltip {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
2+
<div class="tooltip-arrow"></div>
3+
<div class="tooltip-inner"
4+
tooltip-template-transclude="content"
5+
tooltip-template-transclude-scope="originScope()"></div>
6+
</div>

0 commit comments

Comments
 (0)