editorCtrl.js 3.1 KB

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