app.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * main app level module
  3. */
  4. define([
  5. 'angular',
  6. 'jquery',
  7. 'underscore',
  8. 'require',
  9. 'elasticjs',
  10. 'bootstrap',
  11. 'angular-sanitize',
  12. 'angular-strap',
  13. 'angular-dragdrop',
  14. 'extend-jquery'
  15. ],
  16. function (angular, $, _, appLevelRequire) {
  17. "use strict";
  18. var app = angular.module('kibana', []),
  19. // we will keep a reference to each module defined before boot, so that we can
  20. // go back and allow it to define new features later. Once we boot, this will be false
  21. pre_boot_modules = [],
  22. // these are the functions that we need to call to register different
  23. // features if we define them after boot time
  24. register_fns = {};
  25. /**
  26. * Tells the application to watch the module, once bootstraping has completed
  27. * the modules controller, service, etc. functions will be overwritten to register directly
  28. * with this application.
  29. * @param {[type]} module [description]
  30. * @return {[type]} [description]
  31. */
  32. app.useModule = function (module) {
  33. if (pre_boot_modules) {
  34. pre_boot_modules.push(module);
  35. } else {
  36. _.extend(module, register_fns);
  37. }
  38. return module;
  39. };
  40. app.safeApply = function ($scope, fn) {
  41. switch($scope.$$phase) {
  42. case '$apply':
  43. // $digest hasn't started, we should be good
  44. $scope.$eval(fn);
  45. break;
  46. case '$digest':
  47. // waiting to $apply the changes
  48. setTimeout(function () { app.safeApply($scope, fn); }, 10);
  49. break;
  50. default:
  51. // clear to begin an $apply $$phase
  52. $scope.$apply(fn);
  53. break;
  54. }
  55. };
  56. app.config(function ($routeProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {
  57. $routeProvider
  58. .when('/dashboard', {
  59. templateUrl: 'app/partials/dashboard.html',
  60. })
  61. .when('/dashboard/:kbnType/:kbnId', {
  62. templateUrl: 'app/partials/dashboard.html',
  63. })
  64. .when('/dashboard/:kbnType/:kbnId/:params', {
  65. templateUrl: 'app/partials/dashboard.html'
  66. })
  67. .otherwise({
  68. redirectTo: 'dashboard'
  69. });
  70. // this is how the internet told me to dynamically add modules :/
  71. register_fns.controller = $controllerProvider.register;
  72. register_fns.directive = $compileProvider.directive;
  73. register_fns.factory = $provide.factory;
  74. register_fns.service = $provide.service;
  75. register_fns.filter = $filterProvider.register;
  76. });
  77. var apps_deps = [
  78. 'elasticjs.service',
  79. '$strap.directives',
  80. 'ngSanitize',
  81. 'ngDragDrop',
  82. 'kibana'
  83. ];
  84. _.each('controllers directives factories services filters'.split(' '),
  85. function (type) {
  86. var module_name = 'kibana.'+type;
  87. // create the module
  88. app.useModule(angular.module(module_name, []));
  89. // push it into the apps dependencies
  90. apps_deps.push(module_name);
  91. });
  92. app.panel_helpers = {
  93. partial: function (name) {
  94. return 'app/partials/'+name+'.html';
  95. }
  96. };
  97. // load the core components
  98. require([
  99. 'controllers/all',
  100. 'directives/all',
  101. 'filters/all'
  102. ], function () {
  103. // bootstrap the app
  104. angular
  105. .element(document)
  106. .ready(function() {
  107. $('body').attr('ng-controller', 'DashCtrl');
  108. angular.bootstrap(document, apps_deps)
  109. .invoke(['$rootScope', function ($rootScope) {
  110. _.each(pre_boot_modules, function (module) {
  111. _.extend(module, register_fns);
  112. });
  113. pre_boot_modules = false;
  114. $rootScope.requireContext = appLevelRequire;
  115. $rootScope.require = function (deps, fn) {
  116. var $scope = this;
  117. $scope.requireContext(deps, function () {
  118. var deps = _.toArray(arguments);
  119. $scope.$apply(function () {
  120. fn.apply($scope, deps);
  121. });
  122. });
  123. };
  124. }]);
  125. });
  126. });
  127. return app;
  128. });