alert_srv.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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', 7000);
  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-error', options => {
  24. this.set(options[0], options[1], 'error', 7000);
  25. });
  26. appEvents.on('confirm-modal', this.showConfirmModal.bind(this));
  27. }
  28. set(title, text, severity, timeout) {
  29. if (_.isObject(text)) {
  30. console.log('alert error', text);
  31. if (text.statusText) {
  32. text = `HTTP Error (${text.status}) ${text.statusText}`;
  33. }
  34. }
  35. var newAlert = {
  36. title: title || '',
  37. text: text || '',
  38. severity: severity || 'info',
  39. };
  40. var newAlertJson = angular.toJson(newAlert);
  41. // remove same alert if it already exists
  42. _.remove(this.list, function(value) {
  43. return angular.toJson(value) === newAlertJson;
  44. });
  45. this.list.push(newAlert);
  46. if (timeout > 0) {
  47. this.$timeout(() => {
  48. this.list = _.without(this.list, newAlert);
  49. }, timeout);
  50. }
  51. if (!this.$rootScope.$$phase) {
  52. this.$rootScope.$digest();
  53. }
  54. return(newAlert);
  55. }
  56. clear(alert) {
  57. this.list = _.without(this.list, alert);
  58. }
  59. clearAll() {
  60. this.list = [];
  61. }
  62. showConfirmModal(payload) {
  63. var scope = this.$rootScope.$new();
  64. scope.onConfirm = function() {
  65. payload.onConfirm();
  66. scope.dismiss();
  67. };
  68. scope.updateConfirmText = function(value) {
  69. scope.confirmTextValid = payload.confirmText.toLowerCase() === value.toLowerCase();
  70. };
  71. scope.title = payload.title;
  72. scope.text = payload.text;
  73. scope.text2 = payload.text2;
  74. scope.confirmText = payload.confirmText;
  75. scope.onConfirm = payload.onConfirm;
  76. scope.onAltAction = payload.onAltAction;
  77. scope.altActionText = payload.altActionText;
  78. scope.icon = payload.icon || "fa-check";
  79. scope.yesText = payload.yesText || "Yes";
  80. scope.noText = payload.noText || "Cancel";
  81. scope.confirmTextValid = scope.confirmText ? false : true;
  82. var confirmModal = this.$modal({
  83. template: 'public/app/partials/confirm_modal.html',
  84. persist: false,
  85. modalClass: 'confirm-modal',
  86. show: false,
  87. scope: scope,
  88. keyboard: false
  89. });
  90. confirmModal.then(function(modalEl) {
  91. modalEl.modal('show');
  92. });
  93. }
  94. }
  95. coreModule.service('alertSrv', AlertSrv);