app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * main app level module
  3. */
  4. define([
  5. 'angular',
  6. 'jquery',
  7. 'underscore',
  8. 'require',
  9. 'config',
  10. 'elasticjs',
  11. 'bootstrap',
  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('kibana', []),
  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.safeApply = function ($scope, fn) {
  47. switch($scope.$$phase) {
  48. case '$apply':
  49. // $digest hasn't started, we should be good
  50. $scope.$eval(fn);
  51. break;
  52. case '$digest':
  53. // waiting to $apply the changes
  54. setTimeout(function () { app.safeApply($scope, fn); }, 10);
  55. break;
  56. default:
  57. // clear to begin an $apply $$phase
  58. $scope.$apply(fn);
  59. break;
  60. }
  61. };
  62. app.config(function ($routeProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {
  63. $routeProvider.otherwise({ redirectTo: config.default_route });
  64. // this is how the internet told me to dynamically add modules :/
  65. register_fns.controller = $controllerProvider.register;
  66. register_fns.directive = $compileProvider.directive;
  67. register_fns.factory = $provide.factory;
  68. register_fns.service = $provide.service;
  69. register_fns.filter = $filterProvider.register;
  70. });
  71. var apps_deps = [
  72. 'elasticjs.service',
  73. '$strap.directives',
  74. 'ngSanitize',
  75. 'ngDragDrop',
  76. 'kibana',
  77. 'pasvaz.bindonce'
  78. ];
  79. var module_types = ['controllers', 'directives', 'factories', 'services', 'filters', 'routes'];
  80. _.each(module_types, function (type) {
  81. var module_name = 'kibana.'+type;
  82. // create the module
  83. app.useModule(angular.module(module_name, []));
  84. // push it into the apps dependencies
  85. apps_deps.push(module_name);
  86. });
  87. app.panel_helpers = {
  88. partial: function (name) {
  89. return 'app/partials/'+name+'.html';
  90. }
  91. };
  92. // load the core components
  93. require([
  94. 'controllers/all',
  95. 'directives/all',
  96. 'filters/all',
  97. 'components/partials',
  98. 'routes/dashboard-loader',
  99. ], function () {
  100. // bootstrap the app
  101. angular
  102. .element(document)
  103. .ready(function() {
  104. $('body').attr('ng-controller', 'DashCtrl');
  105. angular.bootstrap(document, apps_deps)
  106. .invoke(['$rootScope', function ($rootScope) {
  107. _.each(pre_boot_modules, function (module) {
  108. _.extend(module, register_fns);
  109. });
  110. pre_boot_modules = false;
  111. $rootScope.requireContext = appLevelRequire;
  112. $rootScope.require = function (deps, fn) {
  113. var $scope = this;
  114. $scope.requireContext(deps, function () {
  115. var deps = _.toArray(arguments);
  116. // Check that this is a valid scope.
  117. if($scope.$id) {
  118. $scope.$apply(function () {
  119. fn.apply($scope, deps);
  120. });
  121. }
  122. });
  123. };
  124. }]);
  125. });
  126. });
  127. return app;
  128. });