editor_ctrl.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import coreModule from 'app/core/core_module';
  6. export class AnnotationsEditorCtrl {
  7. mode: any;
  8. datasources: any;
  9. annotations: any;
  10. currentAnnotation: any;
  11. currentDatasource: any;
  12. currentIsNew: any;
  13. annotationDefaults: any = {
  14. name: '',
  15. datasource: null,
  16. iconColor: 'rgba(255, 96, 96, 1)',
  17. enable: true,
  18. showIn: 0,
  19. hide: false,
  20. };
  21. showOptions: any = [
  22. {text: 'All Panels', value: 0},
  23. {text: 'Specific Panels', value: 1},
  24. ];
  25. /** @ngInject */
  26. constructor(private $scope, private datasourceSrv) {
  27. $scope.ctrl = this;
  28. this.mode = 'list';
  29. this.datasources = datasourceSrv.getAnnotationSources();
  30. this.annotations = $scope.dashboard.annotations.list;
  31. this.reset();
  32. $scope.$watch('mode', newVal => {
  33. if (newVal === 'new') {
  34. this.reset();
  35. }
  36. });
  37. }
  38. datasourceChanged() {
  39. return this.datasourceSrv.get(this.currentAnnotation.datasource).then(ds => {
  40. this.currentDatasource = ds;
  41. });
  42. }
  43. edit(annotation) {
  44. this.currentAnnotation = annotation;
  45. this.currentAnnotation.showIn = this.currentAnnotation.showIn || 0;
  46. this.currentIsNew = false;
  47. this.datasourceChanged();
  48. this.mode = 'edit';
  49. $(".tooltip.in").remove();
  50. }
  51. reset() {
  52. this.currentAnnotation = angular.copy(this.annotationDefaults);
  53. this.currentAnnotation.datasource = this.datasources[0].name;
  54. this.currentIsNew = true;
  55. this.datasourceChanged();
  56. }
  57. update() {
  58. this.reset();
  59. this.mode = 'list';
  60. this.$scope.broadcastRefresh();
  61. }
  62. add() {
  63. this.annotations.push(this.currentAnnotation);
  64. this.reset();
  65. this.mode = 'list';
  66. this.$scope.broadcastRefresh();
  67. this.$scope.dashboard.updateSubmenuVisibility();
  68. }
  69. removeAnnotation(annotation) {
  70. var index = _.indexOf(this.annotations, annotation);
  71. this.annotations.splice(index, 1);
  72. this.$scope.dashboard.updateSubmenuVisibility();
  73. this.$scope.broadcastRefresh();
  74. }
  75. }
  76. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);