editorCtrl.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, alertSrv) {
  9. var replacementDefaults = {
  10. type: 'query',
  11. datasource: null,
  12. refresh_on_load: false,
  13. name: '',
  14. options: [],
  15. includeAll: false,
  16. allFormat: 'glob',
  17. multi: false,
  18. multiFormat: 'glob',
  19. };
  20. $scope.init = function() {
  21. $scope.mode = 'list';
  22. $scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
  23. return !ds.meta.builtIn;
  24. });
  25. $scope.variables = templateSrv.variables;
  26. $scope.reset();
  27. $scope.$watch('mode', function(val) {
  28. if (val === 'new') {
  29. $scope.reset();
  30. }
  31. });
  32. };
  33. $scope.add = function() {
  34. if ($scope.isValid()) {
  35. $scope.variables.push($scope.current);
  36. $scope.update();
  37. $scope.updateSubmenuVisibility();
  38. }
  39. };
  40. $scope.isValid = function() {
  41. if (!$scope.current.name) {
  42. $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
  43. return false;
  44. }
  45. if (!$scope.current.name.match(/^\w+$/)) {
  46. $scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
  47. return false;
  48. }
  49. var sameName = _.findWhere($scope.variables, { name: $scope.current.name });
  50. if (sameName && sameName !== $scope.current) {
  51. $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
  52. return false;
  53. }
  54. return true;
  55. };
  56. $scope.runQuery = function() {
  57. return templateValuesSrv.updateOptions($scope.current).then(function() {
  58. }, function(err) {
  59. alertSrv.set('Templating', 'Failed to run query for variable values: ' + err.message, 'error');
  60. });
  61. };
  62. $scope.edit = function(variable) {
  63. $scope.current = variable;
  64. $scope.currentIsNew = false;
  65. $scope.mode = 'edit';
  66. if ($scope.current.datasource === void 0) {
  67. $scope.current.datasource = null;
  68. $scope.current.type = 'query';
  69. $scope.current.allFormat = 'glob';
  70. }
  71. };
  72. $scope.update = function() {
  73. if ($scope.isValid()) {
  74. $scope.runQuery().then(function() {
  75. $scope.reset();
  76. $scope.mode = 'list';
  77. });
  78. }
  79. };
  80. $scope.reset = function() {
  81. $scope.currentIsNew = true;
  82. $scope.current = angular.copy(replacementDefaults);
  83. };
  84. $scope.typeChanged = function () {
  85. if ($scope.current.type === 'interval') {
  86. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  87. }
  88. if ($scope.current.type === 'query') {
  89. $scope.current.query = '';
  90. }
  91. };
  92. $scope.removeVariable = function(variable) {
  93. var index = _.indexOf($scope.variables, variable);
  94. $scope.variables.splice(index, 1);
  95. $scope.updateSubmenuVisibility();
  96. };
  97. });
  98. });