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

feat(popover): support template url for partial #3409

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/popover/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,25 @@ angular.module( 'ui.bootstrap.popover', [ 'ui.bootstrap.tooltip' ] )

.directive( 'popover', [ '$tooltip', function ( $tooltip ) {
return $tooltip( 'popover', 'popover', 'click' );
}])

.directive( 'popoverTemplatePopup', [ function () {
return {
restrict: 'EA',
replace: true,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope: { title: '@', template: '=', placement: '@', animation: '&', isOpen: '&' },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use 1-way binding instead of text binding so title and placement can also be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this and the other comment truly relates to the current popover.js implementation, not exactly to this small effort to make popover-template available...

Here, again, I'm just making it behave exactly the same as the normal popover:
https://github.com/angular-ui/bootstrap/blob/master/src/popover/popover.js#L12

templateUrl: 'template/popover/popover-template.html',
link: function( scope, iElement ) {
var contentEl = angular.element( iElement[0].querySelector( '.popover-content' ) );
scope.$watch( 'template', function( template ) {
if ( !template ) { return; }
contentEl.children().remove();
contentEl.append( template );
});
}
};
}])

.directive( 'popoverTemplate', [ '$tooltip', function ( $tooltip ) {
return $tooltip( 'popoverTemplate', 'popover', 'click' );
}]);
33 changes: 29 additions & 4 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
* Returns the actual instance of the $tooltip service.
* TODO support multiple triggers
*/
this.$get = [ '$window', '$compile', '$timeout', '$document', '$position', '$interpolate', function ( $window, $compile, $timeout, $document, $position, $interpolate ) {
this.$get = [ '$window', '$compile', '$timeout', '$document', '$position', '$interpolate', '$http', '$templateCache',
function ( $window, $compile, $timeout, $document, $position, $interpolate, $http, $templateCache ) {
return function $tooltip ( type, prefix, defaultTriggerShow ) {
var options = angular.extend( {}, defaultOptions, globalOptions );

Expand Down Expand Up @@ -102,6 +103,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
'placement="'+startSym+'placement'+endSym+'" '+
'animation="animation" '+
'is-open="isOpen"'+
'template="template"'+
'>'+
'</div>';

Expand All @@ -118,6 +120,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
var appendToBody = angular.isDefined( options.appendToBody ) ? options.appendToBody : false;
var triggers = getTriggers( undefined );
var hasEnableExp = angular.isDefined(attrs[prefix+'Enable']);
var hasTemplateObserver = false;
var ttScope = scope.$new(true);

var positionTooltip = function () {
Expand Down Expand Up @@ -193,9 +196,18 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap

positionTooltip();

// And show the tooltip.
ttScope.isOpen = true;
ttScope.$digest(); // digest required as $apply is not called
if ( hasTemplateObserver ) {
loadTemplate();
} else {
attrs.$observe( 'popoverTemplate', loadTemplate );
hasTemplateObserver = true;
}

if ( ! attrs.popoverTemplate ) {
// And show the tooltip.
ttScope.isOpen = true;
ttScope.$digest(); // digest required as $apply is not called
}

// Return positioning function as promise callback for correct
// positioning after draw.
Expand Down Expand Up @@ -276,6 +288,19 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
ttScope.title = val;
});

function loadTemplate() {
if ( !attrs.popoverTemplate ) { return; }
$http.get( attrs.popoverTemplate, { cache: $templateCache } )
.then( function ( response ) {
ttScope.template = $compile( response.data.trim() )( ttScope.$parent );
ttScope.isOpen = true;
$timeout(function() {
positionTooltip();
ttScope.$digest();
});
});
}

function prepPlacement() {
var val = attrs[ prefix + 'Placement' ];
ttScope.placement = angular.isDefined( val ) ? val : options.placement;
Expand Down
8 changes: 8 additions & 0 deletions template/popover/popover-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="popover {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
<div class="arrow"></div>

<div class="popover-inner">
<h3 class="popover-title" ng-bind="title" ng-show="title"></h3>
<div class="popover-content"></div>
</div>
</div>