notification_edit_ctrl.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import {appEvents, coreModule} from 'app/core/core';
  6. export class AlertNotificationEditCtrl {
  7. theForm: any;
  8. navModel: any;
  9. testSeverity = "critical";
  10. notifiers: any;
  11. notifierTemplateId: string;
  12. model: any;
  13. defaults: any = {
  14. type: 'email',
  15. settings: {
  16. httpMethod: 'POST',
  17. autoResolve: true,
  18. uploadImage: true,
  19. },
  20. isDefault: false
  21. };
  22. /** @ngInject */
  23. constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
  24. this.navModel = navModelSrv.getAlertingNav();
  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. return _.defaults(this.model, this.defaults);
  33. }
  34. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  35. return result;
  36. });
  37. }).then(model => {
  38. this.model = model;
  39. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  40. });
  41. }
  42. save() {
  43. if (!this.theForm.$valid) {
  44. return;
  45. }
  46. if (this.model.id) {
  47. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  48. this.model = res;
  49. appEvents.emit('alert-success', ['Notification updated', '']);
  50. });
  51. } else {
  52. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  53. appEvents.emit('alert-success', ['Notification created', '']);
  54. this.$location.path('alerting/notifications');
  55. });
  56. }
  57. }
  58. getNotifierTemplateId(type) {
  59. return `notifier-options-${type}`;
  60. }
  61. typeChanged() {
  62. this.model.settings = {};
  63. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  64. }
  65. testNotification() {
  66. if (!this.theForm.$valid) {
  67. return;
  68. }
  69. var payload = {
  70. name: this.model.name,
  71. type: this.model.type,
  72. settings: this.model.settings,
  73. };
  74. this.backendSrv.post(`/api/alert-notifications/test`, payload)
  75. .then(res => {
  76. appEvents.emit('alert-succes', ['Test notification sent', '']);
  77. });
  78. }
  79. }
  80. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);