editor_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.refreshOptions = [
  10. {value: 0, text: "Never"},
  11. {value: 1, text: "On Dashboard Load"},
  12. {value: 2, text: "On Time Range Change"},
  13. ];
  14. $scope.sortOptions = [
  15. {value: 0, text: "Disabled"},
  16. {value: 1, text: "Alphabetical (asc)"},
  17. {value: 2, text: "Alphabetical (desc)"},
  18. {value: 3, text: "Numerical (asc)"},
  19. {value: 4, text: "Numerical (desc)"},
  20. ];
  21. $scope.hideOptions = [
  22. {value: 0, text: ""},
  23. {value: 1, text: "Label"},
  24. {value: 2, text: "Variable"},
  25. ];
  26. $scope.init = function() {
  27. $scope.mode = 'list';
  28. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  29. return !ds.meta.builtIn && ds.value !== null;
  30. });
  31. $scope.datasourceTypes = _($scope.datasources).uniqBy('meta.id').map(function(ds) {
  32. return {text: ds.meta.name, value: ds.meta.id};
  33. }).value();
  34. $scope.variables = variableSrv.variables;
  35. $scope.reset();
  36. $scope.$watch('mode', function(val) {
  37. if (val === 'new') {
  38. $scope.reset();
  39. }
  40. });
  41. };
  42. $scope.add = function() {
  43. if ($scope.isValid()) {
  44. $scope.variables.push($scope.current);
  45. $scope.update();
  46. $scope.updateSubmenuVisibility();
  47. }
  48. };
  49. $scope.isValid = function() {
  50. if (!$scope.current.name) {
  51. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  52. return false;
  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.runQuery = function() {
  66. return variableSrv.updateOptions($scope.current).then(null, function(err) {
  67. if (err.data && err.data.message) { err.message = err.data.message; }
  68. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  69. });
  70. };
  71. $scope.edit = function(variable) {
  72. $scope.current = variable;
  73. $scope.currentIsNew = false;
  74. $scope.mode = 'edit';
  75. };
  76. $scope.duplicate = function(variable) {
  77. var clone = _.cloneDeep(variable.getModel());
  78. $scope.current = variableSrv.createVariableFromModel(clone);
  79. $scope.variables.push($scope.current);
  80. $scope.current.name = 'copy_of_'+variable.name;
  81. $scope.updateSubmenuVisibility();
  82. };
  83. $scope.update = function() {
  84. if ($scope.isValid()) {
  85. $scope.runQuery().then(function() {
  86. $scope.reset();
  87. $scope.mode = 'list';
  88. templateSrv.updateTemplateData();
  89. });
  90. }
  91. };
  92. $scope.reset = function() {
  93. $scope.currentIsNew = true;
  94. $scope.current = variableSrv.createVariableFromModel({type: 'query'});
  95. };
  96. $scope.typeChanged = function() {
  97. var old = $scope.current;
  98. $scope.current = variableSrv.createVariableFromModel({type: $scope.current.type});
  99. $scope.current.name = old.name;
  100. $scope.current.hide = old.hide;
  101. $scope.current.label = old.label;
  102. var oldIndex = _.indexOf(this.variables, old);
  103. if (oldIndex !== -1) {
  104. this.variables[oldIndex] = $scope.current;
  105. }
  106. };
  107. $scope.removeVariable = function(variable) {
  108. var index = _.indexOf($scope.variables, variable);
  109. $scope.variables.splice(index, 1);
  110. $scope.updateSubmenuVisibility();
  111. };
  112. }
  113. }
  114. coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);