notebook.js
2030 lines
| 68.7 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
Brian Granger
|
r4315 | |||
Brian Granger
|
r4299 | //============================================================================ | ||
Brian Granger
|
r4292 | // Notebook | ||
//============================================================================ | ||||
Brian E. Granger
|
r4352 | var IPython = (function (IPython) { | ||
Brian E. Granger
|
r4356 | var utils = IPython.utils; | ||
Matthias BUSSONNIER
|
r7139 | var key = IPython.utils.keycodes; | ||
Brian E. Granger
|
r4356 | |||
David Wyde
|
r10033 | /** | ||
* A notebook contains and manages cells. | ||||
* | ||||
* @class Notebook | ||||
* @constructor | ||||
* @param {String} selector A jQuery selector for the notebook's DOM element | ||||
* @param {Object} [options] A config object | ||||
*/ | ||||
Matthias BUSSONNIER
|
r9505 | var Notebook = function (selector, options) { | ||
var options = options || {}; | ||||
this._baseProjectUrl = options.baseProjectUrl; | ||||
this.read_only = options.read_only || IPython.read_only; | ||||
Brian E. Granger
|
r4352 | this.element = $(selector); | ||
this.element.scroll(); | ||||
this.element.data("notebook", this); | ||||
this.next_prompt_number = 1; | ||||
this.kernel = null; | ||||
Brian Granger
|
r5879 | this.clipboard = null; | ||
David Warde-Farley
|
r8691 | this.undelete_backup = null; | ||
this.undelete_index = null; | ||||
this.undelete_below = false; | ||||
Brian Granger
|
r5912 | this.paste_enabled = false; | ||
Brian E. Granger
|
r4550 | this.dirty = false; | ||
Brian E. Granger
|
r4637 | this.metadata = {}; | ||
MinRK
|
r10501 | this._checkpoint_after_save = false; | ||
this.last_checkpoint = null; | ||||
MinRK
|
r10505 | this.autosave_interval = 0; | ||
this.autosave_timer = null; | ||||
MinRK
|
r10507 | // autosave *at most* every two minutes | ||
this.minimum_autosave_interval = 120000; | ||||
MinRK
|
r7523 | // single worksheet for now | ||
this.worksheet_metadata = {}; | ||||
Brian E. Granger
|
r4645 | this.control_key_active = false; | ||
Brian Granger
|
r6047 | this.notebook_id = null; | ||
this.notebook_name = null; | ||||
Brian Granger
|
r7229 | this.notebook_name_blacklist_re = /[\/\\:]/; | ||
Brian Granger
|
r6061 | this.nbformat = 3 // Increment this when changing the nbformat | ||
MinRK
|
r7546 | this.nbformat_minor = 0 // Increment this when changing the nbformat | ||
Brian E. Granger
|
r4357 | this.style(); | ||
Brian E. Granger
|
r4364 | this.create_elements(); | ||
Brian E. Granger
|
r4352 | this.bind_events(); | ||
}; | ||||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Tweak the notebook's CSS style. | ||||
* | ||||
* @method style | ||||
*/ | ||||
Brian E. Granger
|
r4357 | Notebook.prototype.style = function () { | ||
Brian E. Granger
|
r4363 | $('div#notebook').addClass('border-box-sizing'); | ||
Brian E. Granger
|
r4357 | }; | ||
David Wyde
|
r10033 | /** | ||
* Get the root URL of the notebook server. | ||||
* | ||||
* @method baseProjectUrl | ||||
* @return {String} The base project URL | ||||
*/ | ||||
Bussonnier Matthias
|
r9503 | Notebook.prototype.baseProjectUrl = function(){ | ||
return this._baseProjectUrl || $('body').data('baseProjectUrl'); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Create an HTML and CSS representation of the notebook. | ||||
* | ||||
* @method create_elements | ||||
*/ | ||||
Brian E. Granger
|
r4364 | Notebook.prototype.create_elements = function () { | ||
// We add this end_space div to the end of the notebook div to: | ||||
// i) provide a margin between the last cell and the end of the notebook | ||||
// ii) to prevent the div from scrolling up when the last cell is being | ||||
// edited, but is too low on the page, which browsers will do automatically. | ||||
Brian E. Granger
|
r4628 | var that = this; | ||
Brian Granger
|
r5945 | var end_space = $('<div/>').addClass('end_space').height("30%"); | ||
Brian E. Granger
|
r4628 | end_space.dblclick(function (e) { | ||
MinRK
|
r5200 | if (that.read_only) return; | ||
Brian E. Granger
|
r4628 | var ncells = that.ncells(); | ||
Brian Granger
|
r5945 | that.insert_cell_below('code',ncells-1); | ||
Brian E. Granger
|
r4628 | }); | ||
Brian E. Granger
|
r4604 | this.element.append(end_space); | ||
Brian E. Granger
|
r4364 | $('div#notebook').addClass('border-box-sizing'); | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Bind JavaScript events: key presses and custom IPython events. | ||||
* | ||||
* @method bind_events | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.bind_events = function () { | ||
var that = this; | ||||
Matthias BUSSONNIER
|
r7170 | |||
Brian Granger
|
r7168 | $([IPython.events]).on('set_next_input.Notebook', function (event, data) { | ||
var index = that.find_cell_index(data.cell); | ||||
var new_cell = that.insert_cell_below('code',index); | ||||
new_cell.set_text(data.text); | ||||
that.dirty = true; | ||||
}); | ||||
Brian Granger
|
r7228 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { | ||
that.dirty = data.value; | ||||
}); | ||||
Brian Granger
|
r7168 | $([IPython.events]).on('select.Cell', function (event, data) { | ||
var index = that.find_cell_index(data.cell); | ||||
Matthias BUSSONNIER
|
r7170 | that.select(index); | ||
Brian Granger
|
r7168 | }); | ||
Brian E. Granger
|
r4352 | $(document).keydown(function (event) { | ||
Brian E. Granger
|
r4647 | // console.log(event); | ||
MinRK
|
r5546 | if (that.read_only) return true; | ||
Mikhail Korobov
|
r8839 | |||
// Save (CTRL+S) or (AppleKey+S) | ||||
fawce
|
r5990 | //metaKey = applekey on mac | ||
Mikhail Korobov
|
r8839 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { | ||
MinRK
|
r10509 | that.save_checkpoint(); | ||
fawce
|
r5990 | event.preventDefault(); | ||
fawce
|
r5991 | return false; | ||
Matthias BUSSONNIER
|
r7193 | } else if (event.which === key.ESC) { | ||
Mikhail Korobov
|
r8839 | // Intercept escape at highest level to avoid closing | ||
Matthias BUSSONNIER
|
r5389 | // websocket connection with firefox | ||
event.preventDefault(); | ||||
MinRK
|
r7723 | } else if (event.which === key.SHIFT) { | ||
// ignore shift keydown | ||||
return true; | ||||
Matthias BUSSONNIER
|
r5389 | } | ||
Matthias BUSSONNIER
|
r7193 | if (event.which === key.UPARROW && !event.shiftKey) { | ||
Brian Granger
|
r5945 | var cell = that.get_selected_cell(); | ||
David Wyde
|
r10126 | if (cell && cell.at_top()) { | ||
Brian E. Granger
|
r4352 | event.preventDefault(); | ||
that.select_prev(); | ||||
}; | ||||
Matthias BUSSONNIER
|
r7193 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { | ||
Brian Granger
|
r5945 | var cell = that.get_selected_cell(); | ||
David Wyde
|
r10126 | if (cell && cell.at_bottom()) { | ||
Brian E. Granger
|
r4352 | event.preventDefault(); | ||
that.select_next(); | ||||
}; | ||||
Matthias BUSSONNIER
|
r7193 | } else if (event.which === key.ENTER && event.shiftKey) { | ||
Brian E. Granger
|
r4394 | that.execute_selected_cell(); | ||
Brian E. Granger
|
r4380 | return false; | ||
Zoltán Vörös
|
r7806 | } else if (event.which === key.ENTER && event.altKey) { | ||
v923z
|
r7910 | // Execute code cell, and insert new in place | ||
Zoltán Vörös
|
r7806 | that.execute_selected_cell(); | ||
v923z
|
r7910 | // Only insert a new cell, if we ended up in an already populated cell | ||
v923z
|
r7921 | if (/\S/.test(that.get_selected_cell().get_text()) == true) { | ||
v923z
|
r7910 | that.insert_cell_above('code'); | ||
} | ||||
Zoltán Vörös
|
r7806 | return false; | ||
Matthias BUSSONNIER
|
r7193 | } else if (event.which === key.ENTER && event.ctrlKey) { | ||
Brian E. Granger
|
r4394 | that.execute_selected_cell({terminal:true}); | ||
Brian E. Granger
|
r4390 | return false; | ||
Brian Granger
|
r6049 | } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) { | ||
Brian E. Granger
|
r4645 | that.control_key_active = true; | ||
return false; | ||||
Brian Granger
|
r5880 | } else if (event.which === 88 && that.control_key_active) { | ||
// Cut selected cell = x | ||||
that.cut_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 67 && that.control_key_active) { | ||||
// Copy selected cell = c | ||||
that.copy_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 86 && that.control_key_active) { | ||||
Matthias BUSSONNIER
|
r8770 | // Paste below selected cell = v | ||
that.paste_cell_below(); | ||||
Brian Granger
|
r5880 | that.control_key_active = false; | ||
return false; | ||||
Brian E. Granger
|
r4645 | } else if (event.which === 68 && that.control_key_active) { | ||
// Delete selected cell = d | ||||
that.delete_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 65 && that.control_key_active) { | ||||
Fernando Perez
|
r4659 | // Insert code cell above selected = a | ||
Brian Granger
|
r5945 | that.insert_cell_above('code'); | ||
Brian E. Granger
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 66 && that.control_key_active) { | ||||
Fernando Perez
|
r4659 | // Insert code cell below selected = b | ||
Brian Granger
|
r5945 | that.insert_cell_below('code'); | ||
Brian E. Granger
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
Brian Granger
|
r5880 | } else if (event.which === 89 && that.control_key_active) { | ||
// To code = y | ||||
Brian E. Granger
|
r4645 | that.to_code(); | ||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 77 && that.control_key_active) { | ||||
// To markdown = m | ||||
that.to_markdown(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
Brian Granger
|
r6027 | } else if (event.which === 84 && that.control_key_active) { | ||
MinRK
|
r6248 | // To Raw = t | ||
that.to_raw(); | ||||
Brian Granger
|
r6017 | that.control_key_active = false; | ||
return false; | ||||
Brian Granger
|
r6019 | } else if (event.which === 49 && that.control_key_active) { | ||
// To Heading 1 = 1 | ||||
that.to_heading(undefined, 1); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 50 && that.control_key_active) { | ||||
// To Heading 2 = 2 | ||||
that.to_heading(undefined, 2); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 51 && that.control_key_active) { | ||||
// To Heading 3 = 3 | ||||
that.to_heading(undefined, 3); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 52 && that.control_key_active) { | ||||
// To Heading 4 = 4 | ||||
that.to_heading(undefined, 4); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 53 && that.control_key_active) { | ||||
// To Heading 5 = 5 | ||||
that.to_heading(undefined, 5); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 54 && that.control_key_active) { | ||||
// To Heading 6 = 6 | ||||
that.to_heading(undefined, 6); | ||||
that.control_key_active = false; | ||||
return false; | ||||
Brian Granger
|
r6027 | } else if (event.which === 79 && that.control_key_active) { | ||
// Toggle output = o | ||||
MinRK
|
r7429 | if (event.shiftKey){ | ||
that.toggle_output_scroll(); | ||||
} else { | ||||
that.toggle_output(); | ||||
} | ||||
Brian E. Granger
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 83 && that.control_key_active) { | ||||
// Save notebook = s | ||||
MinRK
|
r10501 | that.save_checkpoint(); | ||
Brian E. Granger
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 74 && that.control_key_active) { | ||||
// Move cell down = j | ||||
that.move_cell_down(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 75 && that.control_key_active) { | ||||
// Move cell up = k | ||||
that.move_cell_up(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
Brian E. Granger
|
r4648 | } else if (event.which === 80 && that.control_key_active) { | ||
// Select previous = p | ||||
Brian E. Granger
|
r4645 | that.select_prev(); | ||
that.control_key_active = false; | ||||
return false; | ||||
Brian E. Granger
|
r4648 | } else if (event.which === 78 && that.control_key_active) { | ||
// Select next = n | ||||
Brian E. Granger
|
r4645 | that.select_next(); | ||
that.control_key_active = false; | ||||
return false; | ||||
Fernando Perez
|
r5026 | } else if (event.which === 76 && that.control_key_active) { | ||
// Toggle line numbers = l | ||||
that.cell_toggle_line_numbers(); | ||||
Brian E. Granger
|
r4646 | that.control_key_active = false; | ||
return false; | ||||
Fernando Perez
|
r5018 | } else if (event.which === 73 && that.control_key_active) { | ||
// Interrupt kernel = i | ||||
Brian Granger
|
r6047 | that.kernel.interrupt(); | ||
Fernando Perez
|
r5018 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 190 && that.control_key_active) { | ||||
// Restart kernel = . # matches qt console | ||||
Brian Granger
|
r6047 | that.restart_kernel(); | ||
Fernando Perez
|
r5018 | that.control_key_active = false; | ||
return false; | ||||
Brian E. Granger
|
r4646 | } else if (event.which === 72 && that.control_key_active) { | ||
// Show keyboard shortcuts = h | ||||
Brian Granger
|
r5862 | IPython.quick_help.show_keyboard_shortcuts(); | ||
Brian E. Granger
|
r4646 | that.control_key_active = false; | ||
return false; | ||||
David Warde-Farley
|
r8691 | } else if (event.which === 90 && that.control_key_active) { | ||
David Warde-Farley
|
r8693 | // Undo last cell delete = z | ||
David Warde-Farley
|
r8691 | that.undelete(); | ||
that.control_key_active = false; | ||||
return false; | ||||
Brian E. Granger
|
r4645 | } else if (that.control_key_active) { | ||
that.control_key_active = false; | ||||
return true; | ||||
Brian Granger
|
r4334 | }; | ||
Stefan van der Walt
|
r5479 | return true; | ||
Brian E. Granger
|
r4352 | }); | ||
Brian E. Granger
|
r4361 | |||
Matthias BUSSONNIER
|
r6723 | var collapse_time = function(time){ | ||
Bussonnier Matthias
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
Brian E. Granger
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
var new_height = app_height - splitter_height; | ||||
Matthias BUSSONNIER
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
} | ||||
this.element.bind('collapse_pager', function (event,extrap) { | ||||
Mikhail Korobov
|
r8839 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
Matthias BUSSONNIER
|
r6723 | collapse_time(time); | ||
Brian E. Granger
|
r4361 | }); | ||
Matthias BUSSONNIER
|
r6723 | var expand_time = function(time) { | ||
Bussonnier Matthias
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
Brian E. Granger
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
Brian E. Granger
|
r4361 | var pager_height = $('div#pager').outerHeight(true); | ||
Mikhail Korobov
|
r8839 | var new_height = app_height - pager_height - splitter_height; | ||
Matthias BUSSONNIER
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
} | ||||
this.element.bind('expand_pager', function (event, extrap) { | ||||
Mikhail Korobov
|
r8839 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
Matthias BUSSONNIER
|
r6723 | expand_time(time); | ||
Brian E. Granger
|
r4361 | }); | ||
Brian E. Granger
|
r4363 | |||
Brian E. Granger
|
r4542 | $(window).bind('beforeunload', function () { | ||
Brian Granger
|
r5857 | // TODO: Make killing the kernel configurable. | ||
var kill_kernel = false; | ||||
Brian E. Granger
|
r4542 | if (kill_kernel) { | ||
that.kernel.kill(); | ||||
Brian E. Granger
|
r4550 | } | ||
MinRK
|
r10511 | // if we are autosaving, trigger an autosave on nav-away | ||
if (that.dirty && that.autosave_interval && ! that.read_only) { | ||||
MinRK
|
r10504 | that.save_notebook(); | ||
Brian E. Granger
|
r4542 | }; | ||
Fernando Perez
|
r5502 | // Null is the *only* return value that will make the browser not | ||
// pop up the "don't leave" dialog. | ||||
return null; | ||||
Brian E. Granger
|
r4542 | }); | ||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Scroll the top of the page to a given cell. | ||||
* | ||||
* @method scroll_to_cell | ||||
* @param {Number} cell_number An index of the cell to view | ||||
* @param {Number} time Animation time in milliseconds | ||||
* @return {Number} Pixel offset from the top of the container | ||||
*/ | ||||
Matthias BUSSONNIER
|
r8419 | Notebook.prototype.scroll_to_cell = function (cell_number, time) { | ||
var cells = this.get_cells(); | ||||
var time = time || 0; | ||||
cell_number = Math.min(cells.length-1,cell_number); | ||||
cell_number = Math.max(0 ,cell_number); | ||||
Mikhail Korobov
|
r8839 | var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; | ||
Matthias BUSSONNIER
|
r8419 | this.element.animate({scrollTop:scroll_value}, time); | ||
return scroll_value; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Scroll to the bottom of the page. | ||||
* | ||||
* @method scroll_to_bottom | ||||
*/ | ||||
Brian E. Granger
|
r4364 | Notebook.prototype.scroll_to_bottom = function () { | ||
Brian Granger
|
r4366 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); | ||
Brian E. Granger
|
r4364 | }; | ||
David Wyde
|
r10033 | /** | ||
* Scroll to the top of the page. | ||||
* | ||||
* @method scroll_to_top | ||||
*/ | ||||
Brian E. Granger
|
r4488 | Notebook.prototype.scroll_to_top = function () { | ||
this.element.animate({scrollTop:0}, 0); | ||||
}; | ||||
Brian E. Granger
|
r4352 | // Cell indexing, retrieval, etc. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get all cell elements in the notebook. | ||||
* | ||||
* @method get_cell_elements | ||||
* @return {jQuery} A selector of all cell elements | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cell_elements = function () { | ||
Brian E. Granger
|
r4352 | return this.element.children("div.cell"); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get a particular cell element. | ||||
* | ||||
* @method get_cell_element | ||||
* @param {Number} index An index of a cell to select | ||||
* @return {jQuery} A selector of the given cell. | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cell_element = function (index) { | ||
var result = null; | ||||
var e = this.get_cell_elements().eq(index); | ||||
if (e.length !== 0) { | ||||
result = e; | ||||
} | ||||
return result; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Count the cells in this notebook. | ||||
* | ||||
* @method ncells | ||||
* @return {Number} The number of cells in this notebook | ||||
*/ | ||||
Notebook.prototype.ncells = function () { | ||||
Brian Granger
|
r5945 | return this.get_cell_elements().length; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get all Cell objects in this notebook. | ||||
* | ||||
* @method get_cells | ||||
* @return {Array} This notebook's Cell objects | ||||
*/ | ||||
Brian E. Granger
|
r4352 | // TODO: we are often calling cells as cells()[i], which we should optimize | ||
// to cells(i) or a new method. | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cells = function () { | ||
return this.get_cell_elements().toArray().map(function (e) { | ||||
Brian E. Granger
|
r4352 | return $(e).data("cell"); | ||
}); | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get a Cell object from this notebook. | ||||
* | ||||
* @method get_cell | ||||
* @param {Number} index An index of a cell to retrieve | ||||
* @return {Cell} A particular cell | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cell = function (index) { | ||
var result = null; | ||||
var ce = this.get_cell_element(index); | ||||
if (ce !== null) { | ||||
result = ce.data('cell'); | ||||
} | ||||
return result; | ||||
} | ||||
David Wyde
|
r10033 | /** | ||
* Get the cell below a given cell. | ||||
* | ||||
* @method get_next_cell | ||||
* @param {Cell} cell The provided cell | ||||
* @return {Cell} The next cell | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_next_cell = function (cell) { | ||
var result = null; | ||||
var index = this.find_cell_index(cell); | ||||
Matthias BUSSONNIER
|
r9549 | if (this.is_valid_cell_index(index+1)) { | ||
Brian Granger
|
r5945 | result = this.get_cell(index+1); | ||
} | ||||
return result; | ||||
} | ||||
David Wyde
|
r10033 | /** | ||
* Get the cell above a given cell. | ||||
* | ||||
* @method get_prev_cell | ||||
* @param {Cell} cell The provided cell | ||||
* @return {Cell} The previous cell | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_prev_cell = function (cell) { | ||
David Wyde
|
r10033 | // TODO: off-by-one | ||
// nb.get_prev_cell(nb.get_cell(1)) is null | ||||
Brian Granger
|
r5945 | var result = null; | ||
var index = this.find_cell_index(cell); | ||||
if (index !== null && index > 1) { | ||||
result = this.get_cell(index-1); | ||||
} | ||||
return result; | ||||
} | ||||
David Wyde
|
r10033 | |||
/** | ||||
* Get the numeric index of a given cell. | ||||
* | ||||
* @method find_cell_index | ||||
* @param {Cell} cell The provided cell | ||||
* @return {Number} The cell's numeric index | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.find_cell_index = function (cell) { | ||
var result = null; | ||||
Brian Granger
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
Brian E. Granger
|
r4352 | if ($(this).data("cell") === cell) { | ||
result = index; | ||||
}; | ||||
}); | ||||
return result; | ||||
}; | ||||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get a given index , or the selected index if none is provided. | ||||
* | ||||
* @method index_or_selected | ||||
* @param {Number} index A cell's index | ||||
* @return {Number} The given index, or selected index if none is provided. | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.index_or_selected = function (index) { | ||
Brian Granger
|
r5898 | var i; | ||
Brian Granger
|
r5945 | if (index === undefined || index === null) { | ||
i = this.get_selected_index(); | ||||
Brian Granger
|
r5898 | if (i === null) { | ||
i = 0; | ||||
} | ||||
} else { | ||||
i = index; | ||||
} | ||||
return i; | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get the currently selected cell. | ||||
* @method get_selected_cell | ||||
* @return {Cell} The selected cell | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_selected_cell = function () { | ||
var index = this.get_selected_index(); | ||||
return this.get_cell(index); | ||||
Brian Granger
|
r4292 | }; | ||
David Wyde
|
r10033 | /** | ||
* Check whether a cell index is valid. | ||||
* | ||||
* @method is_valid_cell_index | ||||
* @param {Number} index A cell index | ||||
* @return True if the index is valid, false otherwise | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.is_valid_cell_index = function (index) { | ||
if (index !== null && index >= 0 && index < this.ncells()) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
Brian Granger
|
r4299 | }; | ||
Brian Granger
|
r5945 | } | ||
Brian Granger
|
r4299 | |||
David Wyde
|
r10033 | /** | ||
* Get the index of the currently selected cell. | ||||
* @method get_selected_index | ||||
* @return {Number} The selected cell's numeric index | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_selected_index = function () { | ||
Brian E. Granger
|
r4352 | var result = null; | ||
Brian Granger
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
Brian E. Granger
|
r4352 | if ($(this).data("cell").selected === true) { | ||
result = index; | ||||
}; | ||||
}); | ||||
return result; | ||||
}; | ||||
Brian Granger
|
r4292 | |||
Brian Granger
|
r5945 | // Cell selection. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Programmatically select a cell. | ||||
* | ||||
* @method select | ||||
* @param {Number} index A cell's index | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select = function (index) { | ||
Matthias BUSSONNIER
|
r9549 | if (this.is_valid_cell_index(index)) { | ||
Mikhail Korobov
|
r8839 | var sindex = this.get_selected_index() | ||
Brian Granger
|
r5946 | if (sindex !== null && index !== sindex) { | ||
Brian Granger
|
r5945 | this.get_cell(sindex).unselect(); | ||
Brian E. Granger
|
r4352 | }; | ||
Mikhail Korobov
|
r8839 | var cell = this.get_cell(index); | ||
Brian Granger
|
r5994 | cell.select(); | ||
Brian Granger
|
r6028 | if (cell.cell_type === 'heading') { | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':cell.cell_type,level:cell.level} | ||||
); | ||||
Brian Granger
|
r6028 | } else { | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':cell.cell_type} | ||||
); | ||||
}; | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r4292 | return this; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Programmatically select the next cell. | ||||
* | ||||
* @method select_next | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select_next = function () { | ||
var index = this.get_selected_index(); | ||||
Matthias BUSSONNIER
|
r9549 | this.select(index+1); | ||
Stefan van der Walt
|
r5479 | return this; | ||
Brian Granger
|
r4292 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Programmatically select the previous cell. | ||||
* | ||||
* @method select_prev | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select_prev = function () { | ||
var index = this.get_selected_index(); | ||||
Matthias BUSSONNIER
|
r9549 | this.select(index-1); | ||
Brian E. Granger
|
r4352 | return this; | ||
Brian Granger
|
r4292 | }; | ||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r5945 | // Cell movement | ||
Matthias BUSSONNIER
|
r9788 | /** | ||
David Wyde
|
r10033 | * Move given (or selected) cell up and select it. | ||
* | ||||
Matthias BUSSONNIER
|
r9788 | * @method move_cell_up | ||
* @param [index] {integer} cell index | ||||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
Matthias BUSSONNIER
|
r9788 | **/ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.move_cell_up = function (index) { | ||
Matthias BUSSONNIER
|
r9788 | var i = this.index_or_selected(index); | ||
if (this.is_valid_cell_index(i) && i > 0) { | ||||
Brian Granger
|
r5945 | var pivot = this.get_cell_element(i-1); | ||
var tomove = this.get_cell_element(i); | ||||
Brian E. Granger
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.before(tomove); | ||||
this.select(i-1); | ||||
Brian Granger
|
r4292 | }; | ||
Matthias BUSSONNIER
|
r9549 | this.dirty = true; | ||
Brian Granger
|
r4292 | }; | ||
Brian E. Granger
|
r4352 | return this; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r9788 | /** | ||
* Move given (or selected) cell down and select it | ||||
David Wyde
|
r10033 | * | ||
Matthias BUSSONNIER
|
r9788 | * @method move_cell_down | ||
* @param [index] {integer} cell index | ||||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
Matthias BUSSONNIER
|
r9788 | **/ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.move_cell_down = function (index) { | ||
Matthias BUSSONNIER
|
r9788 | var i = this.index_or_selected(index); | ||
Matthias BUSSONNIER
|
r9549 | if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { | ||
Brian Granger
|
r5945 | var pivot = this.get_cell_element(i+1); | ||
var tomove = this.get_cell_element(i); | ||||
Brian E. Granger
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.after(tomove); | ||||
this.select(i+1); | ||||
}; | ||||
}; | ||||
Brian E. Granger
|
r4550 | this.dirty = true; | ||
Brian E. Granger
|
r4352 | return this; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r5945 | // Insertion, deletion. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Delete a cell from the notebook. | ||||
* | ||||
* @method delete_cell | ||||
* @param [index] A cell's numeric index | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.delete_cell = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
David Warde-Farley
|
r8691 | var cell = this.get_selected_cell(); | ||
this.undelete_backup = cell.toJSON(); | ||||
MinRK
|
r9552 | $('#undelete_cell').removeClass('ui-state-disabled'); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var ce = this.get_cell_element(i); | ||||
ce.remove(); | ||||
if (i === (this.ncells())) { | ||||
this.select(i-1); | ||||
David Warde-Farley
|
r8691 | this.undelete_index = i - 1; | ||
this.undelete_below = true; | ||||
Brian Granger
|
r5945 | } else { | ||
this.select(i); | ||||
David Warde-Farley
|
r8691 | this.undelete_index = i; | ||
this.undelete_below = false; | ||||
Brian Granger
|
r5945 | }; | ||
this.dirty = true; | ||||
}; | ||||
return this; | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4299 | |||
Matthias BUSSONNIER
|
r9550 | /** | ||
* Insert a cell so that after insertion the cell is at given index. | ||||
* | ||||
* Similar to insert_above, but index parameter is mandatory | ||||
* | ||||
* Index will be brought back into the accissible range [0,n] | ||||
* | ||||
David Wyde
|
r10033 | * @method insert_cell_at_index | ||
David Wyde
|
r10375 | * @param type {string} in ['code','markdown','heading'] | ||
Bussonnier Matthias
|
r9677 | * @param [index] {int} a valid index where to inser cell | ||
Matthias BUSSONNIER
|
r9550 | * | ||
Bussonnier Matthias
|
r9677 | * @return cell {cell|null} created cell or null | ||
Matthias BUSSONNIER
|
r9550 | **/ | ||
Notebook.prototype.insert_cell_at_index = function(type, index){ | ||||
Bussonnier Matthias
|
r9677 | |||
var ncells = this.ncells(); | ||||
Matthias BUSSONNIER
|
r9550 | var index = Math.min(index,ncells); | ||
index = Math.max(index,0); | ||||
Brian Granger
|
r6017 | var cell = null; | ||
Matthias BUSSONNIER
|
r9550 | |||
Matthias BUSSONNIER
|
r9789 | if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { | ||
Brian Granger
|
r5945 | if (type === 'code') { | ||
Brian Granger
|
r7168 | cell = new IPython.CodeCell(this.kernel); | ||
Brian Granger
|
r5945 | cell.set_input_prompt(); | ||
} else if (type === 'markdown') { | ||||
Brian Granger
|
r7168 | cell = new IPython.MarkdownCell(); | ||
MinRK
|
r6248 | } else if (type === 'raw') { | ||
Brian Granger
|
r7168 | cell = new IPython.RawCell(); | ||
Brian Granger
|
r6019 | } else if (type === 'heading') { | ||
Brian Granger
|
r7168 | cell = new IPython.HeadingCell(); | ||
Bussonnier Matthias
|
r9677 | } | ||
if(this._insert_element_at_index(cell.element,index)){ | ||||
Brian Granger
|
r5946 | cell.render(); | ||
this.select(this.find_cell_index(cell)); | ||||
this.dirty = true; | ||||
Bussonnier Matthias
|
r9677 | } | ||
} | ||||
Brian Granger
|
r6017 | return cell; | ||
Matthias BUSSONNIER
|
r9550 | |||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4507 | |||
Bussonnier Matthias
|
r9677 | /** | ||
* Insert an element at given cell index. | ||||
* | ||||
David Wyde
|
r10033 | * @method _insert_element_at_index | ||
Bussonnier Matthias
|
r9677 | * @param element {dom element} a cell element | ||
* @param [index] {int} a valid index where to inser cell | ||||
* @private | ||||
* | ||||
* return true if everything whent fine. | ||||
**/ | ||||
Notebook.prototype._insert_element_at_index = function(element, index){ | ||||
Matthias BUSSONNIER
|
r9788 | if (element === undefined){ | ||
Matthias BUSSONNIER
|
r9688 | return false; | ||
} | ||||
Bussonnier Matthias
|
r9677 | var ncells = this.ncells(); | ||
Matthias BUSSONNIER
|
r9688 | if (ncells === 0) { | ||
// special case append if empty | ||||
this.element.find('div.end_space').before(element); | ||||
Matthias BUSSONNIER
|
r9788 | } else if ( ncells === index ) { | ||
Matthias BUSSONNIER
|
r9688 | // special case append it the end, but not empty | ||
this.get_cell_element(index-1).after(element); | ||||
} else if (this.is_valid_cell_index(index)) { | ||||
// otherwise always somewhere to append to | ||||
this.get_cell_element(index).before(element); | ||||
} else { | ||||
return false; | ||||
} | ||||
Brian E. Granger
|
r4507 | |||
David Warde-Farley
|
r8694 | if (this.undelete_index !== null && index <= this.undelete_index) { | ||
this.undelete_index = this.undelete_index + 1; | ||||
Bussonnier Matthias
|
r9677 | this.dirty = true; | ||
David Warde-Farley
|
r8694 | } | ||
Matthias BUSSONNIER
|
r9688 | return true; | ||
Matthias BUSSONNIER
|
r9550 | }; | ||
/** | ||||
* Insert a cell of given type above given index, or at top | ||||
* of notebook if index smaller than 0. | ||||
* | ||||
* default index value is the one of currently selected cell | ||||
* | ||||
David Wyde
|
r10033 | * @method insert_cell_above | ||
Matthias BUSSONNIER
|
r9550 | * @param type {string} cell type | ||
* @param [index] {integer} | ||||
* | ||||
* @return handle to created cell or null | ||||
**/ | ||||
Notebook.prototype.insert_cell_above = function (type, index) { | ||||
index = this.index_or_selected(index); | ||||
return this.insert_cell_at_index(type, index); | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4507 | |||
Bussonnier Matthias
|
r9677 | /** | ||
* Insert a cell of given type below given index, or at bottom | ||||
* of notebook if index greater thatn number of cell | ||||
* | ||||
* default index value is the one of currently selected cell | ||||
* | ||||
* @method insert_cell_below | ||||
* @param type {string} cell type | ||||
* @param [index] {integer} | ||||
* | ||||
* @return handle to created cell or null | ||||
* | ||||
**/ | ||||
Notebook.prototype.insert_cell_below = function (type, index) { | ||||
index = this.index_or_selected(index); | ||||
return this.insert_cell_at_index(type, index+1); | ||||
}; | ||||
/** | ||||
* Insert cell at end of notebook | ||||
* | ||||
* @method insert_cell_at_bottom | ||||
David Wyde
|
r10033 | * @param {String} type cell type | ||
Bussonnier Matthias
|
r9677 | * | ||
* @return the added cell; or null | ||||
**/ | ||||
Notebook.prototype.insert_cell_at_bottom = function (type){ | ||||
var len = this.ncells(); | ||||
return this.insert_cell_below(type,len-1); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a code cell. | ||||
* | ||||
* @method to_code | ||||
* @param {Number} [index] A cell's index | ||||
*/ | ||||
Brian E. Granger
|
r4507 | Notebook.prototype.to_code = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
if (!(source_cell instanceof IPython.CodeCell)) { | ||||
Mikhail Korobov
|
r8839 | var target_cell = this.insert_cell_below('code',i); | ||
Brian Granger
|
r5945 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
} | ||||
target_cell.set_text(text); | ||||
Paul Ivanov
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
target_cell.code_mirror.clearHistory(); | ||||
Brian Granger
|
r5945 | source_element.remove(); | ||
Brian Granger
|
r6017 | this.dirty = true; | ||
Brian Granger
|
r5945 | }; | ||
Brian E. Granger
|
r4352 | }; | ||
}; | ||||
Brian Granger
|
r4299 | |||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a Markdown cell. | ||||
* | ||||
* @method to_markdown | ||||
* @param {Number} [index] A cell's index | ||||
*/ | ||||
Brian Granger
|
r4508 | Notebook.prototype.to_markdown = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
if (!(source_cell instanceof IPython.MarkdownCell)) { | ||||
Mikhail Korobov
|
r8839 | var target_cell = this.insert_cell_below('markdown',i); | ||
Brian Granger
|
r5945 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
Brian Granger
|
r5946 | text = ''; | ||
Brian Granger
|
r5945 | }; | ||
Brian Granger
|
r6017 | // The edit must come before the set_text. | ||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
Paul Ivanov
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
target_cell.code_mirror.clearHistory(); | ||||
Brian Granger
|
r6017 | source_element.remove(); | ||
Brian Granger
|
r5945 | this.dirty = true; | ||
Brian Granger
|
r5944 | }; | ||
}; | ||||
Brian E. Granger
|
r4507 | }; | ||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a raw text cell. | ||||
* | ||||
* @method to_raw | ||||
* @param {Number} [index] A cell's index | ||||
*/ | ||||
MinRK
|
r6248 | Notebook.prototype.to_raw = function (index) { | ||
Brian Granger
|
r6017 | var i = this.index_or_selected(index); | ||
if (this.is_valid_cell_index(i)) { | ||||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
var target_cell = null; | ||||
MinRK
|
r6248 | if (!(source_cell instanceof IPython.RawCell)) { | ||
target_cell = this.insert_cell_below('raw',i); | ||||
Brian Granger
|
r6017 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
}; | ||||
// The edit must come before the set_text. | ||||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
Paul Ivanov
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
target_cell.code_mirror.clearHistory(); | ||||
Brian Granger
|
r6017 | source_element.remove(); | ||
this.dirty = true; | ||||
}; | ||||
}; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a heading cell. | ||||
* | ||||
* @method to_heading | ||||
* @param {Number} [index] A cell's index | ||||
* @param {Number} [level] A heading level (e.g., 1 becomes <h1>) | ||||
*/ | ||||
Brian Granger
|
r6019 | Notebook.prototype.to_heading = function (index, level) { | ||
level = level || 1; | ||||
var i = this.index_or_selected(index); | ||||
if (this.is_valid_cell_index(i)) { | ||||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
var target_cell = null; | ||||
if (source_cell instanceof IPython.HeadingCell) { | ||||
source_cell.set_level(level); | ||||
} else { | ||||
target_cell = this.insert_cell_below('heading',i); | ||||
var text = source_cell.get_text(); | ||||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
}; | ||||
// The edit must come before the set_text. | ||||
target_cell.set_level(level); | ||||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
Paul Ivanov
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
target_cell.code_mirror.clearHistory(); | ||||
Brian Granger
|
r6019 | source_element.remove(); | ||
this.dirty = true; | ||||
}; | ||||
Brian Granger
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':'heading',level:level} | ||||
); | ||||
Brian Granger
|
r6019 | }; | ||
}; | ||||
Brian Granger
|
r5945 | // Cut/Copy/Paste | ||
Brian Granger
|
r5879 | |||
David Wyde
|
r10033 | /** | ||
* Enable UI elements for pasting cells. | ||||
* | ||||
* @method enable_paste | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.enable_paste = function () { | ||
var that = this; | ||||
Brian Granger
|
r5912 | if (!this.paste_enabled) { | ||
David Warde-Farley
|
r8715 | $('#paste_cell_replace').removeClass('ui-state-disabled') | ||
.on('click', function () {that.paste_cell_replace();}); | ||||
Brian Granger
|
r5912 | $('#paste_cell_above').removeClass('ui-state-disabled') | ||
.on('click', function () {that.paste_cell_above();}); | ||||
$('#paste_cell_below').removeClass('ui-state-disabled') | ||||
.on('click', function () {that.paste_cell_below();}); | ||||
this.paste_enabled = true; | ||||
}; | ||||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
* Disable UI elements for pasting cells. | ||||
* | ||||
* @method disable_paste | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.disable_paste = function () { | ||
Brian Granger
|
r5912 | if (this.paste_enabled) { | ||
David Warde-Farley
|
r8715 | $('#paste_cell_replace').addClass('ui-state-disabled').off('click'); | ||
Brian Granger
|
r5912 | $('#paste_cell_above').addClass('ui-state-disabled').off('click'); | ||
$('#paste_cell_below').addClass('ui-state-disabled').off('click'); | ||||
this.paste_enabled = false; | ||||
}; | ||||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
* Cut a cell. | ||||
* | ||||
* @method cut_cell | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.cut_cell = function () { | ||
this.copy_cell(); | ||||
this.delete_cell(); | ||||
} | ||||
David Wyde
|
r10033 | /** | ||
* Copy a cell. | ||||
* | ||||
* @method copy_cell | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.copy_cell = function () { | ||
Brian Granger
|
r5945 | var cell = this.get_selected_cell(); | ||
Brian Granger
|
r5879 | this.clipboard = cell.toJSON(); | ||
this.enable_paste(); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Replace the selected cell with a cell in the clipboard. | ||||
* | ||||
* @method paste_cell_replace | ||||
*/ | ||||
David Warde-Farley
|
r8715 | Notebook.prototype.paste_cell_replace = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Mikhail Korobov
|
r8839 | var old_cell = this.get_next_cell(new_cell); | ||
Brian Granger
|
r5945 | this.delete_cell(this.find_cell_index(old_cell)); | ||
this.select(this.find_cell_index(new_cell)); | ||||
Brian Granger
|
r5879 | }; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Paste a cell from the clipboard above the selected cell. | ||||
* | ||||
* @method paste_cell_above | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.paste_cell_above = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Brian Granger
|
r5879 | }; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Paste a cell from the clipboard below the selected cell. | ||||
* | ||||
* @method paste_cell_below | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.paste_cell_below = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_below(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Brian Granger
|
r5879 | }; | ||
}; | ||||
David Warde-Farley
|
r8691 | // Cell undelete | ||
David Wyde
|
r10033 | /** | ||
* Restore the most recently deleted cell. | ||||
* | ||||
* @method undelete | ||||
*/ | ||||
David Warde-Farley
|
r8691 | Notebook.prototype.undelete = function() { | ||
if (this.undelete_backup !== null && this.undelete_index !== null) { | ||||
var current_index = this.get_selected_index(); | ||||
if (this.undelete_index < current_index) { | ||||
current_index = current_index + 1; | ||||
} | ||||
if (this.undelete_index >= this.ncells()) { | ||||
this.select(this.ncells() - 1); | ||||
} | ||||
else { | ||||
this.select(this.undelete_index); | ||||
} | ||||
var cell_data = this.undelete_backup; | ||||
var new_cell = null; | ||||
if (this.undelete_below) { | ||||
new_cell = this.insert_cell_below(cell_data.cell_type); | ||||
} else { | ||||
new_cell = this.insert_cell_above(cell_data.cell_type); | ||||
} | ||||
new_cell.fromJSON(cell_data); | ||||
this.select(current_index); | ||||
this.undelete_backup = null; | ||||
this.undelete_index = null; | ||||
} | ||||
MinRK
|
r9552 | $('#undelete_cell').addClass('ui-state-disabled'); | ||
David Warde-Farley
|
r8691 | } | ||
Brian Granger
|
r5879 | |||
Brian Granger
|
r5945 | // Split/merge | ||
David Wyde
|
r10033 | /** | ||
* Split the selected cell into two, at the cursor. | ||||
* | ||||
* @method split_cell | ||||
*/ | ||||
Brian Granger
|
r5897 | Notebook.prototype.split_cell = function () { | ||
Brian Granger
|
r5898 | // Todo: implement spliting for other cell types. | ||
Brian Granger
|
r5945 | var cell = this.get_selected_cell(); | ||
Brian Granger
|
r5946 | if (cell.is_splittable()) { | ||
Mikhail Korobov
|
r8839 | var texta = cell.get_pre_cursor(); | ||
var textb = cell.get_post_cursor(); | ||||
Brian Granger
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(texta); | ||||
var new_cell = this.insert_cell_below('code'); | ||||
new_cell.set_text(textb); | ||||
} else if (cell instanceof IPython.MarkdownCell) { | ||||
cell.set_text(texta); | ||||
cell.render(); | ||||
var new_cell = this.insert_cell_below('markdown'); | ||||
new_cell.edit(); // editor must be visible to call set_text | ||||
new_cell.set_text(textb); | ||||
new_cell.render(); | ||||
David Wyde
|
r10375 | } | ||
Brian Granger
|
r5897 | }; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Combine the selected cell into the cell above it. | ||||
* | ||||
* @method merge_cell_above | ||||
*/ | ||||
Brian Granger
|
r5898 | Notebook.prototype.merge_cell_above = function () { | ||
Brian Granger
|
r5945 | var index = this.get_selected_index(); | ||
Brian Granger
|
r5946 | var cell = this.get_cell(index); | ||
Brian Granger
|
r5898 | if (index > 0) { | ||
Mikhail Korobov
|
r8839 | var upper_cell = this.get_cell(index-1); | ||
var upper_text = upper_cell.get_text(); | ||||
var text = cell.get_text(); | ||||
Brian Granger
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(upper_text+'\n'+text); | ||||
David Wyde
|
r10375 | } else if (cell instanceof IPython.MarkdownCell) { | ||
Brian Granger
|
r5946 | cell.edit(); | ||
cell.set_text(upper_text+'\n'+text); | ||||
cell.render(); | ||||
Brian Granger
|
r5898 | }; | ||
Brian Granger
|
r5946 | this.delete_cell(index-1); | ||
this.select(this.find_cell_index(cell)); | ||||
Brian Granger
|
r5898 | }; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Combine the selected cell into the cell below it. | ||||
* | ||||
* @method merge_cell_below | ||||
*/ | ||||
Brian Granger
|
r5898 | Notebook.prototype.merge_cell_below = function () { | ||
Brian Granger
|
r5945 | var index = this.get_selected_index(); | ||
Brian Granger
|
r5946 | var cell = this.get_cell(index); | ||
Brian Granger
|
r5898 | if (index < this.ncells()-1) { | ||
Mikhail Korobov
|
r8839 | var lower_cell = this.get_cell(index+1); | ||
var lower_text = lower_cell.get_text(); | ||||
var text = cell.get_text(); | ||||
Brian Granger
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(text+'\n'+lower_text); | ||||
David Wyde
|
r10375 | } else if (cell instanceof IPython.MarkdownCell) { | ||
Brian Granger
|
r5946 | cell.edit(); | ||
cell.set_text(text+'\n'+lower_text); | ||||
cell.render(); | ||||
Brian Granger
|
r5898 | }; | ||
Brian Granger
|
r5946 | this.delete_cell(index+1); | ||
this.select(this.find_cell_index(cell)); | ||||
Brian Granger
|
r5898 | }; | ||
}; | ||||
Brian Granger
|
r5959 | |||
Brian E. Granger
|
r4543 | // Cell collapsing and output clearing | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Hide a cell's output. | ||||
* | ||||
* @method collapse | ||||
* @param {Number} index A cell's numeric index | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.collapse = function (index) { | ||
var i = this.index_or_selected(index); | ||||
Brian Granger
|
r5945 | this.get_cell(i).collapse(); | ||
Brian E. Granger
|
r4550 | this.dirty = true; | ||
Brian Granger
|
r4299 | }; | ||
David Wyde
|
r10033 | /** | ||
* Show a cell's output. | ||||
* | ||||
* @method expand | ||||
* @param {Number} index A cell's numeric index | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.expand = function (index) { | ||
var i = this.index_or_selected(index); | ||||
Brian Granger
|
r5945 | this.get_cell(i).expand(); | ||
Brian E. Granger
|
r4550 | this.dirty = true; | ||
Brian E. Granger
|
r4352 | }; | ||
David Wyde
|
r10033 | /** Toggle whether a cell's output is collapsed or expanded. | ||
* | ||||
* @method toggle_output | ||||
* @param {Number} index A cell's numeric index | ||||
*/ | ||||
Brian E. Granger
|
r4639 | Notebook.prototype.toggle_output = function (index) { | ||
var i = this.index_or_selected(index); | ||||
Brian Granger
|
r5945 | this.get_cell(i).toggle_output(); | ||
Brian E. Granger
|
r4639 | this.dirty = true; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Toggle a scrollbar for long cell outputs. | ||||
* | ||||
* @method toggle_output_scroll | ||||
* @param {Number} index A cell's numeric index | ||||
*/ | ||||
MinRK
|
r7429 | Notebook.prototype.toggle_output_scroll = function (index) { | ||
var i = this.index_or_selected(index); | ||||
this.get_cell(i).toggle_output_scroll(); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Hide each code cell's output area. | ||||
* | ||||
* @method collapse_all_output | ||||
*/ | ||||
MinRK
|
r7362 | Notebook.prototype.collapse_all_output = function () { | ||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.collapse(); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Expand each code cell's output area, and add a scrollbar for long output. | ||||
* | ||||
* @method scroll_all_output | ||||
*/ | ||||
MinRK
|
r7362 | Notebook.prototype.scroll_all_output = function () { | ||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.expand(); | ||||
cells[i].output_area.scroll_if_long(20); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Expand each code cell's output area, and remove scrollbars. | ||||
* | ||||
* @method expand_all_output | ||||
*/ | ||||
MinRK
|
r7362 | Notebook.prototype.expand_all_output = function () { | ||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.expand(); | ||||
cells[i].output_area.unscroll_area(); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Clear each code cell's output area. | ||||
* | ||||
* @method clear_all_output | ||||
*/ | ||||
Brian E. Granger
|
r4543 | Notebook.prototype.clear_all_output = function () { | ||
var ncells = this.ncells(); | ||||
Brian Granger
|
r5945 | var cells = this.get_cells(); | ||
Brian E. Granger
|
r4543 | for (var i=0; i<ncells; i++) { | ||
if (cells[i] instanceof IPython.CodeCell) { | ||||
MinRK
|
r5085 | cells[i].clear_output(true,true,true); | ||
Paul Ivanov
|
r6545 | // Make all In[] prompts blank, as well | ||
// TODO: make this configurable (via checkbox?) | ||||
cells[i].set_input_prompt(); | ||||
Brian E. Granger
|
r4543 | } | ||
}; | ||||
Brian E. Granger
|
r4550 | this.dirty = true; | ||
Brian E. Granger
|
r4543 | }; | ||
Brian Granger
|
r5959 | |||
Fernando Perez
|
r5020 | // Other cell functions: line numbers, ... | ||
David Wyde
|
r10033 | /** | ||
* Toggle line numbers in the selected cell's input area. | ||||
* | ||||
* @method cell_toggle_line_numbers | ||||
*/ | ||||
Fernando Perez
|
r5020 | Notebook.prototype.cell_toggle_line_numbers = function() { | ||
Brian Granger
|
r5945 | this.get_selected_cell().toggle_line_numbers(); | ||
Fernando Perez
|
r5020 | }; | ||
Brian E. Granger
|
r4543 | |||
Brian E. Granger
|
r4352 | // Kernel related things | ||
Brian Granger
|
r4315 | |||
David Wyde
|
r10033 | /** | ||
* Start a new kernel and set it on each code cell. | ||||
* | ||||
* @method start_kernel | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.start_kernel = function () { | ||
Matthias BUSSONNIER
|
r7170 | var base_url = $('body').data('baseKernelUrl') + "kernels"; | ||
Brian Granger
|
r7168 | this.kernel = new IPython.Kernel(base_url); | ||
this.kernel.start(this.notebook_id); | ||||
Brian Granger
|
r7197 | // Now that the kernel has been created, tell the CodeCells about it. | ||
var ncells = this.ncells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
var cell = this.get_cell(i); | ||||
if (cell instanceof IPython.CodeCell) { | ||||
cell.set_kernel(this.kernel) | ||||
}; | ||||
}; | ||||
Brian E. Granger
|
r4545 | }; | ||
David Wyde
|
r10033 | /** | ||
* Prompt the user to restart the IPython kernel. | ||||
* | ||||
* @method restart_kernel | ||||
*/ | ||||
Brian E. Granger
|
r4545 | Notebook.prototype.restart_kernel = function () { | ||
Fernando Perez
|
r5025 | var that = this; | ||
Fernando Perez
|
r5021 | var dialog = $('<div/>'); | ||
dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.'); | ||||
$(document).append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Restart kernel or continue running?", | ||||
Brian Granger
|
r5858 | closeText: '', | ||
Fernando Perez
|
r5021 | buttons : { | ||
"Restart": function () { | ||||
Brian Granger
|
r7168 | that.kernel.restart(); | ||
Brian E. Granger
|
r4545 | $(this).dialog('close'); | ||
}, | ||||
Fernando Perez
|
r5022 | "Continue running": function () { | ||
Brian E. Granger
|
r4545 | $(this).dialog('close'); | ||
} | ||||
} | ||||
}); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Run the selected cell. | ||||
* | ||||
David Wyde
|
r10127 | * Execute or render cell outputs. | ||
David Wyde
|
r10033 | * | ||
* @method execute_selected_cell | ||||
* @param {Object} options Customize post-execution behavior | ||||
*/ | ||||
Brian E. Granger
|
r4394 | Notebook.prototype.execute_selected_cell = function (options) { | ||
// add_new: should a new cell be added if we are at the end of the nb | ||||
// terminal: execute in terminal mode, which stays in the current cell | ||||
Mikhail Korobov
|
r8839 | var default_options = {terminal: false, add_new: true}; | ||
Stefan van der Walt
|
r5479 | $.extend(default_options, options); | ||
Brian E. Granger
|
r4378 | var that = this; | ||
Brian Granger
|
r5945 | var cell = that.get_selected_cell(); | ||
Brian E. Granger
|
r4378 | var cell_index = that.find_cell_index(cell); | ||
if (cell instanceof IPython.CodeCell) { | ||||
Matthias BUSSONNIER
|
r7170 | cell.execute(); | ||
Brian E. Granger
|
r4378 | } | ||
Brian E. Granger
|
r4394 | if (default_options.terminal) { | ||
Brian E. Granger
|
r4675 | cell.select_all(); | ||
Brian E. Granger
|
r4378 | } else { | ||
Brian E. Granger
|
r4394 | if ((cell_index === (that.ncells()-1)) && default_options.add_new) { | ||
Brian Granger
|
r5945 | that.insert_cell_below('code'); | ||
Brian E. Granger
|
r4394 | // If we are adding a new cell at the end, scroll down to show it. | ||
that.scroll_to_bottom(); | ||||
} else { | ||||
that.select(cell_index+1); | ||||
}; | ||||
Brian E. Granger
|
r4378 | }; | ||
Brian E. Granger
|
r4550 | this.dirty = true; | ||
Brian E. Granger
|
r4378 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute all cells below the selected cell. | ||||
* | ||||
* @method execute_cells_below | ||||
*/ | ||||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cells_below = function () { | ||
this.execute_cell_range(this.get_selected_index(), this.ncells()); | ||||
Matthias BUSSONNIER
|
r8821 | this.scroll_to_bottom(); | ||
Paul Ivanov
|
r8606 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute all cells above the selected cell. | ||||
* | ||||
* @method execute_cells_above | ||||
*/ | ||||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cells_above = function () { | ||
this.execute_cell_range(0, this.get_selected_index()); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Execute all cells. | ||||
* | ||||
* @method execute_all_cells | ||||
*/ | ||||
Brian E. Granger
|
r4378 | Notebook.prototype.execute_all_cells = function () { | ||
Paul Ivanov
|
r8606 | this.execute_cell_range(0, this.ncells()); | ||
Matthias BUSSONNIER
|
r9816 | this.scroll_to_bottom(); | ||
Paul Ivanov
|
r8606 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute a contiguous range of cells. | ||||
* | ||||
* @method execute_cell_range | ||||
* @param {Number} start Index of the first cell to execute (inclusive) | ||||
* @param {Number} end Index of the last cell to execute (exclusive) | ||||
*/ | ||||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cell_range = function (start, end) { | ||
for (var i=start; i<end; i++) { | ||||
Brian E. Granger
|
r4378 | this.select(i); | ||
Matthias BUSSONNIER
|
r5967 | this.execute_selected_cell({add_new:false}); | ||
Brian E. Granger
|
r4378 | }; | ||
}; | ||||
Brian E. Granger
|
r4352 | // Persistance and loading | ||
David Wyde
|
r10033 | /** | ||
* Getter method for this notebook's ID. | ||||
* | ||||
* @method get_notebook_id | ||||
* @return {String} This notebook's ID | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.get_notebook_id = function () { | ||
return this.notebook_id; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Getter method for this notebook's name. | ||||
* | ||||
* @method get_notebook_name | ||||
* @return {String} This notebook's name | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.get_notebook_name = function () { | ||
return this.notebook_name; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Setter method for this notebook's name. | ||||
* | ||||
* @method set_notebook_name | ||||
* @param {String} name A new name for this notebook | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.set_notebook_name = function (name) { | ||
this.notebook_name = name; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Check that a notebook's name is valid. | ||||
* | ||||
* @method test_notebook_name | ||||
* @param {String} nbname A name for this notebook | ||||
* @return {Boolean} True if the name is valid, false if invalid | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.test_notebook_name = function (nbname) { | ||
nbname = nbname || ''; | ||||
if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
}; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Load a notebook from JSON (.ipynb). | ||||
* | ||||
* This currently handles one worksheet: others are deleted. | ||||
* | ||||
* @method fromJSON | ||||
* @param {Object} data JSON representation of a notebook | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.fromJSON = function (data) { | ||
var ncells = this.ncells(); | ||||
Stefan van der Walt
|
r5479 | var i; | ||
for (i=0; i<ncells; i++) { | ||||
Brian E. Granger
|
r4352 | // Always delete cell 0 as they get renumbered as they are deleted. | ||
this.delete_cell(0); | ||||
}; | ||||
Brian Granger
|
r6047 | // Save the metadata and name. | ||
Brian E. Granger
|
r4637 | this.metadata = data.metadata; | ||
Brian Granger
|
r6047 | this.notebook_name = data.metadata.name; | ||
Brian E. Granger
|
r4484 | // Only handle 1 worksheet for now. | ||
var worksheet = data.worksheets[0]; | ||||
if (worksheet !== undefined) { | ||||
MinRK
|
r7523 | if (worksheet.metadata) { | ||
this.worksheet_metadata = worksheet.metadata; | ||||
} | ||||
Brian E. Granger
|
r4484 | var new_cells = worksheet.cells; | ||
ncells = new_cells.length; | ||||
var cell_data = null; | ||||
Brian E. Granger
|
r4488 | var new_cell = null; | ||
Stefan van der Walt
|
r5479 | for (i=0; i<ncells; i++) { | ||
Brian E. Granger
|
r4484 | cell_data = new_cells[i]; | ||
MinRK
|
r6477 | // VERSIONHACK: plaintext -> raw | ||
// handle never-released plaintext name for raw cells | ||||
MinRK
|
r6255 | if (cell_data.cell_type === 'plaintext'){ | ||
cell_data.cell_type = 'raw'; | ||||
} | ||||
Mikhail Korobov
|
r8839 | |||
Brian Granger
|
r5945 | new_cell = this.insert_cell_below(cell_data.cell_type); | ||
new_cell.fromJSON(cell_data); | ||||
Brian Granger
|
r5879 | }; | ||
Brian E. Granger
|
r4352 | }; | ||
MinRK
|
r7610 | if (data.worksheets.length > 1) { | ||
var dialog = $('<div/>'); | ||||
dialog.html("This notebook has " + data.worksheets.length + " worksheets, " + | ||||
"but this version of IPython can only handle the first. " + | ||||
"If you save this notebook, worksheets after the first will be lost." | ||||
); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Multiple worksheets", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
} | ||||
Brian Granger
|
r4315 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Dump this notebook into a JSON-friendly object. | ||||
* | ||||
* @method toJSON | ||||
* @return {Object} A JSON-friendly representation of this notebook. | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.toJSON = function () { | ||
Brian Granger
|
r5945 | var cells = this.get_cells(); | ||
Brian E. Granger
|
r4352 | var ncells = cells.length; | ||
Brian Granger
|
r7179 | var cell_array = new Array(ncells); | ||
Brian E. Granger
|
r4352 | for (var i=0; i<ncells; i++) { | ||
cell_array[i] = cells[i].toJSON(); | ||||
}; | ||||
Brian Granger
|
r7179 | var data = { | ||
Brian E. Granger
|
r4484 | // Only handle 1 worksheet for now. | ||
MinRK
|
r7523 | worksheets : [{ | ||
cells: cell_array, | ||||
metadata: this.worksheet_metadata | ||||
}], | ||||
Brian E. Granger
|
r4637 | metadata : this.metadata | ||
Stefan van der Walt
|
r5479 | }; | ||
return data; | ||||
Brian E. Granger
|
r4484 | }; | ||
David Wyde
|
r10033 | /** | ||
MinRK
|
r10505 | * Start an autosave timer, for periodically saving the notebook. | ||
* | ||||
MinRK
|
r10508 | * @method set_autosave_interval | ||
MinRK
|
r10505 | * @param {Integer} interval the autosave interval in milliseconds | ||
*/ | ||||
MinRK
|
r10508 | Notebook.prototype.set_autosave_interval = function (interval) { | ||
MinRK
|
r10505 | var that = this; | ||
// clear previous interval, so we don't get simultaneous timers | ||||
if (this.autosave_timer) { | ||||
clearInterval(this.autosave_timer); | ||||
} | ||||
MinRK
|
r10508 | this.autosave_interval = this.minimum_autosave_interval = interval; | ||
MinRK
|
r10505 | if (interval) { | ||
this.autosave_timer = setInterval(function() { | ||||
MinRK
|
r10506 | if (that.dirty) { | ||
that.save_notebook(); | ||||
} | ||||
MinRK
|
r10505 | }, interval); | ||
$([IPython.events]).trigger("autosave_enabled.Notebook", interval); | ||||
} else { | ||||
this.autosave_timer = null; | ||||
$([IPython.events]).trigger("autosave_disabled.Notebook"); | ||||
}; | ||||
}; | ||||
/** | ||||
David Wyde
|
r10033 | * Save this notebook on the server. | ||
* | ||||
* @method save_notebook | ||||
*/ | ||||
Brian E. Granger
|
r4484 | Notebook.prototype.save_notebook = function () { | ||
Brian Granger
|
r5958 | // We may want to move the name/id/nbformat logic inside toJSON? | ||
var data = this.toJSON(); | ||||
Brian Granger
|
r6047 | data.metadata.name = this.notebook_name; | ||
Brian Granger
|
r6061 | data.nbformat = this.nbformat; | ||
MinRK
|
r7546 | data.nbformat_minor = this.nbformat_minor; | ||
MinRK
|
r10505 | |||
// time the ajax call for autosave tuning purposes. | ||||
var start = new Date().getTime(); | ||||
Brian Granger
|
r5958 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "PUT", | ||||
data : JSON.stringify(data), | ||||
headers : {'Content-Type': 'application/json'}, | ||||
MinRK
|
r10505 | success : $.proxy(this.save_notebook_success, this, start), | ||
error : $.proxy(this.save_notebook_error, this) | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('notebook_saving.Notebook'); | ||
Bussonnier Matthias
|
r9503 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; | ||
Brian Granger
|
r5958 | $.ajax(url, settings); | ||
Brian Granger
|
r4315 | }; | ||
MinRK
|
r10501 | |||
David Wyde
|
r10033 | /** | ||
* Success callback for saving a notebook. | ||||
* | ||||
* @method save_notebook_success | ||||
MinRK
|
r10505 | * @param {Integer} start the time when the save request started | ||
David Wyde
|
r10033 | * @param {Object} data JSON representation of a notebook | ||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
MinRK
|
r10505 | Notebook.prototype.save_notebook_success = function (start, data, status, xhr) { | ||
Brian E. Granger
|
r4550 | this.dirty = false; | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('notebook_saved.Notebook'); | ||
MinRK
|
r10505 | this._update_autosave_interval(start); | ||
MinRK
|
r10501 | if (this._checkpoint_after_save) { | ||
this.create_checkpoint(); | ||||
this._checkpoint_after_save = false; | ||||
}; | ||||
Stefan van der Walt
|
r5479 | }; | ||
MinRK
|
r10501 | |||
David Wyde
|
r10033 | /** | ||
MinRK
|
r10505 | * update the autosave interval based on how long the last save took | ||
* | ||||
* @method _update_autosave_interval | ||||
* @param {Integer} timestamp when the save request started | ||||
*/ | ||||
Notebook.prototype._update_autosave_interval = function (start) { | ||||
var duration = (new Date().getTime() - start); | ||||
if (this.autosave_interval) { | ||||
// new save interval: higher of 10x save duration or parameter (default 30 seconds) | ||||
var interval = Math.max(10 * duration, this.minimum_autosave_interval); | ||||
// round to 10 seconds, otherwise we will be setting a new interval too often | ||||
interval = 10000 * Math.round(interval / 10000); | ||||
// set new interval, if it's changed | ||||
if (interval != this.autosave_interval) { | ||||
MinRK
|
r10508 | this.set_autosave_interval(interval); | ||
MinRK
|
r10505 | } | ||
} | ||||
}; | ||||
/** | ||||
David Wyde
|
r10033 | * Failure callback for saving a notebook. | ||
* | ||||
* @method save_notebook_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} status Description of response status | ||||
* @param {String} error_msg HTTP error message | ||||
*/ | ||||
Brian Granger
|
r6061 | Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) { | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('notebook_save_failed.Notebook'); | ||
Stefan van der Walt
|
r5479 | }; | ||
MinRK
|
r10501 | |||
David Wyde
|
r10033 | /** | ||
* Request a notebook's data from the server. | ||||
* | ||||
* @method load_notebook | ||||
* @param {String} notebook_id A notebook to load | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.load_notebook = function (notebook_id) { | ||
Brian E. Granger
|
r4488 | var that = this; | ||
Brian Granger
|
r6047 | this.notebook_id = notebook_id; | ||
Brian E. Granger
|
r4352 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
Brian E. Granger
|
r4488 | processData : false, | ||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
Brian Granger
|
r6061 | success : $.proxy(this.load_notebook_success,this), | ||
error : $.proxy(this.load_notebook_error,this), | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('notebook_loading.Notebook'); | ||
Bussonnier Matthias
|
r9503 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; | ||
Brian E. Granger
|
r5106 | $.ajax(url, settings); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Success callback for loading a notebook from the server. | ||||
* | ||||
* Load notebook data from the JSON response. | ||||
* | ||||
* @method load_notebook_success | ||||
* @param {Object} data JSON representation of a notebook | ||||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
Brian Granger
|
r6047 | Notebook.prototype.load_notebook_success = function (data, status, xhr) { | ||
Brian E. Granger
|
r4484 | this.fromJSON(data); | ||
if (this.ncells() === 0) { | ||||
Brian Granger
|
r5945 | this.insert_cell_below('code'); | ||
Brian E. Granger
|
r4484 | }; | ||
Brian E. Granger
|
r4550 | this.dirty = false; | ||
Brian Granger
|
r5949 | this.select(0); | ||
Brian Granger
|
r5950 | this.scroll_to_top(); | ||
Brian Granger
|
r6061 | if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) { | ||
msg = "This notebook has been converted from an older " + | ||||
"notebook format (v"+data.orig_nbformat+") to the current notebook " + | ||||
"format (v"+data.nbformat+"). The next time you save this notebook, the " + | ||||
"newer notebook format will be used and older verions of IPython " + | ||||
"may not be able to read it. To keep the older version, close the " + | ||||
"notebook without saving it."; | ||||
var dialog = $('<div/>'); | ||||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Notebook converted", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
MinRK
|
r7546 | } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) { | ||
var that = this; | ||||
var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor; | ||||
var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor; | ||||
Mikhail Korobov
|
r8839 | var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + | ||
MinRK
|
r7546 | this_vs + ". You can still work with this notebook, but some features " + | ||
"introduced in later notebook versions may not be available." | ||||
Mikhail Korobov
|
r8839 | |||
MinRK
|
r7546 | var dialog = $('<div/>'); | ||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Newer Notebook", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
Mikhail Korobov
|
r8839 | |||
Brian Granger
|
r6061 | } | ||
MinRK
|
r10503 | |||
Brian Granger
|
r7197 | // Create the kernel after the notebook is completely loaded to prevent | ||
// code execution upon loading, which is a security risk. | ||||
if (! this.read_only) { | ||||
this.start_kernel(); | ||||
MinRK
|
r10503 | // load our checkpoint list | ||
IPython.notebook.list_checkpoints(); | ||||
Brian Granger
|
r7197 | } | ||
Brian Granger
|
r6047 | $([IPython.events]).trigger('notebook_loaded.Notebook'); | ||
Brian E. Granger
|
r4484 | }; | ||
David Wyde
|
r10033 | /** | ||
* Failure callback for loading a notebook from the server. | ||||
* | ||||
* @method load_notebook_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} textStatus Description of response status | ||||
* @param {String} errorThrow HTTP error message | ||||
*/ | ||||
Brian Granger
|
r6061 | Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) { | ||
if (xhr.status === 500) { | ||||
Mikhail Korobov
|
r8839 | var msg = "An error occurred while loading this notebook. Most likely " + | ||
Brian Granger
|
r6061 | "this notebook is in a newer format than is supported by this " + | ||
"version of IPython. This version can load notebook formats " + | ||||
"v"+this.nbformat+" or earlier."; | ||||
var dialog = $('<div/>'); | ||||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Error loading notebook", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
} | ||||
} | ||||
Mikhail Korobov
|
r8839 | |||
MinRK
|
r10501 | /********************* checkpoint-related *********************/ | ||
/** | ||||
* Save the notebook then immediately create a checkpoint. | ||||
* | ||||
* @method save_checkpoint | ||||
*/ | ||||
Notebook.prototype.save_checkpoint = function () { | ||||
this._checkpoint_after_save = true; | ||||
this.save_notebook(); | ||||
}; | ||||
/** | ||||
* List checkpoints for this notebook. | ||||
* | ||||
* @method list_checkpoint | ||||
*/ | ||||
Notebook.prototype.list_checkpoints = function () { | ||||
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints'; | ||||
$.get(url).done( | ||||
$.proxy(this.list_checkpoints_success, this) | ||||
).fail( | ||||
$.proxy(this.list_checkpoints_error, this) | ||||
); | ||||
}; | ||||
/** | ||||
* Success callback for listing checkpoints. | ||||
* | ||||
* @method list_checkpoint_success | ||||
* @param {Object} data JSON representation of a checkpoint | ||||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
Notebook.prototype.list_checkpoints_success = function (data, status, xhr) { | ||||
var data = $.parseJSON(data); | ||||
if (data.length) { | ||||
this.last_checkpoint = data[0]; | ||||
} else { | ||||
this.last_checkpoint = null; | ||||
} | ||||
MinRK
|
r10520 | $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Failure callback for listing a checkpoint. | ||||
* | ||||
* @method list_checkpoint_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} status Description of response status | ||||
* @param {String} error_msg HTTP error message | ||||
*/ | ||||
Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) { | ||||
$([IPython.events]).trigger('list_checkpoints_failed.Notebook'); | ||||
}; | ||||
/** | ||||
* Create a checkpoint of this notebook on the server from the most recent save. | ||||
* | ||||
* @method create_checkpoint | ||||
*/ | ||||
Notebook.prototype.create_checkpoint = function () { | ||||
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints'; | ||||
$.post(url).done( | ||||
$.proxy(this.create_checkpoint_success, this) | ||||
).fail( | ||||
$.proxy(this.create_checkpoint_error, this) | ||||
); | ||||
}; | ||||
/** | ||||
* Success callback for creating a checkpoint. | ||||
* | ||||
* @method create_checkpoint_success | ||||
* @param {Object} data JSON representation of a checkpoint | ||||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
Notebook.prototype.create_checkpoint_success = function (data, status, xhr) { | ||||
var data = $.parseJSON(data); | ||||
this.last_checkpoint = data; | ||||
$([IPython.events]).trigger('checkpoint_created.Notebook', data); | ||||
}; | ||||
/** | ||||
* Failure callback for creating a checkpoint. | ||||
* | ||||
* @method create_checkpoint_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} status Description of response status | ||||
* @param {String} error_msg HTTP error message | ||||
*/ | ||||
Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) { | ||||
$([IPython.events]).trigger('checkpoint_failed.Notebook'); | ||||
}; | ||||
MinRK
|
r10520 | Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) { | ||
MinRK
|
r10501 | var that = this; | ||
MinRK
|
r10520 | var checkpoint = checkpoint || this.last_checkpoint; | ||
MinRK
|
r10501 | if ( ! checkpoint ) { | ||
console.log("restore dialog, but no checkpoint to restore to!"); | ||||
return; | ||||
} | ||||
var dialog = $('<div/>').append( | ||||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text( | ||
"Are you sure you want to revert the notebook to " + | ||||
MinRK
|
r10501 | "the latest checkpoint?" | ||
).append( | ||||
$("<strong/>").text( | ||||
" This cannot be undone." | ||||
) | ||||
) | ||||
).append( | ||||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text("The checkpoint was last updated at:") | ||
MinRK
|
r10501 | ).append( | ||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text( | ||
Date(checkpoint.last_modified) | ||||
).css("text-align", "center") | ||||
MinRK
|
r10501 | ); | ||
$(document).append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Revert notebook to checkpoint", | ||||
closeText: '', | ||||
buttons : { | ||||
"Revert": function () { | ||||
that.restore_checkpoint(checkpoint.checkpoint_id); | ||||
$(this).dialog('close'); | ||||
}, | ||||
"Cancel": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
} | ||||
/** | ||||
* Restore the notebook to a checkpoint state. | ||||
* | ||||
* @method restore_checkpoint | ||||
* @param {String} checkpoint ID | ||||
*/ | ||||
Notebook.prototype.restore_checkpoint = function (checkpoint) { | ||||
$([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint); | ||||
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint; | ||||
$.post(url).done( | ||||
$.proxy(this.restore_checkpoint_success, this) | ||||
).fail( | ||||
$.proxy(this.restore_checkpoint_error, this) | ||||
); | ||||
}; | ||||
/** | ||||
* Success callback for restoring a notebook to a checkpoint. | ||||
* | ||||
* @method restore_checkpoint_success | ||||
* @param {Object} data (ignored, should be empty) | ||||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) { | ||||
$([IPython.events]).trigger('checkpoint_restored.Notebook'); | ||||
this.load_notebook(this.notebook_id); | ||||
}; | ||||
/** | ||||
* Failure callback for restoring a notebook to a checkpoint. | ||||
* | ||||
* @method restore_checkpoint_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} status Description of response status | ||||
* @param {String} error_msg HTTP error message | ||||
*/ | ||||
Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) { | ||||
$([IPython.events]).trigger('checkpoint_restore_failed.Notebook'); | ||||
}; | ||||
/** | ||||
* Delete a notebook checkpoint. | ||||
* | ||||
* @method delete_checkpoint | ||||
* @param {String} checkpoint ID | ||||
*/ | ||||
Notebook.prototype.delete_checkpoint = function (checkpoint) { | ||||
$([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint); | ||||
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint; | ||||
$.ajax(url, { | ||||
type: 'DELETE', | ||||
success: $.proxy(this.delete_checkpoint_success, this), | ||||
error: $.proxy(this.delete_notebook_error,this) | ||||
}); | ||||
}; | ||||
/** | ||||
* Success callback for deleting a notebook checkpoint | ||||
* | ||||
* @method delete_checkpoint_success | ||||
* @param {Object} data (ignored, should be empty) | ||||
* @param {String} status Description of response status | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
*/ | ||||
Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) { | ||||
$([IPython.events]).trigger('checkpoint_deleted.Notebook', data); | ||||
this.load_notebook(this.notebook_id); | ||||
}; | ||||
/** | ||||
* Failure callback for deleting a notebook checkpoint. | ||||
* | ||||
* @method delete_checkpoint_error | ||||
* @param {jqXHR} xhr jQuery Ajax object | ||||
* @param {String} status Description of response status | ||||
* @param {String} error_msg HTTP error message | ||||
*/ | ||||
Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) { | ||||
$([IPython.events]).trigger('checkpoint_delete_failed.Notebook'); | ||||
}; | ||||
Brian E. Granger
|
r4352 | IPython.Notebook = Notebook; | ||
Brian E. Granger
|
r5108 | |||
Brian E. Granger
|
r4352 | return IPython; | ||
}(IPython)); | ||||
Brian Granger
|
r4315 | |||