editorCtrl.js 3.8 KB

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