main.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // adapted from Mozilla Developer Network example at
  4. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
  5. // shim `bind` for testing under casper.js
  6. var bind = function bind(obj) {
  7. var slice = [].slice;
  8. var args = slice.call(arguments, 1),
  9. self = this,
  10. nop = function() {
  11. },
  12. bound = function() {
  13. return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
  14. };
  15. nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
  16. bound.prototype = new nop();
  17. return bound;
  18. };
  19. Function.prototype.bind = Function.prototype.bind || bind ;
  20. requirejs([
  21. 'jquery',
  22. 'contents',
  23. 'base/js/namespace',
  24. 'base/js/dialog',
  25. 'base/js/events',
  26. 'base/js/promises',
  27. 'base/js/page',
  28. 'base/js/utils',
  29. 'services/config',
  30. 'tree/js/notebooklist',
  31. 'tree/js/sessionlist',
  32. 'tree/js/kernellist',
  33. 'tree/js/terminallist',
  34. 'tree/js/newnotebook',
  35. 'tree/js/shutdownbutton',
  36. 'auth/js/loginwidget',
  37. 'bidi/bidi',
  38. ], function(
  39. $,
  40. contents_service,
  41. IPython,
  42. dialog,
  43. events,
  44. promises,
  45. page,
  46. utils,
  47. config,
  48. notebooklist,
  49. sesssionlist,
  50. kernellist,
  51. terminallist,
  52. newnotebook,
  53. shutdownbutton,
  54. loginwidget,
  55. bidi){
  56. "use strict";
  57. try{
  58. requirejs(['custom/custom'], function() {});
  59. bidi.loadLocale();
  60. } catch(err) {
  61. console.log("Error loading custom.js from tree service. Continuing and logging");
  62. console.warn(err);
  63. }
  64. console.log('Welcome to Project Jupyter! Explore the various tools available and their corresponding documentation. If you are interested in contributing to the platform, please visit the community resources section at http://jupyter.org/community.html.');
  65. // Setup all of the config related things
  66. var common_options = {
  67. base_url: utils.get_body_data("baseUrl"),
  68. notebook_path: utils.get_body_data("notebookPath"),
  69. };
  70. var cfg = new config.ConfigSection('tree', common_options);
  71. cfg.load();
  72. common_options.config = cfg;
  73. var common_config = new config.ConfigSection('common', common_options);
  74. common_config.load();
  75. // Instantiate the main objects
  76. page = new page.Page('div#header', 'div#site');
  77. var session_list = new sesssionlist.SesssionList($.extend({
  78. events: events},
  79. common_options));
  80. var contents = new contents_service.Contents({
  81. base_url: common_options.base_url,
  82. common_config: common_config
  83. });
  84. IPython.NotebookList = notebooklist.NotebookList;
  85. var notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({
  86. contents: contents,
  87. session_list: session_list},
  88. common_options));
  89. var kernel_list = new kernellist.KernelList('#running_list', $.extend({
  90. session_list: session_list},
  91. common_options));
  92. var terminal_list;
  93. if (utils.get_body_data("terminalsAvailable") === "True") {
  94. terminal_list = new terminallist.TerminalList('#terminal_list', common_options);
  95. }
  96. var login_widget = new loginwidget.LoginWidget('#login_widget', common_options);
  97. var new_buttons = new newnotebook.NewNotebookWidget("#new-buttons",
  98. $.extend(
  99. {contents: contents, events: events},
  100. common_options
  101. )
  102. );
  103. var interval_id=0;
  104. // auto refresh every xx secondes, no need to be fast,
  105. // update is done most of the time when page get focus
  106. IPython.tree_time_refresh = 60; // in sec
  107. // limit refresh on focus at 1/10sec, otherwise this
  108. // can cause too frequent refresh on switching through windows or tabs.
  109. IPython.min_delta_refresh = 10; // in sec
  110. var _last_refresh = null;
  111. var _refresh_list = function(){
  112. _last_refresh = new Date();
  113. session_list.load_sessions();
  114. if (terminal_list) {
  115. terminal_list.load_terminals();
  116. }
  117. };
  118. var enable_autorefresh = function(){
  119. /**
  120. *refresh immediately , then start interval
  121. */
  122. var now = new Date();
  123. if (now - _last_refresh < IPython.min_delta_refresh*1000){
  124. console.log("Reenabling autorefresh too close to last tree refresh, not refreshing immediately again.");
  125. } else {
  126. _refresh_list();
  127. }
  128. if (!interval_id){
  129. interval_id = setInterval(_refresh_list,
  130. IPython.tree_time_refresh*1000
  131. );
  132. }
  133. };
  134. var disable_autorefresh = function(){
  135. clearInterval(interval_id);
  136. interval_id = 0;
  137. };
  138. // stop autorefresh when page lose focus
  139. $(window).blur(function() {
  140. disable_autorefresh();
  141. });
  142. //re-enable when page get focus back
  143. $(window).focus(function() {
  144. enable_autorefresh();
  145. });
  146. // finally start it, it will refresh immediately
  147. enable_autorefresh();
  148. page.show();
  149. // For backwards compatability.
  150. IPython.page = page;
  151. IPython.notebook_list = notebook_list;
  152. IPython.session_list = session_list;
  153. IPython.kernel_list = kernel_list;
  154. IPython.login_widget = login_widget;
  155. IPython.new_notebook_widget = new_buttons;
  156. events.trigger('app_initialized.DashboardApp');
  157. // Now actually load nbextensions
  158. utils.load_extensions_from_config(cfg);
  159. utils.load_extensions_from_config(common_config);
  160. // bound the upload method to the on change of the file select list
  161. $("#alternate_upload").change(function (event){
  162. notebook_list.handleFilesUpload(event,'form');
  163. });
  164. // set hash on tab click
  165. $("#tabs").find("a").click(function(e) {
  166. // Prevent the document from jumping when the active tab is changed to a
  167. // tab that has a lot of content.
  168. e.preventDefault();
  169. // Set the hash without causing the page to jump.
  170. // https://stackoverflow.com/a/14690177/2824256
  171. var hash = $(this).attr("href");
  172. if(window.history.pushState) {
  173. window.history.pushState(null, null, hash);
  174. } else {
  175. window.location.hash = hash;
  176. }
  177. });
  178. // load tab if url hash
  179. if (window.location.hash) {
  180. $("#tabs").find("a[href='" + window.location.hash + "']").click();
  181. }
  182. shutdownbutton.activate();
  183. });