alertSrv.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.services');
  8. module.service('alertSrv', function($timeout, $sce, $rootScope, $modal, $q) {
  9. var self = this;
  10. this.init = function() {
  11. $rootScope.onAppEvent('alert-error', function(e, alert) {
  12. self.set(alert[0], alert[1], 'error');
  13. });
  14. $rootScope.onAppEvent('alert-warning', function(e, alert) {
  15. self.set(alert[0], alert[1], 'warning', 5000);
  16. });
  17. $rootScope.onAppEvent('alert-success', function(e, alert) {
  18. self.set(alert[0], alert[1], 'success', 3000);
  19. });
  20. $rootScope.onAppEvent('confirm-modal', this.showConfirmModal);
  21. };
  22. // List of all alert objects
  23. this.list = [];
  24. this.set = function(title,text,severity,timeout) {
  25. var
  26. _a = {
  27. title: title || '',
  28. text: $sce.trustAsHtml(text || ''),
  29. severity: severity || 'info',
  30. },
  31. _ca = angular.toJson(_a),
  32. _clist = _.map(self.list,function(alert) {return angular.toJson(alert);});
  33. // If we already have this alert, remove it and add a new one
  34. // Why do this instead of skipping the add because it resets the timer
  35. if(_.contains(_clist,_ca)) {
  36. _.remove(self.list,_.indexOf(_clist,_ca));
  37. }
  38. self.list.push(_a);
  39. if (timeout > 0) {
  40. $timeout(function() {
  41. self.list = _.without(self.list,_a);
  42. }, timeout);
  43. }
  44. return(_a);
  45. };
  46. this.clear = function(alert) {
  47. self.list = _.without(self.list,alert);
  48. };
  49. this.clearAll = function() {
  50. self.list = [];
  51. };
  52. this.showConfirmModal = function(e, payload) {
  53. var scope = $rootScope.$new();
  54. scope.title = payload.title;
  55. scope.text = payload.text;
  56. scope.onConfirm = payload.onConfirm;
  57. var confirmModal = $modal({
  58. template: './app/partials/confirm_modal.html',
  59. persist: true,
  60. show: false,
  61. scope: scope,
  62. keyboard: false
  63. });
  64. $q.when(confirmModal).then(function(modalEl) {
  65. modalEl.modal('show');
  66. });
  67. };
  68. });
  69. });