editorCtrl.js 5.1 KB

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