alert_srv.ts 3.4 KB

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