editor_ctrl.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. import {variableTypes} from './variable';
  4. export class VariableEditorCtrl {
  5. /** @ngInject **/
  6. constructor($scope, datasourceSrv, variableSrv, templateSrv) {
  7. $scope.variableTypes = variableTypes;
  8. $scope.ctrl = {};
  9. $scope.namePattern = /^(?!__).*$/;
  10. $scope.refreshOptions = [
  11. {value: 0, text: "Never"},
  12. {value: 1, text: "On Dashboard Load"},
  13. {value: 2, text: "On Time Range Change"},
  14. ];
  15. $scope.sortOptions = [
  16. {value: 0, text: "Disabled"},
  17. {value: 1, text: "Alphabetical (asc)"},
  18. {value: 2, text: "Alphabetical (desc)"},
  19. {value: 3, text: "Numerical (asc)"},
  20. {value: 4, text: "Numerical (desc)"},
  21. ];
  22. $scope.hideOptions = [
  23. {value: 0, text: ""},
  24. {value: 1, text: "Label"},
  25. {value: 2, text: "Variable"},
  26. ];
  27. $scope.init = function() {
  28. $scope.mode = 'list';
  29. $scope.variables = variableSrv.variables;
  30. $scope.reset();
  31. $scope.$watch('mode', function(val) {
  32. if (val === 'new') {
  33. $scope.reset();
  34. }
  35. });
  36. };
  37. $scope.setMode = function(mode) {
  38. $scope.mode = mode;
  39. };
  40. $scope.add = function() {
  41. if ($scope.isValid()) {
  42. variableSrv.addVariable($scope.current);
  43. $scope.update();
  44. }
  45. };
  46. $scope.isValid = function() {
  47. if (!$scope.ctrl.form.$valid) {
  48. return false;
  49. }
  50. if (!$scope.current.name.match(/^\w+$/)) {
  51. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  52. return false;
  53. }
  54. var sameName = _.find($scope.variables, { name: $scope.current.name });
  55. if (sameName && sameName !== $scope.current) {
  56. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  57. return false;
  58. }
  59. if ($scope.current.type === 'query' && $scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))) {
  60. $scope.appEvent('alert-warning', ['Validation', 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name]);
  61. return false;
  62. }
  63. return true;
  64. };
  65. $scope.validate = function() {
  66. $scope.infoText = '';
  67. if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
  68. $scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
  69. datasourceSrv.get($scope.current.datasource).then(ds => {
  70. if (!ds.getTagKeys) {
  71. $scope.infoText = 'This datasource does not support adhoc filters yet.';
  72. }
  73. });
  74. }
  75. };
  76. $scope.runQuery = function() {
  77. return variableSrv.updateOptions($scope.current).then(null, function(err) {
  78. if (err.data && err.data.message) { err.message = err.data.message; }
  79. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  80. });
  81. };
  82. $scope.edit = function(variable) {
  83. $scope.current = variable;
  84. $scope.currentIsNew = false;
  85. $scope.mode = 'edit';
  86. $scope.validate();
  87. };
  88. $scope.duplicate = function(variable) {
  89. var clone = _.cloneDeep(variable.getSaveModel());
  90. $scope.current = variableSrv.createVariableFromModel(clone);
  91. $scope.current.name = 'copy_of_'+variable.name;
  92. variableSrv.addVariable($scope.current);
  93. };
  94. $scope.update = function() {
  95. if ($scope.isValid()) {
  96. $scope.runQuery().then(function() {
  97. $scope.reset();
  98. $scope.mode = 'list';
  99. templateSrv.updateTemplateData();
  100. });
  101. }
  102. };
  103. $scope.reset = function() {
  104. $scope.currentIsNew = true;
  105. $scope.current = variableSrv.createVariableFromModel({type: 'query'});
  106. // this is done here in case a new data source type variable was added
  107. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  108. return !ds.meta.mixed && ds.value !== null;
  109. });
  110. $scope.datasourceTypes = _($scope.datasources).uniqBy('meta.id').map(function(ds) {
  111. return {text: ds.meta.name, value: ds.meta.id};
  112. }).value();
  113. };
  114. $scope.typeChanged = function() {
  115. var old = $scope.current;
  116. $scope.current = variableSrv.createVariableFromModel({type: $scope.current.type});
  117. $scope.current.name = old.name;
  118. $scope.current.hide = old.hide;
  119. $scope.current.label = old.label;
  120. var oldIndex = _.indexOf(this.variables, old);
  121. if (oldIndex !== -1) {
  122. this.variables[oldIndex] = $scope.current;
  123. }
  124. $scope.validate();
  125. };
  126. $scope.removeVariable = function(variable) {
  127. variableSrv.removeVariable(variable);
  128. };
  129. }
  130. }
  131. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);