editor_ctrl.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. showOptions: any = [{ text: 'All Panels', value: 0 }, { text: 'Specific Panels', value: 1 }];
  24. /** @ngInject */
  25. constructor($scope: any, private datasourceSrv: DatasourceSrv) {
  26. $scope.ctrl = this;
  27. this.dashboard = $scope.dashboard;
  28. this.mode = 'list';
  29. this.datasources = datasourceSrv.getAnnotationSources();
  30. this.annotations = this.dashboard.annotations.list;
  31. this.reset();
  32. this.onColorChange = this.onColorChange.bind(this);
  33. }
  34. async datasourceChanged() {
  35. return (this.currentDatasource = await this.datasourceSrv.get(this.currentAnnotation.datasource));
  36. }
  37. edit(annotation: any) {
  38. this.currentAnnotation = annotation;
  39. this.currentAnnotation.showIn = this.currentAnnotation.showIn || 0;
  40. this.currentIsNew = false;
  41. this.datasourceChanged();
  42. this.mode = 'edit';
  43. $('.tooltip.in').remove();
  44. }
  45. reset() {
  46. this.currentAnnotation = angular.copy(this.annotationDefaults);
  47. this.currentAnnotation.datasource = this.datasources[0].name;
  48. this.currentIsNew = true;
  49. this.datasourceChanged();
  50. }
  51. update() {
  52. this.reset();
  53. this.mode = 'list';
  54. }
  55. setupNew() {
  56. this.mode = 'new';
  57. this.reset();
  58. }
  59. backToList() {
  60. this.mode = 'list';
  61. }
  62. move(index: number, dir: number) {
  63. // @ts-ignore
  64. _.move(this.annotations, index, index + dir);
  65. }
  66. add() {
  67. this.annotations.push(this.currentAnnotation);
  68. this.reset();
  69. this.mode = 'list';
  70. this.dashboard.updateSubmenuVisibility();
  71. }
  72. removeAnnotation(annotation: any) {
  73. const index = _.indexOf(this.annotations, annotation);
  74. this.annotations.splice(index, 1);
  75. this.dashboard.updateSubmenuVisibility();
  76. }
  77. onColorChange(newColor: string) {
  78. this.currentAnnotation.iconColor = newColor;
  79. }
  80. }
  81. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);