2014-08-26 07:17:46 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
],
|
|
|
|
function (angular, _) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2014-08-27 14:47:41 -05:00
|
|
|
module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv) {
|
2014-08-26 07:17:46 -05:00
|
|
|
|
|
|
|
var replacementDefaults = {
|
2014-08-27 08:54:30 -05:00
|
|
|
type: 'query',
|
2014-08-26 07:17:46 -05:00
|
|
|
datasource: null,
|
|
|
|
refresh_on_load: false,
|
|
|
|
name: '',
|
|
|
|
options: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
$scope.editor = { index: 0 };
|
|
|
|
$scope.datasources = datasourceSrv.getMetricSources();
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.variables = templateSrv.variables;
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.reset();
|
2014-08-27 03:41:27 -05:00
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
_.each($scope.variables, function(variable) {
|
|
|
|
if (variable.datasource === void 0) {
|
|
|
|
variable.datasource = null;
|
|
|
|
variable.type = 'query';
|
2014-08-27 08:54:30 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.$watch('editor.index', function(index) {
|
|
|
|
if ($scope.currentIsNew === false && index === 1) {
|
|
|
|
$scope.reset();
|
|
|
|
}
|
2014-08-27 03:41:27 -05:00
|
|
|
});
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.add = function() {
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.variables.push($scope.current);
|
2014-08-29 03:17:00 -05:00
|
|
|
$scope.update();
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.runQuery = function() {
|
2014-08-28 05:44:01 -05:00
|
|
|
templateValuesSrv.updateOptions($scope.current);
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.edit = function(variable) {
|
|
|
|
$scope.current = variable;
|
2014-08-27 03:41:27 -05:00
|
|
|
$scope.currentIsNew = false;
|
|
|
|
$scope.editor.index = 2;
|
|
|
|
};
|
|
|
|
|
2014-08-27 08:54:30 -05:00
|
|
|
$scope.update = function() {
|
2014-08-28 05:44:01 -05:00
|
|
|
templateValuesSrv.updateOptions($scope.current);
|
2014-08-27 08:54:30 -05:00
|
|
|
$scope.reset();
|
|
|
|
$scope.editor.index = 0;
|
|
|
|
};
|
|
|
|
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.reset = function() {
|
|
|
|
$scope.currentIsNew = true;
|
|
|
|
$scope.current = angular.copy(replacementDefaults);
|
|
|
|
};
|
|
|
|
|
2014-08-27 14:47:41 -05:00
|
|
|
$scope.typeChanged = function () {
|
|
|
|
if ($scope.current.type === 'time period') {
|
2014-08-28 09:03:13 -05:00
|
|
|
$scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$scope.current.query = '';
|
2014-08-27 14:47:41 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.removeVariable = function(variable) {
|
|
|
|
var index = _.indexOf($scope.variables, variable);
|
|
|
|
$scope.variables.splice(index, 1);
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|