alert_srv.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from "angular";
  3. import _ from "lodash";
  4. import coreModule from "app/core/core_module";
  5. import appEvents from "app/core/app_events";
  6. export class AlertSrv {
  7. list: any[];
  8. /** @ngInject */
  9. constructor(private $timeout, private $rootScope, private $modal) {
  10. this.list = [];
  11. }
  12. init() {
  13. this.$rootScope.onAppEvent(
  14. "alert-error",
  15. (e, alert) => {
  16. this.set(alert[0], alert[1], "error", 12000);
  17. },
  18. this.$rootScope
  19. );
  20. this.$rootScope.onAppEvent(
  21. "alert-warning",
  22. (e, alert) => {
  23. this.set(alert[0], alert[1], "warning", 5000);
  24. },
  25. this.$rootScope
  26. );
  27. this.$rootScope.onAppEvent(
  28. "alert-success",
  29. (e, alert) => {
  30. this.set(alert[0], alert[1], "success", 3000);
  31. },
  32. this.$rootScope
  33. );
  34. appEvents.on("alert-warning", options =>
  35. this.set(options[0], options[1], "warning", 5000)
  36. );
  37. appEvents.on("alert-success", options =>
  38. this.set(options[0], options[1], "success", 3000)
  39. );
  40. appEvents.on("alert-error", options =>
  41. this.set(options[0], options[1], "error", 7000)
  42. );
  43. appEvents.on("confirm-modal", this.showConfirmModal.bind(this));
  44. }
  45. getIconForSeverity(severity) {
  46. switch (severity) {
  47. case "success":
  48. return "fa fa-check";
  49. case "error":
  50. return "fa fa-exclamation-triangle";
  51. default:
  52. return "fa fa-exclamation";
  53. }
  54. }
  55. set(title, text, severity, timeout) {
  56. if (_.isObject(text)) {
  57. console.log("alert error", text);
  58. if (text.statusText) {
  59. text = `HTTP Error (${text.status}) ${text.statusText}`;
  60. }
  61. }
  62. var newAlert = {
  63. title: title || "",
  64. text: text || "",
  65. severity: severity || "info",
  66. icon: this.getIconForSeverity(severity)
  67. };
  68. var newAlertJson = angular.toJson(newAlert);
  69. // remove same alert if it already exists
  70. _.remove(this.list, function(value) {
  71. return angular.toJson(value) === newAlertJson;
  72. });
  73. this.list.push(newAlert);
  74. if (timeout > 0) {
  75. this.$timeout(() => {
  76. this.list = _.without(this.list, newAlert);
  77. }, timeout);
  78. }
  79. if (!this.$rootScope.$$phase) {
  80. this.$rootScope.$digest();
  81. }
  82. return newAlert;
  83. }
  84. clear(alert) {
  85. this.list = _.without(this.list, alert);
  86. }
  87. clearAll() {
  88. this.list = [];
  89. }
  90. showConfirmModal(payload) {
  91. var scope = this.$rootScope.$new();
  92. scope.onConfirm = function() {
  93. payload.onConfirm();
  94. scope.dismiss();
  95. };
  96. scope.updateConfirmText = function(value) {
  97. scope.confirmTextValid =
  98. payload.confirmText.toLowerCase() === value.toLowerCase();
  99. };
  100. scope.title = payload.title;
  101. scope.text = payload.text;
  102. scope.text2 = payload.text2;
  103. scope.confirmText = payload.confirmText;
  104. scope.onConfirm = payload.onConfirm;
  105. scope.onAltAction = payload.onAltAction;
  106. scope.altActionText = payload.altActionText;
  107. scope.icon = payload.icon || "fa-check";
  108. scope.yesText = payload.yesText || "Yes";
  109. scope.noText = payload.noText || "Cancel";
  110. scope.confirmTextValid = scope.confirmText ? false : true;
  111. var confirmModal = this.$modal({
  112. template: "public/app/partials/confirm_modal.html",
  113. persist: false,
  114. modalClass: "confirm-modal",
  115. show: false,
  116. scope: scope,
  117. keyboard: false
  118. });
  119. confirmModal.then(function(modalEl) {
  120. modalEl.modal("show");
  121. });
  122. }
  123. }
  124. coreModule.service("alertSrv", AlertSrv);