app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * main app level module
  3. */
  4. define([
  5. 'angular',
  6. 'jquery',
  7. 'lodash',
  8. 'require',
  9. 'config',
  10. 'bootstrap',
  11. 'angular-route',
  12. 'angular-sanitize',
  13. 'angular-strap',
  14. 'angular-dragdrop',
  15. 'extend-jquery',
  16. 'bindonce',
  17. ],
  18. function (angular, $, _, appLevelRequire, config) {
  19. "use strict";
  20. var app = angular.module('grafana', []),
  21. // we will keep a reference to each module defined before boot, so that we can
  22. // go back and allow it to define new features later. Once we boot, this will be false
  23. pre_boot_modules = [],
  24. // these are the functions that we need to call to register different
  25. // features if we define them after boot time
  26. register_fns = {};
  27. // This stores the grafana version number
  28. app.constant('grafanaVersion',"@grafanaVersion@");
  29. // Use this for cache busting partials
  30. app.constant('cacheBust',"cache-bust="+Date.now());
  31. /**
  32. * Tells the application to watch the module, once bootstraping has completed
  33. * the modules controller, service, etc. functions will be overwritten to register directly
  34. * with this application.
  35. * @param {[type]} module [description]
  36. * @return {[type]} [description]
  37. */
  38. app.useModule = function (module) {
  39. if (pre_boot_modules) {
  40. pre_boot_modules.push(module);
  41. } else {
  42. _.extend(module, register_fns);
  43. }
  44. return module;
  45. };
  46. app.config(function($locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {
  47. // this is how the internet told me to dynamically add modules :/
  48. register_fns.controller = $controllerProvider.register;
  49. register_fns.directive = $compileProvider.directive;
  50. register_fns.factory = $provide.factory;
  51. register_fns.service = $provide.service;
  52. register_fns.filter = $filterProvider.register;
  53. });
  54. var apps_deps = [
  55. 'ngRoute',
  56. 'ngSanitize',
  57. '$strap.directives',
  58. 'ang-drag-drop',
  59. 'grafana',
  60. 'pasvaz.bindonce'
  61. ];
  62. var module_types = ['controllers', 'directives', 'factories', 'services', 'filters'];
  63. if (window.grafanaBackend) {
  64. module_types.push('routes');
  65. angular.module('grafana.routes.standalone', []);
  66. }
  67. else {
  68. module_types.push('routes.standalone');
  69. angular.module('grafana.routes', []);
  70. }
  71. _.each(module_types, function (type) {
  72. var module_name = 'grafana.'+type;
  73. // create the module
  74. app.useModule(angular.module(module_name, []));
  75. // push it into the apps dependencies
  76. apps_deps.push(module_name);
  77. });
  78. var preBootRequires = [
  79. 'services/all',
  80. 'features/all',
  81. 'controllers/all',
  82. 'directives/all',
  83. 'filters/all',
  84. 'components/partials',
  85. 'routes/standalone/all',
  86. 'routes/backend/all',
  87. ];
  88. _.each(config.plugins.dependencies, function(dep) {
  89. preBootRequires.push('../plugins/' + dep);
  90. });
  91. app.boot = function() {
  92. require(preBootRequires, function () {
  93. // disable tool tip animation
  94. $.fn.tooltip.defaults.animation = false;
  95. // bootstrap the app
  96. angular
  97. .element(document)
  98. .ready(function() {
  99. angular.bootstrap(document, apps_deps)
  100. .invoke(['$rootScope', function ($rootScope) {
  101. _.each(pre_boot_modules, function (module) {
  102. _.extend(module, register_fns);
  103. });
  104. pre_boot_modules = false;
  105. $rootScope.requireContext = appLevelRequire;
  106. $rootScope.require = function (deps, fn) {
  107. var $scope = this;
  108. $scope.requireContext(deps, function () {
  109. var deps = _.toArray(arguments);
  110. // Check that this is a valid scope.
  111. if($scope.$id) {
  112. $scope.$apply(function () {
  113. fn.apply($scope, deps);
  114. });
  115. }
  116. });
  117. };
  118. }]);
  119. });
  120. });
  121. };
  122. return app;
  123. });