annotationsEditorCtrl.js 2.0 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. 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.currentAnnotation = angular.copy(annotationDefaults);
  21. $scope.currentIsNew = true;
  22. $scope.editor = { index: 0 };
  23. $scope.datasources = datasourceSrv.getAnnotationSources();
  24. $scope.annotations = $scope.dashboard.annotations.list;
  25. if ($scope.datasources.length > 0) {
  26. $scope.currentDatasource = $scope.datasources[0];
  27. }
  28. $scope.$watch('editor.index', function(newVal) {
  29. if (newVal !== 2) {
  30. $scope.reset();
  31. }
  32. });
  33. };
  34. $scope.setDatasource = function() {
  35. $scope.currentAnnotation.datasource = $scope.currentDatasource.name;
  36. };
  37. $scope.edit = function(annotation) {
  38. $scope.currentAnnotation = annotation;
  39. $scope.currentIsNew = false;
  40. $scope.currentDatasource = _.findWhere($scope.datasources, { name: annotation.datasource });
  41. if (!$scope.currentDatasource) {
  42. $scope.currentDatasource = $scope.datasources[0];
  43. }
  44. $scope.editor.index = 2;
  45. $(".tooltip.in").remove();
  46. };
  47. $scope.reset = function() {
  48. $scope.currentAnnotation = angular.copy(annotationDefaults);
  49. $scope.currentIsNew = true;
  50. };
  51. $scope.add = function() {
  52. $scope.currentAnnotation.datasource = $scope.currentDatasource.name;
  53. $scope.annotations.push($scope.currentAnnotation);
  54. $scope.reset();
  55. $scope.editor.index = 0;
  56. };
  57. $scope.removeAnnotation = function(annotation) {
  58. var index = _.indexOf($scope.annotations, annotation);
  59. $scope.annotations.splice(index, 1);
  60. };
  61. });
  62. });