app.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(
  48. (
  49. $locationProvider,
  50. $controllerProvider,
  51. $compileProvider,
  52. $filterProvider,
  53. $httpProvider,
  54. $provide
  55. ) => {
  56. // pre assing bindings before constructor calls
  57. $compileProvider.preAssignBindingsEnabled(true);
  58. if (config.buildInfo.env !== "development") {
  59. $compileProvider.debugInfoEnabled(false);
  60. }
  61. $httpProvider.useApplyAsync(true);
  62. this.registerFunctions.controller = $controllerProvider.register;
  63. this.registerFunctions.directive = $compileProvider.directive;
  64. this.registerFunctions.factory = $provide.factory;
  65. this.registerFunctions.service = $provide.service;
  66. this.registerFunctions.filter = $filterProvider.register;
  67. $provide.decorator("$http", [
  68. "$delegate",
  69. "$templateCache",
  70. function($delegate, $templateCache) {
  71. var get = $delegate.get;
  72. $delegate.get = function(url, config) {
  73. if (url.match(/\.html$/)) {
  74. // some template's already exist in the cache
  75. if (!$templateCache.get(url)) {
  76. url += "?v=" + new Date().getTime();
  77. }
  78. }
  79. return get(url, config);
  80. };
  81. return $delegate;
  82. }
  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. var module_types = [
  99. "controllers",
  100. "directives",
  101. "factories",
  102. "services",
  103. "filters",
  104. "routes"
  105. ];
  106. _.each(module_types, type => {
  107. var moduleName = "grafana." + type;
  108. this.useModule(angular.module(moduleName, []));
  109. });
  110. // makes it possible to add dynamic stuff
  111. this.useModule(coreModule);
  112. // register react angular wrappers
  113. registerAngularDirectives();
  114. var preBootRequires = [System.import("app/features/all")];
  115. Promise.all(preBootRequires)
  116. .then(() => {
  117. // disable tool tip animation
  118. $.fn.tooltip.defaults.animation = false;
  119. // bootstrap the app
  120. angular.bootstrap(document, this.ngModuleDependencies).invoke(() => {
  121. _.each(this.preBootModules, module => {
  122. _.extend(module, this.registerFunctions);
  123. });
  124. this.preBootModules = null;
  125. });
  126. })
  127. .catch(function(err) {
  128. console.log("Application boot failed:", err);
  129. });
  130. }
  131. }
  132. export default new GrafanaApp();