AngularApp.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'angular';
  2. import 'angular-route';
  3. import 'angular-sanitize';
  4. import 'angular-bindonce';
  5. import 'vendor/bootstrap/bootstrap';
  6. import angular from 'angular'; // eslint-disable-line no-duplicate-imports
  7. import { extend } from 'lodash';
  8. import { getTemplateSrv } from '@grafana/runtime';
  9. import coreModule, { angularModules } from 'app/angular/core_module';
  10. import appEvents from 'app/core/app_events';
  11. import { config } from 'app/core/config';
  12. import { contextSrv } from 'app/core/services/context_srv';
  13. import { DashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
  14. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  15. import { exposeToPlugin } from 'app/features/plugins/plugin_loader';
  16. import * as sdk from 'app/plugins/sdk';
  17. import { registerAngularDirectives } from './angular_wrappers';
  18. import { initAngularRoutingBridge } from './bridgeReactAngularRouting';
  19. import { monkeyPatchInjectorWithPreAssignedBindings } from './injectorMonkeyPatch';
  20. import { promiseToDigest } from './promiseToDigest';
  21. import { registerComponents } from './registerComponents';
  22. export class AngularApp {
  23. ngModuleDependencies: any[];
  24. preBootModules: any[];
  25. registerFunctions: any;
  26. constructor() {
  27. this.preBootModules = [];
  28. this.ngModuleDependencies = [];
  29. this.registerFunctions = {};
  30. }
  31. init() {
  32. const app = angular.module('grafana', []);
  33. app.config(
  34. (
  35. $controllerProvider: angular.IControllerProvider,
  36. $compileProvider: angular.ICompileProvider,
  37. $filterProvider: angular.IFilterProvider,
  38. $httpProvider: angular.IHttpProvider,
  39. $provide: angular.auto.IProvideService
  40. ) => {
  41. if (config.buildInfo.env !== 'development') {
  42. $compileProvider.debugInfoEnabled(false);
  43. }
  44. $httpProvider.useApplyAsync(true);
  45. this.registerFunctions.controller = $controllerProvider.register;
  46. this.registerFunctions.directive = $compileProvider.directive;
  47. this.registerFunctions.factory = $provide.factory;
  48. this.registerFunctions.service = $provide.service;
  49. this.registerFunctions.filter = $filterProvider.register;
  50. $provide.decorator('$http', [
  51. '$delegate',
  52. '$templateCache',
  53. ($delegate: any, $templateCache: any) => {
  54. const get = $delegate.get;
  55. $delegate.get = (url: string, config: any) => {
  56. if (url.match(/\.html$/)) {
  57. // some template's already exist in the cache
  58. if (!$templateCache.get(url)) {
  59. url += '?v=' + new Date().getTime();
  60. }
  61. }
  62. return get(url, config);
  63. };
  64. return $delegate;
  65. },
  66. ]);
  67. }
  68. );
  69. this.ngModuleDependencies = ['grafana.core', 'ngSanitize', 'grafana', 'pasvaz.bindonce', 'react'];
  70. // makes it possible to add dynamic stuff
  71. angularModules.forEach((m: angular.IModule) => {
  72. this.useModule(m);
  73. });
  74. // register react angular wrappers
  75. angular.module('grafana.services').service('dashboardLoaderSrv', DashboardLoaderSrv);
  76. coreModule.factory('timeSrv', () => getTimeSrv());
  77. coreModule.factory('templateSrv', () => getTemplateSrv());
  78. registerAngularDirectives();
  79. registerComponents();
  80. initAngularRoutingBridge();
  81. // Angular plugins import this
  82. exposeToPlugin('angular', angular);
  83. exposeToPlugin('app/core/utils/promiseToDigest', { promiseToDigest, __esModule: true });
  84. exposeToPlugin('app/plugins/sdk', sdk);
  85. exposeToPlugin('app/core/core_module', coreModule);
  86. exposeToPlugin('app/core/core', {
  87. coreModule: coreModule,
  88. appEvents: appEvents,
  89. contextSrv: contextSrv,
  90. __esModule: true,
  91. });
  92. // disable tool tip animation
  93. $.fn.tooltip.defaults.animation = false;
  94. }
  95. useModule(module: angular.IModule) {
  96. if (this.preBootModules) {
  97. this.preBootModules.push(module);
  98. } else {
  99. extend(module, this.registerFunctions);
  100. }
  101. this.ngModuleDependencies.push(module.name);
  102. return module;
  103. }
  104. bootstrap() {
  105. const injector = angular.bootstrap(document.getElementById('ngRoot')!, this.ngModuleDependencies);
  106. monkeyPatchInjectorWithPreAssignedBindings(injector);
  107. injector.invoke(() => {
  108. this.preBootModules.forEach((module) => {
  109. extend(module, this.registerFunctions);
  110. });
  111. // I don't know
  112. return () => {};
  113. });
  114. return injector;
  115. }
  116. }