editorCtrl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv) {
  9. var replacementDefaults = {
  10. type: 'query',
  11. datasource: null,
  12. refresh: 0,
  13. name: '',
  14. hide: 0,
  15. options: [],
  16. includeAll: false,
  17. multi: false,
  18. };
  19. $scope.refreshOptions = [
  20. {value: 0, text: "Never"},
  21. {value: 1, text: "On Dashboard Load"},
  22. {value: 2, text: "On Time Range Change"},
  23. ];
  24. $scope.hideOptions = [
  25. {value: 0, text: ""},
  26. {value: 1, text: "Label"},
  27. {value: 2, text: "Variable"},
  28. ];
  29. $scope.init = function() {
  30. $scope.mode = 'list';
  31. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  32. return !ds.meta.builtIn;
  33. });
  34. $scope.variables = templateSrv.variables;
  35. $scope.reset();
  36. $scope.$watch('mode', function(val) {
  37. if (val === 'new') {
  38. $scope.reset();
  39. }
  40. });
  41. $scope.$watch('current.datasource', function(val) {
  42. if ($scope.mode === 'new') {
  43. datasourceSrv.get(val).then(function(ds) {
  44. if (ds.meta.defaultMatchFormat) {
  45. $scope.current.allFormat = ds.meta.defaultMatchFormat;
  46. $scope.current.multiFormat = ds.meta.defaultMatchFormat;
  47. }
  48. });
  49. }
  50. });
  51. };
  52. $scope.add = function() {
  53. if ($scope.isValid()) {
  54. $scope.variables.push($scope.current);
  55. $scope.update();
  56. $scope.updateSubmenuVisibility();
  57. }
  58. };
  59. $scope.isValid = function() {
  60. if (!$scope.current.name) {
  61. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  62. return false;
  63. }
  64. if (!$scope.current.name.match(/^\w+$/)) {
  65. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  66. return false;
  67. }
  68. var sameName = _.findWhere($scope.variables, { name: $scope.current.name });
  69. if (sameName && sameName !== $scope.current) {
  70. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  71. return false;
  72. }
  73. return true;
  74. };
  75. $scope.runQuery = function() {
  76. return templateValuesSrv.updateOptions($scope.current).then(null, function(err) {
  77. if (err.data && err.data.message) { err.message = err.data.message; }
  78. $scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
  79. });
  80. };
  81. $scope.edit = function(variable) {
  82. $scope.current = variable;
  83. $scope.currentIsNew = false;
  84. $scope.mode = 'edit';
  85. if ($scope.current.datasource === void 0) {
  86. $scope.current.datasource = null;
  87. $scope.current.type = 'query';
  88. $scope.current.allFormat = 'glob';
  89. }
  90. };
  91. $scope.duplicate = function(variable) {
  92. $scope.current = angular.copy(variable);
  93. $scope.variables.push($scope.current);
  94. $scope.current.name = 'copy_of_'+variable.name;
  95. $scope.updateSubmenuVisibility();
  96. };
  97. $scope.update = function() {
  98. if ($scope.isValid()) {
  99. $scope.runQuery().then(function() {
  100. $scope.reset();
  101. $scope.mode = 'list';
  102. });
  103. }
  104. };
  105. $scope.reset = function() {
  106. $scope.currentIsNew = true;
  107. $scope.current = angular.copy(replacementDefaults);
  108. };
  109. $scope.typeChanged = function () {
  110. if ($scope.current.type === 'interval') {
  111. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  112. }
  113. if ($scope.current.type === 'query') {
  114. $scope.current.query = '';
  115. }
  116. };
  117. $scope.removeVariable = function(variable) {
  118. var index = _.indexOf($scope.variables, variable);
  119. $scope.variables.splice(index, 1);
  120. $scope.updateSubmenuVisibility();
  121. };
  122. });
  123. });