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