editor_ctrl.ts 2.3 KB

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