editor_ctrl.ts 5.2 KB

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