alert_srv.ts 2.3 KB

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