editor_ctrl.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. };
  19. /** @ngInject */
  20. constructor(private $scope, private datasourceSrv) {
  21. $scope.ctrl = this;
  22. this.mode = 'list';
  23. this.datasources = datasourceSrv.getAnnotationSources();
  24. this.annotations = $scope.dashboard.annotations.list;
  25. this.reset();
  26. $scope.$watch('mode', newVal => {
  27. if (newVal === 'new') {
  28. this.reset();
  29. }
  30. });
  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.currentIsNew = false;
  40. this.datasourceChanged();
  41. this.mode = 'edit';
  42. $(".tooltip.in").remove();
  43. }
  44. reset() {
  45. this.currentAnnotation = angular.copy(this.annotationDefaults);
  46. this.currentAnnotation.datasource = this.datasources[0].name;
  47. this.currentIsNew = true;
  48. this.datasourceChanged();
  49. }
  50. update() {
  51. this.reset();
  52. this.mode = 'list';
  53. this.$scope.broadcastRefresh();
  54. };
  55. add() {
  56. this.annotations.push(this.currentAnnotation);
  57. this.reset();
  58. this.mode = 'list';
  59. this.$scope.broadcastRefresh();
  60. this.$scope.dashboard.updateSubmenuVisibility();
  61. };
  62. removeAnnotation(annotation) {
  63. var index = _.indexOf(this.annotations, annotation);
  64. this.annotations.splice(index, 1);
  65. this.$scope.updateSubmenuVisibility();
  66. this.$scope.broadcastRefresh();
  67. }
  68. }
  69. coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl);