app.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import '@babel/polyfill';
  2. import 'file-saver';
  3. import 'lodash';
  4. import 'jquery';
  5. import 'angular';
  6. import 'angular-route';
  7. import 'angular-sanitize';
  8. import 'angular-native-dragdrop';
  9. import 'angular-bindonce';
  10. import 'react';
  11. import 'react-dom';
  12. import 'vendor/bootstrap/bootstrap';
  13. import 'vendor/angular-ui/ui-bootstrap-tpls';
  14. import 'vendor/angular-other/angular-strap';
  15. import $ from 'jquery';
  16. import angular from 'angular';
  17. import config from 'app/core/config';
  18. // @ts-ignore ignoring this for now, otherwise we would have to extend _ interface with move
  19. import _ from 'lodash';
  20. import { addClassIfNoOverlayScrollbar } from 'app/core/utils/scrollbar';
  21. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  22. // add move to lodash for backward compatabiltiy
  23. // @ts-ignore
  24. _.move = (array: [], fromIndex: number, toIndex: number) => {
  25. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0]);
  26. return array;
  27. };
  28. import { coreModule, angularModules } from 'app/core/core_module';
  29. import { registerAngularDirectives } from 'app/core/core';
  30. import { setupAngularRoutes } from 'app/routes/routes';
  31. import 'app/routes/GrafanaCtrl';
  32. import 'app/features/all';
  33. import { setLocale } from '@grafana/ui/src/utils/moment_wrapper';
  34. // import symlinked extensions
  35. const extensionsIndex = (require as any).context('.', true, /extensions\/index.ts/);
  36. extensionsIndex.keys().forEach((key: any) => {
  37. extensionsIndex(key);
  38. });
  39. export class GrafanaApp {
  40. registerFunctions: any;
  41. ngModuleDependencies: any[];
  42. preBootModules: any[];
  43. constructor() {
  44. addClassIfNoOverlayScrollbar('no-overlay-scrollbar');
  45. this.preBootModules = [];
  46. this.registerFunctions = {};
  47. this.ngModuleDependencies = [];
  48. }
  49. useModule(module: angular.IModule) {
  50. if (this.preBootModules) {
  51. this.preBootModules.push(module);
  52. } else {
  53. _.extend(module, this.registerFunctions);
  54. }
  55. this.ngModuleDependencies.push(module.name);
  56. return module;
  57. }
  58. init() {
  59. const app = angular.module('grafana', []);
  60. setLocale(config.bootData.user.locale);
  61. app.config(
  62. (
  63. $locationProvider: angular.ILocationProvider,
  64. $controllerProvider: angular.IControllerProvider,
  65. $compileProvider: angular.ICompileProvider,
  66. $filterProvider: angular.IFilterProvider,
  67. $httpProvider: angular.IHttpProvider,
  68. $provide: angular.auto.IProvideService
  69. ) => {
  70. // pre assing bindings before constructor calls
  71. $compileProvider.preAssignBindingsEnabled(true);
  72. if (config.buildInfo.env !== 'development') {
  73. $compileProvider.debugInfoEnabled(false);
  74. }
  75. $httpProvider.useApplyAsync(true);
  76. this.registerFunctions.controller = $controllerProvider.register;
  77. this.registerFunctions.directive = $compileProvider.directive;
  78. this.registerFunctions.factory = $provide.factory;
  79. this.registerFunctions.service = $provide.service;
  80. this.registerFunctions.filter = $filterProvider.register;
  81. $provide.decorator('$http', [
  82. '$delegate',
  83. '$templateCache',
  84. ($delegate: any, $templateCache: any) => {
  85. const get = $delegate.get;
  86. $delegate.get = (url: string, config: any) => {
  87. if (url.match(/\.html$/)) {
  88. // some template's already exist in the cache
  89. if (!$templateCache.get(url)) {
  90. url += '?v=' + new Date().getTime();
  91. }
  92. }
  93. return get(url, config);
  94. };
  95. return $delegate;
  96. },
  97. ]);
  98. }
  99. );
  100. this.ngModuleDependencies = [
  101. 'grafana.core',
  102. 'ngRoute',
  103. 'ngSanitize',
  104. '$strap.directives',
  105. 'ang-drag-drop',
  106. 'grafana',
  107. 'pasvaz.bindonce',
  108. 'ui.bootstrap',
  109. 'ui.bootstrap.tpls',
  110. 'react',
  111. ];
  112. // makes it possible to add dynamic stuff
  113. _.each(angularModules, (m: angular.IModule) => {
  114. this.useModule(m);
  115. });
  116. // register react angular wrappers
  117. coreModule.config(setupAngularRoutes);
  118. registerAngularDirectives();
  119. // disable tool tip animation
  120. $.fn.tooltip.defaults.animation = false;
  121. // bootstrap the app
  122. angular.bootstrap(document, this.ngModuleDependencies).invoke(() => {
  123. _.each(this.preBootModules, (module: angular.IModule) => {
  124. _.extend(module, this.registerFunctions);
  125. });
  126. this.preBootModules = null;
  127. });
  128. // Preload selected app plugins
  129. for (const modulePath of config.pluginsToPreload) {
  130. importPluginModule(modulePath);
  131. }
  132. }
  133. }
  134. export default new GrafanaApp();