alert_srv.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. ],
  6. function (angular, _, coreModule) {
  7. 'use strict';
  8. coreModule.default.service('alertSrv', function($timeout, $sce, $rootScope, $modal, $q) {
  9. var self = this;
  10. this.init = function() {
  11. $rootScope.onAppEvent('alert-error', function(e, alert) {
  12. self.set(alert[0], alert[1], 'error');
  13. }, $rootScope);
  14. $rootScope.onAppEvent('alert-warning', function(e, alert) {
  15. self.set(alert[0], alert[1], 'warning', 5000);
  16. }, $rootScope);
  17. $rootScope.onAppEvent('alert-success', function(e, alert) {
  18. self.set(alert[0], alert[1], 'success', 3000);
  19. }, $rootScope);
  20. $rootScope.onAppEvent('confirm-modal', this.showConfirmModal, $rootScope);
  21. };
  22. // List of all alert objects
  23. this.list = [];
  24. this.set = function(title,text,severity,timeout) {
  25. var newAlert = {
  26. title: title || '',
  27. text: text || '',
  28. severity: severity || 'info',
  29. };
  30. var newAlertJson = angular.toJson(newAlert);
  31. // remove same alert if it already exists
  32. _.remove(self.list, function(value) {
  33. return angular.toJson(value) === newAlertJson;
  34. });
  35. self.list.push(newAlert);
  36. if (timeout > 0) {
  37. $timeout(function() {
  38. self.list = _.without(self.list,newAlert);
  39. }, timeout);
  40. }
  41. if (!$rootScope.$$phase) {
  42. $rootScope.$digest();
  43. }
  44. return(newAlert);
  45. };
  46. this.clear = function(alert) {
  47. self.list = _.without(self.list,alert);
  48. };
  49. this.clearAll = function() {
  50. self.list = [];
  51. };
  52. this.showConfirmModal = function(e, payload) {
  53. var scope = $rootScope.$new();
  54. scope.title = payload.title;
  55. scope.text = payload.text;
  56. scope.onConfirm = payload.onConfirm;
  57. scope.icon = payload.icon || "fa-check";
  58. scope.yesText = payload.yesText || "Yes";
  59. scope.noText = payload.noText || "Cancel";
  60. var confirmModal = $modal({
  61. template: 'public/app/partials/confirm_modal.html',
  62. persist: false,
  63. modalClass: 'modal-no-header confirm-modal',
  64. show: false,
  65. scope: scope,
  66. keyboard: false
  67. });
  68. $q.when(confirmModal).then(function(modalEl) {
  69. modalEl.modal('show');
  70. });
  71. };
  72. });
  73. });