Angular: support getLegacyAngularInjector() even when angular is disabled. (#67439)

This commit is contained in:
Ryan McKinley 2023-04-28 07:31:51 -07:00 committed by GitHub
parent 2d195741c0
commit cd0fb21f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 4 deletions

View File

@ -10,7 +10,6 @@ import { AngularLoader } from 'app/angular/services/AngularLoader';
import appEvents from 'app/core/app_events';
import config from 'app/core/config';
import { ContextSrv } from 'app/core/services/context_srv';
import { initGrafanaLive } from 'app/features/live';
import { AppEventEmitter, AppEventConsumer } from 'app/types';
import { UtilSrv } from './services/UtilSrv';
@ -32,8 +31,6 @@ export class GrafanaCtrl {
setAngularLoader(angularLoader);
setLegacyAngularInjector($injector);
initGrafanaLive();
$scope.init = () => {
$scope.contextSrv = contextSrv;
$scope.appSubUrl = config.appSubUrl;

View File

@ -1,4 +1,17 @@
import { config, setAngularLoader } from '@grafana/runtime';
import { deprecationWarning } from '@grafana/data';
import {
config,
setAngularLoader,
setLegacyAngularInjector,
getDataSourceSrv,
getBackendSrv,
getTemplateSrv,
} from '@grafana/runtime';
import { contextSrv } from 'app/core/core';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { validationSrv } from 'app/features/manage-dashboards/services/ValidationSrv';
import { getLinkSrv } from 'app/features/panel/panellinks/link_srv';
export async function loadAndInitAngularIfEnabled() {
if (config.angularSupportEnabled) {
@ -7,6 +20,7 @@ export async function loadAndInitAngularIfEnabled() {
app.init();
app.bootstrap();
} else {
// Register a dummy loader that does nothing
setAngularLoader({
load: (elem, scopeProps, template) => {
return {
@ -18,5 +32,61 @@ export async function loadAndInitAngularIfEnabled() {
};
},
});
// Temporary path to allow access to services exposed directly by the angular injector
setLegacyAngularInjector({
get: (key: string) => {
switch (key) {
case 'backendSrv': {
deprecationWarning('getLegacyAngularInjector', 'backendSrv', 'use getBackendSrv() in @grafana/runtime');
return getBackendSrv();
}
case 'contextSrv': {
deprecationWarning('getLegacyAngularInjector', 'contextSrv');
return contextSrv;
}
case 'dashboardSrv': {
// we do not yet have a public interface for this
deprecationWarning('getLegacyAngularInjector', 'getDashboardSrv');
return getDashboardSrv();
}
case 'datasourceSrv': {
deprecationWarning(
'getLegacyAngularInjector',
'datasourceSrv',
'use getDataSourceSrv() in @grafana/runtime'
);
return getDataSourceSrv();
}
case 'linkSrv': {
// we do not yet have a public interface for this
deprecationWarning('getLegacyAngularInjector', 'linkSrv');
return getLinkSrv();
}
case 'validationSrv': {
// we do not yet have a public interface for this
deprecationWarning('getLegacyAngularInjector', 'validationSrv');
return validationSrv;
}
case 'timeSrv': {
// we do not yet have a public interface for this
deprecationWarning('getLegacyAngularInjector', 'timeSrv');
return getTimeSrv();
}
case 'templateSrv': {
deprecationWarning('getLegacyAngularInjector', 'templateSrv', 'use getTemplateSrv() in @grafana/runtime');
return getTemplateSrv();
}
}
throw 'Angular is disabled. Unable to expose: ' + key;
},
} as angular.auto.IInjectorService);
}
}

View File

@ -70,6 +70,7 @@ import { SentryEchoBackend } from './core/services/echo/backends/sentry/SentryBa
import { KeybindingSrv } from './core/services/keybindingSrv';
import { initDevFeatures } from './dev';
import { getTimeSrv } from './features/dashboard/services/TimeSrv';
import { initGrafanaLive } from './features/live';
import { PanelDataErrorView } from './features/panel/components/PanelDataErrorView';
import { PanelRenderer } from './features/panel/components/PanelRenderer';
import { DatasourceSrv } from './features/plugins/datasource_srv';
@ -127,6 +128,7 @@ export class GrafanaApp {
setPanelDataErrorView(PanelDataErrorView);
setLocationSrv(locationService);
setTimeZoneResolver(() => config.bootData.user.timezone);
initGrafanaLive();
// Expose the app-wide eventbus
setAppEvents(appEvents);