From 23f95334731745527e0281b0d237acf3562e3b54 2014-07-10 20:25:08 From: Jonathan Frederic Date: 2014-07-10 20:25:08 Subject: [PATCH] Start of work to make notebook.html requirejs friendly. --- diff --git a/IPython/html/static/auth/js/loginmain.js b/IPython/html/static/auth/js/loginmain.js index d914bf7..b95d70a 100644 --- a/IPython/html/static/auth/js/loginmain.js +++ b/IPython/html/static/auth/js/loginmain.js @@ -1,21 +1,9 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2008-2011 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. -//============================================================================ -// On document ready -//============================================================================ - - -$(document).ready(function () { - - IPython.page = new IPython.Page(); +require(['base/js/namespace', 'base/js/page'], function(IPython, Page) { + IPython.page = new Page(); $('button#login_submit').addClass("btn btn-default"); IPython.page.show(); $('input#password_input').focus(); - }); - diff --git a/IPython/html/static/auth/js/logoutmain.js b/IPython/html/static/auth/js/logoutmain.js index df107c6..aa68082 100644 --- a/IPython/html/static/auth/js/logoutmain.js +++ b/IPython/html/static/auth/js/logoutmain.js @@ -1,20 +1,8 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2008-2011 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. -//============================================================================ -// On document ready -//============================================================================ - - -$(document).ready(function () { - - IPython.page = new IPython.Page(); +require(['base/js/namespace', 'base/js/page'], function(IPython, Page) { + IPython.page = new Page(); $('#ipython-main-app').addClass('border-box-sizing'); IPython.page.show(); - }); - diff --git a/IPython/html/static/notebook/js/cell.js b/IPython/html/static/notebook/js/cell.js index 5a481c5..202d72b 100644 --- a/IPython/html/static/notebook/js/cell.js +++ b/IPython/html/static/notebook/js/cell.js @@ -1,3 +1,22 @@ +"components/codemirror/lib/codemirror.js", +// Set codemirror version. +// CodeMirror.modeURL = "{{ static_url("components/codemirror/mode/%N/%N.js", include_version=False) }}"; +"components/codemirror/addon/mode/loadmode.js", +"components/codemirror/addon/mode/multiplex.js", +"components/codemirror/addon/mode/overlay.js", +"components/codemirror/addon/edit/matchbrackets.js", +"components/codemirror/addon/edit/closebrackets.js", +"components/codemirror/addon/comment/comment.js", +"components/codemirror/mode/htmlmixed/htmlmixed.js", +"components/codemirror/mode/xml/xml.js", +"components/codemirror/mode/javascript/javascript.js", +"components/codemirror/mode/css/css.js", +"components/codemirror/mode/rst/rst.js", +"components/codemirror/mode/markdown/markdown.js", +"components/codemirror/mode/python/python.js", +"notebook/js/codemirror-ipython.js", +"notebook/js/codemirror-ipythongfm.js", + //---------------------------------------------------------------------------- // Copyright (C) 2008-2011 The IPython Development Team // diff --git a/IPython/html/static/notebook/js/layoutmanager.js b/IPython/html/static/notebook/js/layoutmanager.js index 4706be4..5775540 100644 --- a/IPython/html/static/notebook/js/layoutmanager.js +++ b/IPython/html/static/notebook/js/layoutmanager.js @@ -1,19 +1,15 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2011 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- - -//============================================================================ -// Layout -//============================================================================ - -var IPython = (function (IPython) { +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. + +define([ + 'base/js/namespace', + 'components/jquery/jquery.min', +], function(IPython, $) { "use strict"; - var LayoutManager = function () { + var LayoutManager = function (pager) { this.bind_events(); + this.pager = pager; }; LayoutManager.prototype.bind_events = function () { @@ -44,18 +40,18 @@ var IPython = (function (IPython) { $('#ipython-main-app').height(app_height); // content+padding+border height - var pager_height = IPython.pager.percentage_height*app_height; + var pager_height = this.pager.percentage_height*app_height; var pager_splitter_height = $('div#pager_splitter').outerHeight(true); $('div#pager').outerHeight(pager_height); - if (IPython.pager.expanded) { + if (this.pager.expanded) { $('div#notebook').outerHeight(app_height-pager_height-pager_splitter_height); } else { $('div#notebook').outerHeight(app_height-pager_splitter_height); } }; + // Backwards compatability. IPython.LayoutManager = LayoutManager; - return IPython; - -}(IPython)); + return LayoutManager; +}); diff --git a/IPython/html/static/notebook/js/main.js b/IPython/html/static/notebook/js/main.js index d84c603..7332a1c 100644 --- a/IPython/html/static/notebook/js/main.js +++ b/IPython/html/static/notebook/js/main.js @@ -1,78 +1,57 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2011 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. -//============================================================================ -// On document ready -//============================================================================ - -// for the time beeing, we have to pass marked as a parameter here, -// as injecting require.js make marked not to put itself in the globals, -// which make both this file fail at setting marked configuration, and textcell.js -// which search marked into global. -require(['components/marked/lib/marked', - 'widgets/js/init', - 'components/bootstrap-tour/build/js/bootstrap-tour.min'], - -function (marked) { +require([ + 'base/js/namespace', + 'notebook/js/notebook', + 'base/js/utils', + 'base/js/page', + 'notebook/js/layoutmanager', + 'base/js/events', + 'auth/js/loginwidget', + 'notebook/js/maintoolbar', + 'notebook/js/pager', + 'notebook/js/quickhelp', + 'notebook/js/menubar', + 'notebook/js/notificationarea', +], function( + IPython, + Notebook, + Utils, + Page, + LayoutManager, + Events, + LoginWidget, + MainToolBar, + Pager, + QuickHelp, + MenuBar, + NotificationArea + ) { "use strict"; - window.marked = marked; - - // monkey patch CM to be able to syntax highlight cell magics - // bug reported upstream, - // see https://github.com/marijnh/CodeMirror2/issues/670 - if(CodeMirror.getMode(1,'text/plain').indent === undefined ){ - console.log('patching CM for undefined indent'); - CodeMirror.modes.null = function() { - return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0;}}; - }; - } - - CodeMirror.patchedGetMode = function(config, mode){ - var cmmode = CodeMirror.getMode(config, mode); - if(cmmode.indent === null) { - console.log('patch mode "' , mode, '" on the fly'); - cmmode.indent = function(){return 0;}; - } - return cmmode; - }; - // end monkey patching CodeMirror - - IPython.mathjaxutils.init(); - $('#ipython-main-app').addClass('border-box-sizing'); $('div#notebook_panel').addClass('border-box-sizing'); var opts = { - base_url : IPython.utils.get_body_data("baseUrl"), - notebook_path : IPython.utils.get_body_data("notebookPath"), - notebook_name : IPython.utils.get_body_data('notebookName') + base_url : Utils.get_body_data("baseUrl"), + notebook_path : Utils.get_body_data("notebookPath"), + notebook_name : Utils.get_body_data('notebookName') }; - IPython.page = new IPython.Page(); - IPython.layout_manager = new IPython.LayoutManager(); - IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter'); - IPython.quick_help = new IPython.QuickHelp(); - try { - IPython.tour = new IPython.NotebookTour(); - } catch (e) { - console.log("Failed to instantiate Notebook Tour", e); - } - IPython.login_widget = new IPython.LoginWidget('span#login_widget', opts); - IPython.notebook = new IPython.Notebook('div#notebook', opts); - IPython.keyboard_manager = new IPython.KeyboardManager(); - IPython.save_widget = new IPython.SaveWidget('span#save_widget'); - IPython.menubar = new IPython.MenuBar('#menubar', opts); - IPython.toolbar = new IPython.MainToolBar('#maintoolbar-container'); - IPython.tooltip = new IPython.Tooltip(); - IPython.notification_area = new IPython.NotificationArea('#notification_area'); - IPython.notification_area.init_notification_widgets(); + page = new Page(); + pager = new Pager('div#pager', 'div#pager_splitter'); + layout_manager = new LayoutManager(pager); + notebook = new Notebook('div#notebook', opts); + login_widget = new LoginWidget('span#login_widget', opts); + toolbar = new MainToolBar('#maintoolbar-container'); + quick_help = new QuickHelp(); + menubar = new MenuBar('#menubar', opts); - IPython.layout_manager.do_resize(); + notification_area = new NotificationArea('#notification_area'); + notification_area.init_notification_widgets(); + + layout_manager.do_resize(); $('body').append('
x'+
                      'x'+
