Show More
@@ -1,136 +1,142 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | requirejs.config({ | |
|
5 | shim: { | |
|
6 | 'components/highlight.js/build/highlight.pack': {exports: 'hljs'} | |
|
7 | } | |
|
8 | }); | |
|
9 | ||
|
4 | 10 | require([ |
|
5 | 11 | 'base/js/namespace', |
|
6 | 12 | 'jquery', |
|
7 | 13 | 'notebook/js/notebook', |
|
8 | 14 | 'base/js/utils', |
|
9 | 15 | 'base/js/page', |
|
10 | 16 | 'notebook/js/layoutmanager', |
|
11 | 17 | 'base/js/events', |
|
12 | 18 | 'auth/js/loginwidget', |
|
13 | 19 | 'notebook/js/maintoolbar', |
|
14 | 20 | 'notebook/js/pager', |
|
15 | 21 | 'notebook/js/quickhelp', |
|
16 | 22 | 'notebook/js/menubar', |
|
17 | 23 | 'notebook/js/notificationarea', |
|
18 | 24 | 'notebook/js/savewidget', |
|
19 | 25 | 'notebook/js/keyboardmanager', |
|
20 | 26 | 'notebook/js/config', |
|
21 | 27 | ], function( |
|
22 | 28 | IPython, |
|
23 | 29 | $, |
|
24 | 30 | notebook, |
|
25 | 31 | utils, |
|
26 | 32 | page, |
|
27 | 33 | layoutmanager, |
|
28 | 34 | events, |
|
29 | 35 | loginwidget, |
|
30 | 36 | maintoolbar, |
|
31 | 37 | pager, |
|
32 | 38 | quickhelp, |
|
33 | 39 | menubar, |
|
34 | 40 | notificationarea, |
|
35 | 41 | savewidget, |
|
36 | 42 | keyboardmanager, |
|
37 | 43 | config |
|
38 | 44 | ) { |
|
39 | 45 | "use strict"; |
|
40 | 46 | |
|
41 | 47 | $('#ipython-main-app').addClass('border-box-sizing'); |
|
42 | 48 | $('div#notebook_panel').addClass('border-box-sizing'); |
|
43 | 49 | |
|
44 | 50 | var common_options = { |
|
45 | 51 | base_url : utils.get_body_data("baseUrl"), |
|
46 | 52 | notebook_path : utils.get_body_data("notebookPath"), |
|
47 | 53 | notebook_name : utils.get_body_data('notebookName') |
|
48 | 54 | }; |
|
49 | 55 | |
|
50 | 56 | var user_config = $.extend({}, config.default_config); |
|
51 | 57 | var page = new page.Page(); |
|
52 | 58 | var layout_manager = new layoutmanager.LayoutManager(); |
|
53 | 59 | var events = $([new events.Events()]); |
|
54 | 60 | var pager = new pager.Pager('div#pager', 'div#pager_splitter', { |
|
55 | 61 | layout_manager: layout_manager, |
|
56 | 62 | events: events}); |
|
57 | 63 | var keyboard_manager = new keyboardmanager.KeyboardManager({ |
|
58 | 64 | pager: pager, |
|
59 | 65 | events: events}); |
|
60 | 66 | var save_widget = new savewidget.SaveWidget('span#save_widget', { |
|
61 | 67 | events: events, |
|
62 | 68 | keyboard_manager: keyboard_manager}); |
|
63 | 69 | var notebook = new notebook.Notebook('div#notebook', $.extend({ |
|
64 | 70 | events: events, |
|
65 | 71 | keyboard_manager: keyboard_manager, |
|
66 | 72 | save_widget: save_widget, |
|
67 | 73 | config: user_config}, |
|
68 | 74 | common_options)); |
|
69 | 75 | var login_widget = new loginwidget.LoginWidget('span#login_widget', common_options); |
|
70 | 76 | var toolbar = new maintoolbar.MainToolBar('#maintoolbar-container', { |
|
71 | 77 | notebook: notebook, |
|
72 | 78 | events: events}); |
|
73 | 79 | var quick_help = new quickhelp.QuickHelp({ |
|
74 | 80 | keyboard_manager: keyboard_manager, |
|
75 | 81 | events: events, |
|
76 | 82 | notebook: notebook}); |
|
77 | 83 | var menubar = new menubar.MenuBar('#menubar', $.extend({ |
|
78 | 84 | notebook: notebook, |
|
79 | 85 | layout_manager: layout_manager, |
|
80 | 86 | events: events, |
|
81 | 87 | save_widget: save_widget, |
|
82 | 88 | quick_help: quick_help}, |
|
83 | 89 | common_options)); |
|
84 | 90 | var notification_area = new notificationarea.NotificationArea( |
|
85 | 91 | '#notification_area', { |
|
86 | 92 | events: events, |
|
87 | 93 | save_widget: save_widget, |
|
88 | 94 | notebook: notebook, |
|
89 | 95 | keyboard_manager: keyboard_manager}); |
|
90 | 96 | notification_area.init_notification_widgets(); |
|
91 | 97 | |
|
92 | 98 | $('body').append('<div id="fonttest"><pre><span id="test1">x</span>'+ |
|
93 | 99 | '<span id="test2" style="font-weight: bold;">x</span>'+ |
|
94 | 100 | '<span id="test3" style="font-style: italic;">x</span></pre></div>'); |
|
95 | 101 | var nh = $('#test1').innerHeight(); |
|
96 | 102 | var bh = $('#test2').innerHeight(); |
|
97 | 103 | var ih = $('#test3').innerHeight(); |
|
98 | 104 | if(nh != bh || nh != ih) { |
|
99 | 105 | $('head').append('<style>.CodeMirror span { vertical-align: bottom; }</style>'); |
|
100 | 106 | } |
|
101 | 107 | $('#fonttest').remove(); |
|
102 | 108 | |
|
103 | 109 | page.show(); |
|
104 | 110 | |
|
105 | 111 | layout_manager.do_resize(); |
|
106 | 112 | var first_load = function () { |
|
107 | 113 | layout_manager.do_resize(); |
|
108 | 114 | var hash = document.location.hash; |
|
109 | 115 | if (hash) { |
|
110 | 116 | document.location.hash = ''; |
|
111 | 117 | document.location.hash = hash; |
|
112 | 118 | } |
|
113 | 119 | notebook.set_autosave_interval(notebook.minimum_autosave_interval); |
|
114 | 120 | // only do this once |
|
115 | 121 | events.off('notebook_loaded.Notebook', first_load); |
|
116 | 122 | }; |
|
117 | 123 | |
|
118 | 124 | events.on('notebook_loaded.Notebook', first_load); |
|
119 | 125 | events.trigger('app_initialized.NotebookApp'); |
|
120 | 126 | notebook.load_notebook(common_options.notebook_name, common_options.notebook_path); |
|
121 | 127 | |
|
122 | 128 | IPython.page = page; |
|
123 | 129 | IPython.layout_manager = layout_manager; |
|
124 | 130 | IPython.notebook = notebook; |
|
125 | 131 | IPython.pager = pager; |
|
126 | 132 | IPython.quick_help = quick_help; |
|
127 | 133 | IPython.login_widget = login_widget; |
|
128 | 134 | IPython.menubar = menubar; |
|
129 | 135 | IPython.toolbar = toolbar; |
|
130 | 136 | IPython.notification_area = notification_area; |
|
131 | 137 | IPython.events = events; |
|
132 | 138 | IPython.keyboard_manager = keyboard_manager; |
|
133 | 139 | IPython.save_widget = save_widget; |
|
134 | 140 | IPython.config = user_config; |
|
135 | 141 | IPython.tooltip = notebook.tooltip; |
|
136 | 142 | }); |
@@ -1,2535 +1,2537 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'base/js/dialog', |
|
9 | 9 | 'notebook/js/textcell', |
|
10 | 10 | 'notebook/js/codecell', |
|
11 | 11 | 'services/sessions/js/session', |
|
12 | 12 | 'notebook/js/celltoolbar', |
|
13 | 13 | 'components/marked/lib/marked', |
|
14 | 'components/highlight.js/build/highlight.pack', | |
|
14 | 15 | 'notebook/js/mathjaxutils', |
|
15 | 16 | 'base/js/keyboard', |
|
16 | 17 | 'notebook/js/tooltip', |
|
17 | 18 | 'notebook/js/celltoolbarpresets/default', |
|
18 | 19 | 'notebook/js/celltoolbarpresets/rawcell', |
|
19 | 20 | 'notebook/js/celltoolbarpresets/slideshow', |
|
20 | 21 | ], function ( |
|
21 | 22 | IPython, |
|
22 | 23 | $, |
|
23 | 24 | utils, |
|
24 | 25 | dialog, |
|
25 | 26 | textcell, |
|
26 | 27 | codecell, |
|
27 | 28 | session, |
|
28 | 29 | celltoolbar, |
|
29 | 30 | marked, |
|
31 | hljs, | |
|
30 | 32 | mathjaxutils, |
|
31 | 33 | keyboard, |
|
32 | 34 | tooltip, |
|
33 | 35 | default_celltoolbar, |
|
34 | 36 | rawcell_celltoolbar, |
|
35 | 37 | slideshow_celltoolbar |
|
36 | 38 | ) { |
|
37 | 39 | |
|
38 | 40 | var Notebook = function (selector, options) { |
|
39 | 41 | // Constructor |
|
40 | 42 | // |
|
41 | 43 | // A notebook contains and manages cells. |
|
42 | 44 | // |
|
43 | 45 | // Parameters: |
|
44 | 46 | // selector: string |
|
45 | 47 | // options: dictionary |
|
46 | 48 | // Dictionary of keyword arguments. |
|
47 | 49 | // events: $(Events) instance |
|
48 | 50 | // keyboard_manager: KeyboardManager instance |
|
49 | 51 | // save_widget: SaveWidget instance |
|
50 | 52 | // config: dictionary |
|
51 | 53 | // base_url : string |
|
52 | 54 | // notebook_path : string |
|
53 | 55 | // notebook_name : string |
|
54 | 56 | this.config = options.config || {}; |
|
55 | 57 | this.base_url = options.base_url; |
|
56 | 58 | this.notebook_path = options.notebook_path; |
|
57 | 59 | this.notebook_name = options.notebook_name; |
|
58 | 60 | this.events = options.events; |
|
59 | 61 | this.keyboard_manager = options.keyboard_manager; |
|
60 | 62 | this.save_widget = options.save_widget; |
|
61 | 63 | this.tooltip = new tooltip.Tooltip(this.events); |
|
62 | 64 | // default_kernel_name is a temporary measure while we implement proper |
|
63 | 65 | // kernel selection and delayed start. Do not rely on it. |
|
64 | 66 | this.default_kernel_name = 'python'; |
|
65 | 67 | // TODO: This code smells (and the other `= this` line a couple lines down) |
|
66 | 68 | // We need a better way to deal with circular instance references. |
|
67 | 69 | this.keyboard_manager.notebook = this; |
|
68 | 70 | this.save_widget.notebook = this; |
|
69 | 71 | |
|
70 | 72 | mathjaxutils.init(); |
|
71 | 73 | |
|
72 | 74 | if (marked) { |
|
73 | 75 | marked.setOptions({ |
|
74 | 76 | gfm : true, |
|
75 | 77 | tables: true, |
|
76 | 78 | langPrefix: "language-", |
|
77 | 79 | highlight: function(code, lang) { |
|
78 | 80 | if (!lang) { |
|
79 | 81 | // no language, no highlight |
|
80 | 82 | return code; |
|
81 | 83 | } |
|
82 | 84 | var highlighted; |
|
83 | 85 | try { |
|
84 | 86 | highlighted = hljs.highlight(lang, code, false); |
|
85 | 87 | } catch(err) { |
|
86 | 88 | highlighted = hljs.highlightAuto(code); |
|
87 | 89 | } |
|
88 | 90 | return highlighted.value; |
|
89 | 91 | } |
|
90 | 92 | }); |
|
91 | 93 | } |
|
92 | 94 | |
|
93 | 95 | this.element = $(selector); |
|
94 | 96 | this.element.scroll(); |
|
95 | 97 | this.element.data("notebook", this); |
|
96 | 98 | this.next_prompt_number = 1; |
|
97 | 99 | this.session = null; |
|
98 | 100 | this.kernel = null; |
|
99 | 101 | this.clipboard = null; |
|
100 | 102 | this.undelete_backup = null; |
|
101 | 103 | this.undelete_index = null; |
|
102 | 104 | this.undelete_below = false; |
|
103 | 105 | this.paste_enabled = false; |
|
104 | 106 | // It is important to start out in command mode to match the intial mode |
|
105 | 107 | // of the KeyboardManager. |
|
106 | 108 | this.mode = 'command'; |
|
107 | 109 | this.set_dirty(false); |
|
108 | 110 | this.metadata = {}; |
|
109 | 111 | this._checkpoint_after_save = false; |
|
110 | 112 | this.last_checkpoint = null; |
|
111 | 113 | this.checkpoints = []; |
|
112 | 114 | this.autosave_interval = 0; |
|
113 | 115 | this.autosave_timer = null; |
|
114 | 116 | // autosave *at most* every two minutes |
|
115 | 117 | this.minimum_autosave_interval = 120000; |
|
116 | 118 | // single worksheet for now |
|
117 | 119 | this.worksheet_metadata = {}; |
|
118 | 120 | this.notebook_name_blacklist_re = /[\/\\:]/; |
|
119 | 121 | this.nbformat = 3; // Increment this when changing the nbformat |
|
120 | 122 | this.nbformat_minor = 0; // Increment this when changing the nbformat |
|
121 | 123 | this.style(); |
|
122 | 124 | this.create_elements(); |
|
123 | 125 | this.bind_events(); |
|
124 | 126 | this.save_notebook = function() { // don't allow save until notebook_loaded |
|
125 | 127 | this.save_notebook_error(null, null, "Load failed, save is disabled"); |
|
126 | 128 | }; |
|
127 | 129 | |
|
128 | 130 | // Trigger cell toolbar registration. |
|
129 | 131 | default_celltoolbar.register(this, options.events); |
|
130 | 132 | rawcell_celltoolbar.register(this, options.events); |
|
131 | 133 | slideshow_celltoolbar.register(this, options.events); |
|
132 | 134 | }; |
|
133 | 135 | |
|
134 | 136 | /** |
|
135 | 137 | * Tweak the notebook's CSS style. |
|
136 | 138 | * |
|
137 | 139 | * @method style |
|
138 | 140 | */ |
|
139 | 141 | Notebook.prototype.style = function () { |
|
140 | 142 | $('div#notebook').addClass('border-box-sizing'); |
|
141 | 143 | }; |
|
142 | 144 | |
|
143 | 145 | /** |
|
144 | 146 | * Create an HTML and CSS representation of the notebook. |
|
145 | 147 | * |
|
146 | 148 | * @method create_elements |
|
147 | 149 | */ |
|
148 | 150 | Notebook.prototype.create_elements = function () { |
|
149 | 151 | var that = this; |
|
150 | 152 | this.element.attr('tabindex','-1'); |
|
151 | 153 | this.container = $("<div/>").addClass("container").attr("id", "notebook-container"); |
|
152 | 154 | // We add this end_space div to the end of the notebook div to: |
|
153 | 155 | // i) provide a margin between the last cell and the end of the notebook |
|
154 | 156 | // ii) to prevent the div from scrolling up when the last cell is being |
|
155 | 157 | // edited, but is too low on the page, which browsers will do automatically. |
|
156 | 158 | var end_space = $('<div/>').addClass('end_space'); |
|
157 | 159 | end_space.dblclick(function (e) { |
|
158 | 160 | var ncells = that.ncells(); |
|
159 | 161 | that.insert_cell_below('code',ncells-1); |
|
160 | 162 | }); |
|
161 | 163 | this.element.append(this.container); |
|
162 | 164 | this.container.append(end_space); |
|
163 | 165 | }; |
|
164 | 166 | |
|
165 | 167 | /** |
|
166 | 168 | * Bind JavaScript events: key presses and custom IPython events. |
|
167 | 169 | * |
|
168 | 170 | * @method bind_events |
|
169 | 171 | */ |
|
170 | 172 | Notebook.prototype.bind_events = function () { |
|
171 | 173 | var that = this; |
|
172 | 174 | |
|
173 | 175 | this.events.on('set_next_input.Notebook', function (event, data) { |
|
174 | 176 | var index = that.find_cell_index(data.cell); |
|
175 | 177 | var new_cell = that.insert_cell_below('code',index); |
|
176 | 178 | new_cell.set_text(data.text); |
|
177 | 179 | that.dirty = true; |
|
178 | 180 | }); |
|
179 | 181 | |
|
180 | 182 | this.events.on('set_dirty.Notebook', function (event, data) { |
|
181 | 183 | that.dirty = data.value; |
|
182 | 184 | }); |
|
183 | 185 | |
|
184 | 186 | this.events.on('trust_changed.Notebook', function (event, data) { |
|
185 | 187 | that.trusted = data.value; |
|
186 | 188 | }); |
|
187 | 189 | |
|
188 | 190 | this.events.on('select.Cell', function (event, data) { |
|
189 | 191 | var index = that.find_cell_index(data.cell); |
|
190 | 192 | that.select(index); |
|
191 | 193 | }); |
|
192 | 194 | |
|
193 | 195 | this.events.on('edit_mode.Cell', function (event, data) { |
|
194 | 196 | that.handle_edit_mode(data.cell); |
|
195 | 197 | }); |
|
196 | 198 | |
|
197 | 199 | this.events.on('command_mode.Cell', function (event, data) { |
|
198 | 200 | that.handle_command_mode(data.cell); |
|
199 | 201 | }); |
|
200 | 202 | |
|
201 | 203 | this.events.on('status_autorestarting.Kernel', function () { |
|
202 | 204 | dialog.modal({ |
|
203 | 205 | notebook: that, |
|
204 | 206 | keyboard_manager: that.keyboard_manager, |
|
205 | 207 | title: "Kernel Restarting", |
|
206 | 208 | body: "The kernel appears to have died. It will restart automatically.", |
|
207 | 209 | buttons: { |
|
208 | 210 | OK : { |
|
209 | 211 | class : "btn-primary" |
|
210 | 212 | } |
|
211 | 213 | } |
|
212 | 214 | }); |
|
213 | 215 | }); |
|
214 | 216 | |
|
215 | 217 | var collapse_time = function (time) { |
|
216 | 218 | var app_height = $('#ipython-main-app').height(); // content height |
|
217 | 219 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
218 | 220 | var new_height = app_height - splitter_height; |
|
219 | 221 | that.element.animate({height : new_height + 'px'}, time); |
|
220 | 222 | }; |
|
221 | 223 | |
|
222 | 224 | this.element.bind('collapse_pager', function (event, extrap) { |
|
223 | 225 | var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast'; |
|
224 | 226 | collapse_time(time); |
|
225 | 227 | }); |
|
226 | 228 | |
|
227 | 229 | var expand_time = function (time) { |
|
228 | 230 | var app_height = $('#ipython-main-app').height(); // content height |
|
229 | 231 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
230 | 232 | var pager_height = $('div#pager').outerHeight(true); |
|
231 | 233 | var new_height = app_height - pager_height - splitter_height; |
|
232 | 234 | that.element.animate({height : new_height + 'px'}, time); |
|
233 | 235 | }; |
|
234 | 236 | |
|
235 | 237 | this.element.bind('expand_pager', function (event, extrap) { |
|
236 | 238 | var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast'; |
|
237 | 239 | expand_time(time); |
|
238 | 240 | }); |
|
239 | 241 | |
|
240 | 242 | // Firefox 22 broke $(window).on("beforeunload") |
|
241 | 243 | // I'm not sure why or how. |
|
242 | 244 | window.onbeforeunload = function (e) { |
|
243 | 245 | // TODO: Make killing the kernel configurable. |
|
244 | 246 | var kill_kernel = false; |
|
245 | 247 | if (kill_kernel) { |
|
246 | 248 | that.session.kill_kernel(); |
|
247 | 249 | } |
|
248 | 250 | // if we are autosaving, trigger an autosave on nav-away. |
|
249 | 251 | // still warn, because if we don't the autosave may fail. |
|
250 | 252 | if (that.dirty) { |
|
251 | 253 | if ( that.autosave_interval ) { |
|
252 | 254 | // schedule autosave in a timeout |
|
253 | 255 | // this gives you a chance to forcefully discard changes |
|
254 | 256 | // by reloading the page if you *really* want to. |
|
255 | 257 | // the timer doesn't start until you *dismiss* the dialog. |
|
256 | 258 | setTimeout(function () { |
|
257 | 259 | if (that.dirty) { |
|
258 | 260 | that.save_notebook(); |
|
259 | 261 | } |
|
260 | 262 | }, 1000); |
|
261 | 263 | return "Autosave in progress, latest changes may be lost."; |
|
262 | 264 | } else { |
|
263 | 265 | return "Unsaved changes will be lost."; |
|
264 | 266 | } |
|
265 | 267 | } |
|
266 | 268 | // Null is the *only* return value that will make the browser not |
|
267 | 269 | // pop up the "don't leave" dialog. |
|
268 | 270 | return null; |
|
269 | 271 | }; |
|
270 | 272 | }; |
|
271 | 273 | |
|
272 | 274 | /** |
|
273 | 275 | * Set the dirty flag, and trigger the set_dirty.Notebook event |
|
274 | 276 | * |
|
275 | 277 | * @method set_dirty |
|
276 | 278 | */ |
|
277 | 279 | Notebook.prototype.set_dirty = function (value) { |
|
278 | 280 | if (value === undefined) { |
|
279 | 281 | value = true; |
|
280 | 282 | } |
|
281 | 283 | if (this.dirty == value) { |
|
282 | 284 | return; |
|
283 | 285 | } |
|
284 | 286 | this.events.trigger('set_dirty.Notebook', {value: value}); |
|
285 | 287 | }; |
|
286 | 288 | |
|
287 | 289 | /** |
|
288 | 290 | * Scroll the top of the page to a given cell. |
|
289 | 291 | * |
|
290 | 292 | * @method scroll_to_cell |
|
291 | 293 | * @param {Number} cell_number An index of the cell to view |
|
292 | 294 | * @param {Number} time Animation time in milliseconds |
|
293 | 295 | * @return {Number} Pixel offset from the top of the container |
|
294 | 296 | */ |
|
295 | 297 | Notebook.prototype.scroll_to_cell = function (cell_number, time) { |
|
296 | 298 | var cells = this.get_cells(); |
|
297 | 299 | time = time || 0; |
|
298 | 300 | cell_number = Math.min(cells.length-1,cell_number); |
|
299 | 301 | cell_number = Math.max(0 ,cell_number); |
|
300 | 302 | var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; |
|
301 | 303 | this.element.animate({scrollTop:scroll_value}, time); |
|
302 | 304 | return scroll_value; |
|
303 | 305 | }; |
|
304 | 306 | |
|
305 | 307 | /** |
|
306 | 308 | * Scroll to the bottom of the page. |
|
307 | 309 | * |
|
308 | 310 | * @method scroll_to_bottom |
|
309 | 311 | */ |
|
310 | 312 | Notebook.prototype.scroll_to_bottom = function () { |
|
311 | 313 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); |
|
312 | 314 | }; |
|
313 | 315 | |
|
314 | 316 | /** |
|
315 | 317 | * Scroll to the top of the page. |
|
316 | 318 | * |
|
317 | 319 | * @method scroll_to_top |
|
318 | 320 | */ |
|
319 | 321 | Notebook.prototype.scroll_to_top = function () { |
|
320 | 322 | this.element.animate({scrollTop:0}, 0); |
|
321 | 323 | }; |
|
322 | 324 | |
|
323 | 325 | // Edit Notebook metadata |
|
324 | 326 | |
|
325 | 327 | Notebook.prototype.edit_metadata = function () { |
|
326 | 328 | var that = this; |
|
327 | 329 | dialog.edit_metadata({ |
|
328 | 330 | md: this.metadata, |
|
329 | 331 | callback: function (md) { |
|
330 | 332 | that.metadata = md; |
|
331 | 333 | }, |
|
332 | 334 | name: 'Notebook', |
|
333 | 335 | notebook: this, |
|
334 | 336 | keyboard_manager: this.keyboard_manager}); |
|
335 | 337 | }; |
|
336 | 338 | |
|
337 | 339 | // Cell indexing, retrieval, etc. |
|
338 | 340 | |
|
339 | 341 | /** |
|
340 | 342 | * Get all cell elements in the notebook. |
|
341 | 343 | * |
|
342 | 344 | * @method get_cell_elements |
|
343 | 345 | * @return {jQuery} A selector of all cell elements |
|
344 | 346 | */ |
|
345 | 347 | Notebook.prototype.get_cell_elements = function () { |
|
346 | 348 | return this.container.children("div.cell"); |
|
347 | 349 | }; |
|
348 | 350 | |
|
349 | 351 | /** |
|
350 | 352 | * Get a particular cell element. |
|
351 | 353 | * |
|
352 | 354 | * @method get_cell_element |
|
353 | 355 | * @param {Number} index An index of a cell to select |
|
354 | 356 | * @return {jQuery} A selector of the given cell. |
|
355 | 357 | */ |
|
356 | 358 | Notebook.prototype.get_cell_element = function (index) { |
|
357 | 359 | var result = null; |
|
358 | 360 | var e = this.get_cell_elements().eq(index); |
|
359 | 361 | if (e.length !== 0) { |
|
360 | 362 | result = e; |
|
361 | 363 | } |
|
362 | 364 | return result; |
|
363 | 365 | }; |
|
364 | 366 | |
|
365 | 367 | /** |
|
366 | 368 | * Try to get a particular cell by msg_id. |
|
367 | 369 | * |
|
368 | 370 | * @method get_msg_cell |
|
369 | 371 | * @param {String} msg_id A message UUID |
|
370 | 372 | * @return {Cell} Cell or null if no cell was found. |
|
371 | 373 | */ |
|
372 | 374 | Notebook.prototype.get_msg_cell = function (msg_id) { |
|
373 | 375 | return codecell.CodeCell.msg_cells[msg_id] || null; |
|
374 | 376 | }; |
|
375 | 377 | |
|
376 | 378 | /** |
|
377 | 379 | * Count the cells in this notebook. |
|
378 | 380 | * |
|
379 | 381 | * @method ncells |
|
380 | 382 | * @return {Number} The number of cells in this notebook |
|
381 | 383 | */ |
|
382 | 384 | Notebook.prototype.ncells = function () { |
|
383 | 385 | return this.get_cell_elements().length; |
|
384 | 386 | }; |
|
385 | 387 | |
|
386 | 388 | /** |
|
387 | 389 | * Get all Cell objects in this notebook. |
|
388 | 390 | * |
|
389 | 391 | * @method get_cells |
|
390 | 392 | * @return {Array} This notebook's Cell objects |
|
391 | 393 | */ |
|
392 | 394 | // TODO: we are often calling cells as cells()[i], which we should optimize |
|
393 | 395 | // to cells(i) or a new method. |
|
394 | 396 | Notebook.prototype.get_cells = function () { |
|
395 | 397 | return this.get_cell_elements().toArray().map(function (e) { |
|
396 | 398 | return $(e).data("cell"); |
|
397 | 399 | }); |
|
398 | 400 | }; |
|
399 | 401 | |
|
400 | 402 | /** |
|
401 | 403 | * Get a Cell object from this notebook. |
|
402 | 404 | * |
|
403 | 405 | * @method get_cell |
|
404 | 406 | * @param {Number} index An index of a cell to retrieve |
|
405 | 407 | * @return {Cell} A particular cell |
|
406 | 408 | */ |
|
407 | 409 | Notebook.prototype.get_cell = function (index) { |
|
408 | 410 | var result = null; |
|
409 | 411 | var ce = this.get_cell_element(index); |
|
410 | 412 | if (ce !== null) { |
|
411 | 413 | result = ce.data('cell'); |
|
412 | 414 | } |
|
413 | 415 | return result; |
|
414 | 416 | }; |
|
415 | 417 | |
|
416 | 418 | /** |
|
417 | 419 | * Get the cell below a given cell. |
|
418 | 420 | * |
|
419 | 421 | * @method get_next_cell |
|
420 | 422 | * @param {Cell} cell The provided cell |
|
421 | 423 | * @return {Cell} The next cell |
|
422 | 424 | */ |
|
423 | 425 | Notebook.prototype.get_next_cell = function (cell) { |
|
424 | 426 | var result = null; |
|
425 | 427 | var index = this.find_cell_index(cell); |
|
426 | 428 | if (this.is_valid_cell_index(index+1)) { |
|
427 | 429 | result = this.get_cell(index+1); |
|
428 | 430 | } |
|
429 | 431 | return result; |
|
430 | 432 | }; |
|
431 | 433 | |
|
432 | 434 | /** |
|
433 | 435 | * Get the cell above a given cell. |
|
434 | 436 | * |
|
435 | 437 | * @method get_prev_cell |
|
436 | 438 | * @param {Cell} cell The provided cell |
|
437 | 439 | * @return {Cell} The previous cell |
|
438 | 440 | */ |
|
439 | 441 | Notebook.prototype.get_prev_cell = function (cell) { |
|
440 | 442 | // TODO: off-by-one |
|
441 | 443 | // nb.get_prev_cell(nb.get_cell(1)) is null |
|
442 | 444 | var result = null; |
|
443 | 445 | var index = this.find_cell_index(cell); |
|
444 | 446 | if (index !== null && index > 1) { |
|
445 | 447 | result = this.get_cell(index-1); |
|
446 | 448 | } |
|
447 | 449 | return result; |
|
448 | 450 | }; |
|
449 | 451 | |
|
450 | 452 | /** |
|
451 | 453 | * Get the numeric index of a given cell. |
|
452 | 454 | * |
|
453 | 455 | * @method find_cell_index |
|
454 | 456 | * @param {Cell} cell The provided cell |
|
455 | 457 | * @return {Number} The cell's numeric index |
|
456 | 458 | */ |
|
457 | 459 | Notebook.prototype.find_cell_index = function (cell) { |
|
458 | 460 | var result = null; |
|
459 | 461 | this.get_cell_elements().filter(function (index) { |
|
460 | 462 | if ($(this).data("cell") === cell) { |
|
461 | 463 | result = index; |
|
462 | 464 | } |
|
463 | 465 | }); |
|
464 | 466 | return result; |
|
465 | 467 | }; |
|
466 | 468 | |
|
467 | 469 | /** |
|
468 | 470 | * Get a given index , or the selected index if none is provided. |
|
469 | 471 | * |
|
470 | 472 | * @method index_or_selected |
|
471 | 473 | * @param {Number} index A cell's index |
|
472 | 474 | * @return {Number} The given index, or selected index if none is provided. |
|
473 | 475 | */ |
|
474 | 476 | Notebook.prototype.index_or_selected = function (index) { |
|
475 | 477 | var i; |
|
476 | 478 | if (index === undefined || index === null) { |
|
477 | 479 | i = this.get_selected_index(); |
|
478 | 480 | if (i === null) { |
|
479 | 481 | i = 0; |
|
480 | 482 | } |
|
481 | 483 | } else { |
|
482 | 484 | i = index; |
|
483 | 485 | } |
|
484 | 486 | return i; |
|
485 | 487 | }; |
|
486 | 488 | |
|
487 | 489 | /** |
|
488 | 490 | * Get the currently selected cell. |
|
489 | 491 | * @method get_selected_cell |
|
490 | 492 | * @return {Cell} The selected cell |
|
491 | 493 | */ |
|
492 | 494 | Notebook.prototype.get_selected_cell = function () { |
|
493 | 495 | var index = this.get_selected_index(); |
|
494 | 496 | return this.get_cell(index); |
|
495 | 497 | }; |
|
496 | 498 | |
|
497 | 499 | /** |
|
498 | 500 | * Check whether a cell index is valid. |
|
499 | 501 | * |
|
500 | 502 | * @method is_valid_cell_index |
|
501 | 503 | * @param {Number} index A cell index |
|
502 | 504 | * @return True if the index is valid, false otherwise |
|
503 | 505 | */ |
|
504 | 506 | Notebook.prototype.is_valid_cell_index = function (index) { |
|
505 | 507 | if (index !== null && index >= 0 && index < this.ncells()) { |
|
506 | 508 | return true; |
|
507 | 509 | } else { |
|
508 | 510 | return false; |
|
509 | 511 | } |
|
510 | 512 | }; |
|
511 | 513 | |
|
512 | 514 | /** |
|
513 | 515 | * Get the index of the currently selected cell. |
|
514 | 516 | |
|
515 | 517 | * @method get_selected_index |
|
516 | 518 | * @return {Number} The selected cell's numeric index |
|
517 | 519 | */ |
|
518 | 520 | Notebook.prototype.get_selected_index = function () { |
|
519 | 521 | var result = null; |
|
520 | 522 | this.get_cell_elements().filter(function (index) { |
|
521 | 523 | if ($(this).data("cell").selected === true) { |
|
522 | 524 | result = index; |
|
523 | 525 | } |
|
524 | 526 | }); |
|
525 | 527 | return result; |
|
526 | 528 | }; |
|
527 | 529 | |
|
528 | 530 | |
|
529 | 531 | // Cell selection. |
|
530 | 532 | |
|
531 | 533 | /** |
|
532 | 534 | * Programmatically select a cell. |
|
533 | 535 | * |
|
534 | 536 | * @method select |
|
535 | 537 | * @param {Number} index A cell's index |
|
536 | 538 | * @return {Notebook} This notebook |
|
537 | 539 | */ |
|
538 | 540 | Notebook.prototype.select = function (index) { |
|
539 | 541 | if (this.is_valid_cell_index(index)) { |
|
540 | 542 | var sindex = this.get_selected_index(); |
|
541 | 543 | if (sindex !== null && index !== sindex) { |
|
542 | 544 | // If we are about to select a different cell, make sure we are |
|
543 | 545 | // first in command mode. |
|
544 | 546 | if (this.mode !== 'command') { |
|
545 | 547 | this.command_mode(); |
|
546 | 548 | } |
|
547 | 549 | this.get_cell(sindex).unselect(); |
|
548 | 550 | } |
|
549 | 551 | var cell = this.get_cell(index); |
|
550 | 552 | cell.select(); |
|
551 | 553 | if (cell.cell_type === 'heading') { |
|
552 | 554 | this.events.trigger('selected_cell_type_changed.Notebook', |
|
553 | 555 | {'cell_type':cell.cell_type,level:cell.level} |
|
554 | 556 | ); |
|
555 | 557 | } else { |
|
556 | 558 | this.events.trigger('selected_cell_type_changed.Notebook', |
|
557 | 559 | {'cell_type':cell.cell_type} |
|
558 | 560 | ); |
|
559 | 561 | } |
|
560 | 562 | } |
|
561 | 563 | return this; |
|
562 | 564 | }; |
|
563 | 565 | |
|
564 | 566 | /** |
|
565 | 567 | * Programmatically select the next cell. |
|
566 | 568 | * |
|
567 | 569 | * @method select_next |
|
568 | 570 | * @return {Notebook} This notebook |
|
569 | 571 | */ |
|
570 | 572 | Notebook.prototype.select_next = function () { |
|
571 | 573 | var index = this.get_selected_index(); |
|
572 | 574 | this.select(index+1); |
|
573 | 575 | return this; |
|
574 | 576 | }; |
|
575 | 577 | |
|
576 | 578 | /** |
|
577 | 579 | * Programmatically select the previous cell. |
|
578 | 580 | * |
|
579 | 581 | * @method select_prev |
|
580 | 582 | * @return {Notebook} This notebook |
|
581 | 583 | */ |
|
582 | 584 | Notebook.prototype.select_prev = function () { |
|
583 | 585 | var index = this.get_selected_index(); |
|
584 | 586 | this.select(index-1); |
|
585 | 587 | return this; |
|
586 | 588 | }; |
|
587 | 589 | |
|
588 | 590 | |
|
589 | 591 | // Edit/Command mode |
|
590 | 592 | |
|
591 | 593 | /** |
|
592 | 594 | * Gets the index of the cell that is in edit mode. |
|
593 | 595 | * |
|
594 | 596 | * @method get_edit_index |
|
595 | 597 | * |
|
596 | 598 | * @return index {int} |
|
597 | 599 | **/ |
|
598 | 600 | Notebook.prototype.get_edit_index = function () { |
|
599 | 601 | var result = null; |
|
600 | 602 | this.get_cell_elements().filter(function (index) { |
|
601 | 603 | if ($(this).data("cell").mode === 'edit') { |
|
602 | 604 | result = index; |
|
603 | 605 | } |
|
604 | 606 | }); |
|
605 | 607 | return result; |
|
606 | 608 | }; |
|
607 | 609 | |
|
608 | 610 | /** |
|
609 | 611 | * Handle when a a cell blurs and the notebook should enter command mode. |
|
610 | 612 | * |
|
611 | 613 | * @method handle_command_mode |
|
612 | 614 | * @param [cell] {Cell} Cell to enter command mode on. |
|
613 | 615 | **/ |
|
614 | 616 | Notebook.prototype.handle_command_mode = function (cell) { |
|
615 | 617 | if (this.mode !== 'command') { |
|
616 | 618 | cell.command_mode(); |
|
617 | 619 | this.mode = 'command'; |
|
618 | 620 | this.events.trigger('command_mode.Notebook'); |
|
619 | 621 | this.keyboard_manager.command_mode(); |
|
620 | 622 | } |
|
621 | 623 | }; |
|
622 | 624 | |
|
623 | 625 | /** |
|
624 | 626 | * Make the notebook enter command mode. |
|
625 | 627 | * |
|
626 | 628 | * @method command_mode |
|
627 | 629 | **/ |
|
628 | 630 | Notebook.prototype.command_mode = function () { |
|
629 | 631 | var cell = this.get_cell(this.get_edit_index()); |
|
630 | 632 | if (cell && this.mode !== 'command') { |
|
631 | 633 | // We don't call cell.command_mode, but rather call cell.focus_cell() |
|
632 | 634 | // which will blur and CM editor and trigger the call to |
|
633 | 635 | // handle_command_mode. |
|
634 | 636 | cell.focus_cell(); |
|
635 | 637 | } |
|
636 | 638 | }; |
|
637 | 639 | |
|
638 | 640 | /** |
|
639 | 641 | * Handle when a cell fires it's edit_mode event. |
|
640 | 642 | * |
|
641 | 643 | * @method handle_edit_mode |
|
642 | 644 | * @param [cell] {Cell} Cell to enter edit mode on. |
|
643 | 645 | **/ |
|
644 | 646 | Notebook.prototype.handle_edit_mode = function (cell) { |
|
645 | 647 | if (cell && this.mode !== 'edit') { |
|
646 | 648 | cell.edit_mode(); |
|
647 | 649 | this.mode = 'edit'; |
|
648 | 650 | this.events.trigger('edit_mode.Notebook'); |
|
649 | 651 | this.keyboard_manager.edit_mode(); |
|
650 | 652 | } |
|
651 | 653 | }; |
|
652 | 654 | |
|
653 | 655 | /** |
|
654 | 656 | * Make a cell enter edit mode. |
|
655 | 657 | * |
|
656 | 658 | * @method edit_mode |
|
657 | 659 | **/ |
|
658 | 660 | Notebook.prototype.edit_mode = function () { |
|
659 | 661 | var cell = this.get_selected_cell(); |
|
660 | 662 | if (cell && this.mode !== 'edit') { |
|
661 | 663 | cell.unrender(); |
|
662 | 664 | cell.focus_editor(); |
|
663 | 665 | } |
|
664 | 666 | }; |
|
665 | 667 | |
|
666 | 668 | /** |
|
667 | 669 | * Focus the currently selected cell. |
|
668 | 670 | * |
|
669 | 671 | * @method focus_cell |
|
670 | 672 | **/ |
|
671 | 673 | Notebook.prototype.focus_cell = function () { |
|
672 | 674 | var cell = this.get_selected_cell(); |
|
673 | 675 | if (cell === null) {return;} // No cell is selected |
|
674 | 676 | cell.focus_cell(); |
|
675 | 677 | }; |
|
676 | 678 | |
|
677 | 679 | // Cell movement |
|
678 | 680 | |
|
679 | 681 | /** |
|
680 | 682 | * Move given (or selected) cell up and select it. |
|
681 | 683 | * |
|
682 | 684 | * @method move_cell_up |
|
683 | 685 | * @param [index] {integer} cell index |
|
684 | 686 | * @return {Notebook} This notebook |
|
685 | 687 | **/ |
|
686 | 688 | Notebook.prototype.move_cell_up = function (index) { |
|
687 | 689 | var i = this.index_or_selected(index); |
|
688 | 690 | if (this.is_valid_cell_index(i) && i > 0) { |
|
689 | 691 | var pivot = this.get_cell_element(i-1); |
|
690 | 692 | var tomove = this.get_cell_element(i); |
|
691 | 693 | if (pivot !== null && tomove !== null) { |
|
692 | 694 | tomove.detach(); |
|
693 | 695 | pivot.before(tomove); |
|
694 | 696 | this.select(i-1); |
|
695 | 697 | var cell = this.get_selected_cell(); |
|
696 | 698 | cell.focus_cell(); |
|
697 | 699 | } |
|
698 | 700 | this.set_dirty(true); |
|
699 | 701 | } |
|
700 | 702 | return this; |
|
701 | 703 | }; |
|
702 | 704 | |
|
703 | 705 | |
|
704 | 706 | /** |
|
705 | 707 | * Move given (or selected) cell down and select it |
|
706 | 708 | * |
|
707 | 709 | * @method move_cell_down |
|
708 | 710 | * @param [index] {integer} cell index |
|
709 | 711 | * @return {Notebook} This notebook |
|
710 | 712 | **/ |
|
711 | 713 | Notebook.prototype.move_cell_down = function (index) { |
|
712 | 714 | var i = this.index_or_selected(index); |
|
713 | 715 | if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { |
|
714 | 716 | var pivot = this.get_cell_element(i+1); |
|
715 | 717 | var tomove = this.get_cell_element(i); |
|
716 | 718 | if (pivot !== null && tomove !== null) { |
|
717 | 719 | tomove.detach(); |
|
718 | 720 | pivot.after(tomove); |
|
719 | 721 | this.select(i+1); |
|
720 | 722 | var cell = this.get_selected_cell(); |
|
721 | 723 | cell.focus_cell(); |
|
722 | 724 | } |
|
723 | 725 | } |
|
724 | 726 | this.set_dirty(); |
|
725 | 727 | return this; |
|
726 | 728 | }; |
|
727 | 729 | |
|
728 | 730 | |
|
729 | 731 | // Insertion, deletion. |
|
730 | 732 | |
|
731 | 733 | /** |
|
732 | 734 | * Delete a cell from the notebook. |
|
733 | 735 | * |
|
734 | 736 | * @method delete_cell |
|
735 | 737 | * @param [index] A cell's numeric index |
|
736 | 738 | * @return {Notebook} This notebook |
|
737 | 739 | */ |
|
738 | 740 | Notebook.prototype.delete_cell = function (index) { |
|
739 | 741 | var i = this.index_or_selected(index); |
|
740 | 742 | var cell = this.get_selected_cell(); |
|
741 | 743 | this.undelete_backup = cell.toJSON(); |
|
742 | 744 | $('#undelete_cell').removeClass('disabled'); |
|
743 | 745 | if (this.is_valid_cell_index(i)) { |
|
744 | 746 | var old_ncells = this.ncells(); |
|
745 | 747 | var ce = this.get_cell_element(i); |
|
746 | 748 | ce.remove(); |
|
747 | 749 | if (i === 0) { |
|
748 | 750 | // Always make sure we have at least one cell. |
|
749 | 751 | if (old_ncells === 1) { |
|
750 | 752 | this.insert_cell_below('code'); |
|
751 | 753 | } |
|
752 | 754 | this.select(0); |
|
753 | 755 | this.undelete_index = 0; |
|
754 | 756 | this.undelete_below = false; |
|
755 | 757 | } else if (i === old_ncells-1 && i !== 0) { |
|
756 | 758 | this.select(i-1); |
|
757 | 759 | this.undelete_index = i - 1; |
|
758 | 760 | this.undelete_below = true; |
|
759 | 761 | } else { |
|
760 | 762 | this.select(i); |
|
761 | 763 | this.undelete_index = i; |
|
762 | 764 | this.undelete_below = false; |
|
763 | 765 | } |
|
764 | 766 | this.events.trigger('delete.Cell', {'cell': cell, 'index': i}); |
|
765 | 767 | this.set_dirty(true); |
|
766 | 768 | } |
|
767 | 769 | return this; |
|
768 | 770 | }; |
|
769 | 771 | |
|
770 | 772 | /** |
|
771 | 773 | * Restore the most recently deleted cell. |
|
772 | 774 | * |
|
773 | 775 | * @method undelete |
|
774 | 776 | */ |
|
775 | 777 | Notebook.prototype.undelete_cell = function() { |
|
776 | 778 | if (this.undelete_backup !== null && this.undelete_index !== null) { |
|
777 | 779 | var current_index = this.get_selected_index(); |
|
778 | 780 | if (this.undelete_index < current_index) { |
|
779 | 781 | current_index = current_index + 1; |
|
780 | 782 | } |
|
781 | 783 | if (this.undelete_index >= this.ncells()) { |
|
782 | 784 | this.select(this.ncells() - 1); |
|
783 | 785 | } |
|
784 | 786 | else { |
|
785 | 787 | this.select(this.undelete_index); |
|
786 | 788 | } |
|
787 | 789 | var cell_data = this.undelete_backup; |
|
788 | 790 | var new_cell = null; |
|
789 | 791 | if (this.undelete_below) { |
|
790 | 792 | new_cell = this.insert_cell_below(cell_data.cell_type); |
|
791 | 793 | } else { |
|
792 | 794 | new_cell = this.insert_cell_above(cell_data.cell_type); |
|
793 | 795 | } |
|
794 | 796 | new_cell.fromJSON(cell_data); |
|
795 | 797 | if (this.undelete_below) { |
|
796 | 798 | this.select(current_index+1); |
|
797 | 799 | } else { |
|
798 | 800 | this.select(current_index); |
|
799 | 801 | } |
|
800 | 802 | this.undelete_backup = null; |
|
801 | 803 | this.undelete_index = null; |
|
802 | 804 | } |
|
803 | 805 | $('#undelete_cell').addClass('disabled'); |
|
804 | 806 | }; |
|
805 | 807 | |
|
806 | 808 | /** |
|
807 | 809 | * Insert a cell so that after insertion the cell is at given index. |
|
808 | 810 | * |
|
809 | 811 | * If cell type is not provided, it will default to the type of the |
|
810 | 812 | * currently active cell. |
|
811 | 813 | * |
|
812 | 814 | * Similar to insert_above, but index parameter is mandatory |
|
813 | 815 | * |
|
814 | 816 | * Index will be brought back into the accessible range [0,n] |
|
815 | 817 | * |
|
816 | 818 | * @method insert_cell_at_index |
|
817 | 819 | * @param [type] {string} in ['code','markdown','heading'], defaults to 'code' |
|
818 | 820 | * @param [index] {int} a valid index where to insert cell |
|
819 | 821 | * |
|
820 | 822 | * @return cell {cell|null} created cell or null |
|
821 | 823 | **/ |
|
822 | 824 | Notebook.prototype.insert_cell_at_index = function(type, index){ |
|
823 | 825 | |
|
824 | 826 | var ncells = this.ncells(); |
|
825 | 827 | index = Math.min(index,ncells); |
|
826 | 828 | index = Math.max(index,0); |
|
827 | 829 | var cell = null; |
|
828 | 830 | type = type || this.get_selected_cell().cell_type; |
|
829 | 831 | |
|
830 | 832 | if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { |
|
831 | 833 | var cell_options = { |
|
832 | 834 | events: this.events, |
|
833 | 835 | config: this.config, |
|
834 | 836 | keyboard_manager: this.keyboard_manager, |
|
835 | 837 | notebook: this, |
|
836 | 838 | tooltip: this.tooltip, |
|
837 | 839 | }; |
|
838 | 840 | if (type === 'code') { |
|
839 | 841 | cell = new codecell.CodeCell(this.kernel, cell_options); |
|
840 | 842 | cell.set_input_prompt(); |
|
841 | 843 | } else if (type === 'markdown') { |
|
842 | 844 | cell = new textcell.MarkdownCell(cell_options); |
|
843 | 845 | } else if (type === 'raw') { |
|
844 | 846 | cell = new textcell.RawCell(cell_options); |
|
845 | 847 | } else if (type === 'heading') { |
|
846 | 848 | cell = new textcell.HeadingCell(cell_options); |
|
847 | 849 | } |
|
848 | 850 | |
|
849 | 851 | if(this._insert_element_at_index(cell.element,index)) { |
|
850 | 852 | cell.render(); |
|
851 | 853 | this.events.trigger('create.Cell', {'cell': cell, 'index': index}); |
|
852 | 854 | cell.refresh(); |
|
853 | 855 | // We used to select the cell after we refresh it, but there |
|
854 | 856 | // are now cases were this method is called where select is |
|
855 | 857 | // not appropriate. The selection logic should be handled by the |
|
856 | 858 | // caller of the the top level insert_cell methods. |
|
857 | 859 | this.set_dirty(true); |
|
858 | 860 | } |
|
859 | 861 | } |
|
860 | 862 | return cell; |
|
861 | 863 | |
|
862 | 864 | }; |
|
863 | 865 | |
|
864 | 866 | /** |
|
865 | 867 | * Insert an element at given cell index. |
|
866 | 868 | * |
|
867 | 869 | * @method _insert_element_at_index |
|
868 | 870 | * @param element {dom element} a cell element |
|
869 | 871 | * @param [index] {int} a valid index where to inser cell |
|
870 | 872 | * @private |
|
871 | 873 | * |
|
872 | 874 | * return true if everything whent fine. |
|
873 | 875 | **/ |
|
874 | 876 | Notebook.prototype._insert_element_at_index = function(element, index){ |
|
875 | 877 | if (element === undefined){ |
|
876 | 878 | return false; |
|
877 | 879 | } |
|
878 | 880 | |
|
879 | 881 | var ncells = this.ncells(); |
|
880 | 882 | |
|
881 | 883 | if (ncells === 0) { |
|
882 | 884 | // special case append if empty |
|
883 | 885 | this.element.find('div.end_space').before(element); |
|
884 | 886 | } else if ( ncells === index ) { |
|
885 | 887 | // special case append it the end, but not empty |
|
886 | 888 | this.get_cell_element(index-1).after(element); |
|
887 | 889 | } else if (this.is_valid_cell_index(index)) { |
|
888 | 890 | // otherwise always somewhere to append to |
|
889 | 891 | this.get_cell_element(index).before(element); |
|
890 | 892 | } else { |
|
891 | 893 | return false; |
|
892 | 894 | } |
|
893 | 895 | |
|
894 | 896 | if (this.undelete_index !== null && index <= this.undelete_index) { |
|
895 | 897 | this.undelete_index = this.undelete_index + 1; |
|
896 | 898 | this.set_dirty(true); |
|
897 | 899 | } |
|
898 | 900 | return true; |
|
899 | 901 | }; |
|
900 | 902 | |
|
901 | 903 | /** |
|
902 | 904 | * Insert a cell of given type above given index, or at top |
|
903 | 905 | * of notebook if index smaller than 0. |
|
904 | 906 | * |
|
905 | 907 | * default index value is the one of currently selected cell |
|
906 | 908 | * |
|
907 | 909 | * @method insert_cell_above |
|
908 | 910 | * @param [type] {string} cell type |
|
909 | 911 | * @param [index] {integer} |
|
910 | 912 | * |
|
911 | 913 | * @return handle to created cell or null |
|
912 | 914 | **/ |
|
913 | 915 | Notebook.prototype.insert_cell_above = function (type, index) { |
|
914 | 916 | index = this.index_or_selected(index); |
|
915 | 917 | return this.insert_cell_at_index(type, index); |
|
916 | 918 | }; |
|
917 | 919 | |
|
918 | 920 | /** |
|
919 | 921 | * Insert a cell of given type below given index, or at bottom |
|
920 | 922 | * of notebook if index greater than number of cells |
|
921 | 923 | * |
|
922 | 924 | * default index value is the one of currently selected cell |
|
923 | 925 | * |
|
924 | 926 | * @method insert_cell_below |
|
925 | 927 | * @param [type] {string} cell type |
|
926 | 928 | * @param [index] {integer} |
|
927 | 929 | * |
|
928 | 930 | * @return handle to created cell or null |
|
929 | 931 | * |
|
930 | 932 | **/ |
|
931 | 933 | Notebook.prototype.insert_cell_below = function (type, index) { |
|
932 | 934 | index = this.index_or_selected(index); |
|
933 | 935 | return this.insert_cell_at_index(type, index+1); |
|
934 | 936 | }; |
|
935 | 937 | |
|
936 | 938 | |
|
937 | 939 | /** |
|
938 | 940 | * Insert cell at end of notebook |
|
939 | 941 | * |
|
940 | 942 | * @method insert_cell_at_bottom |
|
941 | 943 | * @param {String} type cell type |
|
942 | 944 | * |
|
943 | 945 | * @return the added cell; or null |
|
944 | 946 | **/ |
|
945 | 947 | Notebook.prototype.insert_cell_at_bottom = function (type){ |
|
946 | 948 | var len = this.ncells(); |
|
947 | 949 | return this.insert_cell_below(type,len-1); |
|
948 | 950 | }; |
|
949 | 951 | |
|
950 | 952 | /** |
|
951 | 953 | * Turn a cell into a code cell. |
|
952 | 954 | * |
|
953 | 955 | * @method to_code |
|
954 | 956 | * @param {Number} [index] A cell's index |
|
955 | 957 | */ |
|
956 | 958 | Notebook.prototype.to_code = function (index) { |
|
957 | 959 | var i = this.index_or_selected(index); |
|
958 | 960 | if (this.is_valid_cell_index(i)) { |
|
959 | 961 | var source_element = this.get_cell_element(i); |
|
960 | 962 | var source_cell = source_element.data("cell"); |
|
961 | 963 | if (!(source_cell instanceof codecell.CodeCell)) { |
|
962 | 964 | var target_cell = this.insert_cell_below('code',i); |
|
963 | 965 | var text = source_cell.get_text(); |
|
964 | 966 | if (text === source_cell.placeholder) { |
|
965 | 967 | text = ''; |
|
966 | 968 | } |
|
967 | 969 | target_cell.set_text(text); |
|
968 | 970 | // make this value the starting point, so that we can only undo |
|
969 | 971 | // to this state, instead of a blank cell |
|
970 | 972 | target_cell.code_mirror.clearHistory(); |
|
971 | 973 | source_element.remove(); |
|
972 | 974 | this.select(i); |
|
973 | 975 | var cursor = source_cell.code_mirror.getCursor(); |
|
974 | 976 | target_cell.code_mirror.setCursor(cursor); |
|
975 | 977 | this.set_dirty(true); |
|
976 | 978 | } |
|
977 | 979 | } |
|
978 | 980 | }; |
|
979 | 981 | |
|
980 | 982 | /** |
|
981 | 983 | * Turn a cell into a Markdown cell. |
|
982 | 984 | * |
|
983 | 985 | * @method to_markdown |
|
984 | 986 | * @param {Number} [index] A cell's index |
|
985 | 987 | */ |
|
986 | 988 | Notebook.prototype.to_markdown = function (index) { |
|
987 | 989 | var i = this.index_or_selected(index); |
|
988 | 990 | if (this.is_valid_cell_index(i)) { |
|
989 | 991 | var source_element = this.get_cell_element(i); |
|
990 | 992 | var source_cell = source_element.data("cell"); |
|
991 | 993 | if (!(source_cell instanceof textcell.MarkdownCell)) { |
|
992 | 994 | var target_cell = this.insert_cell_below('markdown',i); |
|
993 | 995 | var text = source_cell.get_text(); |
|
994 | 996 | if (text === source_cell.placeholder) { |
|
995 | 997 | text = ''; |
|
996 | 998 | } |
|
997 | 999 | // We must show the editor before setting its contents |
|
998 | 1000 | target_cell.unrender(); |
|
999 | 1001 | target_cell.set_text(text); |
|
1000 | 1002 | // make this value the starting point, so that we can only undo |
|
1001 | 1003 | // to this state, instead of a blank cell |
|
1002 | 1004 | target_cell.code_mirror.clearHistory(); |
|
1003 | 1005 | source_element.remove(); |
|
1004 | 1006 | this.select(i); |
|
1005 | 1007 | if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) { |
|
1006 | 1008 | target_cell.render(); |
|
1007 | 1009 | } |
|
1008 | 1010 | var cursor = source_cell.code_mirror.getCursor(); |
|
1009 | 1011 | target_cell.code_mirror.setCursor(cursor); |
|
1010 | 1012 | this.set_dirty(true); |
|
1011 | 1013 | } |
|
1012 | 1014 | } |
|
1013 | 1015 | }; |
|
1014 | 1016 | |
|
1015 | 1017 | /** |
|
1016 | 1018 | * Turn a cell into a raw text cell. |
|
1017 | 1019 | * |
|
1018 | 1020 | * @method to_raw |
|
1019 | 1021 | * @param {Number} [index] A cell's index |
|
1020 | 1022 | */ |
|
1021 | 1023 | Notebook.prototype.to_raw = function (index) { |
|
1022 | 1024 | var i = this.index_or_selected(index); |
|
1023 | 1025 | if (this.is_valid_cell_index(i)) { |
|
1024 | 1026 | var source_element = this.get_cell_element(i); |
|
1025 | 1027 | var source_cell = source_element.data("cell"); |
|
1026 | 1028 | var target_cell = null; |
|
1027 | 1029 | if (!(source_cell instanceof textcell.RawCell)) { |
|
1028 | 1030 | target_cell = this.insert_cell_below('raw',i); |
|
1029 | 1031 | var text = source_cell.get_text(); |
|
1030 | 1032 | if (text === source_cell.placeholder) { |
|
1031 | 1033 | text = ''; |
|
1032 | 1034 | } |
|
1033 | 1035 | // We must show the editor before setting its contents |
|
1034 | 1036 | target_cell.unrender(); |
|
1035 | 1037 | target_cell.set_text(text); |
|
1036 | 1038 | // make this value the starting point, so that we can only undo |
|
1037 | 1039 | // to this state, instead of a blank cell |
|
1038 | 1040 | target_cell.code_mirror.clearHistory(); |
|
1039 | 1041 | source_element.remove(); |
|
1040 | 1042 | this.select(i); |
|
1041 | 1043 | var cursor = source_cell.code_mirror.getCursor(); |
|
1042 | 1044 | target_cell.code_mirror.setCursor(cursor); |
|
1043 | 1045 | this.set_dirty(true); |
|
1044 | 1046 | } |
|
1045 | 1047 | } |
|
1046 | 1048 | }; |
|
1047 | 1049 | |
|
1048 | 1050 | /** |
|
1049 | 1051 | * Turn a cell into a heading cell. |
|
1050 | 1052 | * |
|
1051 | 1053 | * @method to_heading |
|
1052 | 1054 | * @param {Number} [index] A cell's index |
|
1053 | 1055 | * @param {Number} [level] A heading level (e.g., 1 becomes <h1>) |
|
1054 | 1056 | */ |
|
1055 | 1057 | Notebook.prototype.to_heading = function (index, level) { |
|
1056 | 1058 | level = level || 1; |
|
1057 | 1059 | var i = this.index_or_selected(index); |
|
1058 | 1060 | if (this.is_valid_cell_index(i)) { |
|
1059 | 1061 | var source_element = this.get_cell_element(i); |
|
1060 | 1062 | var source_cell = source_element.data("cell"); |
|
1061 | 1063 | var target_cell = null; |
|
1062 | 1064 | if (source_cell instanceof textcell.HeadingCell) { |
|
1063 | 1065 | source_cell.set_level(level); |
|
1064 | 1066 | } else { |
|
1065 | 1067 | target_cell = this.insert_cell_below('heading',i); |
|
1066 | 1068 | var text = source_cell.get_text(); |
|
1067 | 1069 | if (text === source_cell.placeholder) { |
|
1068 | 1070 | text = ''; |
|
1069 | 1071 | } |
|
1070 | 1072 | // We must show the editor before setting its contents |
|
1071 | 1073 | target_cell.set_level(level); |
|
1072 | 1074 | target_cell.unrender(); |
|
1073 | 1075 | target_cell.set_text(text); |
|
1074 | 1076 | // make this value the starting point, so that we can only undo |
|
1075 | 1077 | // to this state, instead of a blank cell |
|
1076 | 1078 | target_cell.code_mirror.clearHistory(); |
|
1077 | 1079 | source_element.remove(); |
|
1078 | 1080 | this.select(i); |
|
1079 | 1081 | var cursor = source_cell.code_mirror.getCursor(); |
|
1080 | 1082 | target_cell.code_mirror.setCursor(cursor); |
|
1081 | 1083 | if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) { |
|
1082 | 1084 | target_cell.render(); |
|
1083 | 1085 | } |
|
1084 | 1086 | } |
|
1085 | 1087 | this.set_dirty(true); |
|
1086 | 1088 | this.events.trigger('selected_cell_type_changed.Notebook', |
|
1087 | 1089 | {'cell_type':'heading',level:level} |
|
1088 | 1090 | ); |
|
1089 | 1091 | } |
|
1090 | 1092 | }; |
|
1091 | 1093 | |
|
1092 | 1094 | |
|
1093 | 1095 | // Cut/Copy/Paste |
|
1094 | 1096 | |
|
1095 | 1097 | /** |
|
1096 | 1098 | * Enable UI elements for pasting cells. |
|
1097 | 1099 | * |
|
1098 | 1100 | * @method enable_paste |
|
1099 | 1101 | */ |
|
1100 | 1102 | Notebook.prototype.enable_paste = function () { |
|
1101 | 1103 | var that = this; |
|
1102 | 1104 | if (!this.paste_enabled) { |
|
1103 | 1105 | $('#paste_cell_replace').removeClass('disabled') |
|
1104 | 1106 | .on('click', function () {that.paste_cell_replace();}); |
|
1105 | 1107 | $('#paste_cell_above').removeClass('disabled') |
|
1106 | 1108 | .on('click', function () {that.paste_cell_above();}); |
|
1107 | 1109 | $('#paste_cell_below').removeClass('disabled') |
|
1108 | 1110 | .on('click', function () {that.paste_cell_below();}); |
|
1109 | 1111 | this.paste_enabled = true; |
|
1110 | 1112 | } |
|
1111 | 1113 | }; |
|
1112 | 1114 | |
|
1113 | 1115 | /** |
|
1114 | 1116 | * Disable UI elements for pasting cells. |
|
1115 | 1117 | * |
|
1116 | 1118 | * @method disable_paste |
|
1117 | 1119 | */ |
|
1118 | 1120 | Notebook.prototype.disable_paste = function () { |
|
1119 | 1121 | if (this.paste_enabled) { |
|
1120 | 1122 | $('#paste_cell_replace').addClass('disabled').off('click'); |
|
1121 | 1123 | $('#paste_cell_above').addClass('disabled').off('click'); |
|
1122 | 1124 | $('#paste_cell_below').addClass('disabled').off('click'); |
|
1123 | 1125 | this.paste_enabled = false; |
|
1124 | 1126 | } |
|
1125 | 1127 | }; |
|
1126 | 1128 | |
|
1127 | 1129 | /** |
|
1128 | 1130 | * Cut a cell. |
|
1129 | 1131 | * |
|
1130 | 1132 | * @method cut_cell |
|
1131 | 1133 | */ |
|
1132 | 1134 | Notebook.prototype.cut_cell = function () { |
|
1133 | 1135 | this.copy_cell(); |
|
1134 | 1136 | this.delete_cell(); |
|
1135 | 1137 | }; |
|
1136 | 1138 | |
|
1137 | 1139 | /** |
|
1138 | 1140 | * Copy a cell. |
|
1139 | 1141 | * |
|
1140 | 1142 | * @method copy_cell |
|
1141 | 1143 | */ |
|
1142 | 1144 | Notebook.prototype.copy_cell = function () { |
|
1143 | 1145 | var cell = this.get_selected_cell(); |
|
1144 | 1146 | this.clipboard = cell.toJSON(); |
|
1145 | 1147 | this.enable_paste(); |
|
1146 | 1148 | }; |
|
1147 | 1149 | |
|
1148 | 1150 | /** |
|
1149 | 1151 | * Replace the selected cell with a cell in the clipboard. |
|
1150 | 1152 | * |
|
1151 | 1153 | * @method paste_cell_replace |
|
1152 | 1154 | */ |
|
1153 | 1155 | Notebook.prototype.paste_cell_replace = function () { |
|
1154 | 1156 | if (this.clipboard !== null && this.paste_enabled) { |
|
1155 | 1157 | var cell_data = this.clipboard; |
|
1156 | 1158 | var new_cell = this.insert_cell_above(cell_data.cell_type); |
|
1157 | 1159 | new_cell.fromJSON(cell_data); |
|
1158 | 1160 | var old_cell = this.get_next_cell(new_cell); |
|
1159 | 1161 | this.delete_cell(this.find_cell_index(old_cell)); |
|
1160 | 1162 | this.select(this.find_cell_index(new_cell)); |
|
1161 | 1163 | } |
|
1162 | 1164 | }; |
|
1163 | 1165 | |
|
1164 | 1166 | /** |
|
1165 | 1167 | * Paste a cell from the clipboard above the selected cell. |
|
1166 | 1168 | * |
|
1167 | 1169 | * @method paste_cell_above |
|
1168 | 1170 | */ |
|
1169 | 1171 | Notebook.prototype.paste_cell_above = function () { |
|
1170 | 1172 | if (this.clipboard !== null && this.paste_enabled) { |
|
1171 | 1173 | var cell_data = this.clipboard; |
|
1172 | 1174 | var new_cell = this.insert_cell_above(cell_data.cell_type); |
|
1173 | 1175 | new_cell.fromJSON(cell_data); |
|
1174 | 1176 | new_cell.focus_cell(); |
|
1175 | 1177 | } |
|
1176 | 1178 | }; |
|
1177 | 1179 | |
|
1178 | 1180 | /** |
|
1179 | 1181 | * Paste a cell from the clipboard below the selected cell. |
|
1180 | 1182 | * |
|
1181 | 1183 | * @method paste_cell_below |
|
1182 | 1184 | */ |
|
1183 | 1185 | Notebook.prototype.paste_cell_below = function () { |
|
1184 | 1186 | if (this.clipboard !== null && this.paste_enabled) { |
|
1185 | 1187 | var cell_data = this.clipboard; |
|
1186 | 1188 | var new_cell = this.insert_cell_below(cell_data.cell_type); |
|
1187 | 1189 | new_cell.fromJSON(cell_data); |
|
1188 | 1190 | new_cell.focus_cell(); |
|
1189 | 1191 | } |
|
1190 | 1192 | }; |
|
1191 | 1193 | |
|
1192 | 1194 | // Split/merge |
|
1193 | 1195 | |
|
1194 | 1196 | /** |
|
1195 | 1197 | * Split the selected cell into two, at the cursor. |
|
1196 | 1198 | * |
|
1197 | 1199 | * @method split_cell |
|
1198 | 1200 | */ |
|
1199 | 1201 | Notebook.prototype.split_cell = function () { |
|
1200 | 1202 | var mdc = textcell.MarkdownCell; |
|
1201 | 1203 | var rc = textcell.RawCell; |
|
1202 | 1204 | var cell = this.get_selected_cell(); |
|
1203 | 1205 | if (cell.is_splittable()) { |
|
1204 | 1206 | var texta = cell.get_pre_cursor(); |
|
1205 | 1207 | var textb = cell.get_post_cursor(); |
|
1206 | 1208 | if (cell instanceof codecell.CodeCell) { |
|
1207 | 1209 | // In this case the operations keep the notebook in its existing mode |
|
1208 | 1210 | // so we don't need to do any post-op mode changes. |
|
1209 | 1211 | cell.set_text(textb); |
|
1210 | 1212 | var new_cell = this.insert_cell_above('code'); |
|
1211 | 1213 | new_cell.set_text(texta); |
|
1212 | 1214 | } else if ((cell instanceof mdc && !cell.rendered) || (cell instanceof rc)) { |
|
1213 | 1215 | // We know cell is !rendered so we can use set_text. |
|
1214 | 1216 | cell.set_text(textb); |
|
1215 | 1217 | var new_cell = this.insert_cell_above(cell.cell_type); |
|
1216 | 1218 | // Unrender the new cell so we can call set_text. |
|
1217 | 1219 | new_cell.unrender(); |
|
1218 | 1220 | new_cell.set_text(texta); |
|
1219 | 1221 | } |
|
1220 | 1222 | } |
|
1221 | 1223 | }; |
|
1222 | 1224 | |
|
1223 | 1225 | /** |
|
1224 | 1226 | * Combine the selected cell into the cell above it. |
|
1225 | 1227 | * |
|
1226 | 1228 | * @method merge_cell_above |
|
1227 | 1229 | */ |
|
1228 | 1230 | Notebook.prototype.merge_cell_above = function () { |
|
1229 | 1231 | var mdc = textcell.MarkdownCell; |
|
1230 | 1232 | var rc = textcell.RawCell; |
|
1231 | 1233 | var index = this.get_selected_index(); |
|
1232 | 1234 | var cell = this.get_cell(index); |
|
1233 | 1235 | var render = cell.rendered; |
|
1234 | 1236 | if (!cell.is_mergeable()) { |
|
1235 | 1237 | return; |
|
1236 | 1238 | } |
|
1237 | 1239 | if (index > 0) { |
|
1238 | 1240 | var upper_cell = this.get_cell(index-1); |
|
1239 | 1241 | if (!upper_cell.is_mergeable()) { |
|
1240 | 1242 | return; |
|
1241 | 1243 | } |
|
1242 | 1244 | var upper_text = upper_cell.get_text(); |
|
1243 | 1245 | var text = cell.get_text(); |
|
1244 | 1246 | if (cell instanceof codecell.CodeCell) { |
|
1245 | 1247 | cell.set_text(upper_text+'\n'+text); |
|
1246 | 1248 | } else if ((cell instanceof mdc) || (cell instanceof rc)) { |
|
1247 | 1249 | cell.unrender(); // Must unrender before we set_text. |
|
1248 | 1250 | cell.set_text(upper_text+'\n\n'+text); |
|
1249 | 1251 | if (render) { |
|
1250 | 1252 | // The rendered state of the final cell should match |
|
1251 | 1253 | // that of the original selected cell; |
|
1252 | 1254 | cell.render(); |
|
1253 | 1255 | } |
|
1254 | 1256 | } |
|
1255 | 1257 | this.delete_cell(index-1); |
|
1256 | 1258 | this.select(this.find_cell_index(cell)); |
|
1257 | 1259 | } |
|
1258 | 1260 | }; |
|
1259 | 1261 | |
|
1260 | 1262 | /** |
|
1261 | 1263 | * Combine the selected cell into the cell below it. |
|
1262 | 1264 | * |
|
1263 | 1265 | * @method merge_cell_below |
|
1264 | 1266 | */ |
|
1265 | 1267 | Notebook.prototype.merge_cell_below = function () { |
|
1266 | 1268 | var mdc = textcell.MarkdownCell; |
|
1267 | 1269 | var rc = textcell.RawCell; |
|
1268 | 1270 | var index = this.get_selected_index(); |
|
1269 | 1271 | var cell = this.get_cell(index); |
|
1270 | 1272 | var render = cell.rendered; |
|
1271 | 1273 | if (!cell.is_mergeable()) { |
|
1272 | 1274 | return; |
|
1273 | 1275 | } |
|
1274 | 1276 | if (index < this.ncells()-1) { |
|
1275 | 1277 | var lower_cell = this.get_cell(index+1); |
|
1276 | 1278 | if (!lower_cell.is_mergeable()) { |
|
1277 | 1279 | return; |
|
1278 | 1280 | } |
|
1279 | 1281 | var lower_text = lower_cell.get_text(); |
|
1280 | 1282 | var text = cell.get_text(); |
|
1281 | 1283 | if (cell instanceof codecell.CodeCell) { |
|
1282 | 1284 | cell.set_text(text+'\n'+lower_text); |
|
1283 | 1285 | } else if ((cell instanceof mdc) || (cell instanceof rc)) { |
|
1284 | 1286 | cell.unrender(); // Must unrender before we set_text. |
|
1285 | 1287 | cell.set_text(text+'\n\n'+lower_text); |
|
1286 | 1288 | if (render) { |
|
1287 | 1289 | // The rendered state of the final cell should match |
|
1288 | 1290 | // that of the original selected cell; |
|
1289 | 1291 | cell.render(); |
|
1290 | 1292 | } |
|
1291 | 1293 | } |
|
1292 | 1294 | this.delete_cell(index+1); |
|
1293 | 1295 | this.select(this.find_cell_index(cell)); |
|
1294 | 1296 | } |
|
1295 | 1297 | }; |
|
1296 | 1298 | |
|
1297 | 1299 | |
|
1298 | 1300 | // Cell collapsing and output clearing |
|
1299 | 1301 | |
|
1300 | 1302 | /** |
|
1301 | 1303 | * Hide a cell's output. |
|
1302 | 1304 | * |
|
1303 | 1305 | * @method collapse_output |
|
1304 | 1306 | * @param {Number} index A cell's numeric index |
|
1305 | 1307 | */ |
|
1306 | 1308 | Notebook.prototype.collapse_output = function (index) { |
|
1307 | 1309 | var i = this.index_or_selected(index); |
|
1308 | 1310 | var cell = this.get_cell(i); |
|
1309 | 1311 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1310 | 1312 | cell.collapse_output(); |
|
1311 | 1313 | this.set_dirty(true); |
|
1312 | 1314 | } |
|
1313 | 1315 | }; |
|
1314 | 1316 | |
|
1315 | 1317 | /** |
|
1316 | 1318 | * Hide each code cell's output area. |
|
1317 | 1319 | * |
|
1318 | 1320 | * @method collapse_all_output |
|
1319 | 1321 | */ |
|
1320 | 1322 | Notebook.prototype.collapse_all_output = function () { |
|
1321 | 1323 | $.map(this.get_cells(), function (cell, i) { |
|
1322 | 1324 | if (cell instanceof codecell.CodeCell) { |
|
1323 | 1325 | cell.collapse_output(); |
|
1324 | 1326 | } |
|
1325 | 1327 | }); |
|
1326 | 1328 | // this should not be set if the `collapse` key is removed from nbformat |
|
1327 | 1329 | this.set_dirty(true); |
|
1328 | 1330 | }; |
|
1329 | 1331 | |
|
1330 | 1332 | /** |
|
1331 | 1333 | * Show a cell's output. |
|
1332 | 1334 | * |
|
1333 | 1335 | * @method expand_output |
|
1334 | 1336 | * @param {Number} index A cell's numeric index |
|
1335 | 1337 | */ |
|
1336 | 1338 | Notebook.prototype.expand_output = function (index) { |
|
1337 | 1339 | var i = this.index_or_selected(index); |
|
1338 | 1340 | var cell = this.get_cell(i); |
|
1339 | 1341 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1340 | 1342 | cell.expand_output(); |
|
1341 | 1343 | this.set_dirty(true); |
|
1342 | 1344 | } |
|
1343 | 1345 | }; |
|
1344 | 1346 | |
|
1345 | 1347 | /** |
|
1346 | 1348 | * Expand each code cell's output area, and remove scrollbars. |
|
1347 | 1349 | * |
|
1348 | 1350 | * @method expand_all_output |
|
1349 | 1351 | */ |
|
1350 | 1352 | Notebook.prototype.expand_all_output = function () { |
|
1351 | 1353 | $.map(this.get_cells(), function (cell, i) { |
|
1352 | 1354 | if (cell instanceof codecell.CodeCell) { |
|
1353 | 1355 | cell.expand_output(); |
|
1354 | 1356 | } |
|
1355 | 1357 | }); |
|
1356 | 1358 | // this should not be set if the `collapse` key is removed from nbformat |
|
1357 | 1359 | this.set_dirty(true); |
|
1358 | 1360 | }; |
|
1359 | 1361 | |
|
1360 | 1362 | /** |
|
1361 | 1363 | * Clear the selected CodeCell's output area. |
|
1362 | 1364 | * |
|
1363 | 1365 | * @method clear_output |
|
1364 | 1366 | * @param {Number} index A cell's numeric index |
|
1365 | 1367 | */ |
|
1366 | 1368 | Notebook.prototype.clear_output = function (index) { |
|
1367 | 1369 | var i = this.index_or_selected(index); |
|
1368 | 1370 | var cell = this.get_cell(i); |
|
1369 | 1371 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1370 | 1372 | cell.clear_output(); |
|
1371 | 1373 | this.set_dirty(true); |
|
1372 | 1374 | } |
|
1373 | 1375 | }; |
|
1374 | 1376 | |
|
1375 | 1377 | /** |
|
1376 | 1378 | * Clear each code cell's output area. |
|
1377 | 1379 | * |
|
1378 | 1380 | * @method clear_all_output |
|
1379 | 1381 | */ |
|
1380 | 1382 | Notebook.prototype.clear_all_output = function () { |
|
1381 | 1383 | $.map(this.get_cells(), function (cell, i) { |
|
1382 | 1384 | if (cell instanceof codecell.CodeCell) { |
|
1383 | 1385 | cell.clear_output(); |
|
1384 | 1386 | } |
|
1385 | 1387 | }); |
|
1386 | 1388 | this.set_dirty(true); |
|
1387 | 1389 | }; |
|
1388 | 1390 | |
|
1389 | 1391 | /** |
|
1390 | 1392 | * Scroll the selected CodeCell's output area. |
|
1391 | 1393 | * |
|
1392 | 1394 | * @method scroll_output |
|
1393 | 1395 | * @param {Number} index A cell's numeric index |
|
1394 | 1396 | */ |
|
1395 | 1397 | Notebook.prototype.scroll_output = function (index) { |
|
1396 | 1398 | var i = this.index_or_selected(index); |
|
1397 | 1399 | var cell = this.get_cell(i); |
|
1398 | 1400 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1399 | 1401 | cell.scroll_output(); |
|
1400 | 1402 | this.set_dirty(true); |
|
1401 | 1403 | } |
|
1402 | 1404 | }; |
|
1403 | 1405 | |
|
1404 | 1406 | /** |
|
1405 | 1407 | * Expand each code cell's output area, and add a scrollbar for long output. |
|
1406 | 1408 | * |
|
1407 | 1409 | * @method scroll_all_output |
|
1408 | 1410 | */ |
|
1409 | 1411 | Notebook.prototype.scroll_all_output = function () { |
|
1410 | 1412 | $.map(this.get_cells(), function (cell, i) { |
|
1411 | 1413 | if (cell instanceof codecell.CodeCell) { |
|
1412 | 1414 | cell.scroll_output(); |
|
1413 | 1415 | } |
|
1414 | 1416 | }); |
|
1415 | 1417 | // this should not be set if the `collapse` key is removed from nbformat |
|
1416 | 1418 | this.set_dirty(true); |
|
1417 | 1419 | }; |
|
1418 | 1420 | |
|
1419 | 1421 | /** Toggle whether a cell's output is collapsed or expanded. |
|
1420 | 1422 | * |
|
1421 | 1423 | * @method toggle_output |
|
1422 | 1424 | * @param {Number} index A cell's numeric index |
|
1423 | 1425 | */ |
|
1424 | 1426 | Notebook.prototype.toggle_output = function (index) { |
|
1425 | 1427 | var i = this.index_or_selected(index); |
|
1426 | 1428 | var cell = this.get_cell(i); |
|
1427 | 1429 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1428 | 1430 | cell.toggle_output(); |
|
1429 | 1431 | this.set_dirty(true); |
|
1430 | 1432 | } |
|
1431 | 1433 | }; |
|
1432 | 1434 | |
|
1433 | 1435 | /** |
|
1434 | 1436 | * Hide/show the output of all cells. |
|
1435 | 1437 | * |
|
1436 | 1438 | * @method toggle_all_output |
|
1437 | 1439 | */ |
|
1438 | 1440 | Notebook.prototype.toggle_all_output = function () { |
|
1439 | 1441 | $.map(this.get_cells(), function (cell, i) { |
|
1440 | 1442 | if (cell instanceof codecell.CodeCell) { |
|
1441 | 1443 | cell.toggle_output(); |
|
1442 | 1444 | } |
|
1443 | 1445 | }); |
|
1444 | 1446 | // this should not be set if the `collapse` key is removed from nbformat |
|
1445 | 1447 | this.set_dirty(true); |
|
1446 | 1448 | }; |
|
1447 | 1449 | |
|
1448 | 1450 | /** |
|
1449 | 1451 | * Toggle a scrollbar for long cell outputs. |
|
1450 | 1452 | * |
|
1451 | 1453 | * @method toggle_output_scroll |
|
1452 | 1454 | * @param {Number} index A cell's numeric index |
|
1453 | 1455 | */ |
|
1454 | 1456 | Notebook.prototype.toggle_output_scroll = function (index) { |
|
1455 | 1457 | var i = this.index_or_selected(index); |
|
1456 | 1458 | var cell = this.get_cell(i); |
|
1457 | 1459 | if (cell !== null && (cell instanceof codecell.CodeCell)) { |
|
1458 | 1460 | cell.toggle_output_scroll(); |
|
1459 | 1461 | this.set_dirty(true); |
|
1460 | 1462 | } |
|
1461 | 1463 | }; |
|
1462 | 1464 | |
|
1463 | 1465 | /** |
|
1464 | 1466 | * Toggle the scrolling of long output on all cells. |
|
1465 | 1467 | * |
|
1466 | 1468 | * @method toggle_all_output_scrolling |
|
1467 | 1469 | */ |
|
1468 | 1470 | Notebook.prototype.toggle_all_output_scroll = function () { |
|
1469 | 1471 | $.map(this.get_cells(), function (cell, i) { |
|
1470 | 1472 | if (cell instanceof codecell.CodeCell) { |
|
1471 | 1473 | cell.toggle_output_scroll(); |
|
1472 | 1474 | } |
|
1473 | 1475 | }); |
|
1474 | 1476 | // this should not be set if the `collapse` key is removed from nbformat |
|
1475 | 1477 | this.set_dirty(true); |
|
1476 | 1478 | }; |
|
1477 | 1479 | |
|
1478 | 1480 | // Other cell functions: line numbers, ... |
|
1479 | 1481 | |
|
1480 | 1482 | /** |
|
1481 | 1483 | * Toggle line numbers in the selected cell's input area. |
|
1482 | 1484 | * |
|
1483 | 1485 | * @method cell_toggle_line_numbers |
|
1484 | 1486 | */ |
|
1485 | 1487 | Notebook.prototype.cell_toggle_line_numbers = function() { |
|
1486 | 1488 | this.get_selected_cell().toggle_line_numbers(); |
|
1487 | 1489 | }; |
|
1488 | 1490 | |
|
1489 | 1491 | // Session related things |
|
1490 | 1492 | |
|
1491 | 1493 | /** |
|
1492 | 1494 | * Start a new session and set it on each code cell. |
|
1493 | 1495 | * |
|
1494 | 1496 | * @method start_session |
|
1495 | 1497 | */ |
|
1496 | 1498 | Notebook.prototype.start_session = function () { |
|
1497 | 1499 | this.session = new session.Session({ |
|
1498 | 1500 | base_url: this.base_url, |
|
1499 | 1501 | notebook_path: this.notebook_path, |
|
1500 | 1502 | notebook_name: this.notebook_name, |
|
1501 | 1503 | // For now, create all sessions with the 'python' kernel, which is the |
|
1502 | 1504 | // default. Later, the user will be able to select kernels. This is |
|
1503 | 1505 | // overridden if KernelManager.kernel_cmd is specified for the server. |
|
1504 | 1506 | kernel_name: this.default_kernel_name, |
|
1505 | 1507 | notebook: this}); |
|
1506 | 1508 | |
|
1507 | 1509 | this.session.start($.proxy(this._session_started, this)); |
|
1508 | 1510 | }; |
|
1509 | 1511 | |
|
1510 | 1512 | |
|
1511 | 1513 | /** |
|
1512 | 1514 | * Once a session is started, link the code cells to the kernel and pass the |
|
1513 | 1515 | * comm manager to the widget manager |
|
1514 | 1516 | * |
|
1515 | 1517 | */ |
|
1516 | 1518 | Notebook.prototype._session_started = function(){ |
|
1517 | 1519 | this.kernel = this.session.kernel; |
|
1518 | 1520 | var ncells = this.ncells(); |
|
1519 | 1521 | for (var i=0; i<ncells; i++) { |
|
1520 | 1522 | var cell = this.get_cell(i); |
|
1521 | 1523 | if (cell instanceof codecell.CodeCell) { |
|
1522 | 1524 | cell.set_kernel(this.session.kernel); |
|
1523 | 1525 | } |
|
1524 | 1526 | } |
|
1525 | 1527 | }; |
|
1526 | 1528 | |
|
1527 | 1529 | /** |
|
1528 | 1530 | * Prompt the user to restart the IPython kernel. |
|
1529 | 1531 | * |
|
1530 | 1532 | * @method restart_kernel |
|
1531 | 1533 | */ |
|
1532 | 1534 | Notebook.prototype.restart_kernel = function () { |
|
1533 | 1535 | var that = this; |
|
1534 | 1536 | dialog.modal({ |
|
1535 | 1537 | notebook: this, |
|
1536 | 1538 | keyboard_manager: this.keyboard_manager, |
|
1537 | 1539 | title : "Restart kernel or continue running?", |
|
1538 | 1540 | body : $("<p/>").text( |
|
1539 | 1541 | 'Do you want to restart the current kernel? You will lose all variables defined in it.' |
|
1540 | 1542 | ), |
|
1541 | 1543 | buttons : { |
|
1542 | 1544 | "Continue running" : {}, |
|
1543 | 1545 | "Restart" : { |
|
1544 | 1546 | "class" : "btn-danger", |
|
1545 | 1547 | "click" : function() { |
|
1546 | 1548 | that.session.restart_kernel(); |
|
1547 | 1549 | } |
|
1548 | 1550 | } |
|
1549 | 1551 | } |
|
1550 | 1552 | }); |
|
1551 | 1553 | }; |
|
1552 | 1554 | |
|
1553 | 1555 | /** |
|
1554 | 1556 | * Execute or render cell outputs and go into command mode. |
|
1555 | 1557 | * |
|
1556 | 1558 | * @method execute_cell |
|
1557 | 1559 | */ |
|
1558 | 1560 | Notebook.prototype.execute_cell = function () { |
|
1559 | 1561 | // mode = shift, ctrl, alt |
|
1560 | 1562 | var cell = this.get_selected_cell(); |
|
1561 | 1563 | var cell_index = this.find_cell_index(cell); |
|
1562 | 1564 | |
|
1563 | 1565 | cell.execute(); |
|
1564 | 1566 | this.command_mode(); |
|
1565 | 1567 | this.set_dirty(true); |
|
1566 | 1568 | }; |
|
1567 | 1569 | |
|
1568 | 1570 | /** |
|
1569 | 1571 | * Execute or render cell outputs and insert a new cell below. |
|
1570 | 1572 | * |
|
1571 | 1573 | * @method execute_cell_and_insert_below |
|
1572 | 1574 | */ |
|
1573 | 1575 | Notebook.prototype.execute_cell_and_insert_below = function () { |
|
1574 | 1576 | var cell = this.get_selected_cell(); |
|
1575 | 1577 | var cell_index = this.find_cell_index(cell); |
|
1576 | 1578 | |
|
1577 | 1579 | cell.execute(); |
|
1578 | 1580 | |
|
1579 | 1581 | // If we are at the end always insert a new cell and return |
|
1580 | 1582 | if (cell_index === (this.ncells()-1)) { |
|
1581 | 1583 | this.command_mode(); |
|
1582 | 1584 | this.insert_cell_below(); |
|
1583 | 1585 | this.select(cell_index+1); |
|
1584 | 1586 | this.edit_mode(); |
|
1585 | 1587 | this.scroll_to_bottom(); |
|
1586 | 1588 | this.set_dirty(true); |
|
1587 | 1589 | return; |
|
1588 | 1590 | } |
|
1589 | 1591 | |
|
1590 | 1592 | this.command_mode(); |
|
1591 | 1593 | this.insert_cell_below(); |
|
1592 | 1594 | this.select(cell_index+1); |
|
1593 | 1595 | this.edit_mode(); |
|
1594 | 1596 | this.set_dirty(true); |
|
1595 | 1597 | }; |
|
1596 | 1598 | |
|
1597 | 1599 | /** |
|
1598 | 1600 | * Execute or render cell outputs and select the next cell. |
|
1599 | 1601 | * |
|
1600 | 1602 | * @method execute_cell_and_select_below |
|
1601 | 1603 | */ |
|
1602 | 1604 | Notebook.prototype.execute_cell_and_select_below = function () { |
|
1603 | 1605 | |
|
1604 | 1606 | var cell = this.get_selected_cell(); |
|
1605 | 1607 | var cell_index = this.find_cell_index(cell); |
|
1606 | 1608 | |
|
1607 | 1609 | cell.execute(); |
|
1608 | 1610 | |
|
1609 | 1611 | // If we are at the end always insert a new cell and return |
|
1610 | 1612 | if (cell_index === (this.ncells()-1)) { |
|
1611 | 1613 | this.command_mode(); |
|
1612 | 1614 | this.insert_cell_below(); |
|
1613 | 1615 | this.select(cell_index+1); |
|
1614 | 1616 | this.edit_mode(); |
|
1615 | 1617 | this.scroll_to_bottom(); |
|
1616 | 1618 | this.set_dirty(true); |
|
1617 | 1619 | return; |
|
1618 | 1620 | } |
|
1619 | 1621 | |
|
1620 | 1622 | this.command_mode(); |
|
1621 | 1623 | this.select(cell_index+1); |
|
1622 | 1624 | this.focus_cell(); |
|
1623 | 1625 | this.set_dirty(true); |
|
1624 | 1626 | }; |
|
1625 | 1627 | |
|
1626 | 1628 | /** |
|
1627 | 1629 | * Execute all cells below the selected cell. |
|
1628 | 1630 | * |
|
1629 | 1631 | * @method execute_cells_below |
|
1630 | 1632 | */ |
|
1631 | 1633 | Notebook.prototype.execute_cells_below = function () { |
|
1632 | 1634 | this.execute_cell_range(this.get_selected_index(), this.ncells()); |
|
1633 | 1635 | this.scroll_to_bottom(); |
|
1634 | 1636 | }; |
|
1635 | 1637 | |
|
1636 | 1638 | /** |
|
1637 | 1639 | * Execute all cells above the selected cell. |
|
1638 | 1640 | * |
|
1639 | 1641 | * @method execute_cells_above |
|
1640 | 1642 | */ |
|
1641 | 1643 | Notebook.prototype.execute_cells_above = function () { |
|
1642 | 1644 | this.execute_cell_range(0, this.get_selected_index()); |
|
1643 | 1645 | }; |
|
1644 | 1646 | |
|
1645 | 1647 | /** |
|
1646 | 1648 | * Execute all cells. |
|
1647 | 1649 | * |
|
1648 | 1650 | * @method execute_all_cells |
|
1649 | 1651 | */ |
|
1650 | 1652 | Notebook.prototype.execute_all_cells = function () { |
|
1651 | 1653 | this.execute_cell_range(0, this.ncells()); |
|
1652 | 1654 | this.scroll_to_bottom(); |
|
1653 | 1655 | }; |
|
1654 | 1656 | |
|
1655 | 1657 | /** |
|
1656 | 1658 | * Execute a contiguous range of cells. |
|
1657 | 1659 | * |
|
1658 | 1660 | * @method execute_cell_range |
|
1659 | 1661 | * @param {Number} start Index of the first cell to execute (inclusive) |
|
1660 | 1662 | * @param {Number} end Index of the last cell to execute (exclusive) |
|
1661 | 1663 | */ |
|
1662 | 1664 | Notebook.prototype.execute_cell_range = function (start, end) { |
|
1663 | 1665 | this.command_mode(); |
|
1664 | 1666 | for (var i=start; i<end; i++) { |
|
1665 | 1667 | this.select(i); |
|
1666 | 1668 | this.execute_cell(); |
|
1667 | 1669 | } |
|
1668 | 1670 | }; |
|
1669 | 1671 | |
|
1670 | 1672 | // Persistance and loading |
|
1671 | 1673 | |
|
1672 | 1674 | /** |
|
1673 | 1675 | * Getter method for this notebook's name. |
|
1674 | 1676 | * |
|
1675 | 1677 | * @method get_notebook_name |
|
1676 | 1678 | * @return {String} This notebook's name (excluding file extension) |
|
1677 | 1679 | */ |
|
1678 | 1680 | Notebook.prototype.get_notebook_name = function () { |
|
1679 | 1681 | var nbname = this.notebook_name.substring(0,this.notebook_name.length-6); |
|
1680 | 1682 | return nbname; |
|
1681 | 1683 | }; |
|
1682 | 1684 | |
|
1683 | 1685 | /** |
|
1684 | 1686 | * Setter method for this notebook's name. |
|
1685 | 1687 | * |
|
1686 | 1688 | * @method set_notebook_name |
|
1687 | 1689 | * @param {String} name A new name for this notebook |
|
1688 | 1690 | */ |
|
1689 | 1691 | Notebook.prototype.set_notebook_name = function (name) { |
|
1690 | 1692 | this.notebook_name = name; |
|
1691 | 1693 | }; |
|
1692 | 1694 | |
|
1693 | 1695 | /** |
|
1694 | 1696 | * Check that a notebook's name is valid. |
|
1695 | 1697 | * |
|
1696 | 1698 | * @method test_notebook_name |
|
1697 | 1699 | * @param {String} nbname A name for this notebook |
|
1698 | 1700 | * @return {Boolean} True if the name is valid, false if invalid |
|
1699 | 1701 | */ |
|
1700 | 1702 | Notebook.prototype.test_notebook_name = function (nbname) { |
|
1701 | 1703 | nbname = nbname || ''; |
|
1702 | 1704 | if (nbname.length>0 && !this.notebook_name_blacklist_re.test(nbname)) { |
|
1703 | 1705 | return true; |
|
1704 | 1706 | } else { |
|
1705 | 1707 | return false; |
|
1706 | 1708 | } |
|
1707 | 1709 | }; |
|
1708 | 1710 | |
|
1709 | 1711 | /** |
|
1710 | 1712 | * Load a notebook from JSON (.ipynb). |
|
1711 | 1713 | * |
|
1712 | 1714 | * This currently handles one worksheet: others are deleted. |
|
1713 | 1715 | * |
|
1714 | 1716 | * @method fromJSON |
|
1715 | 1717 | * @param {Object} data JSON representation of a notebook |
|
1716 | 1718 | */ |
|
1717 | 1719 | Notebook.prototype.fromJSON = function (data) { |
|
1718 | 1720 | var content = data.content; |
|
1719 | 1721 | var ncells = this.ncells(); |
|
1720 | 1722 | var i; |
|
1721 | 1723 | for (i=0; i<ncells; i++) { |
|
1722 | 1724 | // Always delete cell 0 as they get renumbered as they are deleted. |
|
1723 | 1725 | this.delete_cell(0); |
|
1724 | 1726 | } |
|
1725 | 1727 | // Save the metadata and name. |
|
1726 | 1728 | this.metadata = content.metadata; |
|
1727 | 1729 | this.notebook_name = data.name; |
|
1728 | 1730 | var trusted = true; |
|
1729 | 1731 | // Only handle 1 worksheet for now. |
|
1730 | 1732 | var worksheet = content.worksheets[0]; |
|
1731 | 1733 | if (worksheet !== undefined) { |
|
1732 | 1734 | if (worksheet.metadata) { |
|
1733 | 1735 | this.worksheet_metadata = worksheet.metadata; |
|
1734 | 1736 | } |
|
1735 | 1737 | var new_cells = worksheet.cells; |
|
1736 | 1738 | ncells = new_cells.length; |
|
1737 | 1739 | var cell_data = null; |
|
1738 | 1740 | var new_cell = null; |
|
1739 | 1741 | for (i=0; i<ncells; i++) { |
|
1740 | 1742 | cell_data = new_cells[i]; |
|
1741 | 1743 | // VERSIONHACK: plaintext -> raw |
|
1742 | 1744 | // handle never-released plaintext name for raw cells |
|
1743 | 1745 | if (cell_data.cell_type === 'plaintext'){ |
|
1744 | 1746 | cell_data.cell_type = 'raw'; |
|
1745 | 1747 | } |
|
1746 | 1748 | |
|
1747 | 1749 | new_cell = this.insert_cell_at_index(cell_data.cell_type, i); |
|
1748 | 1750 | new_cell.fromJSON(cell_data); |
|
1749 | 1751 | if (new_cell.cell_type == 'code' && !new_cell.output_area.trusted) { |
|
1750 | 1752 | trusted = false; |
|
1751 | 1753 | } |
|
1752 | 1754 | } |
|
1753 | 1755 | } |
|
1754 | 1756 | if (trusted != this.trusted) { |
|
1755 | 1757 | this.trusted = trusted; |
|
1756 | 1758 | this.events.trigger("trust_changed.Notebook", trusted); |
|
1757 | 1759 | } |
|
1758 | 1760 | if (content.worksheets.length > 1) { |
|
1759 | 1761 | dialog.modal({ |
|
1760 | 1762 | notebook: this, |
|
1761 | 1763 | keyboard_manager: this.keyboard_manager, |
|
1762 | 1764 | title : "Multiple worksheets", |
|
1763 | 1765 | body : "This notebook has " + data.worksheets.length + " worksheets, " + |
|
1764 | 1766 | "but this version of IPython can only handle the first. " + |
|
1765 | 1767 | "If you save this notebook, worksheets after the first will be lost.", |
|
1766 | 1768 | buttons : { |
|
1767 | 1769 | OK : { |
|
1768 | 1770 | class : "btn-danger" |
|
1769 | 1771 | } |
|
1770 | 1772 | } |
|
1771 | 1773 | }); |
|
1772 | 1774 | } |
|
1773 | 1775 | }; |
|
1774 | 1776 | |
|
1775 | 1777 | /** |
|
1776 | 1778 | * Dump this notebook into a JSON-friendly object. |
|
1777 | 1779 | * |
|
1778 | 1780 | * @method toJSON |
|
1779 | 1781 | * @return {Object} A JSON-friendly representation of this notebook. |
|
1780 | 1782 | */ |
|
1781 | 1783 | Notebook.prototype.toJSON = function () { |
|
1782 | 1784 | var cells = this.get_cells(); |
|
1783 | 1785 | var ncells = cells.length; |
|
1784 | 1786 | var cell_array = new Array(ncells); |
|
1785 | 1787 | var trusted = true; |
|
1786 | 1788 | for (var i=0; i<ncells; i++) { |
|
1787 | 1789 | var cell = cells[i]; |
|
1788 | 1790 | if (cell.cell_type == 'code' && !cell.output_area.trusted) { |
|
1789 | 1791 | trusted = false; |
|
1790 | 1792 | } |
|
1791 | 1793 | cell_array[i] = cell.toJSON(); |
|
1792 | 1794 | } |
|
1793 | 1795 | var data = { |
|
1794 | 1796 | // Only handle 1 worksheet for now. |
|
1795 | 1797 | worksheets : [{ |
|
1796 | 1798 | cells: cell_array, |
|
1797 | 1799 | metadata: this.worksheet_metadata |
|
1798 | 1800 | }], |
|
1799 | 1801 | metadata : this.metadata |
|
1800 | 1802 | }; |
|
1801 | 1803 | if (trusted != this.trusted) { |
|
1802 | 1804 | this.trusted = trusted; |
|
1803 | 1805 | this.events.trigger("trust_changed.Notebook", trusted); |
|
1804 | 1806 | } |
|
1805 | 1807 | return data; |
|
1806 | 1808 | }; |
|
1807 | 1809 | |
|
1808 | 1810 | /** |
|
1809 | 1811 | * Start an autosave timer, for periodically saving the notebook. |
|
1810 | 1812 | * |
|
1811 | 1813 | * @method set_autosave_interval |
|
1812 | 1814 | * @param {Integer} interval the autosave interval in milliseconds |
|
1813 | 1815 | */ |
|
1814 | 1816 | Notebook.prototype.set_autosave_interval = function (interval) { |
|
1815 | 1817 | var that = this; |
|
1816 | 1818 | // clear previous interval, so we don't get simultaneous timers |
|
1817 | 1819 | if (this.autosave_timer) { |
|
1818 | 1820 | clearInterval(this.autosave_timer); |
|
1819 | 1821 | } |
|
1820 | 1822 | |
|
1821 | 1823 | this.autosave_interval = this.minimum_autosave_interval = interval; |
|
1822 | 1824 | if (interval) { |
|
1823 | 1825 | this.autosave_timer = setInterval(function() { |
|
1824 | 1826 | if (that.dirty) { |
|
1825 | 1827 | that.save_notebook(); |
|
1826 | 1828 | } |
|
1827 | 1829 | }, interval); |
|
1828 | 1830 | this.events.trigger("autosave_enabled.Notebook", interval); |
|
1829 | 1831 | } else { |
|
1830 | 1832 | this.autosave_timer = null; |
|
1831 | 1833 | this.events.trigger("autosave_disabled.Notebook"); |
|
1832 | 1834 | } |
|
1833 | 1835 | }; |
|
1834 | 1836 | |
|
1835 | 1837 | /** |
|
1836 | 1838 | * Save this notebook on the server. This becomes a notebook instance's |
|
1837 | 1839 | * .save_notebook method *after* the entire notebook has been loaded. |
|
1838 | 1840 | * |
|
1839 | 1841 | * @method save_notebook |
|
1840 | 1842 | */ |
|
1841 | 1843 | Notebook.prototype.save_notebook = function (extra_settings) { |
|
1842 | 1844 | // Create a JSON model to be sent to the server. |
|
1843 | 1845 | var model = {}; |
|
1844 | 1846 | model.name = this.notebook_name; |
|
1845 | 1847 | model.path = this.notebook_path; |
|
1846 | 1848 | model.content = this.toJSON(); |
|
1847 | 1849 | model.content.nbformat = this.nbformat; |
|
1848 | 1850 | model.content.nbformat_minor = this.nbformat_minor; |
|
1849 | 1851 | // time the ajax call for autosave tuning purposes. |
|
1850 | 1852 | var start = new Date().getTime(); |
|
1851 | 1853 | // We do the call with settings so we can set cache to false. |
|
1852 | 1854 | var settings = { |
|
1853 | 1855 | processData : false, |
|
1854 | 1856 | cache : false, |
|
1855 | 1857 | type : "PUT", |
|
1856 | 1858 | data : JSON.stringify(model), |
|
1857 | 1859 | headers : {'Content-Type': 'application/json'}, |
|
1858 | 1860 | success : $.proxy(this.save_notebook_success, this, start), |
|
1859 | 1861 | error : $.proxy(this.save_notebook_error, this) |
|
1860 | 1862 | }; |
|
1861 | 1863 | if (extra_settings) { |
|
1862 | 1864 | for (var key in extra_settings) { |
|
1863 | 1865 | settings[key] = extra_settings[key]; |
|
1864 | 1866 | } |
|
1865 | 1867 | } |
|
1866 | 1868 | this.events.trigger('notebook_saving.Notebook'); |
|
1867 | 1869 | var url = utils.url_join_encode( |
|
1868 | 1870 | this.base_url, |
|
1869 | 1871 | 'api/notebooks', |
|
1870 | 1872 | this.notebook_path, |
|
1871 | 1873 | this.notebook_name |
|
1872 | 1874 | ); |
|
1873 | 1875 | $.ajax(url, settings); |
|
1874 | 1876 | }; |
|
1875 | 1877 | |
|
1876 | 1878 | /** |
|
1877 | 1879 | * Success callback for saving a notebook. |
|
1878 | 1880 | * |
|
1879 | 1881 | * @method save_notebook_success |
|
1880 | 1882 | * @param {Integer} start the time when the save request started |
|
1881 | 1883 | * @param {Object} data JSON representation of a notebook |
|
1882 | 1884 | * @param {String} status Description of response status |
|
1883 | 1885 | * @param {jqXHR} xhr jQuery Ajax object |
|
1884 | 1886 | */ |
|
1885 | 1887 | Notebook.prototype.save_notebook_success = function (start, data, status, xhr) { |
|
1886 | 1888 | this.set_dirty(false); |
|
1887 | 1889 | this.events.trigger('notebook_saved.Notebook'); |
|
1888 | 1890 | this._update_autosave_interval(start); |
|
1889 | 1891 | if (this._checkpoint_after_save) { |
|
1890 | 1892 | this.create_checkpoint(); |
|
1891 | 1893 | this._checkpoint_after_save = false; |
|
1892 | 1894 | } |
|
1893 | 1895 | }; |
|
1894 | 1896 | |
|
1895 | 1897 | /** |
|
1896 | 1898 | * update the autosave interval based on how long the last save took |
|
1897 | 1899 | * |
|
1898 | 1900 | * @method _update_autosave_interval |
|
1899 | 1901 | * @param {Integer} timestamp when the save request started |
|
1900 | 1902 | */ |
|
1901 | 1903 | Notebook.prototype._update_autosave_interval = function (start) { |
|
1902 | 1904 | var duration = (new Date().getTime() - start); |
|
1903 | 1905 | if (this.autosave_interval) { |
|
1904 | 1906 | // new save interval: higher of 10x save duration or parameter (default 30 seconds) |
|
1905 | 1907 | var interval = Math.max(10 * duration, this.minimum_autosave_interval); |
|
1906 | 1908 | // round to 10 seconds, otherwise we will be setting a new interval too often |
|
1907 | 1909 | interval = 10000 * Math.round(interval / 10000); |
|
1908 | 1910 | // set new interval, if it's changed |
|
1909 | 1911 | if (interval != this.autosave_interval) { |
|
1910 | 1912 | this.set_autosave_interval(interval); |
|
1911 | 1913 | } |
|
1912 | 1914 | } |
|
1913 | 1915 | }; |
|
1914 | 1916 | |
|
1915 | 1917 | /** |
|
1916 | 1918 | * Failure callback for saving a notebook. |
|
1917 | 1919 | * |
|
1918 | 1920 | * @method save_notebook_error |
|
1919 | 1921 | * @param {jqXHR} xhr jQuery Ajax object |
|
1920 | 1922 | * @param {String} status Description of response status |
|
1921 | 1923 | * @param {String} error HTTP error message |
|
1922 | 1924 | */ |
|
1923 | 1925 | Notebook.prototype.save_notebook_error = function (xhr, status, error) { |
|
1924 | 1926 | this.events.trigger('notebook_save_failed.Notebook', [xhr, status, error]); |
|
1925 | 1927 | }; |
|
1926 | 1928 | |
|
1927 | 1929 | /** |
|
1928 | 1930 | * Explicitly trust the output of this notebook. |
|
1929 | 1931 | * |
|
1930 | 1932 | * @method trust_notebook |
|
1931 | 1933 | */ |
|
1932 | 1934 | Notebook.prototype.trust_notebook = function (extra_settings) { |
|
1933 | 1935 | var body = $("<div>").append($("<p>") |
|
1934 | 1936 | .text("A trusted IPython notebook may execute hidden malicious code ") |
|
1935 | 1937 | .append($("<strong>") |
|
1936 | 1938 | .append( |
|
1937 | 1939 | $("<em>").text("when you open it") |
|
1938 | 1940 | ) |
|
1939 | 1941 | ).append(".").append( |
|
1940 | 1942 | " Selecting trust will immediately reload this notebook in a trusted state." |
|
1941 | 1943 | ).append( |
|
1942 | 1944 | " For more information, see the " |
|
1943 | 1945 | ).append($("<a>").attr("href", "http://ipython.org/ipython-doc/2/notebook/security.html") |
|
1944 | 1946 | .text("IPython security documentation") |
|
1945 | 1947 | ).append(".") |
|
1946 | 1948 | ); |
|
1947 | 1949 | |
|
1948 | 1950 | var nb = this; |
|
1949 | 1951 | dialog.modal({ |
|
1950 | 1952 | notebook: this, |
|
1951 | 1953 | keyboard_manager: this.keyboard_manager, |
|
1952 | 1954 | title: "Trust this notebook?", |
|
1953 | 1955 | body: body, |
|
1954 | 1956 | |
|
1955 | 1957 | buttons: { |
|
1956 | 1958 | Cancel : {}, |
|
1957 | 1959 | Trust : { |
|
1958 | 1960 | class : "btn-danger", |
|
1959 | 1961 | click : function () { |
|
1960 | 1962 | var cells = nb.get_cells(); |
|
1961 | 1963 | for (var i = 0; i < cells.length; i++) { |
|
1962 | 1964 | var cell = cells[i]; |
|
1963 | 1965 | if (cell.cell_type == 'code') { |
|
1964 | 1966 | cell.output_area.trusted = true; |
|
1965 | 1967 | } |
|
1966 | 1968 | } |
|
1967 | 1969 | this.events.on('notebook_saved.Notebook', function () { |
|
1968 | 1970 | window.location.reload(); |
|
1969 | 1971 | }); |
|
1970 | 1972 | nb.save_notebook(); |
|
1971 | 1973 | } |
|
1972 | 1974 | } |
|
1973 | 1975 | } |
|
1974 | 1976 | }); |
|
1975 | 1977 | }; |
|
1976 | 1978 | |
|
1977 | 1979 | Notebook.prototype.new_notebook = function(){ |
|
1978 | 1980 | var path = this.notebook_path; |
|
1979 | 1981 | var base_url = this.base_url; |
|
1980 | 1982 | var settings = { |
|
1981 | 1983 | processData : false, |
|
1982 | 1984 | cache : false, |
|
1983 | 1985 | type : "POST", |
|
1984 | 1986 | dataType : "json", |
|
1985 | 1987 | async : false, |
|
1986 | 1988 | success : function (data, status, xhr){ |
|
1987 | 1989 | var notebook_name = data.name; |
|
1988 | 1990 | window.open( |
|
1989 | 1991 | utils.url_join_encode( |
|
1990 | 1992 | base_url, |
|
1991 | 1993 | 'notebooks', |
|
1992 | 1994 | path, |
|
1993 | 1995 | notebook_name |
|
1994 | 1996 | ), |
|
1995 | 1997 | '_blank' |
|
1996 | 1998 | ); |
|
1997 | 1999 | }, |
|
1998 | 2000 | error : utils.log_ajax_error, |
|
1999 | 2001 | }; |
|
2000 | 2002 | var url = utils.url_join_encode( |
|
2001 | 2003 | base_url, |
|
2002 | 2004 | 'api/notebooks', |
|
2003 | 2005 | path |
|
2004 | 2006 | ); |
|
2005 | 2007 | $.ajax(url,settings); |
|
2006 | 2008 | }; |
|
2007 | 2009 | |
|
2008 | 2010 | |
|
2009 | 2011 | Notebook.prototype.copy_notebook = function(){ |
|
2010 | 2012 | var path = this.notebook_path; |
|
2011 | 2013 | var base_url = this.base_url; |
|
2012 | 2014 | var settings = { |
|
2013 | 2015 | processData : false, |
|
2014 | 2016 | cache : false, |
|
2015 | 2017 | type : "POST", |
|
2016 | 2018 | dataType : "json", |
|
2017 | 2019 | data : JSON.stringify({copy_from : this.notebook_name}), |
|
2018 | 2020 | async : false, |
|
2019 | 2021 | success : function (data, status, xhr) { |
|
2020 | 2022 | window.open(utils.url_join_encode( |
|
2021 | 2023 | base_url, |
|
2022 | 2024 | 'notebooks', |
|
2023 | 2025 | data.path, |
|
2024 | 2026 | data.name |
|
2025 | 2027 | ), '_blank'); |
|
2026 | 2028 | }, |
|
2027 | 2029 | error : utils.log_ajax_error, |
|
2028 | 2030 | }; |
|
2029 | 2031 | var url = utils.url_join_encode( |
|
2030 | 2032 | base_url, |
|
2031 | 2033 | 'api/notebooks', |
|
2032 | 2034 | path |
|
2033 | 2035 | ); |
|
2034 | 2036 | $.ajax(url,settings); |
|
2035 | 2037 | }; |
|
2036 | 2038 | |
|
2037 | 2039 | Notebook.prototype.rename = function (nbname) { |
|
2038 | 2040 | var that = this; |
|
2039 | 2041 | if (!nbname.match(/\.ipynb$/)) { |
|
2040 | 2042 | nbname = nbname + ".ipynb"; |
|
2041 | 2043 | } |
|
2042 | 2044 | var data = {name: nbname}; |
|
2043 | 2045 | var settings = { |
|
2044 | 2046 | processData : false, |
|
2045 | 2047 | cache : false, |
|
2046 | 2048 | type : "PATCH", |
|
2047 | 2049 | data : JSON.stringify(data), |
|
2048 | 2050 | dataType: "json", |
|
2049 | 2051 | headers : {'Content-Type': 'application/json'}, |
|
2050 | 2052 | success : $.proxy(that.rename_success, this), |
|
2051 | 2053 | error : $.proxy(that.rename_error, this) |
|
2052 | 2054 | }; |
|
2053 | 2055 | this.events.trigger('rename_notebook.Notebook', data); |
|
2054 | 2056 | var url = utils.url_join_encode( |
|
2055 | 2057 | this.base_url, |
|
2056 | 2058 | 'api/notebooks', |
|
2057 | 2059 | this.notebook_path, |
|
2058 | 2060 | this.notebook_name |
|
2059 | 2061 | ); |
|
2060 | 2062 | $.ajax(url, settings); |
|
2061 | 2063 | }; |
|
2062 | 2064 | |
|
2063 | 2065 | Notebook.prototype.delete = function () { |
|
2064 | 2066 | var that = this; |
|
2065 | 2067 | var settings = { |
|
2066 | 2068 | processData : false, |
|
2067 | 2069 | cache : false, |
|
2068 | 2070 | type : "DELETE", |
|
2069 | 2071 | dataType: "json", |
|
2070 | 2072 | error : utils.log_ajax_error, |
|
2071 | 2073 | }; |
|
2072 | 2074 | var url = utils.url_join_encode( |
|
2073 | 2075 | this.base_url, |
|
2074 | 2076 | 'api/notebooks', |
|
2075 | 2077 | this.notebook_path, |
|
2076 | 2078 | this.notebook_name |
|
2077 | 2079 | ); |
|
2078 | 2080 | $.ajax(url, settings); |
|
2079 | 2081 | }; |
|
2080 | 2082 | |
|
2081 | 2083 | |
|
2082 | 2084 | Notebook.prototype.rename_success = function (json, status, xhr) { |
|
2083 | 2085 | var name = this.notebook_name = json.name; |
|
2084 | 2086 | var path = json.path; |
|
2085 | 2087 | this.session.rename_notebook(name, path); |
|
2086 | 2088 | this.events.trigger('notebook_renamed.Notebook', json); |
|
2087 | 2089 | }; |
|
2088 | 2090 | |
|
2089 | 2091 | Notebook.prototype.rename_error = function (xhr, status, error) { |
|
2090 | 2092 | var that = this; |
|
2091 | 2093 | var dialog_body = $('<div/>').append( |
|
2092 | 2094 | $("<p/>").addClass("rename-message") |
|
2093 | 2095 | .text('This notebook name already exists.') |
|
2094 | 2096 | ); |
|
2095 | 2097 | this.events.trigger('notebook_rename_failed.Notebook', [xhr, status, error]); |
|
2096 | 2098 | dialog.modal({ |
|
2097 | 2099 | notebook: this, |
|
2098 | 2100 | keyboard_manager: this.keyboard_manager, |
|
2099 | 2101 | title: "Notebook Rename Error!", |
|
2100 | 2102 | body: dialog_body, |
|
2101 | 2103 | buttons : { |
|
2102 | 2104 | "Cancel": {}, |
|
2103 | 2105 | "OK": { |
|
2104 | 2106 | class: "btn-primary", |
|
2105 | 2107 | click: function () { |
|
2106 | 2108 | this.save_widget.rename_notebook({notebook:that}); |
|
2107 | 2109 | }} |
|
2108 | 2110 | }, |
|
2109 | 2111 | open : function (event, ui) { |
|
2110 | 2112 | var that = $(this); |
|
2111 | 2113 | // Upon ENTER, click the OK button. |
|
2112 | 2114 | that.find('input[type="text"]').keydown(function (event, ui) { |
|
2113 | 2115 | if (event.which === this.keyboard.keycodes.enter) { |
|
2114 | 2116 | that.find('.btn-primary').first().click(); |
|
2115 | 2117 | } |
|
2116 | 2118 | }); |
|
2117 | 2119 | that.find('input[type="text"]').focus(); |
|
2118 | 2120 | } |
|
2119 | 2121 | }); |
|
2120 | 2122 | }; |
|
2121 | 2123 | |
|
2122 | 2124 | /** |
|
2123 | 2125 | * Request a notebook's data from the server. |
|
2124 | 2126 | * |
|
2125 | 2127 | * @method load_notebook |
|
2126 | 2128 | * @param {String} notebook_name and path A notebook to load |
|
2127 | 2129 | */ |
|
2128 | 2130 | Notebook.prototype.load_notebook = function (notebook_name, notebook_path) { |
|
2129 | 2131 | var that = this; |
|
2130 | 2132 | this.notebook_name = notebook_name; |
|
2131 | 2133 | this.notebook_path = notebook_path; |
|
2132 | 2134 | // We do the call with settings so we can set cache to false. |
|
2133 | 2135 | var settings = { |
|
2134 | 2136 | processData : false, |
|
2135 | 2137 | cache : false, |
|
2136 | 2138 | type : "GET", |
|
2137 | 2139 | dataType : "json", |
|
2138 | 2140 | success : $.proxy(this.load_notebook_success,this), |
|
2139 | 2141 | error : $.proxy(this.load_notebook_error,this), |
|
2140 | 2142 | }; |
|
2141 | 2143 | this.events.trigger('notebook_loading.Notebook'); |
|
2142 | 2144 | var url = utils.url_join_encode( |
|
2143 | 2145 | this.base_url, |
|
2144 | 2146 | 'api/notebooks', |
|
2145 | 2147 | this.notebook_path, |
|
2146 | 2148 | this.notebook_name |
|
2147 | 2149 | ); |
|
2148 | 2150 | $.ajax(url, settings); |
|
2149 | 2151 | }; |
|
2150 | 2152 | |
|
2151 | 2153 | /** |
|
2152 | 2154 | * Success callback for loading a notebook from the server. |
|
2153 | 2155 | * |
|
2154 | 2156 | * Load notebook data from the JSON response. |
|
2155 | 2157 | * |
|
2156 | 2158 | * @method load_notebook_success |
|
2157 | 2159 | * @param {Object} data JSON representation of a notebook |
|
2158 | 2160 | * @param {String} status Description of response status |
|
2159 | 2161 | * @param {jqXHR} xhr jQuery Ajax object |
|
2160 | 2162 | */ |
|
2161 | 2163 | Notebook.prototype.load_notebook_success = function (data, status, xhr) { |
|
2162 | 2164 | this.fromJSON(data); |
|
2163 | 2165 | if (this.ncells() === 0) { |
|
2164 | 2166 | this.insert_cell_below('code'); |
|
2165 | 2167 | this.edit_mode(0); |
|
2166 | 2168 | } else { |
|
2167 | 2169 | this.select(0); |
|
2168 | 2170 | this.handle_command_mode(this.get_cell(0)); |
|
2169 | 2171 | } |
|
2170 | 2172 | this.set_dirty(false); |
|
2171 | 2173 | this.scroll_to_top(); |
|
2172 | 2174 | if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) { |
|
2173 | 2175 | var msg = "This notebook has been converted from an older " + |
|
2174 | 2176 | "notebook format (v"+data.orig_nbformat+") to the current notebook " + |
|
2175 | 2177 | "format (v"+data.nbformat+"). The next time you save this notebook, the " + |
|
2176 | 2178 | "newer notebook format will be used and older versions of IPython " + |
|
2177 | 2179 | "may not be able to read it. To keep the older version, close the " + |
|
2178 | 2180 | "notebook without saving it."; |
|
2179 | 2181 | dialog.modal({ |
|
2180 | 2182 | notebook: this, |
|
2181 | 2183 | keyboard_manager: this.keyboard_manager, |
|
2182 | 2184 | title : "Notebook converted", |
|
2183 | 2185 | body : msg, |
|
2184 | 2186 | buttons : { |
|
2185 | 2187 | OK : { |
|
2186 | 2188 | class : "btn-primary" |
|
2187 | 2189 | } |
|
2188 | 2190 | } |
|
2189 | 2191 | }); |
|
2190 | 2192 | } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) { |
|
2191 | 2193 | var that = this; |
|
2192 | 2194 | var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor; |
|
2193 | 2195 | var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor; |
|
2194 | 2196 | var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + |
|
2195 | 2197 | this_vs + ". You can still work with this notebook, but some features " + |
|
2196 | 2198 | "introduced in later notebook versions may not be available."; |
|
2197 | 2199 | |
|
2198 | 2200 | dialog.modal({ |
|
2199 | 2201 | notebook: this, |
|
2200 | 2202 | keyboard_manager: this.keyboard_manager, |
|
2201 | 2203 | title : "Newer Notebook", |
|
2202 | 2204 | body : msg, |
|
2203 | 2205 | buttons : { |
|
2204 | 2206 | OK : { |
|
2205 | 2207 | class : "btn-danger" |
|
2206 | 2208 | } |
|
2207 | 2209 | } |
|
2208 | 2210 | }); |
|
2209 | 2211 | |
|
2210 | 2212 | } |
|
2211 | 2213 | |
|
2212 | 2214 | // Create the session after the notebook is completely loaded to prevent |
|
2213 | 2215 | // code execution upon loading, which is a security risk. |
|
2214 | 2216 | if (this.session === null) { |
|
2215 | 2217 | this.start_session(); |
|
2216 | 2218 | } |
|
2217 | 2219 | // load our checkpoint list |
|
2218 | 2220 | this.list_checkpoints(); |
|
2219 | 2221 | |
|
2220 | 2222 | // load toolbar state |
|
2221 | 2223 | if (this.metadata.celltoolbar) { |
|
2222 | 2224 | celltoolbar.CellToolbar.global_show(); |
|
2223 | 2225 | celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar, this.events); |
|
2224 | 2226 | } else { |
|
2225 | 2227 | celltoolbar.CellToolbar.global_hide(); |
|
2226 | 2228 | } |
|
2227 | 2229 | |
|
2228 | 2230 | // now that we're fully loaded, it is safe to restore save functionality |
|
2229 | 2231 | delete(this.save_notebook); |
|
2230 | 2232 | this.events.trigger('notebook_loaded.Notebook'); |
|
2231 | 2233 | }; |
|
2232 | 2234 | |
|
2233 | 2235 | /** |
|
2234 | 2236 | * Failure callback for loading a notebook from the server. |
|
2235 | 2237 | * |
|
2236 | 2238 | * @method load_notebook_error |
|
2237 | 2239 | * @param {jqXHR} xhr jQuery Ajax object |
|
2238 | 2240 | * @param {String} status Description of response status |
|
2239 | 2241 | * @param {String} error HTTP error message |
|
2240 | 2242 | */ |
|
2241 | 2243 | Notebook.prototype.load_notebook_error = function (xhr, status, error) { |
|
2242 | 2244 | this.events.trigger('notebook_load_failed.Notebook', [xhr, status, error]); |
|
2243 | 2245 | var msg; |
|
2244 | 2246 | if (xhr.status === 400) { |
|
2245 | 2247 | msg = error; |
|
2246 | 2248 | } else if (xhr.status === 500) { |
|
2247 | 2249 | msg = "An unknown error occurred while loading this notebook. " + |
|
2248 | 2250 | "This version can load notebook formats " + |
|
2249 | 2251 | "v" + this.nbformat + " or earlier."; |
|
2250 | 2252 | } |
|
2251 | 2253 | dialog.modal({ |
|
2252 | 2254 | notebook: this, |
|
2253 | 2255 | keyboard_manager: this.keyboard_manager, |
|
2254 | 2256 | title: "Error loading notebook", |
|
2255 | 2257 | body : msg, |
|
2256 | 2258 | buttons : { |
|
2257 | 2259 | "OK": {} |
|
2258 | 2260 | } |
|
2259 | 2261 | }); |
|
2260 | 2262 | }; |
|
2261 | 2263 | |
|
2262 | 2264 | /********************* checkpoint-related *********************/ |
|
2263 | 2265 | |
|
2264 | 2266 | /** |
|
2265 | 2267 | * Save the notebook then immediately create a checkpoint. |
|
2266 | 2268 | * |
|
2267 | 2269 | * @method save_checkpoint |
|
2268 | 2270 | */ |
|
2269 | 2271 | Notebook.prototype.save_checkpoint = function () { |
|
2270 | 2272 | this._checkpoint_after_save = true; |
|
2271 | 2273 | this.save_notebook(); |
|
2272 | 2274 | }; |
|
2273 | 2275 | |
|
2274 | 2276 | /** |
|
2275 | 2277 | * Add a checkpoint for this notebook. |
|
2276 | 2278 | * for use as a callback from checkpoint creation. |
|
2277 | 2279 | * |
|
2278 | 2280 | * @method add_checkpoint |
|
2279 | 2281 | */ |
|
2280 | 2282 | Notebook.prototype.add_checkpoint = function (checkpoint) { |
|
2281 | 2283 | var found = false; |
|
2282 | 2284 | for (var i = 0; i < this.checkpoints.length; i++) { |
|
2283 | 2285 | var existing = this.checkpoints[i]; |
|
2284 | 2286 | if (existing.id == checkpoint.id) { |
|
2285 | 2287 | found = true; |
|
2286 | 2288 | this.checkpoints[i] = checkpoint; |
|
2287 | 2289 | break; |
|
2288 | 2290 | } |
|
2289 | 2291 | } |
|
2290 | 2292 | if (!found) { |
|
2291 | 2293 | this.checkpoints.push(checkpoint); |
|
2292 | 2294 | } |
|
2293 | 2295 | this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1]; |
|
2294 | 2296 | }; |
|
2295 | 2297 | |
|
2296 | 2298 | /** |
|
2297 | 2299 | * List checkpoints for this notebook. |
|
2298 | 2300 | * |
|
2299 | 2301 | * @method list_checkpoints |
|
2300 | 2302 | */ |
|
2301 | 2303 | Notebook.prototype.list_checkpoints = function () { |
|
2302 | 2304 | var url = utils.url_join_encode( |
|
2303 | 2305 | this.base_url, |
|
2304 | 2306 | 'api/notebooks', |
|
2305 | 2307 | this.notebook_path, |
|
2306 | 2308 | this.notebook_name, |
|
2307 | 2309 | 'checkpoints' |
|
2308 | 2310 | ); |
|
2309 | 2311 | $.get(url).done( |
|
2310 | 2312 | $.proxy(this.list_checkpoints_success, this) |
|
2311 | 2313 | ).fail( |
|
2312 | 2314 | $.proxy(this.list_checkpoints_error, this) |
|
2313 | 2315 | ); |
|
2314 | 2316 | }; |
|
2315 | 2317 | |
|
2316 | 2318 | /** |
|
2317 | 2319 | * Success callback for listing checkpoints. |
|
2318 | 2320 | * |
|
2319 | 2321 | * @method list_checkpoint_success |
|
2320 | 2322 | * @param {Object} data JSON representation of a checkpoint |
|
2321 | 2323 | * @param {String} status Description of response status |
|
2322 | 2324 | * @param {jqXHR} xhr jQuery Ajax object |
|
2323 | 2325 | */ |
|
2324 | 2326 | Notebook.prototype.list_checkpoints_success = function (data, status, xhr) { |
|
2325 | 2327 | data = $.parseJSON(data); |
|
2326 | 2328 | this.checkpoints = data; |
|
2327 | 2329 | if (data.length) { |
|
2328 | 2330 | this.last_checkpoint = data[data.length - 1]; |
|
2329 | 2331 | } else { |
|
2330 | 2332 | this.last_checkpoint = null; |
|
2331 | 2333 | } |
|
2332 | 2334 | this.events.trigger('checkpoints_listed.Notebook', [data]); |
|
2333 | 2335 | }; |
|
2334 | 2336 | |
|
2335 | 2337 | /** |
|
2336 | 2338 | * Failure callback for listing a checkpoint. |
|
2337 | 2339 | * |
|
2338 | 2340 | * @method list_checkpoint_error |
|
2339 | 2341 | * @param {jqXHR} xhr jQuery Ajax object |
|
2340 | 2342 | * @param {String} status Description of response status |
|
2341 | 2343 | * @param {String} error_msg HTTP error message |
|
2342 | 2344 | */ |
|
2343 | 2345 | Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) { |
|
2344 | 2346 | this.events.trigger('list_checkpoints_failed.Notebook'); |
|
2345 | 2347 | }; |
|
2346 | 2348 | |
|
2347 | 2349 | /** |
|
2348 | 2350 | * Create a checkpoint of this notebook on the server from the most recent save. |
|
2349 | 2351 | * |
|
2350 | 2352 | * @method create_checkpoint |
|
2351 | 2353 | */ |
|
2352 | 2354 | Notebook.prototype.create_checkpoint = function () { |
|
2353 | 2355 | var url = utils.url_join_encode( |
|
2354 | 2356 | this.base_url, |
|
2355 | 2357 | 'api/notebooks', |
|
2356 | 2358 | this.notebook_path, |
|
2357 | 2359 | this.notebook_name, |
|
2358 | 2360 | 'checkpoints' |
|
2359 | 2361 | ); |
|
2360 | 2362 | $.post(url).done( |
|
2361 | 2363 | $.proxy(this.create_checkpoint_success, this) |
|
2362 | 2364 | ).fail( |
|
2363 | 2365 | $.proxy(this.create_checkpoint_error, this) |
|
2364 | 2366 | ); |
|
2365 | 2367 | }; |
|
2366 | 2368 | |
|
2367 | 2369 | /** |
|
2368 | 2370 | * Success callback for creating a checkpoint. |
|
2369 | 2371 | * |
|
2370 | 2372 | * @method create_checkpoint_success |
|
2371 | 2373 | * @param {Object} data JSON representation of a checkpoint |
|
2372 | 2374 | * @param {String} status Description of response status |
|
2373 | 2375 | * @param {jqXHR} xhr jQuery Ajax object |
|
2374 | 2376 | */ |
|
2375 | 2377 | Notebook.prototype.create_checkpoint_success = function (data, status, xhr) { |
|
2376 | 2378 | data = $.parseJSON(data); |
|
2377 | 2379 | this.add_checkpoint(data); |
|
2378 | 2380 | this.events.trigger('checkpoint_created.Notebook', data); |
|
2379 | 2381 | }; |
|
2380 | 2382 | |
|
2381 | 2383 | /** |
|
2382 | 2384 | * Failure callback for creating a checkpoint. |
|
2383 | 2385 | * |
|
2384 | 2386 | * @method create_checkpoint_error |
|
2385 | 2387 | * @param {jqXHR} xhr jQuery Ajax object |
|
2386 | 2388 | * @param {String} status Description of response status |
|
2387 | 2389 | * @param {String} error_msg HTTP error message |
|
2388 | 2390 | */ |
|
2389 | 2391 | Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) { |
|
2390 | 2392 | this.events.trigger('checkpoint_failed.Notebook'); |
|
2391 | 2393 | }; |
|
2392 | 2394 | |
|
2393 | 2395 | Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) { |
|
2394 | 2396 | var that = this; |
|
2395 | 2397 | checkpoint = checkpoint || this.last_checkpoint; |
|
2396 | 2398 | if ( ! checkpoint ) { |
|
2397 | 2399 | console.log("restore dialog, but no checkpoint to restore to!"); |
|
2398 | 2400 | return; |
|
2399 | 2401 | } |
|
2400 | 2402 | var body = $('<div/>').append( |
|
2401 | 2403 | $('<p/>').addClass("p-space").text( |
|
2402 | 2404 | "Are you sure you want to revert the notebook to " + |
|
2403 | 2405 | "the latest checkpoint?" |
|
2404 | 2406 | ).append( |
|
2405 | 2407 | $("<strong/>").text( |
|
2406 | 2408 | " This cannot be undone." |
|
2407 | 2409 | ) |
|
2408 | 2410 | ) |
|
2409 | 2411 | ).append( |
|
2410 | 2412 | $('<p/>').addClass("p-space").text("The checkpoint was last updated at:") |
|
2411 | 2413 | ).append( |
|
2412 | 2414 | $('<p/>').addClass("p-space").text( |
|
2413 | 2415 | Date(checkpoint.last_modified) |
|
2414 | 2416 | ).css("text-align", "center") |
|
2415 | 2417 | ); |
|
2416 | 2418 | |
|
2417 | 2419 | dialog.modal({ |
|
2418 | 2420 | notebook: this, |
|
2419 | 2421 | keyboard_manager: this.keyboard_manager, |
|
2420 | 2422 | title : "Revert notebook to checkpoint", |
|
2421 | 2423 | body : body, |
|
2422 | 2424 | buttons : { |
|
2423 | 2425 | Revert : { |
|
2424 | 2426 | class : "btn-danger", |
|
2425 | 2427 | click : function () { |
|
2426 | 2428 | that.restore_checkpoint(checkpoint.id); |
|
2427 | 2429 | } |
|
2428 | 2430 | }, |
|
2429 | 2431 | Cancel : {} |
|
2430 | 2432 | } |
|
2431 | 2433 | }); |
|
2432 | 2434 | }; |
|
2433 | 2435 | |
|
2434 | 2436 | /** |
|
2435 | 2437 | * Restore the notebook to a checkpoint state. |
|
2436 | 2438 | * |
|
2437 | 2439 | * @method restore_checkpoint |
|
2438 | 2440 | * @param {String} checkpoint ID |
|
2439 | 2441 | */ |
|
2440 | 2442 | Notebook.prototype.restore_checkpoint = function (checkpoint) { |
|
2441 | 2443 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
2442 | 2444 | var url = utils.url_join_encode( |
|
2443 | 2445 | this.base_url, |
|
2444 | 2446 | 'api/notebooks', |
|
2445 | 2447 | this.notebook_path, |
|
2446 | 2448 | this.notebook_name, |
|
2447 | 2449 | 'checkpoints', |
|
2448 | 2450 | checkpoint |
|
2449 | 2451 | ); |
|
2450 | 2452 | $.post(url).done( |
|
2451 | 2453 | $.proxy(this.restore_checkpoint_success, this) |
|
2452 | 2454 | ).fail( |
|
2453 | 2455 | $.proxy(this.restore_checkpoint_error, this) |
|
2454 | 2456 | ); |
|
2455 | 2457 | }; |
|
2456 | 2458 | |
|
2457 | 2459 | /** |
|
2458 | 2460 | * Success callback for restoring a notebook to a checkpoint. |
|
2459 | 2461 | * |
|
2460 | 2462 | * @method restore_checkpoint_success |
|
2461 | 2463 | * @param {Object} data (ignored, should be empty) |
|
2462 | 2464 | * @param {String} status Description of response status |
|
2463 | 2465 | * @param {jqXHR} xhr jQuery Ajax object |
|
2464 | 2466 | */ |
|
2465 | 2467 | Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) { |
|
2466 | 2468 | this.events.trigger('checkpoint_restored.Notebook'); |
|
2467 | 2469 | this.load_notebook(this.notebook_name, this.notebook_path); |
|
2468 | 2470 | }; |
|
2469 | 2471 | |
|
2470 | 2472 | /** |
|
2471 | 2473 | * Failure callback for restoring a notebook to a checkpoint. |
|
2472 | 2474 | * |
|
2473 | 2475 | * @method restore_checkpoint_error |
|
2474 | 2476 | * @param {jqXHR} xhr jQuery Ajax object |
|
2475 | 2477 | * @param {String} status Description of response status |
|
2476 | 2478 | * @param {String} error_msg HTTP error message |
|
2477 | 2479 | */ |
|
2478 | 2480 | Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) { |
|
2479 | 2481 | this.events.trigger('checkpoint_restore_failed.Notebook'); |
|
2480 | 2482 | }; |
|
2481 | 2483 | |
|
2482 | 2484 | /** |
|
2483 | 2485 | * Delete a notebook checkpoint. |
|
2484 | 2486 | * |
|
2485 | 2487 | * @method delete_checkpoint |
|
2486 | 2488 | * @param {String} checkpoint ID |
|
2487 | 2489 | */ |
|
2488 | 2490 | Notebook.prototype.delete_checkpoint = function (checkpoint) { |
|
2489 | 2491 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
2490 | 2492 | var url = utils.url_join_encode( |
|
2491 | 2493 | this.base_url, |
|
2492 | 2494 | 'api/notebooks', |
|
2493 | 2495 | this.notebook_path, |
|
2494 | 2496 | this.notebook_name, |
|
2495 | 2497 | 'checkpoints', |
|
2496 | 2498 | checkpoint |
|
2497 | 2499 | ); |
|
2498 | 2500 | $.ajax(url, { |
|
2499 | 2501 | type: 'DELETE', |
|
2500 | 2502 | success: $.proxy(this.delete_checkpoint_success, this), |
|
2501 | 2503 | error: $.proxy(this.delete_checkpoint_error, this) |
|
2502 | 2504 | }); |
|
2503 | 2505 | }; |
|
2504 | 2506 | |
|
2505 | 2507 | /** |
|
2506 | 2508 | * Success callback for deleting a notebook checkpoint |
|
2507 | 2509 | * |
|
2508 | 2510 | * @method delete_checkpoint_success |
|
2509 | 2511 | * @param {Object} data (ignored, should be empty) |
|
2510 | 2512 | * @param {String} status Description of response status |
|
2511 | 2513 | * @param {jqXHR} xhr jQuery Ajax object |
|
2512 | 2514 | */ |
|
2513 | 2515 | Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) { |
|
2514 | 2516 | this.events.trigger('checkpoint_deleted.Notebook', data); |
|
2515 | 2517 | this.load_notebook(this.notebook_name, this.notebook_path); |
|
2516 | 2518 | }; |
|
2517 | 2519 | |
|
2518 | 2520 | /** |
|
2519 | 2521 | * Failure callback for deleting a notebook checkpoint. |
|
2520 | 2522 | * |
|
2521 | 2523 | * @method delete_checkpoint_error |
|
2522 | 2524 | * @param {jqXHR} xhr jQuery Ajax object |
|
2523 | 2525 | * @param {String} status Description of response status |
|
2524 | 2526 | * @param {String} error_msg HTTP error message |
|
2525 | 2527 | */ |
|
2526 | 2528 | Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) { |
|
2527 | 2529 | this.events.trigger('checkpoint_delete_failed.Notebook'); |
|
2528 | 2530 | }; |
|
2529 | 2531 | |
|
2530 | 2532 | |
|
2531 | 2533 | // For backwards compatability. |
|
2532 | 2534 | IPython.Notebook = Notebook; |
|
2533 | 2535 | |
|
2534 | 2536 | return {'Notebook': Notebook}; |
|
2535 | 2537 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now