editorCtrl.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, alertSrv) {
  9. var replacementDefaults = {
  10. type: 'query',
  11. datasource: null,
  12. refresh_on_load: false,
  13. name: '',
  14. options: [],
  15. includeAll: false,
  16. allFormat: 'glob',
  17. };
  18. $scope.init = function() {
  19. $scope.editor = { index: 0 };
  20. $scope.datasources = datasourceSrv.getMetricSources();
  21. $scope.variables = templateSrv.variables;
  22. $scope.reset();
  23. $scope.$watch('editor.index', function(index) {
  24. if ($scope.currentIsNew === false && index === 1) {
  25. $scope.reset();
  26. }
  27. });
  28. };
  29. $scope.add = function() {
  30. if ($scope.isValid()) {
  31. $scope.variables.push($scope.current);
  32. $scope.update();
  33. $scope.updateSubmenuVisibility();
  34. }
  35. };
  36. $scope.isValid = function() {
  37. if (!$scope.current.name) {
  38. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  39. return false;
  40. }
  41. if (!$scope.current.name.match(/^\w+$/)) {
  42. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  43. return false;
  44. }
  45. var sameName = _.findWhere($scope.variables, { name: $scope.current.name });
  46. if (sameName && sameName !== $scope.current) {
  47. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  48. return false;
  49. }
  50. return true;
  51. };
  52. $scope.runQuery = function() {
  53. return templateValuesSrv.updateOptions($scope.current).then(function() {
  54. }, function(err) {
  55. alertSrv.set('Templating', 'Failed to run query for variable values: ' + err.message, 'error');
  56. });
  57. };
  58. $scope.edit = function(variable) {
  59. $scope.current = variable;
  60. $scope.currentIsNew = false;
  61. $scope.editor.index = 2;
  62. if ($scope.current.datasource === void 0) {
  63. $scope.current.datasource = null;
  64. $scope.current.type = 'query';
  65. $scope.current.allFormat = 'Glob';
  66. }
  67. };
  68. $scope.update = function() {
  69. if ($scope.isValid()) {
  70. $scope.runQuery().then(function() {
  71. $scope.reset();
  72. $scope.editor.index = 0;
  73. });
  74. }
  75. };
  76. $scope.reset = function() {
  77. $scope.currentIsNew = true;
  78. $scope.current = angular.copy(replacementDefaults);
  79. };
  80. $scope.typeChanged = function () {
  81. if ($scope.current.type === 'interval') {
  82. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  83. }
  84. if ($scope.current.type === 'query') {
  85. $scope.current.query = '';
  86. }
  87. };
  88. $scope.removeVariable = function(variable) {
  89. var index = _.indexOf($scope.variables, variable);
  90. $scope.variables.splice(index, 1);
  91. $scope.updateSubmenuVisibility();
  92. };
  93. });
  94. });