From cd0fb21f297be56c7d8de5d0fd898ce8ed468e0d Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 28 Apr 2023 07:31:51 -0700 Subject: [PATCH] Angular: support getLegacyAngularInjector() even when angular is disabled. (#67439) --- public/app/angular/GrafanaCtrl.ts | 3 - .../angular/loadAndInitAngularIfEnabled.ts | 72 ++++++++++++++++++- public/app/app.ts | 2 + 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/public/app/angular/GrafanaCtrl.ts b/public/app/angular/GrafanaCtrl.ts index 395c3ed97f1..cb6a144c224 100644 --- a/public/app/angular/GrafanaCtrl.ts +++ b/public/app/angular/GrafanaCtrl.ts @@ -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; diff --git a/public/app/angular/loadAndInitAngularIfEnabled.ts b/public/app/angular/loadAndInitAngularIfEnabled.ts index 211f44ae3b8..ee37609818b 100644 --- a/public/app/angular/loadAndInitAngularIfEnabled.ts +++ b/public/app/angular/loadAndInitAngularIfEnabled.ts @@ -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); } } diff --git a/public/app/app.ts b/public/app/app.ts index 1ea3967a202..f2f9eb799e8 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -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);