alert.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.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. var requestAnimationFrame = $window.requestAnimationFrame || $window.setTimeout;
  49. return {
  50. restrict: 'EAC',
  51. scope: true,
  52. link: function postLink(scope, element, attr, transclusion) {
  53. var options = {
  54. scope: scope,
  55. element: element,
  56. show: false
  57. };
  58. angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'placement', 'keyboard', 'html', 'container', 'animation', 'duration', 'dismissable' ], function(key) {
  59. if (angular.isDefined(attr[key])) options[key] = attr[key];
  60. });
  61. var falseValueRegExp = /^(false|0|)$/i;
  62. angular.forEach([ 'keyboard', 'html', 'container', 'dismissable' ], function(key) {
  63. if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
  64. });
  65. if (!scope.hasOwnProperty('title')) {
  66. scope.title = '';
  67. }
  68. angular.forEach([ 'title', 'content', 'type' ], function(key) {
  69. attr[key] && attr.$observe(key, function(newValue, oldValue) {
  70. scope[key] = $sce.trustAsHtml(newValue);
  71. });
  72. });
  73. attr.bsAlert && scope.$watch(attr.bsAlert, function(newValue, oldValue) {
  74. if (angular.isObject(newValue)) {
  75. angular.extend(scope, newValue);
  76. } else {
  77. scope.content = newValue;
  78. }
  79. }, true);
  80. var alert = $alert(options);
  81. element.on(attr.trigger || 'click', alert.toggle);
  82. scope.$on('$destroy', function() {
  83. if (alert) alert.destroy();
  84. options = null;
  85. alert = null;
  86. });
  87. }
  88. };
  89. } ]);