app.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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", ["$delegate", "$templateCache", function($delegate, $templateCache) {
  60. var get = $delegate.get;
  61. $delegate.get = function(url, config) {
  62. if (url.match(/\.html$/)) {
  63. // some template's already exist in the cache
  64. if (!$templateCache.get(url)) {
  65. url += "?v=" + new Date().getTime();
  66. }
  67. }
  68. return get(url, config);
  69. };
  70. return $delegate;
  71. }]);
  72. });
  73. this.ngModuleDependencies = [
  74. 'grafana.core',
  75. 'ngRoute',
  76. 'ngSanitize',
  77. '$strap.directives',
  78. 'ang-drag-drop',
  79. 'grafana',
  80. 'pasvaz.bindonce',
  81. 'ui.bootstrap',
  82. 'ui.bootstrap.tpls',
  83. 'react'
  84. ];
  85. var module_types = ['controllers', 'directives', 'factories', 'services', 'filters', 'routes'];
  86. _.each(module_types, type => {
  87. var moduleName = 'grafana.' + type;
  88. this.useModule(angular.module(moduleName, []));
  89. });
  90. // makes it possible to add dynamic stuff
  91. this.useModule(coreModule);
  92. // register react angular wrappers
  93. registerAngularDirectives();
  94. var preBootRequires = [System.import('app/features/all')];
  95. Promise.all(preBootRequires).then(() => {
  96. // disable tool tip animation
  97. $.fn.tooltip.defaults.animation = false;
  98. // bootstrap the app
  99. angular.bootstrap(document, this.ngModuleDependencies).invoke(() => {
  100. _.each(this.preBootModules, module => {
  101. _.extend(module, this.registerFunctions);
  102. });
  103. this.preBootModules = null;
  104. });
  105. }).catch(function(err) {
  106. console.log('Application boot failed:', err);
  107. });
  108. }
  109. }
  110. export default new GrafanaApp();