notification_edit_ctrl.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from "lodash";
  3. import { appEvents, coreModule } from "app/core/core";
  4. export class AlertNotificationEditCtrl {
  5. theForm: any;
  6. navModel: any;
  7. testSeverity = "critical";
  8. notifiers: any;
  9. notifierTemplateId: string;
  10. isNew: boolean;
  11. model: any;
  12. defaults: any = {
  13. type: "email",
  14. settings: {
  15. httpMethod: "POST",
  16. autoResolve: true,
  17. uploadImage: true
  18. },
  19. isDefault: false
  20. };
  21. /** @ngInject */
  22. constructor(
  23. private $routeParams,
  24. private backendSrv,
  25. private $location,
  26. private $templateCache,
  27. navModelSrv
  28. ) {
  29. this.navModel = navModelSrv.getNav("alerting", "channels", 0);
  30. this.isNew = !this.$routeParams.id;
  31. this.backendSrv
  32. .get(`/api/alert-notifiers`)
  33. .then(notifiers => {
  34. this.notifiers = notifiers;
  35. // add option templates
  36. for (let notifier of this.notifiers) {
  37. this.$templateCache.put(
  38. this.getNotifierTemplateId(notifier.type),
  39. notifier.optionsTemplate
  40. );
  41. }
  42. if (!this.$routeParams.id) {
  43. this.navModel.breadcrumbs.push({ text: "New channel" });
  44. this.navModel.node = { text: "New channel" };
  45. return _.defaults(this.model, this.defaults);
  46. }
  47. return this.backendSrv
  48. .get(`/api/alert-notifications/${this.$routeParams.id}`)
  49. .then(result => {
  50. this.navModel.breadcrumbs.push({ text: result.name });
  51. this.navModel.node = { text: result.name };
  52. return result;
  53. });
  54. })
  55. .then(model => {
  56. this.model = model;
  57. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  58. });
  59. }
  60. save() {
  61. if (!this.theForm.$valid) {
  62. return;
  63. }
  64. if (this.model.id) {
  65. this.backendSrv
  66. .put(`/api/alert-notifications/${this.model.id}`, this.model)
  67. .then(res => {
  68. this.model = res;
  69. appEvents.emit("alert-success", ["Notification updated", ""]);
  70. });
  71. } else {
  72. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  73. appEvents.emit("alert-success", ["Notification created", ""]);
  74. this.$location.path("alerting/notifications");
  75. });
  76. }
  77. }
  78. getNotifierTemplateId(type) {
  79. return `notifier-options-${type}`;
  80. }
  81. typeChanged() {
  82. this.model.settings = {};
  83. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  84. }
  85. testNotification() {
  86. if (!this.theForm.$valid) {
  87. return;
  88. }
  89. var payload = {
  90. name: this.model.name,
  91. type: this.model.type,
  92. settings: this.model.settings
  93. };
  94. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  95. appEvents.emit("alert-success", ["Test notification sent", ""]);
  96. });
  97. }
  98. }
  99. coreModule.controller("AlertNotificationEditCtrl", AlertNotificationEditCtrl);