editor_ctrl.ts 4.8 KB

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