@@ -85,43 +64,34 @@ function (marked) {
     }
     $('#fonttest').remove();
 
-    IPython.page.show();
+    page.show();
 
-    IPython.layout_manager.do_resize();
+    layout_manager.do_resize();
     var first_load = function () {
-        IPython.layout_manager.do_resize();
+        layout_manager.do_resize();
         var hash = document.location.hash;
         if (hash) {
             document.location.hash = '';
             document.location.hash = hash;
         }
-        IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval);
+        notebook.set_autosave_interval(notebook.minimum_autosave_interval);
         // only do this once
-        $([IPython.events]).off('notebook_loaded.Notebook', first_load);
+        $([Events]).off('notebook_loaded.Notebook', first_load);
     };
     
-    $([IPython.events]).on('notebook_loaded.Notebook', first_load);
-    $([IPython.events]).trigger('app_initialized.NotebookApp');
-    IPython.notebook.load_notebook(opts.notebook_name, opts.notebook_path);
+    $([Events]).on('notebook_loaded.Notebook', first_load);
+    $([Events]).trigger('app_initialized.NotebookApp');
+    notebook.load_notebook(opts.notebook_name, opts.notebook_path);
 
-    if (marked) {
-        marked.setOptions({
-            gfm : true,
-            tables: true,
-            langPrefix: "language-",
-            highlight: function(code, lang) {
-                if (!lang) {
-                    // no language, no highlight
-                    return code;
-                }
-                var highlighted;
-                try {
-                    highlighted = hljs.highlight(lang, code, false);
-                } catch(err) {
-                    highlighted = hljs.highlightAuto(code);
-                }
-                return highlighted.value;
-            }
-        });
-    }
+    // Backwards compatability.
+    IPython.page = page;
+    IPython.layout_manager = layout_manager;
+    IPython.notebook = notebook;
+    IPython.pager = pager;
+    IPython.quick_help = quick_help;
+    IPython.login_widget = login_widget;
+    IPython.menubar = menubar;
+    IPython.toolbar = toolbar;
+    IPython.notification_area = notification_area;
+    IPython.notification_area = notification_area;
 });
