alert.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.alert', [ 'mgcrea.ngStrap.modal' ]).provider('$alert', function() {
  10. var defaults = this.defaults = {
  11. animation: 'am-fade',
  12. prefixClass: 'alert',
  13. prefixEvent: 'alert',
  14. placement: null,
  15. templateUrl: 'alert/alert.tpl.html',
  16. container: false,
  17. element: null,
  18. backdrop: false,
  19. keyboard: true,
  20. show: true,
  21. duration: false,
  22. type: false,
  23. dismissable: true
  24. };
  25. this.$get = [ '$modal', '$timeout', function($modal, $timeout) {
  26. function AlertFactory(config) {
  27. var $alert = {};
  28. var options = angular.extend({}, defaults, config);
  29. $alert = $modal(options);
  30. $alert.$scope.dismissable = !!options.dismissable;
  31. if (options.type) {
  32. $alert.$scope.type = options.type;
  33. }
  34. var show = $alert.show;
  35. if (options.duration) {
  36. $alert.show = function() {
  37. show();
  38. $timeout(function() {
  39. $alert.hide();
  40. }, options.duration * 1e3);
  41. };
  42. }
  43. return $alert;
  44. }
  45. return AlertFactory;
  46. } ];
  47. }).directive('bsAlert', [ '$window', '$sce', '$alert', function($window, $sce, $alert) {
  48. return {
  49. restrict: 'EAC',
  50. scope: true,
  51. link: function postLink(scope, element, attr, transclusion) {
  52. var options = {
  53. scope: scope,
  54. element: element,
  55. show: false
  56. };
  57. angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'placement', 'keyboard', 'html', 'container', 'animation', 'duration', 'dismissable' ], function(key) {
  58. if (angular.isDefined(attr[key])) options[key] = attr[key];
  59. });
  60. var falseValueRegExp = /^(false|0|)$/i;
  61. angular.forEach([ 'keyboard', 'html', 'container', 'dismissable' ], function(key) {
  62. if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
  63. });
  64. angular.forEach([ 'onBeforeShow', 'onShow', 'onBeforeHide', 'onHide' ], function(key) {
  65. var bsKey = 'bs' + key.charAt(0).toUpperCase() + key.slice(1);
  66. if (angular.isDefined(attr[bsKey])) {
  67. options[key] = scope.$eval(attr[bsKey]);
  68. }
  69. });
  70. if (!scope.hasOwnProperty('title')) {
  71. scope.title = '';
  72. }
  73. angular.forEach([ 'title', 'content', 'type' ], function(key) {
  74. if (attr[key]) {
  75. attr.$observe(key, function(newValue, oldValue) {
  76. scope[key] = $sce.trustAsHtml(newValue);
  77. });
  78. }
  79. });
  80. if (attr.bsAlert) {
  81. scope.$watch(attr.bsAlert, function(newValue, oldValue) {
  82. if (angular.isObject(newValue)) {
  83. angular.extend(scope, newValue);
  84. } else {
  85. scope.content = newValue;
  86. }
  87. }, true);
  88. }
  89. var alert = $alert(options);
  90. element.on(attr.trigger || 'click', alert.toggle);
  91. scope.$on('$destroy', function() {
  92. if (alert) alert.destroy();
  93. options = null;
  94. alert = null;
  95. });
  96. }
  97. };
  98. } ]);