alert_srv.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.text2 = payload.text2;
  57. scope.onConfirm = payload.onConfirm;
  58. scope.icon = payload.icon || "fa-check";
  59. scope.yesText = payload.yesText || "Yes";
  60. scope.noText = payload.noText || "Cancel";
  61. var confirmModal = $modal({
  62. template: 'public/app/partials/confirm_modal.html',
  63. persist: false,
  64. modalClass: 'confirm-modal',
  65. show: false,
  66. scope: scope,
  67. keyboard: false
  68. });
  69. $q.when(confirmModal).then(function(modalEl) {
  70. modalEl.modal('show');
  71. });
  72. };
  73. });
  74. });