notification_edit_ctrl.ts 2.4 KB

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