editorCtrl.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv) {
  9. var replacementDefaults = {
  10. type: 'query',
  11. datasource: null,
  12. refresh: false,
  13. name: '',
  14. options: [],
  15. includeAll: false,
  16. multi: false,
  17. };
  18. $scope.init = function() {
  19. $scope.mode = 'list';
  20. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  21. return !ds.meta.builtIn;
  22. });
  23. $scope.variables = templateSrv.variables;
  24. $scope.reset();
  25. $scope.$watch('mode', function(val) {
  26. if (val === 'new') {
  27. $scope.reset();
  28. }
  29. });
  30. $scope.$watch('current.datasource', function(val) {
  31. if ($scope.mode === 'new') {
  32. datasourceSrv.get(val).then(function(ds) {
  33. if (ds.meta.defaultMatchFormat) {
  34. $scope.current.allFormat = ds.meta.defaultMatchFormat;
  35. $scope.current.multiFormat = ds.meta.defaultMatchFormat;
  36. }
  37. });
  38. }
  39. });
  40. };
  41. $scope.add = function() {
  42. if ($scope.isValid()) {
  43. $scope.variables.push($scope.current);
  44. $scope.update();
  45. $scope.updateSubmenuVisibility();
  46. }
  47. };
  48. $scope.isValid = function() {
  49. if (!$scope.current.name) {
  50. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  51. return false;
  52. }
  53. if (!$scope.current.name.match(/^\w+$/)) {
  54. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  55. return false;
  56. }
  57. var sameName = _.findWhere($scope.variables, { name: $scope.current.name });
  58. if (sameName && sameName !== $scope.current) {
  59. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  60. return false;
  61. }
  62. return true;
  63. };
  64. $scope.runQuery = function() {
  65. return templateValuesSrv.updateOptions($scope.current).then(null, function(err) {
  66. if (err.data && err.data.message) { err.message = err.data.message; }
  67. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  68. });
  69. };
  70. $scope.edit = function(variable) {
  71. $scope.current = variable;
  72. $scope.currentIsNew = false;
  73. $scope.mode = 'edit';
  74. if ($scope.current.datasource === void 0) {
  75. $scope.current.datasource = null;
  76. $scope.current.type = 'query';
  77. $scope.current.allFormat = 'glob';
  78. }
  79. };
  80. $scope.duplicate = function(variable) {
  81. $scope.current = angular.copy(variable);
  82. $scope.variables.push($scope.current);
  83. $scope.current.name = 'copy_of_'+variable.name;
  84. $scope.updateSubmenuVisibility();
  85. };
  86. $scope.update = function() {
  87. if ($scope.isValid()) {
  88. $scope.runQuery().then(function() {
  89. $scope.reset();
  90. $scope.mode = 'list';
  91. });
  92. }
  93. };
  94. $scope.reset = function() {
  95. $scope.currentIsNew = true;
  96. $scope.current = angular.copy(replacementDefaults);
  97. };
  98. $scope.typeChanged = function () {
  99. if ($scope.current.type === 'interval') {
  100. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  101. }
  102. if ($scope.current.type === 'query') {
  103. $scope.current.query = '';
  104. }
  105. };
  106. $scope.removeVariable = function(variable) {
  107. var index = _.indexOf($scope.variables, variable);
  108. $scope.variables.splice(index, 1);
  109. $scope.updateSubmenuVisibility();
  110. };
  111. });
  112. });