editor_ctrl.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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', [
  52. 'Validation',
  53. 'Only word and digit characters are allowed in variable names',
  54. ]);
  55. return false;
  56. }
  57. var sameName = _.find($scope.variables, { name: $scope.current.name });
  58. if (sameName && sameName !== $scope.current) {
  59. $scope.appEvent('alert-warning', [
  60. 'Validation',
  61. 'Variable with the same name already exists',
  62. ]);
  63. return false;
  64. }
  65. if (
  66. $scope.current.type === 'query' &&
  67. $scope.current.query.match(
  68. new RegExp('\\$' + $scope.current.name + '(/| |$)')
  69. )
  70. ) {
  71. $scope.appEvent('alert-warning', [
  72. 'Validation',
  73. 'Query cannot contain a reference to itself. Variable: $' +
  74. $scope.current.name,
  75. ]);
  76. return false;
  77. }
  78. return true;
  79. };
  80. $scope.validate = function() {
  81. $scope.infoText = '';
  82. if (
  83. $scope.current.type === 'adhoc' &&
  84. $scope.current.datasource !== null
  85. ) {
  86. $scope.infoText =
  87. 'Adhoc filters are applied automatically to all queries that target this datasource';
  88. datasourceSrv.get($scope.current.datasource).then(ds => {
  89. if (!ds.getTagKeys) {
  90. $scope.infoText =
  91. 'This datasource does not support adhoc filters yet.';
  92. }
  93. });
  94. }
  95. };
  96. $scope.runQuery = function() {
  97. return variableSrv
  98. .updateOptions($scope.current)
  99. .then(null, function(err) {
  100. if (err.data && err.data.message) {
  101. err.message = err.data.message;
  102. }
  103. $scope.appEvent('alert-error', [
  104. 'Templating',
  105. 'Template variables could not be initialized: ' + err.message,
  106. ]);
  107. });
  108. };
  109. $scope.edit = function(variable) {
  110. $scope.current = variable;
  111. $scope.currentIsNew = false;
  112. $scope.mode = 'edit';
  113. $scope.validate();
  114. };
  115. $scope.duplicate = function(variable) {
  116. var clone = _.cloneDeep(variable.getSaveModel());
  117. $scope.current = variableSrv.createVariableFromModel(clone);
  118. $scope.current.name = 'copy_of_' + variable.name;
  119. variableSrv.addVariable($scope.current);
  120. };
  121. $scope.update = function() {
  122. if ($scope.isValid()) {
  123. $scope.runQuery().then(function() {
  124. $scope.reset();
  125. $scope.mode = 'list';
  126. templateSrv.updateTemplateData();
  127. });
  128. }
  129. };
  130. $scope.reset = function() {
  131. $scope.currentIsNew = true;
  132. $scope.current = variableSrv.createVariableFromModel({ type: 'query' });
  133. // this is done here in case a new data source type variable was added
  134. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(
  135. ds
  136. ) {
  137. return !ds.meta.mixed && ds.value !== null;
  138. });
  139. $scope.datasourceTypes = _($scope.datasources)
  140. .uniqBy('meta.id')
  141. .map(function(ds) {
  142. return { text: ds.meta.name, value: ds.meta.id };
  143. })
  144. .value();
  145. };
  146. $scope.typeChanged = function() {
  147. var old = $scope.current;
  148. $scope.current = variableSrv.createVariableFromModel({
  149. type: $scope.current.type,
  150. });
  151. $scope.current.name = old.name;
  152. $scope.current.hide = old.hide;
  153. $scope.current.label = old.label;
  154. var oldIndex = _.indexOf(this.variables, old);
  155. if (oldIndex !== -1) {
  156. this.variables[oldIndex] = $scope.current;
  157. }
  158. $scope.validate();
  159. };
  160. $scope.removeVariable = function(variable) {
  161. variableSrv.removeVariable(variable);
  162. };
  163. }
  164. }
  165. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);