alert_srv.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. ],
  6. function (angular, _, coreModule) {
  7. 'use strict';
  8. coreModule.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. return(newAlert);
  42. };
  43. this.clear = function(alert) {
  44. self.list = _.without(self.list,alert);
  45. };
  46. this.clearAll = function() {
  47. self.list = [];
  48. };
  49. this.showConfirmModal = function(e, payload) {
  50. var scope = $rootScope.$new();
  51. scope.title = payload.title;
  52. scope.text = payload.text;
  53. scope.onConfirm = payload.onConfirm;
  54. scope.icon = payload.icon || "fa-check";
  55. scope.yesText = payload.yesText || "Yes";
  56. scope.noText = payload.noText || "Cancel";
  57. var confirmModal = $modal({
  58. template: './app/partials/confirm_modal.html',
  59. persist: false,
  60. modalClass: 'modal-no-header confirm-modal',
  61. show: false,
  62. scope: scope,
  63. keyboard: false
  64. });
  65. $q.when(confirmModal).then(function(modalEl) {
  66. modalEl.modal('show');
  67. });
  68. };
  69. });
  70. });