editor_ctrl.ts 2.0 KB

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