editor_ctrl.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. showLine: true,
  14. iconColor: '#C0C6BE',
  15. lineColor: 'rgba(255, 96, 96, 0.592157)',
  16. iconSize: 13,
  17. enable: true
  18. };
  19. $scope.init = function() {
  20. $scope.mode = 'list';
  21. $scope.datasources = datasourceSrv.getAnnotationSources();
  22. $scope.annotations = $scope.dashboard.annotations.list;
  23. $scope.reset();
  24. $scope.$watch('mode', function(newVal) {
  25. if (newVal === 'new') { $scope.reset(); }
  26. });
  27. };
  28. $scope.datasourceChanged = function() {
  29. return datasourceSrv.get($scope.currentAnnotation.datasource).then(function(ds) {
  30. $scope.currentDatasource = ds;
  31. $scope.currentAnnotation.datasource = ds.name;
  32. });
  33. };
  34. $scope.edit = function(annotation) {
  35. $scope.currentAnnotation = annotation;
  36. $scope.currentIsNew = false;
  37. $scope.datasourceChanged();
  38. $scope.mode = 'edit';
  39. $(".tooltip.in").remove();
  40. };
  41. $scope.reset = function() {
  42. $scope.currentAnnotation = angular.copy(annotationDefaults);
  43. $scope.currentIsNew = true;
  44. $scope.datasourceChanged();
  45. };
  46. $scope.update = function() {
  47. $scope.reset();
  48. $scope.mode = 'list';
  49. $scope.broadcastRefresh();
  50. };
  51. $scope.add = function() {
  52. $scope.annotations.push($scope.currentAnnotation);
  53. $scope.reset();
  54. $scope.mode = 'list';
  55. $scope.updateSubmenuVisibility();
  56. $scope.broadcastRefresh();
  57. };
  58. $scope.removeAnnotation = function(annotation) {
  59. var index = _.indexOf($scope.annotations, annotation);
  60. $scope.annotations.splice(index, 1);
  61. $scope.updateSubmenuVisibility();
  62. $scope.broadcastRefresh();
  63. };
  64. });
  65. });