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-other/angular-strap';
  14. import $ from 'jquery';
  15. import angular from 'angular';
  16. import config from 'app/core/config';
  17. // @ts-ignore ignoring this for now, otherwise we would have to extend _ interface with move
  18. import _ from 'lodash';
  19. import { addClassIfNoOverlayScrollbar } from 'app/core/utils/scrollbar';
  20. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  21. // add move to lodash for backward compatabiltiy
  22. // @ts-ignore
  23. _.move = (array: [], fromIndex: number, toIndex: number) => {
  24. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0]);
  25. return array;
  26. };
  27. import { coreModule, angularModules } from 'app/core/core_module';
  28. import { registerAngularDirectives } from 'app/core/core';
  29. import { setupAngularRoutes } from 'app/routes/routes';
  30. import 'app/routes/GrafanaCtrl';
  31. import 'app/features/all';
  32. import { setLocale } from '@grafana/data';
  33. import { setMarkdownOptions } from '@grafana/data';
  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[] | null;
  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. setMarkdownOptions({ sanitize: !config.disableSanitizeHtml });
  62. app.config(
  63. (
  64. $locationProvider: angular.ILocationProvider,
  65. $controllerProvider: angular.IControllerProvider,
  66. $compileProvider: angular.ICompileProvider,
  67. $filterProvider: angular.IFilterProvider,
  68. $httpProvider: angular.IHttpProvider,
  69. $provide: angular.auto.IProvideService
  70. ) => {
  71. // pre assing bindings before constructor calls
  72. $compileProvider.preAssignBindingsEnabled(true);
  73. if (config.buildInfo.env !== 'development') {
  74. $compileProvider.debugInfoEnabled(false);
  75. }
  76. $httpProvider.useApplyAsync(true);
  77. this.registerFunctions.controller = $controllerProvider.register;
  78. this.registerFunctions.directive = $compileProvider.directive;
  79. this.registerFunctions.factory = $provide.factory;
  80. this.registerFunctions.service = $provide.service;
  81. this.registerFunctions.filter = $filterProvider.register;
  82. $provide.decorator('$http', [
  83. '$delegate',
  84. '$templateCache',
  85. ($delegate: any, $templateCache: any) => {
  86. const get = $delegate.get;
  87. $delegate.get = (url: string, config: any) => {
  88. if (url.match(/\.html$/)) {
  89. // some template's already exist in the cache
  90. if (!$templateCache.get(url)) {
  91. url += '?v=' + new Date().getTime();
  92. }
  93. }
  94. return get(url, config);
  95. };
  96. return $delegate;
  97. },
  98. ]);
  99. }
  100. );
  101. this.ngModuleDependencies = [
  102. 'grafana.core',
  103. 'ngRoute',
  104. 'ngSanitize',
  105. '$strap.directives',
  106. 'ang-drag-drop',
  107. 'grafana',
  108. 'pasvaz.bindonce',
  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();