editorCtrl.js 5.5 KB

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