Commit 8b68ba5c by Torkel Ödegaard

Minor refactoring and adding some typing

parent 78cb3239
...@@ -135,7 +135,7 @@ export class VariableEditorCtrl { ...@@ -135,7 +135,7 @@ export class VariableEditorCtrl {
$scope.runQuery().then(() => { $scope.runQuery().then(() => {
$scope.reset(); $scope.reset();
$scope.mode = 'list'; $scope.mode = 'list';
templateSrv.updateTemplateData(); templateSrv.updateIndex();
}); });
} }
}; };
......
...@@ -348,7 +348,7 @@ describe('templateSrv', () => { ...@@ -348,7 +348,7 @@ describe('templateSrv', () => {
}); });
}); });
describe('updateTemplateData with simple value', () => { describe('updateIndex with simple value', () => {
beforeEach(() => { beforeEach(() => {
initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]); initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
}); });
...@@ -476,7 +476,7 @@ describe('templateSrv', () => { ...@@ -476,7 +476,7 @@ describe('templateSrv', () => {
} }
]); ]);
_templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m'); _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
_templateSrv.updateTemplateData(); _templateSrv.updateIndex();
}); });
it('should replace with text except for grafanaVariables', () => { it('should replace with text except for grafanaVariables', () => {
......
...@@ -23,7 +23,7 @@ describe('VariableSrv', function(this: any) { ...@@ -23,7 +23,7 @@ describe('VariableSrv', function(this: any) {
init: vars => { init: vars => {
this.variables = vars; this.variables = vars;
}, },
updateTemplateData: () => {}, updateIndex: () => {},
replace: str => replace: str =>
str.replace(this.regex, match => { str.replace(this.regex, match => {
return match; return match;
......
...@@ -11,7 +11,7 @@ describe('VariableSrv init', function(this: any) { ...@@ -11,7 +11,7 @@ describe('VariableSrv init', function(this: any) {
this.variables = vars; this.variables = vars;
}, },
variableInitialized: () => {}, variableInitialized: () => {},
updateTemplateData: () => {}, updateIndex: () => {},
replace: str => replace: str =>
str.replace(this.regex, match => { str.replace(this.regex, match => {
return match; return match;
...@@ -53,6 +53,7 @@ describe('VariableSrv init', function(this: any) { ...@@ -53,6 +53,7 @@ describe('VariableSrv init', function(this: any) {
templateSrv, templateSrv,
}; };
// @ts-ignore
ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv); ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv);
$injector.instantiate = (variable, model) => { $injector.instantiate = (variable, model) => {
......
...@@ -25,10 +25,10 @@ export class TemplateSrv { ...@@ -25,10 +25,10 @@ export class TemplateSrv {
init(variables, timeRange?: TimeRange) { init(variables, timeRange?: TimeRange) {
this.variables = variables; this.variables = variables;
this.timeRange = timeRange; this.timeRange = timeRange;
this.updateTemplateData(); this.updateIndex();
} }
updateTemplateData() { updateIndex() {
const existsOrEmpty = value => value || value === ''; const existsOrEmpty = value => value || value === '';
this.index = this.variables.reduce((acc, currentValue) => { this.index = this.variables.reduce((acc, currentValue) => {
...@@ -54,9 +54,9 @@ export class TemplateSrv { ...@@ -54,9 +54,9 @@ export class TemplateSrv {
} }
} }
updateTimeVariables(timeRange: TimeRange) { updateTimeRange(timeRange: TimeRange) {
this.timeRange = timeRange; this.timeRange = timeRange;
this.updateTemplateData(); this.updateIndex();
} }
variableInitialized(variable) { variableInitialized(variable) {
...@@ -289,7 +289,7 @@ export class TemplateSrv { ...@@ -289,7 +289,7 @@ export class TemplateSrv {
}); });
} }
fillVariableValuesForUrl(params, scopedVars) { fillVariableValuesForUrl(params, scopedVars?) {
_.each(this.variables, variable => { _.each(this.variables, variable => {
if (scopedVars && scopedVars[variable.name] !== void 0) { if (scopedVars && scopedVars[variable.name] !== void 0) {
if (scopedVars[variable.name].skipUrlSync) { if (scopedVars[variable.name].skipUrlSync) {
......
...@@ -6,18 +6,28 @@ import _ from 'lodash'; ...@@ -6,18 +6,28 @@ import _ from 'lodash';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import { variableTypes } from './variable'; import { variableTypes } from './variable';
import { Graph } from 'app/core/utils/dag'; import { Graph } from 'app/core/utils/dag';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { TimeSrv } from 'app/features/dashboard/time_srv';
import { DashboardModel } from 'app/features/dashboard/dashboard_model';
// Types
import { TimeRange } from '@grafana/ui/src'; import { TimeRange } from '@grafana/ui/src';
export class VariableSrv { export class VariableSrv {
dashboard: any; dashboard: DashboardModel;
variables: any; variables: any[];
/** @ngInject */ /** @ngInject */
constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv, private timeSrv) { constructor(private $rootScope,
private $q,
private $location,
private $injector,
private templateSrv: TemplateSrv,
private timeSrv: TimeSrv) {
$rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope); $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope);
} }
init(dashboard) { init(dashboard: DashboardModel) {
this.dashboard = dashboard; this.dashboard = dashboard;
this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this)); this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
...@@ -38,12 +48,12 @@ export class VariableSrv { ...@@ -38,12 +48,12 @@ export class VariableSrv {
}) })
) )
.then(() => { .then(() => {
this.templateSrv.updateTemplateData(); this.templateSrv.updateIndex();
}); });
} }
onTimeRangeUpdated(timeRange: TimeRange) { onTimeRangeUpdated(timeRange: TimeRange) {
this.templateSrv.updateTimeVariables(timeRange); this.templateSrv.updateTimeRange(timeRange);
const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => { const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
const previousOptions = variable.options.slice(); const previousOptions = variable.options.slice();
...@@ -102,14 +112,14 @@ export class VariableSrv { ...@@ -102,14 +112,14 @@ export class VariableSrv {
addVariable(variable) { addVariable(variable) {
this.variables.push(variable); this.variables.push(variable);
this.templateSrv.updateTemplateData(); this.templateSrv.updateIndex();
this.dashboard.updateSubmenuVisibility(); this.dashboard.updateSubmenuVisibility();
} }
removeVariable(variable) { removeVariable(variable) {
const index = _.indexOf(this.variables, variable); const index = _.indexOf(this.variables, variable);
this.variables.splice(index, 1); this.variables.splice(index, 1);
this.templateSrv.updateTemplateData(); this.templateSrv.updateIndex();
this.dashboard.updateSubmenuVisibility(); this.dashboard.updateSubmenuVisibility();
} }
......
...@@ -182,7 +182,7 @@ export function TemplateSrvStub(this: any) { ...@@ -182,7 +182,7 @@ export function TemplateSrvStub(this: any) {
return []; return [];
}; };
this.fillVariableValuesForUrl = () => {}; this.fillVariableValuesForUrl = () => {};
this.updateTemplateData = () => {}; this.updateIndex = () => {};
this.variableExists = () => { this.variableExists = () => {
return false; return false;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment