editorCtrl.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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: 0,
  13. name: '',
  14. hide: 0,
  15. options: [],
  16. includeAll: false,
  17. multi: false,
  18. };
  19. $scope.variableTypes = [
  20. {value: "query", text: "Query"},
  21. {value: "interval", text: "Interval"},
  22. {value: "datasource", text: "Data source"},
  23. {value: "custom", text: "Custom"},
  24. ];
  25. $scope.refreshOptions = [
  26. {value: 0, text: "Never"},
  27. {value: 1, text: "On Dashboard Load"},
  28. {value: 2, text: "On Time Range Change"},
  29. ];
  30. $scope.hideOptions = [
  31. {value: 0, text: ""},
  32. {value: 1, text: "Label"},
  33. {value: 2, text: "Variable"},
  34. ];
  35. $scope.init = function() {
  36. $scope.mode = 'list';
  37. $scope.datasourceTypes = {};
  38. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  39. $scope.datasourceTypes[ds.meta.id] = {text: ds.meta.name, value: ds.meta.id};
  40. return !ds.meta.builtIn;
  41. });
  42. $scope.datasourceTypes = _.map($scope.datasourceTypes, function(value) {
  43. return value;
  44. });
  45. $scope.variables = templateSrv.variables;
  46. $scope.reset();
  47. $scope.$watch('mode', function(val) {
  48. if (val === 'new') {
  49. $scope.reset();
  50. }
  51. });
  52. $scope.$watch('current.datasource', function(val) {
  53. if ($scope.mode === 'new') {
  54. datasourceSrv.get(val).then(function(ds) {
  55. if (ds.meta.defaultMatchFormat) {
  56. $scope.current.allFormat = ds.meta.defaultMatchFormat;
  57. $scope.current.multiFormat = ds.meta.defaultMatchFormat;
  58. }
  59. });
  60. }
  61. });
  62. };
  63. $scope.add = function() {
  64. if ($scope.isValid()) {
  65. $scope.variables.push($scope.current);
  66. $scope.update();
  67. $scope.updateSubmenuVisibility();
  68. }
  69. };
  70. $scope.isValid = function() {
  71. if (!$scope.current.name) {
  72. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  73. return false;
  74. }
  75. if (!$scope.current.name.match(/^\w+$/)) {
  76. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  77. return false;
  78. }
  79. var sameName = _.findWhere($scope.variables, { name: $scope.current.name });
  80. if (sameName && sameName !== $scope.current) {
  81. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  82. return false;
  83. }
  84. return true;
  85. };
  86. $scope.runQuery = function() {
  87. return templateValuesSrv.updateOptions($scope.current).then(null, function(err) {
  88. if (err.data && err.data.message) { err.message = err.data.message; }
  89. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  90. });
  91. };
  92. $scope.edit = function(variable) {
  93. $scope.current = variable;
  94. $scope.currentIsNew = false;
  95. $scope.mode = 'edit';
  96. if ($scope.current.datasource === void 0) {
  97. $scope.current.datasource = null;
  98. $scope.current.type = 'query';
  99. $scope.current.allFormat = 'glob';
  100. }
  101. };
  102. $scope.duplicate = function(variable) {
  103. $scope.current = angular.copy(variable);
  104. $scope.variables.push($scope.current);
  105. $scope.current.name = 'copy_of_'+variable.name;
  106. $scope.updateSubmenuVisibility();
  107. };
  108. $scope.update = function() {
  109. if ($scope.isValid()) {
  110. $scope.runQuery().then(function() {
  111. $scope.reset();
  112. $scope.mode = 'list';
  113. });
  114. }
  115. };
  116. $scope.reset = function() {
  117. $scope.currentIsNew = true;
  118. $scope.current = angular.copy(replacementDefaults);
  119. };
  120. $scope.typeChanged = function () {
  121. if ($scope.current.type === 'interval') {
  122. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  123. }
  124. if ($scope.current.type === 'query') {
  125. $scope.current.query = '';
  126. }
  127. if ($scope.current.type === 'datasource') {
  128. $scope.current.query = $scope.datasourceTypes[0].value;
  129. $scope.current.regex = '';
  130. $scope.current.refresh = 1;
  131. }
  132. };
  133. $scope.removeVariable = function(variable) {
  134. var index = _.indexOf($scope.variables, variable);
  135. $scope.variables.splice(index, 1);
  136. $scope.updateSubmenuVisibility();
  137. };
  138. });
  139. });