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