app.js 3.6 KB

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