app.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 moment from 'moment';
  21. import { addClassIfNoOverlayScrollbar } from 'app/core/utils/scrollbar';
  22. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  23. // add move to lodash for backward compatabiltiy
  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 symlinked extensions
  34. const extensionsIndex = (require as any).context('.', true, /extensions\/index.ts/);
  35. extensionsIndex.keys().forEach((key: any) => {
  36. extensionsIndex(key);
  37. });
  38. export class GrafanaApp {
  39. registerFunctions: any;
  40. ngModuleDependencies: any[];
  41. preBootModules: any[];
  42. constructor() {
  43. addClassIfNoOverlayScrollbar('no-overlay-scrollbar');
  44. this.preBootModules = [];
  45. this.registerFunctions = {};
  46. this.ngModuleDependencies = [];
  47. }
  48. useModule(module: angular.IModule) {
  49. if (this.preBootModules) {
  50. this.preBootModules.push(module);
  51. } else {
  52. _.extend(module, this.registerFunctions);
  53. }
  54. this.ngModuleDependencies.push(module.name);
  55. return module;
  56. }
  57. init() {
  58. const app = angular.module('grafana', []);
  59. moment.locale(config.bootData.user.locale);
  60. app.config(
  61. (
  62. $locationProvider: angular.ILocationProvider,
  63. $controllerProvider: angular.IControllerProvider,
  64. $compileProvider: angular.ICompileProvider,
  65. $filterProvider: angular.IFilterProvider,
  66. $httpProvider: angular.IHttpProvider,
  67. $provide: angular.auto.IProvideService
  68. ) => {
  69. // pre assing bindings before constructor calls
  70. $compileProvider.preAssignBindingsEnabled(true);
  71. if (config.buildInfo.env !== 'development') {
  72. $compileProvider.debugInfoEnabled(false);
  73. }
  74. $httpProvider.useApplyAsync(true);
  75. this.registerFunctions.controller = $controllerProvider.register;
  76. this.registerFunctions.directive = $compileProvider.directive;
  77. this.registerFunctions.factory = $provide.factory;
  78. this.registerFunctions.service = $provide.service;
  79. this.registerFunctions.filter = $filterProvider.register;
  80. $provide.decorator('$http', [
  81. '$delegate',
  82. '$templateCache',
  83. ($delegate: any, $templateCache: any) => {
  84. const get = $delegate.get;
  85. $delegate.get = (url: string, config: any) => {
  86. if (url.match(/\.html$/)) {
  87. // some template's already exist in the cache
  88. if (!$templateCache.get(url)) {
  89. url += '?v=' + new Date().getTime();
  90. }
  91. }
  92. return get(url, config);
  93. };
  94. return $delegate;
  95. },
  96. ]);
  97. }
  98. );
  99. this.ngModuleDependencies = [
  100. 'grafana.core',
  101. 'ngRoute',
  102. 'ngSanitize',
  103. '$strap.directives',
  104. 'ang-drag-drop',
  105. 'grafana',
  106. 'pasvaz.bindonce',
  107. 'ui.bootstrap',
  108. 'ui.bootstrap.tpls',
  109. 'react',
  110. ];
  111. // makes it possible to add dynamic stuff
  112. _.each(angularModules, (m: angular.IModule) => {
  113. this.useModule(m);
  114. });
  115. // register react angular wrappers
  116. coreModule.config(setupAngularRoutes);
  117. registerAngularDirectives();
  118. // disable tool tip animation
  119. $.fn.tooltip.defaults.animation = false;
  120. // bootstrap the app
  121. angular.bootstrap(document, this.ngModuleDependencies).invoke(() => {
  122. _.each(this.preBootModules, (module: angular.IModule) => {
  123. _.extend(module, this.registerFunctions);
  124. });
  125. this.preBootModules = null;
  126. });
  127. // Preload selected app plugins
  128. for (const modulePath of config.pluginsToPreload) {
  129. importPluginModule(modulePath);
  130. }
  131. }
  132. }
  133. export default new GrafanaApp();