editor_ctrl.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.refreshOptions = [
  13. { value: 0, text: 'Never' },
  14. { value: 1, text: 'On Dashboard Load' },
  15. { value: 2, text: 'On Time Range Change' },
  16. ];
  17. $scope.sortOptions = [
  18. { value: 0, text: 'Disabled' },
  19. { value: 1, text: 'Alphabetical (asc)' },
  20. { value: 2, text: 'Alphabetical (desc)' },
  21. { value: 3, text: 'Numerical (asc)' },
  22. { value: 4, text: 'Numerical (desc)' },
  23. { value: 5, text: 'Alphabetical (case-insensitive, asc)' },
  24. { value: 6, text: 'Alphabetical (case-insensitive, desc)' },
  25. ];
  26. $scope.hideOptions = [{ value: 0, text: '' }, { value: 1, text: 'Label' }, { value: 2, text: 'Variable' }];
  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. appEvents.emit('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. appEvents.emit('alert-warning', ['Validation', 'Variable with the same name already exists']);
  57. return false;
  58. }
  59. if (
  60. $scope.current.type === 'query' &&
  61. $scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))
  62. ) {
  63. appEvents.emit('alert-warning', [
  64. 'Validation',
  65. 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name,
  66. ]);
  67. return false;
  68. }
  69. return true;
  70. };
  71. $scope.validate = function() {
  72. $scope.infoText = '';
  73. if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
  74. $scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
  75. datasourceSrv.get($scope.current.datasource).then(ds => {
  76. if (!ds.getTagKeys) {
  77. $scope.infoText = 'This datasource does not support adhoc filters yet.';
  78. }
  79. });
  80. }
  81. };
  82. $scope.runQuery = function() {
  83. return variableSrv.updateOptions($scope.current).catch(err => {
  84. if (err.data && err.data.message) {
  85. err.message = err.data.message;
  86. }
  87. appEvents.emit('alert-error', ['Templating', 'Template variables could not be initialized: ' + err.message]);
  88. });
  89. };
  90. $scope.edit = function(variable) {
  91. $scope.current = variable;
  92. $scope.currentIsNew = false;
  93. $scope.mode = 'edit';
  94. $scope.validate();
  95. };
  96. $scope.duplicate = function(variable) {
  97. var clone = _.cloneDeep(variable.getSaveModel());
  98. $scope.current = variableSrv.createVariableFromModel(clone);
  99. $scope.current.name = 'copy_of_' + variable.name;
  100. variableSrv.addVariable($scope.current);
  101. };
  102. $scope.update = function() {
  103. if ($scope.isValid()) {
  104. $scope.runQuery().then(function() {
  105. $scope.reset();
  106. $scope.mode = 'list';
  107. templateSrv.updateTemplateData();
  108. });
  109. }
  110. };
  111. $scope.reset = function() {
  112. $scope.currentIsNew = true;
  113. $scope.current = variableSrv.createVariableFromModel({ type: 'query' });
  114. // this is done here in case a new data source type variable was added
  115. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  116. return !ds.meta.mixed && ds.value !== null;
  117. });
  118. $scope.datasourceTypes = _($scope.datasources)
  119. .uniqBy('meta.id')
  120. .map(function(ds) {
  121. return { text: ds.meta.name, value: ds.meta.id };
  122. })
  123. .value();
  124. };
  125. $scope.typeChanged = function() {
  126. var old = $scope.current;
  127. $scope.current = variableSrv.createVariableFromModel({
  128. type: $scope.current.type,
  129. });
  130. $scope.current.name = old.name;
  131. $scope.current.hide = old.hide;
  132. $scope.current.label = old.label;
  133. var oldIndex = _.indexOf(this.variables, old);
  134. if (oldIndex !== -1) {
  135. this.variables[oldIndex] = $scope.current;
  136. }
  137. $scope.validate();
  138. };
  139. $scope.removeVariable = function(variable) {
  140. variableSrv.removeVariable(variable);
  141. };
  142. }
  143. }
  144. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);