editor_ctrl.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.currentIsNew = true;
  41. $scope.datasourceChanged();
  42. };
  43. $scope.update = function() {
  44. $scope.reset();
  45. $scope.mode = 'list';
  46. $scope.broadcastRefresh();
  47. };
  48. $scope.add = function() {
  49. $scope.annotations.push($scope.currentAnnotation);
  50. $scope.reset();
  51. $scope.mode = 'list';
  52. $scope.updateSubmenuVisibility();
  53. $scope.broadcastRefresh();
  54. };
  55. $scope.removeAnnotation = function(annotation) {
  56. var index = _.indexOf($scope.annotations, annotation);
  57. $scope.annotations.splice(index, 1);
  58. $scope.updateSubmenuVisibility();
  59. $scope.broadcastRefresh();
  60. };
  61. });
  62. });