alert_srv.ts 3.4 KB

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