notification_edit_ctrl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
  23. this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
  24. this.isNew = !this.$routeParams.id;
  25. this.backendSrv
  26. .get(`/api/alert-notifiers`)
  27. .then(notifiers => {
  28. this.notifiers = notifiers;
  29. // add option templates
  30. for (let notifier of this.notifiers) {
  31. this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
  32. }
  33. if (!this.$routeParams.id) {
  34. this.navModel.breadcrumbs.push({ text: 'New channel' });
  35. this.navModel.node = { text: 'New channel' };
  36. return _.defaults(this.model, this.defaults);
  37. }
  38. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  39. this.navModel.breadcrumbs.push({ text: result.name });
  40. this.navModel.node = { text: result.name };
  41. return result;
  42. });
  43. })
  44. .then(model => {
  45. this.model = model;
  46. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  47. });
  48. }
  49. save() {
  50. if (!this.theForm.$valid) {
  51. return;
  52. }
  53. if (this.model.id) {
  54. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  55. this.model = res;
  56. appEvents.emit('alert-success', ['Notification updated', '']);
  57. });
  58. } else {
  59. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  60. appEvents.emit('alert-success', ['Notification created', '']);
  61. this.$location.path('alerting/notifications');
  62. });
  63. }
  64. }
  65. getNotifierTemplateId(type) {
  66. return `notifier-options-${type}`;
  67. }
  68. typeChanged() {
  69. this.model.settings = {};
  70. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  71. }
  72. testNotification() {
  73. if (!this.theForm.$valid) {
  74. return;
  75. }
  76. var payload = {
  77. name: this.model.name,
  78. type: this.model.type,
  79. settings: this.model.settings,
  80. };
  81. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  82. appEvents.emit('alert-success', ['Test notification sent', '']);
  83. });
  84. }
  85. }
  86. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);