notification_edit_ctrl.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. testSeverity = "critical";
  9. notifiers: any;
  10. notifierTemplateId: string;
  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) {
  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-succes', ['Test notification sent', '']);
  75. });
  76. }
  77. }
  78. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);