app.js 4.2 KB

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