app.ts 4.3 KB

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