2015-07-14 13:45:54 -07:00

119 lines
3.5 KiB
JavaScript

/**
* angular-strap
* @version v2.2.1 - 2015-03-10
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes (olivier@mg-crea.com)
* @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',
template: '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) {
// Common vars
var options = angular.extend({}, defaults, config);
var $popover = $tooltip(element, options);
// Support scope as string options [/*title, */content]
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) {
// Directive options
var options = {scope: scope};
angular.forEach(['template', 'contentTemplate', 'placement', 'container', 'target', 'delay', 'trigger', 'keyboard', 'html', 'animation', 'customClass', 'autoClose', 'id'], function(key) {
if(angular.isDefined(attr[key])) options[key] = attr[key];
});
// Support scope as data-attrs
angular.forEach(['title', 'content'], function(key) {
attr[key] && attr.$observe(key, function(newValue, oldValue) {
scope[key] = $sce.trustAsHtml(newValue);
angular.isDefined(oldValue) && requestAnimationFrame(function() {
popover && popover.$applyPlacement();
});
});
});
// Support scope as an object
attr.bsPopover && scope.$watch(attr.bsPopover, function(newValue, oldValue) {
if(angular.isObject(newValue)) {
angular.extend(scope, newValue);
} else {
scope.content = newValue;
}
angular.isDefined(oldValue) && requestAnimationFrame(function() {
popover && popover.$applyPlacement();
});
}, true);
// Visibility binding support
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);
newValue === true ? popover.show() : popover.hide();
});
// Viewport support
attr.viewport && scope.$watch(attr.viewport, function (newValue) {
if(!popover || !angular.isDefined(newValue)) return;
popover.setViewport(newValue);
});
// Initialize popover
var popover = $popover(element, options);
// Garbage collection
scope.$on('$destroy', function() {
if (popover) popover.destroy();
options = null;
popover = null;
});
}
};
}]);