diff --git a/IPython/html/static/notebook/js/maintoolbar.js b/IPython/html/static/notebook/js/maintoolbar.js
index 01af262..95497e6 100644
--- a/IPython/html/static/notebook/js/maintoolbar.js
+++ b/IPython/html/static/notebook/js/maintoolbar.js
@@ -1,26 +1,23 @@
-//----------------------------------------------------------------------------
-//  Copyright (C) 2011 The IPython Development Team
-//
-//  Distributed under the terms of the BSD License.  The full license is in
-//  the file COPYING, distributed as part of this software.
-//----------------------------------------------------------------------------
-
-//============================================================================
-// ToolBar
-//============================================================================
-
-var IPython = (function (IPython) {
+// Copyright (c) IPython Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+define([
+    'base/js/namespace',
+    'components/jquery/jquery.min',
+    'notebook/js/toolbar',
+], function(IPython, $, Toolbar) {
     "use strict";
 
-    var MainToolBar = function (selector) {
-        IPython.ToolBar.apply(this, arguments);
+    var MainToolBar = function (selector, notebook) {
+        ToolBar.apply(this, arguments);
+        this.notebook = notebook;
         this.construct();
         this.add_celltype_list();
         this.add_celltoolbar_list();
         this.bind_events();
     };
 
-    MainToolBar.prototype = new IPython.ToolBar();
+    MainToolBar.prototype = new ToolBar();
 
     MainToolBar.prototype.construct = function () {
         this.add_buttons_group([
@@ -29,7 +26,7 @@ var IPython = (function (IPython) {
                     label : 'Save and Checkpoint',
                     icon : 'icon-save',
                     callback : function () {
-                        IPython.notebook.save_checkpoint();
+                        this.notebook.save_checkpoint();
                         }
                 }
             ]);
@@ -40,9 +37,9 @@ var IPython = (function (IPython) {
                     label : 'Insert Cell Below',
                     icon : 'icon-plus-sign',
                     callback : function () {
-                        IPython.notebook.insert_cell_below('code');
-                        IPython.notebook.select_next();
-                        IPython.notebook.focus_cell();
+                        this.notebook.insert_cell_below('code');
+                        this.notebook.select_next();
+                        this.notebook.focus_cell();
                         }
                 }
             ],'insert_above_below');
@@ -53,7 +50,7 @@ var IPython = (function (IPython) {
                     label : 'Cut Cell',
                     icon : 'icon-cut',
                     callback : function () {
-                        IPython.notebook.cut_cell();
+                        this.notebook.cut_cell();
                         }
                 },
                 {
@@ -61,7 +58,7 @@ var IPython = (function (IPython) {
                     label : 'Copy Cell',
                     icon : 'icon-copy',
                     callback : function () {
-                        IPython.notebook.copy_cell();
+                        this.notebook.copy_cell();
                         }
                 },
                 {
@@ -69,7 +66,7 @@ var IPython = (function (IPython) {
                     label : 'Paste Cell Below',
                     icon : 'icon-paste',
                     callback : function () {
-                        IPython.notebook.paste_cell_below();
+                        this.notebook.paste_cell_below();
                         }
                 }
             ],'cut_copy_paste');
@@ -80,7 +77,7 @@ var IPython = (function (IPython) {
                     label : 'Move Cell Up',
                     icon : 'icon-arrow-up',
                     callback : function () {
-                        IPython.notebook.move_cell_up();
+                        this.notebook.move_cell_up();
                         }
                 },
                 {
@@ -88,7 +85,7 @@ var IPython = (function (IPython) {
                     label : 'Move Cell Down',
                     icon : 'icon-arrow-down',
                     callback : function () {
-                        IPython.notebook.move_cell_down();
+                        this.notebook.move_cell_down();
                         }
                 }
             ],'move_up_down');
@@ -101,7 +98,7 @@ var IPython = (function (IPython) {
                     icon : 'icon-play',
                     callback : function () {
                         // emulate default shift-enter behavior
-                        IPython.notebook.execute_cell_and_select_below();
+                        this.notebook.execute_cell_and_select_below();
                     }
                 },
                 {
@@ -109,7 +106,7 @@ var IPython = (function (IPython) {
                     label : 'Interrupt',
                     icon : 'icon-stop',
                     callback : function () {
-                        IPython.notebook.session.interrupt_kernel();
+                        this.notebook.session.interrupt_kernel();
                         }
                 },
                 {
@@ -117,7 +114,7 @@ var IPython = (function (IPython) {
                     label : 'Restart Kernel',
                     icon : 'icon-repeat',
                     callback : function () {
-                        IPython.notebook.restart_kernel();
+                        this.notebook.restart_kernel();
                         }
                 }
             ],'run_int');
@@ -151,14 +148,14 @@ var IPython = (function (IPython) {
             .append($('