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

Commit 215804d

Browse files
committed
feat(popoverWindow): support template urls (WIP)
Proof of concept
1 parent 30a3419 commit 215804d

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

src/popover/popover.js

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
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',

src/tooltip/tooltip.js

+71-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
110110
compile: function (tElem, tAttrs) {
111111
var tooltipLinker = $compile( template );
112112

113-
return function link ( scope, element, attrs ) {
113+
return function link ( scope, element, attrs, tooltipCtrl ) {
114114
var tooltip;
115115
var tooltipLinkedScope;
116116
var transitionTimeout;
@@ -130,6 +130,9 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
130130
tooltip.css( ttPosition );
131131
};
132132

133+
// Set up the correct scope to allow transclusion later
134+
ttScope.origScope = scope;
135+
133136
// By default, the tooltip is not open.
134137
// TODO add ability to start tooltip opened
135138
ttScope.isOpen = false;
@@ -195,7 +198,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
195198

196199
// And show the tooltip.
197200
ttScope.isOpen = true;
198-
ttScope.$digest(); // digest required as $apply is not called
201+
ttScope.$apply(); // digest required as $apply is not called
199202

200203
// Return positioning function as promise callback for correct
201204
// positioning after draw.
@@ -339,6 +342,72 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
339342
}];
340343
})
341344

345+
// This is mostly ngInclude code but with a custom scope
346+
.directive( 'tooltipTemplateTransclude', [
347+
'$animate', '$sce', '$compile', '$templateRequest',
348+
function ($animate , $sce , $compile , $templateRequest) {
349+
return {
350+
link: function ( scope, elem, attrs ) {
351+
var origScope = scope.origScope;
352+
353+
var changeCounter = 0,
354+
currentScope,
355+
previousElement,
356+
currentElement;
357+
358+
var cleanupLastIncludeContent = function() {
359+
if (previousElement) {
360+
previousElement.remove();
361+
previousElement = null;
362+
}
363+
if (currentScope) {
364+
currentScope.$destroy();
365+
currentScope = null;
366+
}
367+
if (currentElement) {
368+
$animate.leave(currentElement).then(function() {
369+
previousElement = null;
370+
});
371+
previousElement = currentElement;
372+
currentElement = null;
373+
}
374+
};
375+
376+
scope.$watch($sce.parseAsResourceUrl('content'), function (src) {
377+
var thisChangeId = ++changeCounter;
378+
379+
if (src) {
380+
//set the 2nd param to true to ignore the template request error so that the inner
381+
//contents and scope can be cleaned up.
382+
$templateRequest(src, true).then(function(response) {
383+
if (thisChangeId !== changeCounter) { return; }
384+
var newScope = origScope.$new();
385+
var template = response;
386+
387+
var clone = $compile(template)(newScope, function(clone) {
388+
cleanupLastIncludeContent();
389+
$animate.enter(clone, elem);
390+
});
391+
392+
currentScope = newScope;
393+
currentElement = clone;
394+
395+
currentScope.$emit('$includeContentLoaded', src);
396+
}, function() {
397+
if (thisChangeId === changeCounter) {
398+
cleanupLastIncludeContent();
399+
scope.$emit('$includeContentError', src);
400+
}
401+
});
402+
scope.$emit('$includeContentRequested', src);
403+
} else {
404+
cleanupLastIncludeContent();
405+
}
406+
});
407+
}
408+
};
409+
}])
410+
342411
.directive( 'tooltipPopup', function () {
343412
return {
344413
restrict: 'EA',

template/popover/popover-window.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="popover {{placement}}" ng-class="{ in: isOpen, fade: animation }">
2+
<div class="arrow"></div>
3+
4+
<div class="popover-inner">
5+
<h3 class="popover-title" ng-bind="title" ng-show="title"></h3>
6+
<div class="popover-content" tooltip-template-transclude></div>
7+
</div>
8+
</div>

0 commit comments

Comments
 (0)