editor_ctrl.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.builtIn && 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. return true;
  65. };
  66. $scope.validate = function() {
  67. $scope.infoText = '';
  68. if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
  69. $scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
  70. datasourceSrv.get($scope.current.datasource).then(ds => {
  71. if (!ds.getTagKeys) {
  72. $scope.infoText = 'This datasource does not support adhoc filters yet.';
  73. }
  74. });
  75. }
  76. };
  77. $scope.runQuery = function() {
  78. return variableSrv.updateOptions($scope.current).then(null, function(err) {
  79. if (err.data && err.data.message) { err.message = err.data.message; }
  80. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  81. });
  82. };
  83. $scope.edit = function(variable) {
  84. $scope.current = variable;
  85. $scope.currentIsNew = false;
  86. $scope.mode = 'edit';
  87. $scope.validate();
  88. };
  89. $scope.duplicate = function(variable) {
  90. var clone = _.cloneDeep(variable.getSaveModel());
  91. $scope.current = variableSrv.createVariableFromModel(clone);
  92. $scope.variables.push($scope.current);
  93. $scope.current.name = 'copy_of_'+variable.name;
  94. $scope.dashboard.updateSubmenuVisibility();
  95. };
  96. $scope.update = function() {
  97. if ($scope.isValid()) {
  98. $scope.runQuery().then(function() {
  99. $scope.reset();
  100. $scope.mode = 'list';
  101. templateSrv.updateTemplateData();
  102. });
  103. }
  104. };
  105. $scope.reset = function() {
  106. $scope.currentIsNew = true;
  107. $scope.current = variableSrv.createVariableFromModel({type: 'query'});
  108. };
  109. $scope.typeChanged = function() {
  110. var old = $scope.current;
  111. $scope.current = variableSrv.createVariableFromModel({type: $scope.current.type});
  112. $scope.current.name = old.name;
  113. $scope.current.hide = old.hide;
  114. $scope.current.label = old.label;
  115. var oldIndex = _.indexOf(this.variables, old);
  116. if (oldIndex !== -1) {
  117. this.variables[oldIndex] = $scope.current;
  118. }
  119. $scope.validate();
  120. };
  121. $scope.removeVariable = function(variable) {
  122. var index = _.indexOf($scope.variables, variable);
  123. $scope.variables.splice(index, 1);
  124. $scope.dashboard.updateSubmenuVisibility();
  125. };
  126. }
  127. }
  128. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);