editor_ctrl.ts 5.0 KB

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