alert_srv.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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', 0);
  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('confirm-modal', this.showConfirmModal.bind(this));
  24. this.$rootScope.onAppEvent('confirm-modal', (e, data) => {
  25. this.showConfirmModal(data);
  26. }, this.$rootScope);
  27. }
  28. set(title, text, severity, timeout) {
  29. var newAlert = {
  30. title: title || '',
  31. text: text || '',
  32. severity: severity || 'info',
  33. };
  34. var newAlertJson = angular.toJson(newAlert);
  35. // remove same alert if it already exists
  36. _.remove(this.list, function(value) {
  37. return angular.toJson(value) === newAlertJson;
  38. });
  39. this.list.push(newAlert);
  40. if (timeout > 0) {
  41. this.$timeout(() => {
  42. this.list = _.without(this.list, newAlert);
  43. }, timeout);
  44. }
  45. if (!this.$rootScope.$$phase) {
  46. this.$rootScope.$digest();
  47. }
  48. return(newAlert);
  49. }
  50. clear(alert) {
  51. this.list = _.without(this.list, alert);
  52. }
  53. clearAll() {
  54. this.list = [];
  55. }
  56. showConfirmModal(payload) {
  57. var scope = this.$rootScope.$new();
  58. scope.title = payload.title;
  59. scope.text = payload.text;
  60. scope.text2 = payload.text2;
  61. scope.onConfirm = payload.onConfirm;
  62. scope.icon = payload.icon || "fa-check";
  63. scope.yesText = payload.yesText || "Yes";
  64. scope.noText = payload.noText || "Cancel";
  65. var confirmModal = this.$modal({
  66. template: 'public/app/partials/confirm_modal.html',
  67. persist: false,
  68. modalClass: 'confirm-modal',
  69. show: false,
  70. scope: scope,
  71. keyboard: false
  72. });
  73. confirmModal.then(function(modalEl) {
  74. modalEl.modal('show');
  75. });
  76. }
  77. }
  78. coreModule.service('alertSrv', AlertSrv);