forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopover.js
More file actions
114 lines (113 loc) · 3.81 KB
/
popover.js
File metadata and controls
114 lines (113 loc) · 3.81 KB
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
106
107
108
109
110
111
112
113
114
/**
* angular-strap
* @version v2.3.8 - 2016-03-31
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
'use strict';
angular.module('mgcrea.ngStrap.popover', [ 'mgcrea.ngStrap.tooltip' ]).provider('$popover', function() {
var defaults = this.defaults = {
animation: 'am-fade',
customClass: '',
container: false,
target: false,
placement: 'right',
templateUrl: 'popover/popover.tpl.html',
contentTemplate: false,
trigger: 'click',
keyboard: true,
html: false,
title: '',
content: '',
delay: 0,
autoClose: false
};
this.$get = [ '$tooltip', function($tooltip) {
function PopoverFactory(element, config) {
var options = angular.extend({}, defaults, config);
var $popover = $tooltip(element, options);
if (options.content) {
$popover.$scope.content = options.content;
}
return $popover;
}
return PopoverFactory;
} ];
}).directive('bsPopover', [ '$window', '$sce', '$popover', function($window, $sce, $popover) {
var requestAnimationFrame = $window.requestAnimationFrame || $window.setTimeout;
return {
restrict: 'EAC',
scope: true,
link: function postLink(scope, element, attr) {
var popover;
var options = {
scope: scope
};
angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'contentTemplate', 'placement', 'container', 'delay', 'trigger', 'html', 'animation', 'customClass', 'autoClose', 'id', 'prefixClass', 'prefixEvent' ], function(key) {
if (angular.isDefined(attr[key])) options[key] = attr[key];
});
var falseValueRegExp = /^(false|0|)$/i;
angular.forEach([ 'html', 'container', 'autoClose' ], function(key) {
if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
});
var dataTarget = element.attr('data-target');
if (angular.isDefined(dataTarget)) {
if (falseValueRegExp.test(dataTarget)) {
options.target = false;
} else {
options.target = dataTarget;
}
}
angular.forEach([ 'title', 'content' ], function(key) {
if (attr[key]) {
attr.$observe(key, function(newValue, oldValue) {
scope[key] = $sce.trustAsHtml(newValue);
if (angular.isDefined(oldValue)) {
requestAnimationFrame(function() {
if (popover) popover.$applyPlacement();
});
}
});
}
});
if (attr.bsPopover) {
scope.$watch(attr.bsPopover, function(newValue, oldValue) {
if (angular.isObject(newValue)) {
angular.extend(scope, newValue);
} else {
scope.content = newValue;
}
if (angular.isDefined(oldValue)) {
requestAnimationFrame(function() {
if (popover) popover.$applyPlacement();
});
}
}, true);
}
if (attr.bsShow) {
scope.$watch(attr.bsShow, function(newValue, oldValue) {
if (!popover || !angular.isDefined(newValue)) return;
if (angular.isString(newValue)) newValue = !!newValue.match(/true|,?(popover),?/i);
if (newValue === true) {
popover.show();
} else {
popover.hide();
}
});
}
if (attr.viewport) {
scope.$watch(attr.viewport, function(newValue) {
if (!popover || !angular.isDefined(newValue)) return;
popover.setViewport(newValue);
});
}
popover = $popover(element, options);
scope.$on('$destroy', function() {
if (popover) popover.destroy();
options = null;
popover = null;
});
}
};
} ]);