popover.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * angular-strap
  3. * @version v2.3.9 - 2016-06-10
  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 popover;
  44. var options = {
  45. scope: scope
  46. };
  47. angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'contentTemplate', 'placement', 'container', 'delay', 'trigger', 'html', 'animation', 'customClass', 'autoClose', 'id', 'prefixClass', 'prefixEvent' ], function(key) {
  48. if (angular.isDefined(attr[key])) options[key] = attr[key];
  49. });
  50. var falseValueRegExp = /^(false|0|)$/i;
  51. angular.forEach([ 'html', 'container', 'autoClose' ], function(key) {
  52. if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
  53. });
  54. angular.forEach([ 'onBeforeShow', 'onShow', 'onBeforeHide', 'onHide' ], function(key) {
  55. var bsKey = 'bs' + key.charAt(0).toUpperCase() + key.slice(1);
  56. if (angular.isDefined(attr[bsKey])) {
  57. options[key] = scope.$eval(attr[bsKey]);
  58. }
  59. });
  60. var dataTarget = element.attr('data-target');
  61. if (angular.isDefined(dataTarget)) {
  62. if (falseValueRegExp.test(dataTarget)) {
  63. options.target = false;
  64. } else {
  65. options.target = dataTarget;
  66. }
  67. }
  68. angular.forEach([ 'title', 'content' ], function(key) {
  69. if (attr[key]) {
  70. attr.$observe(key, function(newValue, oldValue) {
  71. scope[key] = $sce.trustAsHtml(newValue);
  72. if (angular.isDefined(oldValue)) {
  73. requestAnimationFrame(function() {
  74. if (popover) popover.$applyPlacement();
  75. });
  76. }
  77. });
  78. }
  79. });
  80. if (attr.bsPopover) {
  81. scope.$watch(attr.bsPopover, function(newValue, oldValue) {
  82. if (angular.isObject(newValue)) {
  83. angular.extend(scope, newValue);
  84. } else {
  85. scope.content = newValue;
  86. }
  87. if (angular.isDefined(oldValue)) {
  88. requestAnimationFrame(function() {
  89. if (popover) popover.$applyPlacement();
  90. });
  91. }
  92. }, true);
  93. }
  94. if (attr.bsShow) {
  95. scope.$watch(attr.bsShow, function(newValue, oldValue) {
  96. if (!popover || !angular.isDefined(newValue)) return;
  97. if (angular.isString(newValue)) newValue = !!newValue.match(/true|,?(popover),?/i);
  98. if (newValue === true) {
  99. popover.show();
  100. } else {
  101. popover.hide();
  102. }
  103. });
  104. }
  105. if (attr.viewport) {
  106. scope.$watch(attr.viewport, function(newValue) {
  107. if (!popover || !angular.isDefined(newValue)) return;
  108. popover.setViewport(newValue);
  109. });
  110. }
  111. popover = $popover(element, options);
  112. scope.$on('$destroy', function() {
  113. if (popover) popover.destroy();
  114. options = null;
  115. popover = null;
  116. });
  117. }
  118. };
  119. } ]);