alert_srv.ts 3.5 KB

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