templateEditorCtrl.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. };
  18. $scope.init = function() {
  19. $scope.editor = { index: 0 };
  20. $scope.datasources = datasourceSrv.getMetricSources();
  21. $scope.variables = templateSrv.variables;
  22. $scope.reset();
  23. $scope.$watch('editor.index', function(index) {
  24. if ($scope.currentIsNew === false && index === 1) {
  25. $scope.reset();
  26. }
  27. });
  28. };
  29. $scope.add = function() {
  30. $scope.variables.push($scope.current);
  31. $scope.update();
  32. };
  33. $scope.runQuery = function() {
  34. return templateValuesSrv.updateOptions($scope.current).then(function() {
  35. }, function(err) {
  36. alertSrv.set('Templating', 'Failed to run query for variable values: ' + err.message, 'error');
  37. });
  38. };
  39. $scope.edit = function(variable) {
  40. $scope.current = variable;
  41. $scope.currentIsNew = false;
  42. $scope.editor.index = 2;
  43. if ($scope.current.datasource === void 0) {
  44. $scope.current.datasource = null;
  45. $scope.current.type = 'query';
  46. $scope.current.allFormat = 'Glob';
  47. }
  48. };
  49. $scope.update = function() {
  50. $scope.runQuery().then(function() {
  51. $scope.reset();
  52. $scope.editor.index = 0;
  53. });
  54. };
  55. $scope.reset = function() {
  56. $scope.currentIsNew = true;
  57. $scope.current = angular.copy(replacementDefaults);
  58. };
  59. $scope.typeChanged = function () {
  60. if ($scope.current.type === 'interval') {
  61. $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
  62. }
  63. if ($scope.current.type === 'query') {
  64. $scope.current.query = '';
  65. }
  66. };
  67. $scope.removeVariable = function(variable) {
  68. var index = _.indexOf($scope.variables, variable);
  69. $scope.variables.splice(index, 1);
  70. };
  71. });
  72. });