Skip to content

Commit 6fb45a3

Browse files
authored
Merge pull request #204 from leonard-thieu/template-cache
Fix template caching
2 parents 44da315 + 9384997 commit 6fb45a3

File tree

4 files changed

+79
-48
lines changed

4 files changed

+79
-48
lines changed

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ Sometimes you may need to set all of your tooltips options in one place, you can
106106
```javascript
107107
.config(['tooltipsConfProvider', function configConf(tooltipsConfProvider) {
108108
tooltipsConfProvider.configure({
109-
'smart':true,
110-
'size':'large',
109+
'smart': true,
110+
'size': 'large',
111111
'speed': 'slow',
112112
'tooltipTemplateUrlCache': true
113113
//etc...

demo/views/template-url-cache.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>I'm a tooltip loaded using templateUrlCache!</div>

index.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,24 @@
339339
tooltip-template="A tooltip with text">Tooltip Bottom</button>
340340
</div>
341341

342-
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.0/angular.min.js"></script>
343-
<script type="text/javascript" src="dist/angular-tooltips.min.js"></script>
342+
<div>
343+
<span tooltips
344+
tooltip-template-url="demo/views/template-url-cache.html"
345+
tooltip-template-url-cache="true">
346+
Tooltip using templateUrlCache
347+
</span>
348+
</div>
349+
350+
<div>
351+
<span tooltips
352+
tooltip-template-url="demo/views/template-url-cache.html"
353+
tooltip-template-url-cache="true">
354+
Tooltip using templateUrlCache
355+
</span>
356+
</div>
357+
358+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.0/angular.js"></script>
359+
<script type="text/javascript" src="dist/angular-tooltips.js"></script>
344360
<script type="text/javascript" src="demo/js/index.js"></script>
345361
</body>
346362
</html>

lib/angular-tooltips.js

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -561,65 +561,79 @@
561561
registerOnScrollFrom(parentElement);
562562
}
563563
}
564+
, showTemplate = function showTemplate(template) {
565+
566+
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
567+
tipTipElement.empty();
568+
tipTipElement.append(closeButtonElement);
569+
tipTipElement.append(template);
570+
$timeout(function doLater() {
571+
572+
onTooltipShow();
573+
});
574+
}
575+
, hideTemplate = function hideTemplate() {
576+
577+
//hide tooltip because is empty
578+
tipTipElement.empty();
579+
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
580+
}
581+
, getTemplate = function getTemplate(tooltipTemplateUrl) {
582+
583+
var template = $templateCache.get(tooltipTemplateUrl);
584+
585+
if (typeof template === 'undefined') {
586+
587+
// How should failing to load the template be handled?
588+
template = $http.get(tooltipTemplateUrl).then(function onGetTemplateSuccess(response) {
589+
590+
return response.data;
591+
});
592+
$templateCache.put(tooltipTemplateUrl, template);
593+
}
594+
595+
return template;
596+
}
564597
, onTooltipTemplateChange = function onTooltipTemplateChange(newValue) {
598+
565599
if (newValue) {
566-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
567-
tipTipElement.empty();
568-
tipTipElement.append(closeButtonElement);
569-
tipTipElement.append(newValue);
570-
$timeout(function doLaterShow() {
571-
572-
onTooltipShow();
573-
});
600+
601+
showTemplate(newValue);
574602
} else {
575-
//hide tooltip because is empty
576-
tipTipElement.empty();
577-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
603+
604+
hideTemplate();
578605
}
579606
}
580607
, onTooltipTemplateUrlChange = function onTooltipTemplateUrlChange(newValue) {
608+
581609
if (newValue && !$attrs.tooltipTemplateUrlCache) {
582-
583-
$http.get(newValue).then(function onResponse(response) {
584-
585-
if (response &&
586-
response.data) {
587-
588-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
589-
tipTipElement.empty();
590-
tipTipElement.append(closeButtonElement);
591-
tipTipElement.append($compile(response.data)(scope));
592-
$timeout(function doLater() {
593-
594-
onTooltipShow();
595-
});
596-
}
610+
611+
getTemplate(newValue).then(function onGetTemplateSuccess(template) {
612+
613+
showTemplate($compile(template)(scope));
614+
}).catch(function onGetTemplateFailure(reason) {
615+
616+
$log.error(reason);
597617
});
598618
} else {
599-
//hide tooltip because is empty
600-
tipTipElement.empty();
601-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
619+
620+
hideTemplate();
602621
}
603622
}
604623
, onTooltipTemplateUrlCacheChange = function onTooltipTemplateUrlCacheChange(newValue) {
624+
605625
if (newValue && $attrs.tooltipTemplateUrl) {
626+
627+
getTemplate($attrs.tooltipTemplateUrl).then(function onGetTemplateSuccess(template) {
628+
629+
showTemplate($compile(template)(scope));
630+
}).catch(function onGetTemplateFailure(reason) {
606631

607-
var template = $templateCache.get($attrs.tooltipTemplateUrl);
608-
609-
if (typeof template !== 'undefined') {
610-
611-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
612-
tipTipElement.empty();
613-
tipTipElement.append(closeButtonElement);
614-
tipTipElement.append($compile(template)(scope));
615-
$timeout(function doLater() {
616-
onTooltipShow();
617-
});
618-
}
632+
$log.error(reason);
633+
});
619634
} else {
620-
//hide tooltip because is empty
621-
tipTipElement.empty();
622-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
635+
636+
hideTemplate();
623637
}
624638
}
625639
, onTooltipSideChange = function onTooltipSideChange(newValue) {

0 commit comments

Comments
 (0)