editor_ctrl.ts 4.6 KB

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