editorCtrl.js 5.5 KB

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