editorCtrl.js 2.1 KB

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