mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Types: Adds type safety to appEvents (#19418)
* Types: Add type safety to appEvents
This commit is contained in:
@@ -5,6 +5,7 @@ import appEvents from 'app/core/app_events';
|
||||
import DatasourceSrv from '../plugins/datasource_srv';
|
||||
import { VariableSrv } from './all';
|
||||
import { TemplateSrv } from './template_srv';
|
||||
import { AppEvents } from '@grafana/data';
|
||||
|
||||
export class VariableEditorCtrl {
|
||||
/** @ngInject */
|
||||
@@ -85,13 +86,16 @@ export class VariableEditorCtrl {
|
||||
}
|
||||
|
||||
if (!$scope.current.name.match(/^\w+$/)) {
|
||||
appEvents.emit('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
|
||||
appEvents.emit(AppEvents.alertWarning, [
|
||||
'Validation',
|
||||
'Only word and digit characters are allowed in variable names',
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
|
||||
const sameName: any = _.find($scope.variables, { name: $scope.current.name });
|
||||
if (sameName && sameName !== $scope.current) {
|
||||
appEvents.emit('alert-warning', ['Validation', 'Variable with the same name already exists']);
|
||||
appEvents.emit(AppEvents.alertWarning, ['Validation', 'Variable with the same name already exists']);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -100,7 +104,7 @@ export class VariableEditorCtrl {
|
||||
_.isString($scope.current.query) &&
|
||||
$scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))
|
||||
) {
|
||||
appEvents.emit('alert-warning', [
|
||||
appEvents.emit(AppEvents.alertWarning, [
|
||||
'Validation',
|
||||
'Query cannot contain a reference to itself. Variable: $' + $scope.current.name,
|
||||
]);
|
||||
@@ -128,7 +132,10 @@ export class VariableEditorCtrl {
|
||||
if (err.data && err.data.message) {
|
||||
err.message = err.data.message;
|
||||
}
|
||||
appEvents.emit('alert-error', ['Templating', 'Template variables could not be initialized: ' + err.message]);
|
||||
appEvents.emit(AppEvents.alertError, [
|
||||
'Templating',
|
||||
'Template variables could not be initialized: ' + err.message,
|
||||
]);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { VariableEditorCtrl } from '../editor_ctrl';
|
||||
import { TemplateSrv } from '../template_srv';
|
||||
import { AppEvents } from '@grafana/data';
|
||||
|
||||
let mockEmit: any;
|
||||
jest.mock('app/core/app_events', () => {
|
||||
@@ -32,7 +33,7 @@ describe('VariableEditorCtrl', () => {
|
||||
it('should emit an error', () => {
|
||||
return scope.runQuery().then(res => {
|
||||
expect(mockEmit).toBeCalled();
|
||||
expect(mockEmit.mock.calls[0][0]).toBe('alert-error');
|
||||
expect(mockEmit.mock.calls[0][0]).toBe(AppEvents.alertError);
|
||||
expect(mockEmit.mock.calls[0][1][0]).toBe('Templating');
|
||||
expect(mockEmit.mock.calls[0][1][1]).toBe('Template variables could not be initialized: error');
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||
|
||||
// Types
|
||||
import { TimeRange } from '@grafana/data';
|
||||
import { CoreEvents } from 'app/types';
|
||||
|
||||
export class VariableSrv {
|
||||
dashboard: DashboardModel;
|
||||
@@ -28,8 +29,11 @@ export class VariableSrv {
|
||||
|
||||
init(dashboard: DashboardModel) {
|
||||
this.dashboard = dashboard;
|
||||
this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this));
|
||||
this.dashboard.events.on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this));
|
||||
this.dashboard.events.on(CoreEvents.timeRangeUpdated, this.onTimeRangeUpdated.bind(this));
|
||||
this.dashboard.events.on(
|
||||
CoreEvents.templateVariableValueUpdated,
|
||||
this.updateUrlParamsWithCurrentVariables.bind(this)
|
||||
);
|
||||
|
||||
// create working class models representing variables
|
||||
this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
|
||||
|
||||
Reference in New Issue
Block a user