editorCtrl.js 3.7 KB

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