Skip to content

Commit b7beaae

Browse files
committed
feat(popover): support templates (WIP) angular-ui#1848
1 parent 60aec2e commit b7beaae

File tree

9 files changed

+68
-22
lines changed

9 files changed

+68
-22
lines changed

src/popover/docs/demo.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
<h4>Dynamic</h4>
33
<div class="form-group">
44
<label>Popup Text:</label>
5-
<input type="text" ng-model="dynamicPopover" class="form-control">
5+
<input type="text" ng-model="dynamicPopover.content" class="form-control">
66
</div>
77
<div class="form-group">
88
<label>Popup Title:</label>
9-
<input type="text" ng-model="dynamicPopoverTitle" class="form-control">
9+
<input type="text" ng-model="dynamicPopover.title" class="form-control">
1010
</div>
11-
<button popover="{{dynamicPopover}}" popover-title="{{dynamicPopoverTitle}}" class="btn btn-default">Dynamic Popover</button>
11+
<button popover="{{dynamicPopover.content}}" popover-title="{{dynamicPopover.title}}" class="btn btn-default">Dynamic Popover</button>
1212

13+
<button popover-window="templatePopover.html" popover-title="{{dynamicPopover.title}}" class="btn btn-default">Popover With Template</button>
14+
15+
<script type="text/ng-template" id="templatePopover.html">
16+
<div class="form-group">
17+
<label>Popup Title:</label>
18+
<input type="text" ng-model="dynamicPopover.title" class="form-control">
19+
</div>
20+
</script>
21+
1322
<hr />
1423
<h4>Positional</h4>
1524
<button popover-placement="top" popover="On the Top!" class="btn btn-default">Top</button>

src/popover/docs/demo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function ($scope) {
2-
$scope.dynamicPopover = 'Hello, World!';
3-
$scope.dynamicPopoverTitle = 'Title';
2+
$scope.dynamicPopover = {
3+
content: 'Hello, World!',
4+
title: 'Title'
5+
};
46
});

src/popover/popover.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55
*/
66
angular.module( 'ui.bootstrap.popover', [ 'ui.bootstrap.tooltip' ] )
77

8+
.directive( 'popoverWindowPopup', function () {
9+
return {
10+
restrict: 'EA',
11+
replace: true,
12+
templateUrl: 'template/popover/popover-window.html'
13+
};
14+
})
15+
16+
.directive( 'popoverWindow', [ '$tooltip', function ( $tooltip ) {
17+
return $tooltip( 'popoverWindow', 'popoverWindow', 'click' );
18+
}])
19+
820
.directive( 'popoverPopup', function () {
921
return {
1022
restrict: 'EA',
1123
replace: true,
12-
scope: { title: '@', content: '@', placement: '@', animation: '&', isOpen: '&' },
1324
templateUrl: 'template/popover/popover.html'
1425
};
1526
})

src/tooltip/test/tooltip.spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ describe('tooltip', function() {
135135
}));
136136

