app.ts 3.8 KB

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