app.js 3.6 KB

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