notification_edit_ctrl.ts 2.7 KB

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