editor_ctrl.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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($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
  35. .get(this.currentAnnotation.datasource)
  36. .then(ds => {
  37. this.currentDatasource = ds;
  38. });
  39. }
  40. edit(annotation) {
  41. this.currentAnnotation = annotation;
  42. this.currentAnnotation.showIn = this.currentAnnotation.showIn || 0;
  43. this.currentIsNew = false;
  44. this.datasourceChanged();
  45. this.mode = 'edit';
  46. $('.tooltip.in').remove();
  47. }
  48. reset() {
  49. this.currentAnnotation = angular.copy(this.annotationDefaults);
  50. this.currentAnnotation.datasource = this.datasources[0].name;
  51. this.currentIsNew = true;
  52. this.datasourceChanged();
  53. }
  54. update() {
  55. this.reset();
  56. this.mode = 'list';
  57. }
  58. setupNew() {
  59. this.mode = 'new';
  60. this.reset();
  61. }
  62. backToList() {
  63. this.mode = 'list';
  64. }
  65. add() {
  66. this.annotations.push(this.currentAnnotation);
  67. this.reset();
  68. this.mode = 'list';
  69. }
  70. removeAnnotation(annotation) {
  71. var index = _.indexOf(this.annotations, annotation);
  72. this.annotations.splice(index, 1);
  73. }
  74. onColorChange(newColor) {
  75. this.currentAnnotation.iconColor = newColor;
  76. }
  77. }
  78. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);