alert_srv.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import appEvents from 'app/core/app_events';
  5. export class AlertSrv {
  6. list: any[];
  7. /** @ngInject */
  8. constructor(private $timeout, private $rootScope, private $modal) {
  9. this.list = [];
  10. }
  11. init() {
  12. this.$rootScope.onAppEvent(
  13. 'alert-error',
  14. (e, alert) => {
  15. this.set(alert[0], alert[1], 'error', 12000);
  16. },
  17. this.$rootScope
  18. );
  19. this.$rootScope.onAppEvent(
  20. 'alert-warning',
  21. (e, alert) => {
  22. this.set(alert[0], alert[1], 'warning', 5000);
  23. },
  24. this.$rootScope
  25. );
  26. this.$rootScope.onAppEvent(
  27. 'alert-success',
  28. (e, alert) => {
  29. this.set(alert[0], alert[1], 'success', 3000);
  30. },
  31. this.$rootScope
  32. );
  33. appEvents.on('alert-warning', options => this.set(options[0], options[1], 'warning', 5000));
  34. appEvents.on('alert-success', options => this.set(options[0], options[1], 'success', 3000));
  35. appEvents.on('alert-error', options => this.set(options[0], options[1], 'error', 7000));
  36. appEvents.on('confirm-modal', this.showConfirmModal.bind(this));
  37. }
  38. getIconForSeverity(severity) {
  39. switch (severity) {
  40. case 'success':
  41. return 'fa fa-check';
  42. case 'error':
  43. return 'fa fa-exclamation-triangle';
  44. default:
  45. return 'fa fa-exclamation';
  46. }
  47. }
  48. set(title, text, severity, timeout) {
  49. if (_.isObject(text)) {
  50. console.log('alert error', text);
  51. if (text.statusText) {
  52. text = `HTTP Error (${text.status}) ${text.statusText}`;
  53. }
  54. }
  55. var newAlert = {
  56. title: title || '',
  57. text: text || '',
  58. severity: severity || 'info',
  59. icon: this.getIconForSeverity(severity),
  60. };
  61. var newAlertJson = angular.toJson(newAlert);
  62. // remove same alert if it already exists
  63. _.remove(this.list, function(value) {
  64. return angular.toJson(value) === newAlertJson;
  65. });
  66. this.list.push(newAlert);
  67. if (timeout > 0) {
  68. this.$timeout(() => {
  69. this.list = _.without(this.list, newAlert);
  70. }, timeout);
  71. }
  72. if (!this.$rootScope.$$phase) {
  73. this.$rootScope.$digest();
  74. }
  75. return newAlert;
  76. }
  77. clear(alert) {
  78. this.list = _.without(this.list, alert);
  79. }
  80. clearAll() {
  81. this.list = [];
  82. }
  83. showConfirmModal(payload) {
  84. var scope = this.$rootScope.$new();
  85. scope.onConfirm = function() {
  86. payload.onConfirm();
  87. scope.dismiss();
  88. };
  89. scope.updateConfirmText = function(value) {
  90. scope.confirmTextValid = payload.confirmText.toLowerCase() === value.toLowerCase();
  91. };
  92. scope.title = payload.title;
  93. scope.text = payload.text;
  94. scope.text2 = payload.text2;
  95. scope.confirmText = payload.confirmText;
  96. scope.onConfirm = payload.onConfirm;
  97. scope.onAltAction = payload.onAltAction;
  98. scope.altActionText = payload.altActionText;
  99. scope.icon = payload.icon || 'fa-check';
  100. scope.yesText = payload.yesText || 'Yes';
  101. scope.noText = payload.noText || 'Cancel';
  102. scope.confirmTextValid = scope.confirmText ? false : true;
  103. var confirmModal = this.$modal({
  104. template: 'public/app/partials/confirm_modal.html',
  105. persist: false,
  106. modalClass: 'confirm-modal',
  107. show: false,
  108. scope: scope,
  109. keyboard: false,
  110. });
  111. confirmModal.then(function(modalEl) {
  112. modalEl.modal('show');
  113. });
  114. }
  115. }
  116. coreModule.service('alertSrv', AlertSrv);