editor_ctrl.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import coreModule from 'app/core/core_module';
  5. export class AnnotationsEditorCtrl {
  6. mode: any;
  7. datasources: any;
  8. annotations: any;
  9. currentAnnotation: any;
  10. currentDatasource: any;
  11. currentIsNew: any;
  12. annotationDefaults: any = {
  13. name: '',
  14. datasource: null,
  15. iconColor: 'rgba(255, 96, 96, 1)',
  16. enable: true,
  17. showIn: 0,
  18. hide: false,
  19. };
  20. showOptions: any = [
  21. {text: 'All Panels', value: 0},
  22. {text: 'Specific Panels', value: 1},
  23. ];
  24. /** @ngInject */
  25. constructor(private $scope, private datasourceSrv) {
  26. $scope.ctrl = this;
  27. this.mode = 'list';
  28. this.datasources = datasourceSrv.getAnnotationSources();
  29. this.annotations = $scope.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. this.$scope.broadcastRefresh();
  56. }
  57. setupNew() {
  58. this.mode = 'new';
  59. this.reset();
  60. }
  61. add() {
  62. this.annotations.push(this.currentAnnotation);
  63. this.reset();
  64. this.mode = 'list';
  65. this.$scope.broadcastRefresh();
  66. this.$scope.dashboard.updateSubmenuVisibility();
  67. }
  68. removeAnnotation(annotation) {
  69. var index = _.indexOf(this.annotations, annotation);
  70. this.annotations.splice(index, 1);
  71. this.$scope.dashboard.updateSubmenuVisibility();
  72. this.$scope.broadcastRefresh();
  73. }
  74. onColorChange(newColor) {
  75. this.currentAnnotation.iconColor = newColor;
  76. }
  77. annotationEnabledChange() {
  78. this.$scope.broadcastRefresh();
  79. }
  80. annotationHiddenChanged() {
  81. this.$scope.dashboard.updateSubmenuVisibility();
  82. }
  83. }
  84. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);