app.ts 3.9 KB

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