137137
it('should only have an isolate scope on the popup', inject( function ( $compile ) {
138-
var ttScope;
139138

140139
scope.tooltipMsg = 'Tooltip Text';
141140
scope.alt = 'Alt Message';
@@ -148,22 +147,21 @@ describe('tooltip', function() {
148147
scope.$digest();
149148
elm = elmBody.find( 'span' );
150149
elmScope = elm.scope();
150+
tooltipScope = elmScope.$$childTail;
151151

152152
elm.trigger( 'mouseenter' );
153153
expect( elm.attr( 'alt' ) ).toBe( scope.alt );
154154

155-
ttScope = angular.element( elmBody.children()[1] ).isolateScope();
156-
expect( ttScope.placement ).toBe( 'top' );
157-
expect( ttScope.content ).toBe( scope.tooltipMsg );
155+
expect( tooltipScope.placement ).toBe('top');
156+
expect( tooltipScope.content ).toBe(scope.tooltipMsg);
158157

159158
elm.trigger( 'mouseleave' );
160159

161160
//Isolate scope contents should be the same after hiding and showing again (issue 1191)
162161
elm.trigger( 'mouseenter' );
163162

164-
ttScope = angular.element( elmBody.children()[1] ).isolateScope();
165-
expect( ttScope.placement ).toBe( 'top' );
166-
expect( ttScope.content ).toBe( scope.tooltipMsg );
163+
expect( tooltipScope.placement ).toBe('top');
164+
expect( tooltipScope.content ).toBe(scope.tooltipMsg);
167165
}));
168166

169167
it('should not show tooltips if there is nothing to show - issue #129', inject(function ($compile) {

src/tooltip/tooltip.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
3636
* $tooltipProvider.options( { placement: 'left' } );
3737
* });
3838
*/
39-
this.options = function( value ) {
40-
angular.extend( globalOptions, value );
41-
};
39+
this.options = function( value ) {
40+
angular.extend( globalOptions, value );
41+
};
4242

4343
/**
4444
* This allows you to extend the set of trigger mappings available. E.g.:
@@ -202,7 +202,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
202202

203203
// And show the tooltip.
204204
ttScope.isOpen = true;
205-
ttScope.$digest(); // digest required as $apply is not called
205+
ttScope.$apply(); // digest required as $apply is not called
206206

207207
// Return positioning function as promise callback for correct
208208
// positioning after draw.
@@ -289,6 +289,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
289289
}
290290

291291
var unregisterTriggers = function () {
292+
element.unbind(triggers.show, toggleTooltipBind);
292293
element.unbind(triggers.show, showTooltipBind);
293294
element.unbind(triggers.hide, hideTooltipBind);
294295
};
@@ -340,11 +341,29 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
340341
}];
341342
})
342343

344+
.directive( 'tooltipTemplateTransclude', ['$http', '$compile', '$templateCache', function ($http , $compile , $templateCache) {
345+
return {
346+
link: function ( scope, elem, attrs ) {
347+
if (scope.content) {
348+
// TODO: How to solve the problem of pre-loading the template?
349+
// TODO: Should this be watching for changes in scope.content?
350+
var templateUrl = scope.content,
351+
transcludeScope = scope.$parent.$parent;
352+
353+
$http.get( templateUrl, { cache: $templateCache })
354+
.then(function (response) {
355+
elem.html(response.data);
356+
$compile(elem.contents())(transcludeScope);
357+
});
358+
}
359+
}
360+
};
361+
}])
362+
343363
.directive( 'tooltipPopup', function () {
344364
return {
345365
restrict: 'EA',
346366
replace: true,
347-
scope: { content: '@', placement: '@', animation: '&', isOpen: '&' },
348367
templateUrl: 'template/tooltip/tooltip-popup.html'
349368
};
350369
})
@@ -357,11 +376,11 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
357376
return {
358377
restrict: 'EA',
359378
replace: true,
360-
scope: { content: '@', placement: '@', animation: '&', isOpen: '&' },
361379
templateUrl: 'template/tooltip/tooltip-html-unsafe-popup.html'
362380
};
363381
})
364382

365383
.directive( 'tooltipHtmlUnsafe', [ '$tooltip', function ( $tooltip ) {
366384
return $tooltip( 'tooltipHtmlUnsafe', 'tooltip', 'mouseenter' );
367385
}]);
386+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="popover {{placement}}" ng-class="{ in: isOpen, fade: animation }">
2+
<div class="arrow"></div>
3+
<div class="popover-inner">
4+
<h3 class="popover-title" ng-bind="title" ng-show="title"></h3>
5+
<div class="popover-content" tooltip-template-transclude></div>
6+
</div>
7+
</div>

template/popover/popover.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="popover {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
1+
<div class="popover {{placement}}" ng-class="{ in: isOpen, fade: animation }">
22
<div class="arrow"></div>
33

44
<div class="popover-inner">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="tooltip {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
1+
<div class="tooltip {{placement}}" ng-class="{ in: isOpen, fade: animation }">
22
<div class="tooltip-arrow"></div>
33
<div class="tooltip-inner" bind-html-unsafe="content"></div>
44
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="tooltip {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
1+
<div class="tooltip {{placement}}" ng-class="{ in: isOpen, fade: animation }">
22
<div class="tooltip-arrow"></div>
33
<div class="tooltip-inner" ng-bind="content"></div>
44
</div>

0 commit comments

Comments
 (0)