mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Minor refactoring and adding some typing
This commit is contained in:
parent
78cb3239b8
commit
8b68ba5cbb
@ -135,7 +135,7 @@ export class VariableEditorCtrl {
|
||||
$scope.runQuery().then(() => {
|
||||
$scope.reset();
|
||||
$scope.mode = 'list';
|
||||
templateSrv.updateTemplateData();
|
||||
templateSrv.updateIndex();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -348,7 +348,7 @@ describe('templateSrv', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateTemplateData with simple value', () => {
|
||||
describe('updateIndex with simple value', () => {
|
||||
beforeEach(() => {
|
||||
initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
|
||||
});
|
||||
@ -476,7 +476,7 @@ describe('templateSrv', () => {
|
||||
}
|
||||
]);
|
||||
_templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
|
||||
_templateSrv.updateTemplateData();
|
||||
_templateSrv.updateIndex();
|
||||
});
|
||||
|
||||
it('should replace with text except for grafanaVariables', () => {
|
||||
|
@ -23,7 +23,7 @@ describe('VariableSrv', function(this: any) {
|
||||
init: vars => {
|
||||
this.variables = vars;
|
||||
},
|
||||
updateTemplateData: () => {},
|
||||
updateIndex: () => {},
|
||||
replace: str =>
|
||||
str.replace(this.regex, match => {
|
||||
return match;
|
||||
|
@ -11,7 +11,7 @@ describe('VariableSrv init', function(this: any) {
|
||||
this.variables = vars;
|
||||
},
|
||||
variableInitialized: () => {},
|
||||
updateTemplateData: () => {},
|
||||
updateIndex: () => {},
|
||||
replace: str =>
|
||||
str.replace(this.regex, match => {
|
||||
return match;
|
||||
@ -53,6 +53,7 @@ describe('VariableSrv init', function(this: any) {
|
||||
templateSrv,
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv);
|
||||
|
||||
$injector.instantiate = (variable, model) => {
|
||||
|
@ -25,10 +25,10 @@ export class TemplateSrv {
|
||||
init(variables, timeRange?: TimeRange) {
|
||||
this.variables = variables;
|
||||
this.timeRange = timeRange;
|
||||
this.updateTemplateData();
|
||||
this.updateIndex();
|
||||
}
|
||||
|
||||
updateTemplateData() {
|
||||
updateIndex() {
|
||||
const existsOrEmpty = value => value || value === '';
|
||||
|
||||
this.index = this.variables.reduce((acc, currentValue) => {
|
||||
@ -54,9 +54,9 @@ export class TemplateSrv {
|
||||
}
|
||||
}
|
||||
|
||||
updateTimeVariables(timeRange: TimeRange) {
|
||||
updateTimeRange(timeRange: TimeRange) {
|
||||
this.timeRange = timeRange;
|
||||
this.updateTemplateData();
|
||||
this.updateIndex();
|
||||
}
|
||||
|
||||
variableInitialized(variable) {
|
||||
@ -289,7 +289,7 @@ export class TemplateSrv {
|
||||
});
|
||||
}
|
||||
|
||||
fillVariableValuesForUrl(params, scopedVars) {
|
||||
fillVariableValuesForUrl(params, scopedVars?) {
|
||||
_.each(this.variables, variable => {
|
||||
if (scopedVars && scopedVars[variable.name] !== void 0) {
|
||||
if (scopedVars[variable.name].skipUrlSync) {
|
||||
|
@ -6,18 +6,28 @@ import _ from 'lodash';
|
||||
import coreModule from 'app/core/core_module';
|
||||
import { variableTypes } from './variable';
|
||||
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';
|
||||
|
||||
export class VariableSrv {
|
||||
dashboard: any;
|
||||
variables: any;
|
||||
dashboard: DashboardModel;
|
||||
variables: any[];
|
||||
|
||||
/** @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);
|
||||
}
|
||||
|
||||
init(dashboard) {
|
||||
init(dashboard: DashboardModel) {
|
||||
this.dashboard = dashboard;
|
||||
this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
|
||||
|
||||
@ -38,12 +48,12 @@ export class VariableSrv {
|
||||
})
|
||||
)
|
||||
.then(() => {
|
||||
this.templateSrv.updateTemplateData();
|
||||
this.templateSrv.updateIndex();
|
||||
});
|
||||
}
|
||||
|
||||
onTimeRangeUpdated(timeRange: TimeRange) {
|
||||
this.templateSrv.updateTimeVariables(timeRange);
|
||||
this.templateSrv.updateTimeRange(timeRange);
|
||||
const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
|
||||
const previousOptions = variable.options.slice();
|
||||
|
||||
@ -102,14 +112,14 @@ export class VariableSrv {
|
||||
|
||||
addVariable(variable) {
|
||||
this.variables.push(variable);
|
||||
this.templateSrv.updateTemplateData();
|
||||
this.templateSrv.updateIndex();
|
||||
this.dashboard.updateSubmenuVisibility();
|
||||
}
|
||||
|
||||
removeVariable(variable) {
|
||||
const index = _.indexOf(this.variables, variable);
|
||||
this.variables.splice(index, 1);
|
||||
this.templateSrv.updateTemplateData();
|
||||
this.templateSrv.updateIndex();
|
||||
this.dashboard.updateSubmenuVisibility();
|
||||
}
|
||||
|
||||
|
@ -182,7 +182,7 @@ export function TemplateSrvStub(this: any) {
|
||||
return [];
|
||||
};
|
||||
this.fillVariableValuesForUrl = () => {};
|
||||
this.updateTemplateData = () => {};
|
||||
this.updateIndex = () => {};
|
||||
this.variableExists = () => {
|
||||
return false;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user