editor_ctrl.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(private $scope, private datasourceSrv, private 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.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  31. return !ds.meta.mixed && ds.value !== null;
  32. });
  33. $scope.datasourceTypes = _($scope.datasources).uniqBy('meta.id').map(function(ds) {
  34. return {text: ds.meta.name, value: ds.meta.id};
  35. }).value();
  36. $scope.variables = variableSrv.variables;
  37. $scope.reset();
  38. $scope.$watch('mode', function(val) {
  39. if (val === 'new') {
  40. $scope.reset();
  41. }
  42. });
  43. };
  44. $scope.add = function() {
  45. if ($scope.isValid()) {
  46. $scope.variables.push($scope.current);
  47. $scope.update();
  48. $scope.dashboard.updateSubmenuVisibility();
  49. }
  50. };
  51. $scope.isValid = function() {
  52. if (!$scope.ctrl.form.$valid) {
  53. return;
  54. }
  55. if (!$scope.current.name.match(/^\w+$/)) {
  56. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  57. return false;
  58. }
  59. var sameName = _.find($scope.variables, { name: $scope.current.name });
  60. if (sameName && sameName !== $scope.current) {
  61. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  62. return false;
  63. }
  64. if ($scope.current.type === 'query' && $scope.current.query.match(new RegExp('\\$' + $scope.current.name))) {
  65. $scope.appEvent('alert-warning', ['Validation', 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name]);
  66. return false;
  67. }
  68. return true;
  69. };
  70. $scope.validate = function() {
  71. $scope.infoText = '';
  72. if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
  73. $scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
  74. datasourceSrv.get($scope.current.datasource).then(ds => {
  75. if (!ds.getTagKeys) {
  76. $scope.infoText = 'This datasource does not support adhoc filters yet.';
  77. }
  78. });
  79. }
  80. };
  81. $scope.runQuery = function() {
  82. return variableSrv.updateOptions($scope.current).then(null, function(err) {
  83. if (err.data && err.data.message) { err.message = err.data.message; }
  84. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  85. });
  86. };
  87. $scope.edit = function(variable) {
  88. $scope.current = variable;
  89. $scope.currentIsNew = false;
  90. $scope.mode = 'edit';
  91. $scope.validate();
  92. };
  93. $scope.duplicate = function(variable) {
  94. var clone = _.cloneDeep(variable.getSaveModel());
  95. $scope.current = variableSrv.createVariableFromModel(clone);
  96. $scope.variables.push($scope.current);
  97. $scope.current.name = 'copy_of_'+variable.name;
  98. $scope.dashboard.updateSubmenuVisibility();
  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. };
  113. $scope.typeChanged = function() {
  114. var old = $scope.current;
  115. $scope.current = variableSrv.createVariableFromModel({type: $scope.current.type});
  116. $scope.current.name = old.name;
  117. $scope.current.hide = old.hide;
  118. $scope.current.label = old.label;
  119. var oldIndex = _.indexOf(this.variables, old);
  120. if (oldIndex !== -1) {
  121. this.variables[oldIndex] = $scope.current;
  122. }
  123. $scope.validate();
  124. };
  125. $scope.removeVariable = function(variable) {
  126. var index = _.indexOf($scope.variables, variable);
  127. $scope.variables.splice(index, 1);
  128. $scope.dashboard.updateSubmenuVisibility();
  129. };
  130. }
  131. }
  132. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);