editor_ctrl.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import coreModule from 'app/core/core_module';
  5. import { DashboardModel } from 'app/features/dashboard/state';
  6. import DatasourceSrv from '../plugins/datasource_srv';
  7. export class AnnotationsEditorCtrl {
  8. mode: any;
  9. datasources: any;
  10. annotations: any;
  11. currentAnnotation: any;
  12. currentDatasource: any;
  13. currentIsNew: any;
  14. dashboard: DashboardModel;
  15. annotationDefaults: any = {
  16. name: '',
  17. datasource: null,
  18. iconColor: 'rgba(255, 96, 96, 1)',
  19. enable: true,
  20. showIn: 0,
  21. hide: false,
  22. };
  23. emptyListCta = {
  24. title: 'There are no custom annotation queries added yet',
  25. buttonIcon: 'gicon gicon-annotation',
  26. buttonTitle: 'Add Annotation Query',
  27. infoBox: {
  28. __html: `<p>Annotations provide a way to integrate event data into your graphs. They are visualized as vertical lines
  29. and icons on all graph panels. When you hover over an annotation icon you can get event text &amp; tags for
  30. the event. You can add annotation events directly from grafana by holding CTRL or CMD + click on graph (or
  31. drag region). These will be stored in Grafana's annotation database.
  32. </p>
  33. Checkout the
  34. <a class='external-link' target='_blank' href='http://docs.grafana.org/reference/annotations/'
  35. >Annotations documentation</a
  36. >
  37. for more information.`,
  38. },
  39. infoBoxTitle: 'What are annotations?',
  40. };
  41. showOptions: any = [{ text: 'All Panels', value: 0 }, { text: 'Specific Panels', value: 1 }];
  42. /** @ngInject */
  43. constructor($scope: any, private datasourceSrv: DatasourceSrv) {
  44. $scope.ctrl = this;
  45. this.dashboard = $scope.dashboard;
  46. this.mode = 'list';
  47. this.datasources = datasourceSrv.getAnnotationSources();
  48. this.annotations = this.dashboard.annotations.list;
  49. this.reset();
  50. this.onColorChange = this.onColorChange.bind(this);
  51. }
  52. async datasourceChanged() {
  53. return (this.currentDatasource = await this.datasourceSrv.get(this.currentAnnotation.datasource));
  54. }
  55. edit(annotation: any) {
  56. this.currentAnnotation = annotation;
  57. this.currentAnnotation.showIn = this.currentAnnotation.showIn || 0;
  58. this.currentIsNew = false;
  59. this.datasourceChanged();
  60. this.mode = 'edit';
  61. $('.tooltip.in').remove();
  62. }
  63. reset() {
  64. this.currentAnnotation = angular.copy(this.annotationDefaults);
  65. this.currentAnnotation.datasource = this.datasources[0].name;
  66. this.currentIsNew = true;
  67. this.datasourceChanged();
  68. }
  69. update() {
  70. this.reset();
  71. this.mode = 'list';
  72. }
  73. setupNew = () => {
  74. this.mode = 'new';
  75. this.reset();
  76. };
  77. backToList() {
  78. this.mode = 'list';
  79. }
  80. move(index: number, dir: number) {
  81. // @ts-ignore
  82. _.move(this.annotations, index, index + dir);
  83. }
  84. add() {
  85. this.annotations.push(this.currentAnnotation);
  86. this.reset();
  87. this.mode = 'list';
  88. this.dashboard.updateSubmenuVisibility();
  89. }
  90. removeAnnotation(annotation: any) {
  91. const index = _.indexOf(this.annotations, annotation);
  92. this.annotations.splice(index, 1);
  93. this.dashboard.updateSubmenuVisibility();
  94. }
  95. onColorChange(newColor: string) {
  96. this.currentAnnotation.iconColor = newColor;
  97. }
  98. }
  99. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);