notification_edit_ctrl.ts 2.7 KB

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