popover.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * angular-strap
  3. * @version v2.3.5 - 2015-10-29
  4. * @link http://mgcrea.github.io/angular-strap
  5. * @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. 'use strict';
  9. angular.module('mgcrea.ngStrap.popover', [ 'mgcrea.ngStrap.tooltip' ]).provider('$popover', function() {
  10. var defaults = this.defaults = {
  11. animation: 'am-fade',
  12. customClass: '',
  13. container: false,
  14. target: false,
  15. placement: 'right',
  16. templateUrl: 'popover/popover.tpl.html',
  17. contentTemplate: false,
  18. trigger: 'click',
  19. keyboard: true,
  20. html: false,
  21. title: '',
  22. content: '',
  23. delay: 0,
  24. autoClose: false
  25. };
  26. this.$get = [ '$tooltip', function($tooltip) {
  27. function PopoverFactory(element, config) {
  28. var options = angular.extend({}, defaults, config);
  29. var $popover = $tooltip(element, options);
  30. if (options.content) {
  31. $popover.$scope.content = options.content;
  32. }
  33. return $popover;
  34. }
  35. return PopoverFactory;
  36. } ];
  37. }).directive('bsPopover', [ '$window', '$sce', '$popover', function($window, $sce, $popover) {
  38. var requestAnimationFrame = $window.requestAnimationFrame || $window.setTimeout;
  39. return {
  40. restrict: 'EAC',
  41. scope: true,
  42. link: function postLink(scope, element, attr) {
  43. var options = {
  44. scope: scope
  45. };
  46. angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'contentTemplate', 'placement', 'container', 'delay', 'trigger', 'html', 'animation', 'customClass', 'autoClose', 'id', 'prefixClass', 'prefixEvent' ], function(key) {
  47. if (angular.isDefined(attr[key])) options[key] = attr[key];
  48. });
  49. var falseValueRegExp = /^(false|0|)$/i;
  50. angular.forEach([ 'html', 'container', 'autoClose' ], function(key) {
  51. if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
  52. });
  53. var dataTarget = element.attr('data-target');
  54. if (angular.isDefined(dataTarget)) {
  55. if (falseValueRegExp.test(dataTarget)) options.target = false; else options.target = dataTarget;
  56. }
  57. angular.forEach([ 'title', 'content' ], function(key) {
  58. attr[key] && attr.$observe(key, function(newValue, oldValue) {
  59. scope[key] = $sce.trustAsHtml(newValue);
  60. angular.isDefined(oldValue) && requestAnimationFrame(function() {
  61. popover && popover.$applyPlacement();
  62. });
  63. });
  64. });
  65. attr.bsPopover && scope.$watch(attr.bsPopover, function(newValue, oldValue) {
  66. if (angular.isObject(newValue)) {
  67. angular.extend(scope, newValue);
  68. } else {
  69. scope.content = newValue;
  70. }
  71. angular.isDefined(oldValue) && requestAnimationFrame(function() {
  72. popover && popover.$applyPlacement();
  73. });
  74. }, true);
  75. attr.bsShow && scope.$watch(attr.bsShow, function(newValue, oldValue) {
  76. if (!popover || !angular.isDefined(newValue)) return;
  77. if (angular.isString(newValue)) newValue = !!newValue.match(/true|,?(popover),?/i);
  78. newValue === true ? popover.show() : popover.hide();
  79. });
  80. attr.viewport && scope.$watch(attr.viewport, function(newValue) {
  81. if (!popover || !angular.isDefined(newValue)) return;
  82. popover.setViewport(newValue);
  83. });
  84. var popover = $popover(element, options);
  85. scope.$on('$destroy', function() {
  86. if (popover) popover.destroy();
  87. options = null;
  88. popover = null;
  89. });
  90. }
  91. };
  92. } ]);