editor_ctrl.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  32. datasourceChanged() {
  33. return this.datasourceSrv.get(this.currentAnnotation.datasource).then(ds => {
  34. this.currentDatasource = ds;
  35. });
  36. }
  37. edit(annotation) {
  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. this.$scope.broadcastRefresh();
  55. }
  56. setupNew() {
  57. this.mode = 'new';
  58. this.reset();
  59. }
  60. add() {
  61. this.annotations.push(this.currentAnnotation);
  62. this.reset();
  63. this.mode = 'list';
  64. this.$scope.broadcastRefresh();
  65. this.$scope.dashboard.updateSubmenuVisibility();
  66. }
  67. removeAnnotation(annotation) {
  68. var index = _.indexOf(this.annotations, annotation);
  69. this.annotations.splice(index, 1);
  70. this.$scope.dashboard.updateSubmenuVisibility();
  71. this.$scope.broadcastRefresh();
  72. }
  73. annotationEnabledChange() {
  74. this.$scope.broadcastRefresh();
  75. }
  76. annotationHiddenChanged() {
  77. this.$scope.dashboard.updateSubmenuVisibility();
  78. }
  79. }
  80. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);