editor_ctrl.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 = [{ value: 0, text: '' }, { value: 1, text: 'Label' }, { value: 2, text: 'Variable' }];
  23. $scope.init = function() {
  24. $scope.mode = 'list';
  25. $scope.variables = variableSrv.variables;
  26. $scope.reset();
  27. $scope.$watch('mode', function(val) {
  28. if (val === 'new') {
  29. $scope.reset();
  30. }
  31. });
  32. };
  33. $scope.setMode = function(mode) {
  34. $scope.mode = mode;
  35. };
  36. $scope.add = function() {
  37. if ($scope.isValid()) {
  38. variableSrv.addVariable($scope.current);
  39. $scope.update();
  40. }
  41. };
  42. $scope.isValid = function() {
  43. if (!$scope.ctrl.form.$valid) {
  44. return false;
  45. }
  46. if (!$scope.current.name.match(/^\w+$/)) {
  47. $scope.appEvent('alert-warning', [
  48. 'Validation',
  49. 'Only word and digit characters are allowed in variable names',
  50. ]);
  51. return false;
  52. }
  53. var sameName = _.find($scope.variables, { name: $scope.current.name });
  54. if (sameName && sameName !== $scope.current) {
  55. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  56. return false;
  57. }
  58. if (
  59. $scope.current.type === 'query' &&
  60. $scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))
  61. ) {
  62. $scope.appEvent('alert-warning', [
  63. 'Validation',
  64. 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name,
  65. ]);
  66. return false;
  67. }
  68. return true;
  69. };
  70. $scope.validate = function() {
  71. $scope.infoText = '';
  72. if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
  73. $scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
  74. datasourceSrv.get($scope.current.datasource).then(ds => {
  75. if (!ds.getTagKeys) {
  76. $scope.infoText = 'This datasource does not support adhoc filters yet.';
  77. }
  78. });
  79. }
  80. };
  81. $scope.runQuery = function() {
  82. return variableSrv.updateOptions($scope.current).then(null, function(err) {
  83. if (err.data && err.data.message) {
  84. err.message = err.data.message;
  85. }
  86. $scope.appEvent('alert-error', ['Templating', 'Template variables could not be initialized: ' + err.message]);
  87. });
  88. };
  89. $scope.edit = function(variable) {
  90. $scope.current = variable;
  91. $scope.currentIsNew = false;
  92. $scope.mode = 'edit';
  93. $scope.validate();
  94. };
  95. $scope.duplicate = function(variable) {
  96. var clone = _.cloneDeep(variable.getSaveModel());
  97. $scope.current = variableSrv.createVariableFromModel(clone);
  98. $scope.current.name = 'copy_of_' + variable.name;
  99. variableSrv.addVariable($scope.current);
  100. };
  101. $scope.update = function() {
  102. if ($scope.isValid()) {
  103. $scope.runQuery().then(function() {
  104. $scope.reset();
  105. $scope.mode = 'list';
  106. templateSrv.updateTemplateData();
  107. });
  108. }
  109. };
  110. $scope.reset = function() {
  111. $scope.currentIsNew = true;
  112. $scope.current = variableSrv.createVariableFromModel({ type: 'query' });
  113. // this is done here in case a new data source type variable was added
  114. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  115. return !ds.meta.mixed && ds.value !== null;
  116. });
  117. $scope.datasourceTypes = _($scope.datasources)
  118. .uniqBy('meta.id')
  119. .map(function(ds) {
  120. return { text: ds.meta.name, value: ds.meta.id };
  121. })
  122. .value();
  123. };
  124. $scope.typeChanged = function() {
  125. var old = $scope.current;
  126. $scope.current = variableSrv.createVariableFromModel({
  127. type: $scope.current.type,
  128. });
  129. $scope.current.name = old.name;
  130. $scope.current.hide = old.hide;
  131. $scope.current.label = old.label;
  132. var oldIndex = _.indexOf(this.variables, old);
  133. if (oldIndex !== -1) {
  134. this.variables[oldIndex] = $scope.current;
  135. }
  136. $scope.validate();
  137. };
  138. $scope.removeVariable = function(variable) {
  139. variableSrv.removeVariable(variable);
  140. };
  141. }
  142. }
  143. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);