-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswAngular-Popover.js
105 lines (98 loc) · 4.15 KB
/
swAngular-Popover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
angular.module('swAngularPopover', [])
.service('swPopoverService', function() {
var popoverRepository = {};
return {
popoverRepository: popoverRepository,
close: function(id) {
popoverRepository[id].popover('destroy');
}
};
})
.directive('swAngularPopover', function (swPopoverService, $compile, $sce) {
return {
restrict: 'A',
scope: {
content: '@swAngularPopover',
url: '@swUrl',
options: '=swOptions',
popoverId: '@swPopoverId'
},
link: function ($scope, $element) {
swPopoverService.popoverRepository[$scope.popoverId] = $element;
var htmlParam = true;
var animationParam = true;
var placementParam = 'auto';
var triggerParam = 'click'; //click | hover | focus | manual
var titleParam = '';
var delayParam = 0;
var containerParam = false;
for (var fieldKey in $scope.options) {
switch (fieldKey) {
case 'html':
htmlParam = $scope.options.html;
break;
case 'animation':
animationParam = $scope.options.animation;
break;
case 'placement':
placementParam = $scope.options.placement;
break;
case 'trigger':
triggerParam = $scope.options.trigger;
break;
case 'title':
titleParam = $scope.options.title;
break;
case 'delay':
delayParam = $scope.options.delay;
break;
case 'container':
containerParam = $scope.options.container;
break;
default:
break;
}
}
var contentElement;
if ($scope.url && $scope.url.length > 0) {
/*
* need two capsulating elements
* probably because:
* - contents() dismisses one layer
* - <div data-ng-include>-element is compiled to comment
* - popover dismisses comments without encapsulating html element
*/
contentElement = angular.element('<div><div><div data-ng-include="\'' + $scope.url + '\'"></div></div></div>');
} else {
contentElement = angular.element('<span>' + $scope.content + '</span>');
}
$compile(contentElement)($scope.$parent);
// Store contents
var contents = contentElement.contents();
var isShown = false;
$element.bind(triggerParam, function () {
if (!isShown) {
$element.popover({
html: htmlParam,
content: contents,
animation: animationParam,
placement: placementParam,
trigger: 'manual',
title: titleParam,
delay: delayParam,
container: containerParam
});
$element.on('shown.bs.popover', function() {
isShown = true;
});
$element.on('hidden.bs.popover', function() {
isShown = false;
});
$element.popover('show');
} else {
$element.popover('destroy');
}
});
}
}
});