app.ts 3.7 KB

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