editor_ctrl.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery'
  5. ],
  6. function (angular, _, $) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('AnnotationsEditorCtrl', function($scope, datasourceSrv) {
  10. var annotationDefaults = {
  11. name: '',
  12. datasource: null,
  13. iconColor: 'rgba(255, 96, 96, 1)',
  14. enable: true
  15. };
  16. $scope.init = function() {
  17. $scope.mode = 'list';
  18. $scope.datasources = datasourceSrv.getAnnotationSources();
  19. $scope.annotations = $scope.dashboard.annotations.list;
  20. $scope.reset();
  21. $scope.$watch('mode', function(newVal) {
  22. if (newVal === 'new') { $scope.reset(); }
  23. });
  24. };
  25. $scope.datasourceChanged = function() {
  26. return datasourceSrv.get($scope.currentAnnotation.datasource).then(function(ds) {
  27. $scope.currentDatasource = ds;
  28. $scope.currentAnnotation.datasource = ds.name;
  29. });
  30. };
  31. $scope.edit = function(annotation) {
  32. $scope.currentAnnotation = annotation;
  33. $scope.currentIsNew = false;
  34. $scope.datasourceChanged();
  35. $scope.mode = 'edit';
  36. $(".tooltip.in").remove();
  37. };
  38. $scope.reset = function() {
  39. $scope.currentAnnotation = angular.copy(annotationDefaults);
  40. $scope.currentAnnotation.datasource = $scope.datasources[0].name;
  41. $scope.currentIsNew = true;
  42. $scope.datasourceChanged();
  43. };
  44. $scope.update = function() {
  45. $scope.reset();
  46. $scope.mode = 'list';
  47. $scope.broadcastRefresh();
  48. };
  49. $scope.add = function() {
  50. $scope.annotations.push($scope.currentAnnotation);
  51. $scope.reset();
  52. $scope.mode = 'list';
  53. $scope.updateSubmenuVisibility();
  54. $scope.broadcastRefresh();
  55. };
  56. $scope.removeAnnotation = function(annotation) {
  57. var index = _.indexOf($scope.annotations, annotation);
  58. $scope.annotations.splice(index, 1);
  59. $scope.updateSubmenuVisibility();
  60. $scope.broadcastRefresh();
  61. };
  62. });
  63. });